Skip to content

Commit ea74f68

Browse files
committed
auto merge of #6083 : jbclements/rust/parser-cleanup, r=jbclements
r? @pcwalton A month's worth of parser cleanup here. Much of this is new comments and renaming. A number of these commits also remove unneeded code. Probably the biggest refactor here is splitting "parse_item_or_view_item" into two functions; it turns out that the only overlap between items in foreign modules and items in regular modules was macros, so this refactor should make things substantially easier for future maintenance.
2 parents 7b7a0fc + cce13c1 commit ea74f68

File tree

17 files changed

+889
-545
lines changed

17 files changed

+889
-545
lines changed

src/librustc/driver/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub fn parse_input(sess: Session, cfg: ast::crate_cfg, input: &input)
149149
-> @ast::crate {
150150
match *input {
151151
file_input(ref file) => {
152-
parse::parse_crate_from_file_using_tts(&(*file), cfg, sess.parse_sess)
152+
parse::parse_crate_from_file(&(*file), cfg, sess.parse_sess)
153153
}
154154
str_input(ref src) => {
155155
// FIXME (#2319): Don't really want to box the source string

src/librustdoc/attr_parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ mod test {
7979

8080
let parse_sess = syntax::parse::new_parse_sess(None);
8181
let parser = parse::new_parser_from_source_str(
82-
parse_sess, ~[], ~"-", codemap::FssNone, @source);
82+
parse_sess, ~[], ~"-", @source);
8383
8484
parser.parse_outer_attributes()
8585
}

src/librustpkg/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn test_make_dir_rwx() {
6666
fn test_install_valid() {
6767
let ctxt = fake_ctxt();
6868
let temp_pkg_id = fake_pkg();
69-
let temp_workspace() = mk_temp_workspace();
69+
let temp_workspace = mk_temp_workspace();
7070
// should have test, bench, lib, and main
7171
ctxt.install(&temp_workspace, temp_pkg_id);
7272
// Check that all files exist

src/libsyntax/ast_util.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ pub fn operator_prec(op: ast::binop) -> uint {
355355
}
356356
}
357357
358+
/// Precedence of the `as` operator, which is a binary operator
359+
/// not appearing in the prior table.
360+
pub static as_prec: uint = 11u;
361+
358362
pub fn dtor_ty() -> @ast::Ty {
359363
@ast::Ty {id: 0, node: ty_nil, span: dummy_sp()}
360364
}
@@ -756,7 +760,6 @@ mod test {
756760
assert_eq!(refold_test_sc(3,&t),test_sc);
757761
}
758762
759-
760763
// extend a syntax context with a sequence of marks given
761764
// in a vector. v[0] will be the outermost mark.
762765
fn unfold_marks(mrks:~[Mrk],tail:SyntaxContext,table: &mut SCTable) -> SyntaxContext {

src/libsyntax/ext/base.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,7 @@ impl <K: Eq + Hash + IterBytes ,V: Copy> MapChain<K,V>{
461461

462462
// ugh: can't get this to compile with mut because of the
463463
// lack of flow sensitivity.
464-
#[cfg(stage1)]
465-
#[cfg(stage2)]
466-
#[cfg(stage3)]
464+
#[cfg(not(stage0))]
467465
fn get_map<'a>(&'a self) -> &'a HashMap<K,@V> {
468466
match *self {
469467
BaseMapChain (~ref map) => map,

src/libsyntax/opt_vec.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ impl<T> OptVec<T> {
6969
}
7070
}
7171

72-
#[cfg(stage1)]
73-
#[cfg(stage2)]
74-
#[cfg(stage3)]
72+
#[cfg(not(stage0))]
7573
fn get<'a>(&'a self, i: uint) -> &'a T {
7674
match *self {
7775
Empty => fail!(fmt!("Invalid index %u", i)),

src/libsyntax/parse/attr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ impl parser_attr for Parser {
6262
return attrs;
6363
}
6464

65+
// matches attribute = # attribute_naked
6566
fn parse_attribute(&self, style: ast::attr_style) -> ast::attribute {
6667
let lo = self.span.lo;
6768
self.expect(&token::POUND);
6869
return self.parse_attribute_naked(style, lo);
6970
}
7071

72+
// matches attribute_naked = [ meta_item ]
7173
fn parse_attribute_naked(&self, style: ast::attr_style, lo: BytePos) ->
7274
ast::attribute {
7375
self.expect(&token::LBRACKET);
@@ -86,6 +88,7 @@ impl parser_attr for Parser {
8688
// is an inner attribute of the containing item or an outer attribute of
8789
// the first contained item until we see the semi).
8890

91+
// matches inner_attrs* outer_attr?
8992
// you can make the 'next' field an Option, but the result is going to be
9093
// more useful as a vector.
9194
fn parse_inner_attrs_and_next(&self) ->
@@ -134,6 +137,9 @@ impl parser_attr for Parser {
134137
(inner_attrs, next_outer_attrs)
135138
}
136139

140+
// matches meta_item = IDENT
141+
// | IDENT = lit
142+
// | IDENT meta_seq
137143
fn parse_meta_item(&self) -> @ast::meta_item {
138144
let lo = self.span.lo;
139145
let name = self.id_to_str(self.parse_ident());
@@ -156,6 +162,7 @@ impl parser_attr for Parser {
156162
}
157163
}
158164

165+
// matches meta_seq = ( COMMASEP(meta_item) )
159166
fn parse_meta_seq(&self) -> ~[@ast::meta_item] {
160167
copy self.parse_seq(
161168
&token::LPAREN,

src/libsyntax/parse/classify.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
use ast;
1616
use codemap;
1717

18+
// does this expression require a semicolon to be treated
19+
// as a statement? The negation of this: 'can this expression
20+
// be used as a statement without a semicolon' -- is used
21+
// as an early-bail-out in the parser so that, for instance,
22+
// 'if true {...} else {...}
23+
// |x| 5 '
24+
// isn't parsed as (if true {...} else {...} | x) | 5
1825
pub fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool {
1926
match e.node {
2027
ast::expr_if(*)
@@ -40,6 +47,9 @@ pub fn expr_is_simple_block(e: @ast::expr) -> bool {
4047
}
4148
}
4249

50+
// this statement requires a semicolon after it.
51+
// note that in one case (stmt_semi), we've already
52+
// seen the semicolon, and thus don't need another.
4353
pub fn stmt_ends_with_semi(stmt: &ast::stmt) -> bool {
4454
return match stmt.node {
4555
ast::stmt_decl(d, _) => {

src/libsyntax/parse/comments.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ pub struct lit {
309309
pos: BytePos
310310
}
311311
312+
// it appears this function is called only from pprust... that's
313+
// probably not a good thing.
312314
pub fn gather_comments_and_literals(span_diagnostic:
313315
@diagnostic::span_handler,
314316
path: ~str,

src/libsyntax/parse/lexer.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,12 @@ pub fn is_whitespace(c: char) -> bool {
225225
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
226226
}
227227

228-
fn may_begin_ident(c: char) -> bool { return is_alpha(c) || c == '_'; }
229-
230228
fn in_range(c: char, lo: char, hi: char) -> bool {
231229
return lo <= c && c <= hi
232230
}
233231

234-
fn is_alpha(c: char) -> bool {
235-
return in_range(c, 'a', 'z') || in_range(c, 'A', 'Z');
236-
}
237-
238232
fn is_dec_digit(c: char) -> bool { return in_range(c, '0', '9'); }
239233

240-
fn is_alnum(c: char) -> bool { return is_alpha(c) || is_dec_digit(c); }
241-
242234
fn is_hex_digit(c: char) -> bool {
243235
return in_range(c, '0', '9') || in_range(c, 'a', 'f') ||
244236
in_range(c, 'A', 'F');
@@ -294,6 +286,8 @@ fn consume_any_line_comment(rdr: @mut StringReader)
294286
}
295287
} else if rdr.curr == '#' {
296288
if nextch(rdr) == '!' {
289+
// I guess this is the only way to figure out if
290+
// we're at the beginning of the file...
297291
let cmap = @CodeMap::new();
298292
(*cmap).files.push(rdr.filemap);
299293
let loc = cmap.lookup_char_pos_adj(rdr.last_pos);
@@ -444,8 +438,7 @@ fn scan_number(c: char, rdr: @mut StringReader) -> token::Token {
444438
}
445439
}
446440
let mut is_float = false;
447-
if rdr.curr == '.' && !(is_alpha(nextch(rdr)) || nextch(rdr) == '_' ||
448-
nextch(rdr) == '.') {
441+
if rdr.curr == '.' && !(ident_start(nextch(rdr)) || nextch(rdr) == '.') {
449442
is_float = true;
450443
bump(rdr);
451444
let dec_part = scan_digits(rdr, 10u);

0 commit comments

Comments
 (0)