Permalink
Browse files

Preserve parenthesization in the AST

for better pretty-printing, as per #1458
  • Loading branch information...
1 parent 17a5d0f commit cf50e1d147738865b2ee196a07df9b8fb24c9583 @catamorphism catamorphism committed Oct 28, 2012
View
@@ -744,7 +744,10 @@ enum expr_ {
expr_struct(@path, ~[field], Option<@expr>),
// A vector literal constructed from one repeated element.
- expr_repeat(@expr /* element */, @expr /* count */, mutability)
+ expr_repeat(@expr /* element */, @expr /* count */, mutability),
+
+ // No-op: used solely so we can pretty-print faithfully
+ expr_paren(@expr)
}
#[auto_serialize]
View
@@ -494,7 +494,8 @@ fn noop_fold_expr(e: expr_, fld: ast_fold) -> expr_ {
expr_struct(fld.fold_path(path),
vec::map(fields, |x| fold_field(*x)),
option::map(&maybe_expr, |x| fld.fold_expr(*x)))
- }
+ },
+ expr_paren(ex) => expr_paren(fld.fold_expr(ex))
}
}
@@ -36,39 +36,3 @@ fn stmt_ends_with_semi(stmt: ast::stmt) -> bool {
}
}
}
-
-fn need_parens(expr: @ast::expr, outer_prec: uint) -> bool {
- match 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_swap(_, _) => true,
- ast::expr_assign_op(_, _, _) => true,
- ast::expr_ret(_) => true,
- ast::expr_assert(_) => true,
- ast::expr_log(_, _, _) => true,
- _ => !parse::classify::expr_requires_semi_to_be_stmt(expr)
- }
-}
-
-fn ends_in_lit_int(ex: @ast::expr) -> bool {
- match ex.node {
- ast::expr_lit(node) => match node {
- @{node: ast::lit_int(_, ast::ty_i), _}
- | @{node: ast::lit_int_unsuffixed(_), _} => true,
- _ => false
- },
- ast::expr_binary(_, _, sub) | ast::expr_unary(_, sub) |
- ast::expr_copy(sub) | ast::expr_assign(_, sub) |
- ast::expr_assign_op(_, _, sub) | ast::expr_swap(_, sub) |
- ast::expr_log(_, _, sub) | ast::expr_assert(sub) => {
- ends_in_lit_int(sub)
- }
- ast::expr_fail(osub) | ast::expr_ret(osub) => match osub {
- Some(ex) => ends_in_lit_int(ex),
- _ => false
- },
- _ => false
- }
-}
Oops, something went wrong.

0 comments on commit cf50e1d

Please sign in to comment.