diff options
| author | Marijn Haverbeke <[email protected]> | 2011-03-24 16:33:20 +0100 |
|---|---|---|
| committer | Graydon Hoare <[email protected]> | 2011-03-31 14:41:39 +0000 |
| commit | e7e6f396d888574e7184119818ac3300f10aacbf (patch) | |
| tree | 143c2c918a5123d6ed07b8ee61909981d3aa47e5 /src/comp/front | |
| parent | Tweak build command on rustc. (diff) | |
| download | rust-e7e6f396d888574e7184119818ac3300f10aacbf.tar.xz rust-e7e6f396d888574e7184119818ac3300f10aacbf.zip | |
Preserve comments when pretty-printing.
The patch also includes a number of smaller fixes to the
pretty-printer that were encountered on the way.
Diffstat (limited to 'src/comp/front')
| -rw-r--r-- | src/comp/front/lexer.rs | 80 | ||||
| -rw-r--r-- | src/comp/front/parser.rs | 4 |
2 files changed, 83 insertions, 1 deletions
diff --git a/src/comp/front/lexer.rs b/src/comp/front/lexer.rs index 86b872da..2229a4e6 100644 --- a/src/comp/front/lexer.rs +++ b/src/comp/front/lexer.rs @@ -1,5 +1,6 @@ import std.io; import std._str; +import std._vec; import std._int; import std.map; import std.map.hashmap; @@ -781,6 +782,85 @@ impure fn next_token(reader rdr) -> token.token { fail; } +tag cmnt_ { + cmnt_line(str); + cmnt_block(vec[str]); +} +type cmnt = rec(cmnt_ val, common.pos pos, bool space_after); + +impure fn consume_whitespace(reader rdr) -> uint { + auto lines = 0u; + while (is_whitespace(rdr.curr())) { + if (rdr.curr() == '\n') {lines += 1u;} + rdr.bump(); + } + ret lines; +} + +impure fn read_line_comment(reader rdr) -> cmnt { + auto p = rdr.get_curr_pos(); + rdr.bump(); rdr.bump(); + consume_whitespace(rdr); + auto val = ""; + while (rdr.curr() != '\n') { + _str.push_char(val, rdr.curr()); + rdr.bump(); + } + ret rec(val=cmnt_line(val), + pos=p, + space_after=consume_whitespace(rdr) > 1u); +} + +impure fn read_block_comment(reader rdr) -> cmnt { + auto p = rdr.get_curr_pos(); + rdr.bump(); rdr.bump(); + consume_whitespace(rdr); + let vec[str] lines = vec(); + auto val = ""; + auto level = 1; + while (true) { + if (rdr.curr() == '\n') { + _vec.push[str](lines, val); + val = ""; + consume_whitespace(rdr); + } else { + if (rdr.curr() == '*' && rdr.next() == '/') { + level -= 1; + if (level == 0) { + rdr.bump(); rdr.bump(); + _vec.push[str](lines, val); + break; + } + } else if (rdr.curr() == '/' && rdr.next() == '*') { + level += 1; + } + _str.push_char(val, rdr.curr()); + rdr.bump(); + } + } + ret rec(val=cmnt_block(lines), + pos=p, + space_after=consume_whitespace(rdr) > 1u); +} + +impure fn gather_comments(str path) -> vec[cmnt] { + auto srdr = io.file_reader(path); + auto rdr = lexer.new_reader(srdr, path); + let vec[cmnt] comments = vec(); + while (!rdr.is_eof()) { + while (true) { + consume_whitespace(rdr); + if (rdr.curr() == '/' && rdr.next() == '/') { + _vec.push[cmnt](comments, read_line_comment(rdr)); + } else if (rdr.curr() == '/' && rdr.next() == '*') { + _vec.push[cmnt](comments, read_block_comment(rdr)); + } else { break; } + } + next_token(rdr); + } + ret comments; +} + // // Local Variables: diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index a247c824..82f7712f 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -116,6 +116,8 @@ impure fn new_parser(session.session sess, } auto srdr = io.file_reader(path); auto rdr = lexer.new_reader(srdr, path); + // Make sure npos points at first actual token. + lexer.consume_any_whitespace(rdr); auto npos = rdr.get_curr_pos(); ret stdio_parser(sess, env, ftype, lexer.next_token(rdr), npos, npos, initial_def._1, UNRESTRICTED, initial_def._0, @@ -1748,8 +1750,8 @@ impure fn parse_block(parser p) -> ast.block { } } - p.bump(); auto hi = p.get_span(); + p.bump(); auto bloc = index_block(stmts, expr); ret spanned[ast.block_](lo, hi, bloc); |