2012-04-19 14:46:11 -07:00
|
|
|
/*
|
|
|
|
Predicates on exprs and stmts that the pretty-printer and parser use
|
|
|
|
*/
|
2012-05-22 10:54:12 -07:00
|
|
|
|
|
|
|
import ast_util::operator_prec;
|
2012-04-17 22:02:00 -07:00
|
|
|
|
|
|
|
fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool {
|
2012-08-06 12:34:08 -07:00
|
|
|
match e.node {
|
2012-08-21 17:22:45 -07:00
|
|
|
ast::expr_if(*) | ast::expr_match(*) | ast::expr_block(_)
|
|
|
|
| ast::expr_while(*) | ast::expr_loop(*)
|
2012-08-03 19:59:04 -07:00
|
|
|
| ast::expr_call(_, _, true) => false,
|
|
|
|
_ => true
|
2012-04-17 22:02:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 17:14:32 -07:00
|
|
|
fn expr_is_simple_block(e: @ast::expr) -> bool {
|
|
|
|
match e.node {
|
|
|
|
ast::expr_block({node: {rules: ast::default_blk, _}, _}) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-17 22:02:00 -07:00
|
|
|
fn stmt_ends_with_semi(stmt: ast::stmt) -> bool {
|
2012-08-06 12:34:08 -07:00
|
|
|
match stmt.node {
|
2012-08-03 19:59:04 -07:00
|
|
|
ast::stmt_decl(d, _) => {
|
2012-08-06 12:34:08 -07:00
|
|
|
return match d.node {
|
2012-08-03 19:59:04 -07:00
|
|
|
ast::decl_local(_) => true,
|
|
|
|
ast::decl_item(_) => false
|
2012-04-17 22:02:00 -07:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
ast::stmt_expr(e, _) => {
|
2012-08-01 17:30:05 -07:00
|
|
|
return expr_requires_semi_to_be_stmt(e);
|
2012-04-17 22:02:00 -07:00
|
|
|
}
|
2012-08-24 14:04:38 -07:00
|
|
|
ast::stmt_semi(*) => {
|
2012-08-01 17:30:05 -07:00
|
|
|
return false;
|
2012-07-03 17:30:25 -07:00
|
|
|
}
|
2012-04-17 22:02:00 -07:00
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:46:11 -07:00
|
|
|
|
2012-04-26 16:13:59 -07:00
|
|
|
fn need_parens(expr: @ast::expr, outer_prec: uint) -> bool {
|
2012-08-06 12:34:08 -07:00
|
|
|
match expr.node {
|
2012-08-03 19:59:04 -07:00
|
|
|
ast::expr_binary(op, _, _) => operator_prec(op) < outer_prec,
|
|
|
|
ast::expr_cast(_, _) => parse::prec::as_prec < outer_prec,
|
2012-04-19 14:46:11 -07:00
|
|
|
// This may be too conservative in some cases
|
2012-08-03 19:59:04 -07:00
|
|
|
ast::expr_assign(_, _) => true,
|
|
|
|
ast::expr_move(_, _) => 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)
|
2012-04-19 14:46:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ends_in_lit_int(ex: @ast::expr) -> bool {
|
2012-08-06 12:34:08 -07:00
|
|
|
match ex.node {
|
|
|
|
ast::expr_lit(node) => match node {
|
2012-08-03 19:59:04 -07:00
|
|
|
@{node: ast::lit_int(_, ast::ty_i), _}
|
|
|
|
| @{node: ast::lit_int_unsuffixed(_), _} => true,
|
|
|
|
_ => false
|
2012-08-06 17:14:32 -07:00
|
|
|
},
|
2012-04-19 14:46:11 -07:00
|
|
|
ast::expr_binary(_, _, sub) | ast::expr_unary(_, sub) |
|
|
|
|
ast::expr_move(_, sub) | ast::expr_copy(sub) |
|
2012-05-14 16:55:01 -07:00
|
|
|
ast::expr_assign(_, sub) |
|
2012-04-19 14:46:11 -07:00
|
|
|
ast::expr_assign_op(_, _, sub) | ast::expr_swap(_, sub) |
|
2012-08-03 19:59:04 -07:00
|
|
|
ast::expr_log(_, _, sub) | ast::expr_assert(sub) => {
|
2012-07-13 18:43:52 -07:00
|
|
|
ends_in_lit_int(sub)
|
|
|
|
}
|
2012-08-06 12:34:08 -07:00
|
|
|
ast::expr_fail(osub) | ast::expr_ret(osub) => match osub {
|
2012-08-20 12:23:37 -07:00
|
|
|
Some(ex) => ends_in_lit_int(ex),
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => false
|
2012-08-06 17:14:32 -07:00
|
|
|
},
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => false
|
2012-04-19 14:46:11 -07:00
|
|
|
}
|
|
|
|
}
|