Skip to content

Commit

Permalink
Preserve parenthesization in the AST
Browse files Browse the repository at this point in the history
for better pretty-printing, as per #1458
  • Loading branch information
catamorphism committed Oct 30, 2012
1 parent 17a5d0f commit cf50e1d
Show file tree
Hide file tree
Showing 20 changed files with 148 additions and 217 deletions.
5 changes: 4 additions & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
36 changes: 0 additions & 36 deletions src/libsyntax/parse/classify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading

0 comments on commit cf50e1d

Please sign in to comment.