From a1ecdb103d32551ffb5bf552f95c88292b7eac1c Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Tue, 3 Aug 2010 16:28:50 -0700 Subject: [PATCH] Fix some naughtiness of handling newlines in bracequotes and multi-line comments. Closes #142. --- src/boot/fe/lexer.mll | 20 ++++++++++++------- .../multiline-comment-line-tracking.rs | 10 ++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 src/test/compile-fail/multiline-comment-line-tracking.rs diff --git a/src/boot/fe/lexer.mll b/src/boot/fe/lexer.mll index bb1d881e617..ed548b1e78c 100644 --- a/src/boot/fe/lexer.mll +++ b/src/boot/fe/lexer.mll @@ -22,6 +22,11 @@ Lexing.pos_bol = p.Lexing.pos_cnum } ;; + let newline lexbuf = + lexbuf.Lexing.lex_curr_p + <- (bump_line lexbuf.Lexing.lex_curr_p) + ;; + let mach_suf_table = Hashtbl.create 0 ;; let _ = @@ -155,8 +160,7 @@ let id = ['a'-'z' 'A'-'Z' '_']['a'-'z' 'A'-'Z' '0'-'9' '_']* rule token = parse ws+ { token lexbuf } -| '\n' { lexbuf.Lexing.lex_curr_p - <- (bump_line lexbuf.Lexing.lex_curr_p); +| '\n' { newline lexbuf; token lexbuf } | "//" [^'\n']* { token lexbuf } | "/*" { comment 1 lexbuf } @@ -389,8 +393,9 @@ and bracequote buf depth = parse bracequote buf depth lexbuf } -| [^'\\' '{' '}']+ { let s = Lexing.lexeme lexbuf in - Buffer.add_string buf s; +| [^'\\' '{' '}'] as c { Buffer.add_char buf c; + if c = '\n' + then newline lexbuf; bracequote buf depth lexbuf } @@ -402,6 +407,7 @@ and comment depth = parse then token lexbuf else comment (depth-1) lexbuf } -| '*' [^'{'] { comment depth lexbuf } -| '/' [^'*'] { comment depth lexbuf } -| [^'/' '*']+ { comment depth lexbuf } +| '\n' { newline lexbuf; + comment depth lexbuf } + +| _ { comment depth lexbuf } diff --git a/src/test/compile-fail/multiline-comment-line-tracking.rs b/src/test/compile-fail/multiline-comment-line-tracking.rs new file mode 100644 index 00000000000..3f555bfc87b --- /dev/null +++ b/src/test/compile-fail/multiline-comment-line-tracking.rs @@ -0,0 +1,10 @@ +// -*- rust -*- +// error-pattern:9:2:E + +/* 1 + * 2 + * 3 + */ +fn main() { + %; // parse error on line 9, but is reported on line 6 instead. +}