Skip to content

Commit 6dd6623

Browse files
committed
auto merge of #9936 : madjar/rust/master, r=alexcrichton
This should close #9468. I removed the test stating that nested comments should not be implemented. I had a little chicken-and-egg problem because a comment of the std contains "/*", and adding support for nested comment creates a backward incompatibility in that case, so I had to use a dirty hack to get stage1 and stage2 to compile. This part should be revert when this commit lands in a snapshot. This is my first non-typo contribution, so I'm open to any comment.
2 parents ece5028 + 1dc3d0b commit 6dd6623

File tree

4 files changed

+70
-75
lines changed

4 files changed

+70
-75
lines changed

src/libextra/glob.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,32 @@ pub struct GlobIterator {
3939
priv todo: ~[(Path,uint)]
4040
}
4141

42-
/**
43-
* Return an iterator that produces all the Paths that match the given pattern,
44-
* which may be absolute or relative to the current working directory.
45-
*
46-
* This method uses the default match options and is equivalent to calling
47-
* `glob_with(pattern, MatchOptions::new())`. Use `glob_with` directly if you
48-
* want to use non-default match options.
49-
*
50-
* # Example
51-
*
52-
* Consider a directory `/media/pictures` containing only the files `kittens.jpg`,
53-
* `puppies.jpg` and `hamsters.gif`:
54-
*
55-
* ```rust
56-
* for path in glob("/media/pictures/*.jpg") {
57-
* println(path.to_str());
58-
* }
59-
* ```
60-
*
61-
* The above code will print:
62-
*
63-
* ```
64-
* /media/pictures/kittens.jpg
65-
* /media/pictures/puppies.jpg
66-
* ```
67-
*/
42+
///
43+
/// Return an iterator that produces all the Paths that match the given pattern,
44+
/// which may be absolute or relative to the current working directory.
45+
///
46+
/// is method uses the default match options and is equivalent to calling
47+
/// `glob_with(pattern, MatchOptions::new())`. Use `glob_with` directly if you
48+
/// want to use non-default match options.
49+
///
50+
/// # Example
51+
///
52+
/// Consider a directory `/media/pictures` containing only the files `kittens.jpg`,
53+
/// `puppies.jpg` and `hamsters.gif`:
54+
///
55+
/// ```rust
56+
/// for path in glob("/media/pictures/*.jpg") {
57+
/// println(path.to_str());
58+
/// }
59+
/// ```
60+
///
61+
/// The above code will print:
62+
///
63+
/// ```
64+
/// /media/pictures/kittens.jpg
65+
/// /media/pictures/puppies.jpg
66+
/// ```
67+
///
6868
pub fn glob(pattern: &str) -> GlobIterator {
6969
glob_with(pattern, MatchOptions::new())
7070
}

src/libstd/os.rs

-6
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,6 @@ pub fn self_exe_path() -> Option<Path> {
546546
load_self().and_then(|path| Path::new_opt(path).map(|mut p| { p.pop(); p }))
547547
}
548548

