diff options
| author | Tim Chevalier <chevalier@alum.wellesley.edu> | 2012-04-19 14:46:11 -0700 |
|---|---|---|
| committer | Tim Chevalier <chevalier@alum.wellesley.edu> | 2012-04-19 15:30:58 -0700 |
| commit | 37ea010b0d9e8cbccbe040134708028e6bb05c48 (patch) | |
| tree | ba58af53a05a74db01f406497e530267707c8a64 | |
| parent | 1da18c70ac96ddec31627d2454a60ce441cf29e1 (diff) | |
| download | rust-37ea010b0d9e8cbccbe040134708028e6bb05c48.tar.gz rust-37ea010b0d9e8cbccbe040134708028e6bb05c48.zip | |
Move some functions from pprust to classify
As per a FIXME in syntax::parse::classify, move predicates on exprs and stmts into classify, out of pprust
| -rw-r--r-- | src/librustsyntax/parse/classify.rs | 49 | ||||
| -rw-r--r-- | src/librustsyntax/print/pprust.rs | 60 |
2 files changed, 54 insertions, 55 deletions
diff --git a/src/librustsyntax/parse/classify.rs b/src/librustsyntax/parse/classify.rs index f2c9aece0c1..caba09bfefc 100644 --- a/src/librustsyntax/parse/classify.rs +++ b/src/librustsyntax/parse/classify.rs @@ -1,5 +1,6 @@ -// FIXME: There are a bunch of similar functions in pprust that -// likely belong here +/* + Predicates on exprs and stmts that the pretty-printer and parser use + */ fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool { alt e.node { @@ -29,3 +30,47 @@ fn stmt_ends_with_semi(stmt: ast::stmt) -> bool { } } } + +fn operator_prec(op: ast::binop) -> int { + for vec::each(*parse::prec::binop_prec_table()) {|spec| + if spec.op == op { ret spec.prec; } + } + core::unreachable(); +} + +fn need_parens(expr: @ast::expr, outer_prec: int) -> bool { + alt expr.node { + ast::expr_binary(op, _, _) { operator_prec(op) < outer_prec } + ast::expr_cast(_, _) { parse::prec::as_prec < outer_prec } + // This may be too conservative in some cases + ast::expr_assign(_, _) { true } + ast::expr_move(_, _) { true } + ast::expr_swap(_, _) { true } + ast::expr_assign_op(_, _, _) { true } + ast::expr_ret(_) { true } + ast::expr_be(_) { true } + ast::expr_assert(_) { true } + ast::expr_check(_, _) { true } + ast::expr_log(_, _, _) { true } + _ { !parse::classify::expr_requires_semi_to_be_stmt(expr) } + } +} + +fn ends_in_lit_int(ex: @ast::expr) -> bool { + alt ex.node { + ast::expr_lit(@{node: ast::lit_int(_, ast::ty_i), _}) { true } + ast::expr_binary(_, _, sub) | ast::expr_unary(_, sub) | + ast::expr_move(_, sub) | ast::expr_copy(sub) | + ast::expr_assign(_, sub) | ast::expr_be(sub) | + ast::expr_assign_op(_, _, sub) | ast::expr_swap(_, sub) | + ast::expr_log(_, _, sub) | ast::expr_assert(sub) | + ast::expr_check(_, sub) { ends_in_lit_int(sub) } + ast::expr_fail(osub) | ast::expr_ret(osub) { + alt osub { + some(ex) { ends_in_lit_int(ex) } + _ { false } + } + } + _ { false } + } +} diff --git a/src/librustsyntax/print/pprust.rs b/src/librustsyntax/print/pprust.rs index dbe5f3472d8..29557785fc7 100644 --- a/src/librustsyntax/print/pprust.rs +++ b/src/librustsyntax/print/pprust.rs @@ -1,3 +1,4 @@ +import parse::classify::*; import parse::comments; import parse::lexer; import codemap::codemap; @@ -242,6 +243,12 @@ fn is_bol(s: ps) -> bool { s.s.last_token() == pp::hardbreak_tok(); } +fn in_cbox(s: ps) -> bool { + let len = vec::len(s.boxes); + if len == 0u { ret false; } + ret s.boxes[len - 1u] == pp::consistent; +} + fn hardbreak_if_not_bol(s: ps) { if !is_bol(s) { hardbreak(s.s); } } fn space_if_not_bol(s: ps) { if !is_bol(s) { space(s.s); } } fn break_offset_if_not_bol(s: ps, n: uint, off: int) { @@ -1484,34 +1491,6 @@ fn print_view_item(s: ps, item: @ast::view_item) { end(s); // end outer head-block } - -// FIXME: The fact that this builds up the table anew for every call is -// not good. Eventually, table should be a const. -fn operator_prec(op: ast::binop) -> int { - for vec::each(*parse::prec::binop_prec_table()) {|spec| - if spec.op == op { ret spec.prec; } - } - core::unreachable(); -} - -fn need_parens(expr: @ast::expr, outer_prec: int) -> bool { - alt expr.node { - ast::expr_binary(op, _, _) { operator_prec(op) < outer_prec } - ast::expr_cast(_, _) { parse::prec::as_prec < outer_prec } - // This may be too conservative in some cases - ast::expr_assign(_, _) { true } - ast::expr_move(_, _) { true } - ast::expr_swap(_, _) { true } - ast::expr_assign_op(_, _, _) { true } - ast::expr_ret(_) { true } - ast::expr_be(_) { true } - ast::expr_assert(_) { true } - ast::expr_check(_, _) { true } - ast::expr_log(_, _, _) { true } - _ { !parse::classify::expr_requires_semi_to_be_stmt(expr) } - } -} - fn print_op_maybe_parens(s: ps, expr: @ast::expr, outer_prec: int) { let add_them = need_parens(expr, outer_prec); if add_them { popen(s); } @@ -1596,12 +1575,6 @@ fn print_remaining_comments(s: ps) { } } -fn in_cbox(s: ps) -> bool { - let len = vec::len(s.boxes); - if len == 0u { ret false; } - ret s.boxes[len - 1u] == pp::consistent; -} - fn print_literal(s: ps, &&lit: @ast::lit) { maybe_print_comment(s, lit.span.lo); alt next_lit(s, lit.span.lo) { @@ -1839,25 +1812,6 @@ fn proto_to_str(p: ast::proto) -> str { }; } -fn ends_in_lit_int(ex: @ast::expr) -> bool { - alt ex.node { - ast::expr_lit(@{node: ast::lit_int(_, ast::ty_i), _}) { true } - ast::expr_binary(_, _, sub) | ast::expr_unary(_, sub) | - ast::expr_move(_, sub) | ast::expr_copy(sub) | - ast::expr_assign(_, sub) | ast::expr_be(sub) | - ast::expr_assign_op(_, _, sub) | ast::expr_swap(_, sub) | - ast::expr_log(_, _, sub) | ast::expr_assert(sub) | - ast::expr_check(_, sub) { ends_in_lit_int(sub) } - ast::expr_fail(osub) | ast::expr_ret(osub) { - alt osub { - some(ex) { ends_in_lit_int(ex) } - _ { false } - } - } - _ { false } - } -} - // // Local Variables: // mode: rust |
