Skip to content

Commit 2c0cb90

Browse files
committed
syntax: Begin moving functions from mod parser to mod classify
1 parent d51973a commit 2c0cb90

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

src/librustsyntax/parse/classify.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// FIXME: There are a bunch of similar functions in pprust that
2+
// likely belong here
3+
4+
fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool {
5+
alt e.node {
6+
ast::expr_if(_, _, _) | ast::expr_if_check(_, _, _)
7+
| ast::expr_alt(_, _, _) | ast::expr_block(_)
8+
| ast::expr_do_while(_, _) | ast::expr_while(_, _)
9+
| ast::expr_loop(_) | ast::expr_call(_, _, true) {
10+
false
11+
}
12+
_ { true }
13+
}
14+
}
15+
16+
fn stmt_ends_with_semi(stmt: ast::stmt) -> bool {
17+
alt stmt.node {
18+
ast::stmt_decl(d, _) {
19+
ret alt d.node {
20+
ast::decl_local(_) { true }
21+
ast::decl_item(_) { false }
22+
}
23+
}
24+
ast::stmt_expr(e, _) {
25+
ret expr_requires_semi_to_be_stmt(e);
26+
}
27+
ast::stmt_semi(e, _) {
28+
ret false;
29+
}
30+
}
31+
}

src/librustsyntax/parse/parser.rs

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import ast_util::{mk_sp, ident_to_path};
99
import lexer::reader;
1010
import prec::{op_spec, binop_prec_table, as_prec};
1111

12-
export expr_requires_semi_to_be_stmt;
1312
export file_type;
1413
export mk_item;
1514
export next_node_id;
@@ -31,7 +30,6 @@ export parse_pat;
3130
export parse_sess;
3231
export parse_stmt;
3332
export parse_ty;
34-
export stmt_ends_with_semi;
3533

3634
enum restriction {
3735
UNRESTRICTED,
@@ -1776,38 +1774,9 @@ fn parse_stmt(p: parser, first_item_attrs: [ast::attribute]) -> @ast::stmt {
17761774
fn expr_is_complete(p: parser, e: pexpr) -> bool {
17771775
log(debug, ("expr_is_complete", p.restriction,
17781776
print::pprust::expr_to_str(*e),
1779-
expr_requires_semi_to_be_stmt(*e)));
1777+
classify::expr_requires_semi_to_be_stmt(*e)));
17801778
ret p.restriction == RESTRICT_STMT_EXPR &&
1781-
!expr_requires_semi_to_be_stmt(*e);
1782-
}
1783-
1784-
fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool {
1785-
alt e.node {
1786-
ast::expr_if(_, _, _) | ast::expr_if_check(_, _, _)
1787-
| ast::expr_alt(_, _, _) | ast::expr_block(_)
1788-
| ast::expr_do_while(_, _) | ast::expr_while(_, _)
1789-
| ast::expr_loop(_) | ast::expr_call(_, _, true) {
1790-
false
1791-
}
1792-
_ { true }
1793-
}
1794-
}
1795-
1796-
fn stmt_ends_with_semi(stmt: ast::stmt) -> bool {
1797-
alt stmt.node {
1798-
ast::stmt_decl(d, _) {
1799-
ret alt d.node {
1800-
ast::decl_local(_) { true }
1801-
ast::decl_item(_) { false }
1802-
}
1803-
}
1804-
ast::stmt_expr(e, _) {
1805-
ret expr_requires_semi_to_be_stmt(e);
1806-
}
1807-
ast::stmt_semi(e, _) {
1808-
ret false;
1809-
}
1810-
}
1779+
!classify::expr_requires_semi_to_be_stmt(*e);
18111780
}
18121781

18131782
fn parse_block(p: parser) -> ast::blk {
@@ -1890,7 +1859,7 @@ fn parse_block_tail_(p: parser, lo: uint, s: ast::blk_check_mode,
18901859
expr = some(e);
18911860
}
18921861
t {
1893-
if stmt_ends_with_semi(*stmt) {
1862+
if classify::stmt_ends_with_semi(*stmt) {
18941863
p.fatal("expected ';' or '}' after expression but \
18951864
found '" + token_to_str(p.reader, t) +
18961865
"'");
@@ -1903,7 +1872,7 @@ fn parse_block_tail_(p: parser, lo: uint, s: ast::blk_check_mode,
19031872
_ { // All other kinds of statements:
19041873
stmts += [stmt];
19051874

1906-
if stmt_ends_with_semi(*stmt) {
1875+
if classify::stmt_ends_with_semi(*stmt) {
19071876
expect(p, token::SEMI);
19081877
}
19091878
}

src/librustsyntax/print/pprust.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ fn print_stmt(s: ps, st: ast::stmt) {
685685
word(s.s, ";");
686686
}
687687
}
688-
if parse::parser::stmt_ends_with_semi(st) { word(s.s, ";"); }
688+
if parse::classify::stmt_ends_with_semi(st) { word(s.s, ";"); }
689689
maybe_print_trailing_comment(s, st.span, none::<uint>);
690690
}
691691

@@ -1508,7 +1508,7 @@ fn need_parens(expr: @ast::expr, outer_prec: int) -> bool {
15081508
ast::expr_assert(_) { true }
15091509
ast::expr_check(_, _) { true }
15101510
ast::expr_log(_, _, _) { true }
1511-
_ { !parse::parser::expr_requires_semi_to_be_stmt(expr) }
1511+
_ { !parse::classify::expr_requires_semi_to_be_stmt(expr) }
15121512
}
15131513
}
15141514

src/librustsyntax/rustsyntax.rc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod parse {
2727
export lexer;
2828
export comments;
2929
export prec;
30+
export classify;
3031

3132
mod eval;
3233
mod lexer;
@@ -36,6 +37,9 @@ mod parse {
3637

3738
#[doc = "Functions dealing with operator precedence"]
3839
mod prec;
40+
41+
#[doc = "Routines the parser uses to classify AST nodes"]
42+
mod classify;
3943
}
4044

4145
mod print {

0 commit comments

Comments
 (0)