Skip to content

Commit dbf53b4

Browse files
committed
Connect the crate and source parsers together.
1 parent 9acf4b9 commit dbf53b4

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

src/comp/front/parser.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,29 @@ tag restriction {
1818
RESTRICT_NO_CALL_EXPRS;
1919
}
2020

21+
tag file_type {
22+
CRATE_FILE;
23+
SOURCE_FILE;
24+
}
25+
2126
state type parser =
2227
state obj {
2328
fn peek() -> token.token;
2429
impure fn bump();
2530
impure fn err(str s);
2631
impure fn restrict(restriction r);
2732
fn get_restriction() -> restriction;
33+
fn get_file_type() -> file_type;
2834
fn get_session() -> session.session;
2935
fn get_span() -> common.span;
3036
fn next_def_id() -> ast.def_id;
3137
};
3238

3339
impure fn new_parser(session.session sess,
34-
ast.crate_num crate, str path) -> parser {
40+
ast.crate_num crate,
41+
str path) -> parser {
3542
state obj stdio_parser(session.session sess,
43+
file_type ftype,
3644
mutable token.token tok,
3745
mutable common.pos lo,
3846
mutable common.pos hi,
@@ -80,11 +88,20 @@ impure fn new_parser(session.session sess,
8088
def += 1;
8189
ret tup(crate, def);
8290
}
91+
92+
fn get_file_type() -> file_type {
93+
ret ftype;
94+
}
95+
8396
}
97+
auto ftype = SOURCE_FILE;
98+
if (_str.ends_with(path, ".rc")) {
99+
ftype = CRATE_FILE;
100+
}
84101
auto srdr = io.new_stdio_reader(path);
85102
auto rdr = lexer.new_reader(srdr, path);
86103
auto npos = rdr.get_curr_pos();
87-
ret stdio_parser(sess, lexer.next_token(rdr),
104+
ret stdio_parser(sess, ftype, lexer.next_token(rdr),
88105
npos, npos, 0, UNRESTRICTED, crate, rdr);
89106
}
90107

@@ -1310,6 +1327,20 @@ impure fn parse_auto(parser p) -> @ast.decl {
13101327
}
13111328

13121329
impure fn parse_stmt(parser p) -> @ast.stmt {
1330+
if (p.get_file_type() == SOURCE_FILE) {
1331+
ret parse_source_stmt(p);
1332+
} else {
1333+
ret parse_crate_stmt(p);
1334+
}
1335+
}
1336+
1337+
impure fn parse_crate_stmt(parser p) -> @ast.stmt {
1338+
auto cdir = parse_crate_directive(p);
1339+
ret @spanned(cdir.span, cdir.span,
1340+
ast.stmt_crate_directive(@cdir));
1341+
}
1342+
1343+
impure fn parse_source_stmt(parser p) -> @ast.stmt {
13131344
auto lo = p.get_span();
13141345
alt (p.peek()) {
13151346

@@ -1485,6 +1516,16 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool {
14851516
case (ast.expr_check_expr(_)) { ret true; }
14861517
}
14871518
}
1519+
case (ast.stmt_crate_directive(?cdir)) {
1520+
alt (cdir.node) {
1521+
case (ast.cdir_src_mod(_, _)) { ret true; }
1522+
case (ast.cdir_view_item(_)) { ret true; }
1523+
case (ast.cdir_meta(_)) { ret true; }
1524+
case (ast.cdir_syntax(_)) { ret true; }
1525+
case (ast.cdir_auth(_, _)) { ret true; }
1526+
case (_) { ret false; }
1527+
}
1528+
}
14881529
}
14891530
}
14901531

@@ -2182,6 +2223,21 @@ impure fn parse_crate_directive(parser p) -> ast.crate_directive
21822223
expect(p, token.RBRACE);
21832224
ret spanned(lo, hi, ast.cdir_let(id, x, v));
21842225
}
2226+
2227+
case (token.USE) {
2228+
auto vi = parse_use_or_import(p);
2229+
ret spanned(lo, vi.span, ast.cdir_view_item(vi));
2230+
}
2231+
2232+
case (token.IMPORT) {
2233+
auto vi = parse_use_or_import(p);
2234+
ret spanned(lo, vi.span, ast.cdir_view_item(vi));
2235+
}
2236+
2237+
case (_) {
2238+
auto x = parse_expr(p);
2239+
ret spanned(lo, x.span, ast.cdir_expr(x));
2240+
}
21852241
}
21862242
fail;
21872243
}

0 commit comments

Comments
 (0)