549-
550-
/**
551-
* Returns the path to the user's home directory, if known.
552-
}
553-
554-
555549
/**
556550
* Returns the path to the user's home directory, if known.
557551
*

src/libsyntax/parse/lexer.rs

+41-33
Original file line numberDiff line numberDiff line change
@@ -373,49 +373,49 @@ pub fn is_block_non_doc_comment(s: &str) -> bool {
373373
fn consume_block_comment(rdr: @mut StringReader)
374374
-> Option<TokenAndSpan> {
375375
// block comments starting with "/**" or "/*!" are doc-comments
376-
let res = if rdr.curr == '*' || rdr.curr == '!' {
377-
let start_bpos = rdr.pos - BytePos(3u);
378-
while !(rdr.curr == '*' && nextch(rdr) == '/') && !is_eof(rdr) {
379-
bump(rdr);
380-
}
376+
let is_doc_comment = rdr.curr == '*' || rdr.curr == '!';
377+
let start_bpos = rdr.pos - BytePos(if is_doc_comment {3u} else {2u});
378+
379+
let mut level: int = 1;
380+
while level > 0 {
381381
if is_eof(rdr) {
382-
fatal_span(rdr, start_bpos, rdr.last_pos,
383-
~"unterminated block doc-comment");
384-
} else {
382+
let msg = if is_doc_comment {
383+
~"unterminated block doc-comment"
384+
} else {
385+
~"unterminated block comment"
386+
};
387+
fatal_span(rdr, start_bpos, rdr.last_pos, msg);
388+
} else if rdr.curr == '/' && nextch(rdr) == '*' {
389+
level += 1;
385390
bump(rdr);
386391
bump(rdr);
387-
do with_str_from(rdr, start_bpos) |string| {
388-
// but comments with only "*"s between two "/"s are not
389-
if !is_block_non_doc_comment(string) {
390-
Some(TokenAndSpan{
391-
tok: token::DOC_COMMENT(str_to_ident(string)),
392-
sp: codemap::mk_sp(start_bpos, rdr.pos)
393-
})
394-
} else {
395-
None
396-
}
397-
}
392+
} else if rdr.curr == '*' && nextch(rdr) == '/' {
393+
level -= 1;
394+
bump(rdr);
395+
bump(rdr);
396+
} else {
397+
bump(rdr);
398398
}
399-
} else {
400-
let start_bpos = rdr.last_pos - BytePos(2u);
401-
loop {
402-
if is_eof(rdr) {
403-
fatal_span(rdr, start_bpos, rdr.last_pos,
404-
~"unterminated block comment");
405-
}
406-
if rdr.curr == '*' && nextch(rdr) == '/' {
407-
bump(rdr);
408-
bump(rdr);
409-
break;
399+
}
400+
401+
let res = if is_doc_comment {
402+
do with_str_from(rdr, start_bpos) |string| {
403+
// but comments with only "*"s between two "/"s are not
404+
if !is_block_non_doc_comment(string) {
405+
Some(TokenAndSpan{
406+
tok: token::DOC_COMMENT(str_to_ident(string)),
407+
sp: codemap::mk_sp(start_bpos, rdr.pos)
408+
})
410409
} else {
411-
bump(rdr);
410+
None
412411
}
413412
}
413+
} else {
414414
None
415415
};
416-
// restart whitespace munch.
417416

418-
if res.is_some() { res } else { consume_whitespace_and_comments(rdr) }
417+
// restart whitespace munch.
418+
if res.is_some() { res } else { consume_whitespace_and_comments(rdr) }
419419
}
420420

421421
fn scan_exponent(rdr: @mut StringReader, start_bpos: BytePos) -> Option<~str> {
@@ -1056,4 +1056,12 @@ mod test {
10561056
assert!(!is_line_non_doc_comment("/// blah"));
10571057
assert!(is_line_non_doc_comment("////"));
10581058
}
1059+
1060+
#[test] fn nested_block_comments() {
1061+
let env = setup(@"/* /* */ */'a'");
1062+
let TokenAndSpan {tok, sp: _} =
1063+
env.string_reader.next_token();
1064+
assert_eq!(tok,token::LIT_CHAR('a' as u32));
1065+
}
1066+
10591067
}

src/test/compile-fail/no-comment-balancing.rs renamed to src/test/run-pass/nested-block-comment.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:
12-
13-
/* This is a test to ensure that we do _not_ support nested/balanced comments. I know you might be
14-
thinking "but nested comments are cool", and that would be a valid point, but they are also a
15-
thing that would make our lexical syntax non-regular, and we do not want that to be true.
16-
17-
omitting-things at a higher level (tokens) should be done via token-trees / macros,
18-
not comments.
11+
/* This test checks that nested comments are supported
1912
2013
/*
21-
fail here
14+
This should not fail
2215
*/
2316
*/
2417

25-
fn main() {
18+
pub fn main() {
2619
}

0 commit comments

Comments
 (0)