Skip to content

Commit

Permalink
Issue 66: Multi-line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck authored and graydon committed Jul 8, 2010
1 parent 115e14a commit 244ea68
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
10 changes: 8 additions & 2 deletions doc/rust.texi
Original file line number Diff line number Diff line change
Expand Up @@ -616,11 +616,17 @@ otherwise a free-form language.
@dfn{Whitespace} is any of the following Unicode characters: U+0020 (space),
U+0009 (tab, @code{'\t'}), U+000A (LF, @code{'\n'}), U+000D (CR, @code{'\r'}).

@dfn{Comments} are any sequence of Unicode characters beginning with U+002F
U+002F (@code{"//"}) and extending to the next U+000A character,
@dfn{Comments} are @emph{single-line comments} or @emph{multi-line comments}.

A @dfn{single-line comment} is any sequence of Unicode characters beginning
with U+002F U+002F (@code{"//"}) and extending to the next U+000A character,
@emph{excluding} cases in which such a sequence occurs within a string literal
token or a syntactic extension token.

A @dfn{multi-line comments} is any sequence of Unicode characters beginning
with U+002F U+002A (@code{"/*"}) and ending with U+002A U+002F (@code{"*/"}),
@emph{excluding} cases in which such a sequence occurs within a string literal
token or a syntactic extension token. Multi-line comments may be nested.

@node Ref.Lex.Ident
@subsection Ref.Lex.Ident
Expand Down
15 changes: 14 additions & 1 deletion src/boot/fe/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ rule token = parse
<- (bump_line lexbuf.Lexing.lex_curr_p);
token lexbuf }
| "//" [^'\n']* { token lexbuf }

| "/*" { comment 1 lexbuf }
| '+' { PLUS }
| '-' { MINUS }
| '*' { STAR }
Expand Down Expand Up @@ -362,3 +362,16 @@ and bracequote buf depth = parse
| [^'\\' '{' '}']+ { let s = Lexing.lexeme lexbuf in
Buffer.add_string buf s;
bracequote buf depth lexbuf }


and comment depth = parse

'/' '*' { comment (depth+1) lexbuf }

| '*' '/' { if depth = 1
then token lexbuf
else comment (depth-1) lexbuf }

| '*' [^'{'] { comment depth lexbuf }
| '/' [^'*'] { comment depth lexbuf }
| [^'/' '*']+ { comment depth lexbuf }
9 changes: 9 additions & 0 deletions src/test/compile-fail/unbalanced-comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// -*- rust -*-

/*
* This is an un-balanced /* multi-line comment.
*/

fn main() {
log "hello, world.";
}
8 changes: 8 additions & 0 deletions src/test/run-pass/multiline-comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// -*- rust -*-

/*
* This is a /* depth-balanced */ multi-line comment.
*/

fn main() {
}

0 comments on commit 244ea68

Please sign in to comment.