Skip to content

Added support for #! comments. Closes issue 1772. #2430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export new_filemap;
export new_filemap_w_substr;
export mk_substr_filename;
export lookup_char_pos;
export lookup_char_pos_adj;
export adjust_span;
export span_to_str;
export span_to_filename;
Expand Down
20 changes: 17 additions & 3 deletions src/librustsyntax/parse/comments.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io::reader_util;
import io::println;//XXXXXXXXxxx
import util::interner;
import lexer::{ reader, new_reader, next_token, is_whitespace };

Expand Down Expand Up @@ -28,7 +29,8 @@ fn read_to_eol(rdr: reader) -> str {

fn read_one_line_comment(rdr: reader) -> str {
let val = read_to_eol(rdr);
assert (val[0] == '/' as u8 && val[1] == '/' as u8);
assert ((val[0] == '/' as u8 && val[1] == '/' as u8) ||
(val[0] == '#' as u8 && val[1] == '!' as u8));
ret val;
}

Expand All @@ -53,6 +55,15 @@ fn consume_whitespace_counting_blank_lines(rdr: reader, &comments: [cmnt]) {
}
}

fn read_shebang_comment(rdr: reader, code_to_the_left: bool) -> cmnt {
#debug(">>> shebang comment");
let p = rdr.chpos;
#debug("<<< shebang comment");
ret {style: if code_to_the_left { trailing } else { isolated },
lines: [read_one_line_comment(rdr)],
pos: p};
}

fn read_line_comments(rdr: reader, code_to_the_left: bool) -> cmnt {
#debug(">>> line comments");
let p = rdr.chpos;
Expand Down Expand Up @@ -134,8 +145,9 @@ fn read_block_comment(rdr: reader, code_to_the_left: bool) -> cmnt {
}

fn peeking_at_comment(rdr: reader) -> bool {
ret rdr.curr == '/' && rdr.next() == '/' ||
rdr.curr == '/' && rdr.next() == '*';
ret ((rdr.curr == '/' && rdr.next() == '/') ||
(rdr.curr == '/' && rdr.next() == '*')) ||
(rdr.curr == '#' && rdr.next() == '!');
}

fn consume_comment(rdr: reader, code_to_the_left: bool, &comments: [cmnt]) {
Expand All @@ -144,6 +156,8 @@ fn consume_comment(rdr: reader, code_to_the_left: bool, &comments: [cmnt]) {
comments += [read_line_comments(rdr, code_to_the_left)];
} else if rdr.curr == '/' && rdr.next() == '*' {
comments += [read_block_comment(rdr, code_to_the_left)];
} else if rdr.curr == '#' && rdr.next() == '!' {
comments += [read_shebang_comment(rdr, code_to_the_left)];
} else { fail; }
#debug("<<< consume comment");
}
Expand Down
10 changes: 10 additions & 0 deletions src/librustsyntax/parse/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ fn consume_any_line_comment(rdr: reader) {
'*' { rdr.bump(); rdr.bump(); ret consume_block_comment(rdr); }
_ { ret; }
}
} else if rdr.curr == '#' {
if rdr.next() == '!' {
let cmap = codemap::new_codemap();
(*cmap).files.push(rdr.filemap);
let loc = codemap::lookup_char_pos_adj(cmap, rdr.chpos);
if loc.line == 1u && loc.col == 0u {
while rdr.curr != '\n' && !rdr.is_eof() { rdr.bump(); }
ret consume_whitespace_and_comments(rdr);
}
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/run-pass/shebang.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env rustx
// pp-exact

import io::println;

fn main() { io::println("Hello World"); }