diff --git a/src/librustsyntax/parse/classify.rs b/src/librustsyntax/parse/classify.rs new file mode 100644 index 00000000000..f2c9aece0c1 --- /dev/null +++ b/src/librustsyntax/parse/classify.rs @@ -0,0 +1,31 @@ +// FIXME: There are a bunch of similar functions in pprust that +// likely belong here + +fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool { + alt e.node { + ast::expr_if(_, _, _) | ast::expr_if_check(_, _, _) + | ast::expr_alt(_, _, _) | ast::expr_block(_) + | ast::expr_do_while(_, _) | ast::expr_while(_, _) + | ast::expr_loop(_) | ast::expr_call(_, _, true) { + false + } + _ { true } + } +} + +fn stmt_ends_with_semi(stmt: ast::stmt) -> bool { + alt stmt.node { + ast::stmt_decl(d, _) { + ret alt d.node { + ast::decl_local(_) { true } + ast::decl_item(_) { false } + } + } + ast::stmt_expr(e, _) { + ret expr_requires_semi_to_be_stmt(e); + } + ast::stmt_semi(e, _) { + ret false; + } + } +} diff --git a/src/librustsyntax/parse/parser.rs b/src/librustsyntax/parse/parser.rs index a5eb2d6b8be..798afc70e05 100644 --- a/src/librustsyntax/parse/parser.rs +++ b/src/librustsyntax/parse/parser.rs @@ -9,7 +9,6 @@ import ast_util::{mk_sp, ident_to_path}; import lexer::reader; import prec::{op_spec, binop_prec_table, as_prec}; -export expr_requires_semi_to_be_stmt; export file_type; export mk_item; export next_node_id; @@ -31,7 +30,6 @@ export parse_pat; export parse_sess; export parse_stmt; export parse_ty; -export stmt_ends_with_semi; enum restriction { UNRESTRICTED, @@ -1776,38 +1774,9 @@ fn parse_stmt(p: parser, first_item_attrs: [ast::attribute]) -> @ast::stmt { fn expr_is_complete(p: parser, e: pexpr) -> bool { log(debug, ("expr_is_complete", p.restriction, print::pprust::expr_to_str(*e), - expr_requires_semi_to_be_stmt(*e))); + classify::expr_requires_semi_to_be_stmt(*e))); ret p.restriction == RESTRICT_STMT_EXPR && - !expr_requires_semi_to_be_stmt(*e); -} - -fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool { - alt e.node { - ast::expr_if(_, _, _) | ast::expr_if_check(_, _, _) - | ast::expr_alt(_, _, _) | ast::expr_block(_) - | ast::expr_do_while(_, _) | ast::expr_while(_, _) - | ast::expr_loop(_) | ast::expr_call(_, _, true) { - false - } - _ { true } - } -} - -fn stmt_ends_with_semi(stmt: ast::stmt) -> bool { - alt stmt.node { - ast::stmt_decl(d, _) { - ret alt d.node { - ast::decl_local(_) { true } - ast::decl_item(_) { false } - } - } - ast::stmt_expr(e, _) { - ret expr_requires_semi_to_be_stmt(e); - } - ast::stmt_semi(e, _) { - ret false; - } - } + !classify::expr_requires_semi_to_be_stmt(*e); } fn parse_block(p: parser) -> ast::blk { @@ -1890,7 +1859,7 @@ fn parse_block_tail_(p: parser, lo: uint, s: ast::blk_check_mode, expr = some(e); } t { - if stmt_ends_with_semi(*stmt) { + if classify::stmt_ends_with_semi(*stmt) { p.fatal("expected ';' or '}' after expression but \ found '" + token_to_str(p.reader, t) + "'"); @@ -1903,7 +1872,7 @@ fn parse_block_tail_(p: parser, lo: uint, s: ast::blk_check_mode, _ { // All other kinds of statements: stmts += [stmt]; - if stmt_ends_with_semi(*stmt) { + if classify::stmt_ends_with_semi(*stmt) { expect(p, token::SEMI); } } diff --git a/src/librustsyntax/print/pprust.rs b/src/librustsyntax/print/pprust.rs index c27976a314a..dbe5f3472d8 100644 --- a/src/librustsyntax/print/pprust.rs +++ b/src/librustsyntax/print/pprust.rs @@ -685,7 +685,7 @@ fn print_stmt(s: ps, st: ast::stmt) { word(s.s, ";"); } } - if parse::parser::stmt_ends_with_semi(st) { word(s.s, ";"); } + if parse::classify::stmt_ends_with_semi(st) { word(s.s, ";"); } maybe_print_trailing_comment(s, st.span, none::); } @@ -1508,7 +1508,7 @@ fn need_parens(expr: @ast::expr, outer_prec: int) -> bool { ast::expr_assert(_) { true } ast::expr_check(_, _) { true } ast::expr_log(_, _, _) { true } - _ { !parse::parser::expr_requires_semi_to_be_stmt(expr) } + _ { !parse::classify::expr_requires_semi_to_be_stmt(expr) } } } diff --git a/src/librustsyntax/rustsyntax.rc b/src/librustsyntax/rustsyntax.rc index d481670b8b3..68a41985aa6 100644 --- a/src/librustsyntax/rustsyntax.rc +++ b/src/librustsyntax/rustsyntax.rc @@ -27,6 +27,7 @@ mod parse { export lexer; export comments; export prec; + export classify; mod eval; mod lexer; @@ -36,6 +37,9 @@ mod parse { #[doc = "Functions dealing with operator precedence"] mod prec; + + #[doc = "Routines the parser uses to classify AST nodes"] + mod classify; } mod print {