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 /src/librustsyntax/parse | |
| 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
Diffstat (limited to 'src/librustsyntax/parse')
| -rw-r--r-- | src/librustsyntax/parse/classify.rs | 49 |
1 files changed, 47 insertions, 2 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 } + } +} |
