Skip to content

Commit 37ea010

Browse files
committed
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
1 parent 1da18c7 commit 37ea010

File tree

2 files changed

+54
-55
lines changed

2 files changed

+54
-55
lines changed

src/librustsyntax/parse/classify.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// FIXME: There are a bunch of similar functions in pprust that
2-
// likely belong here
1+
/*
2+
Predicates on exprs and stmts that the pretty-printer and parser use
3+
*/
34

45
fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool {
56
alt e.node {
@@ -29,3 +30,47 @@ fn stmt_ends_with_semi(stmt: ast::stmt) -> bool {
2930
}
3031
}
3132
}
33+
34+
fn operator_prec(op: ast::binop) -> int {
35+
for vec::each(*parse::prec::binop_prec_table()) {|spec|
36+
if spec.op == op { ret spec.prec; }
37+
}
38+
core::unreachable();
39+
}
40+
41+
fn need_parens(expr: @ast::expr, outer_prec: int) -> bool {
42+
alt expr.node {
43+
ast::expr_binary(op, _, _) { operator_prec(op) < outer_prec }
44+
ast::expr_cast(_, _) { parse::prec::as_prec < outer_prec }
45+
// This may be too conservative in some cases
46+
ast::expr_assign(_, _) { true }
47+
ast::expr_move(_, _) { true }
48+
ast::expr_swap(_, _) { true }
49+
ast::expr_assign_op(_, _, _) { true }
50+
ast::expr_ret(_) { true }
51+
ast::expr_be(_) { true }
52+
ast::expr_assert(_) { true }
53+
ast::expr_check(_, _) { true }
54+
ast::expr_log(_, _, _) { true }
55+
_ { !parse::classify::expr_requires_semi_to_be_stmt(expr) }
56+
}
57+
}
58+
59+
fn ends_in_lit_int(ex: @ast::expr) -> bool {
60+
alt ex.node {
61+
ast::expr_lit(@{node: ast::lit_int(_, ast::ty_i), _}) { true }
62+
ast::expr_binary(_, _, sub) | ast::expr_unary(_, sub) |
63+
ast::expr_move(_, sub) | ast::expr_copy(sub) |
64+
ast::expr_assign(_, sub) | ast::expr_be(sub) |
65+
ast::expr_assign_op(_, _, sub) | ast::expr_swap(_, sub) |
66+
ast::expr_log(_, _, sub) | ast::expr_assert(sub) |
67+
ast::expr_check(_, sub) { ends_in_lit_int(sub) }
68+
ast::expr_fail(osub) | ast::expr_ret(osub) {
69+
alt osub {
70+
some(ex) { ends_in_lit_int(ex) }
71+
_ { false }
72+
}
73+
}
74+
_ { false }
75+
}
76+
}

src/librustsyntax/print/pprust.rs

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import parse::classify::*;
12
import parse::comments;
23
import parse::lexer;
34
import codemap::codemap;
@@ -242,6 +243,12 @@ fn is_bol(s: ps) -> bool {
242243
s.s.last_token() == pp::hardbreak_tok();
243244
}
244245

246+
fn in_cbox(s: ps) -> bool {
247+
let len = vec::len(s.boxes);
248+
if len == 0u { ret false; }
249+
ret s.boxes[len - 1u] == pp::consistent;
250+
}
251+
245252
fn hardbreak_if_not_bol(s: ps) { if !is_bol(s) { hardbreak(s.s); } }
246253
fn space_if_not_bol(s: ps) { if !is_bol(s) { space(s.s); } }
247254
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) {
14841491
end(s); // end outer head-block
14851492
}
14861493

1487-
1488-
// FIXME: The fact that this builds up the table anew for every call is
1489-
// not good. Eventually, table should be a const.
1490-
fn operator_prec(op: ast::binop) -> int {
1491-
for vec::each(*parse::prec::binop_prec_table()) {|spec|
1492-
if spec.op == op { ret spec.prec; }
1493-
}
1494-
core::unreachable();
1495-
}
1496-
1497-
fn need_parens(expr: @ast::expr, outer_prec: int) -> bool {
1498-
alt expr.node {
1499-
ast::expr_binary(op, _, _) { operator_prec(op) < outer_prec }
1500-
ast::expr_cast(_, _) { parse::prec::as_prec < outer_prec }
1501-
// This may be too conservative in some cases
1502-
ast::expr_assign(_, _) { true }
1503-
ast::expr_move(_, _) { true }
1504-
ast::expr_swap(_, _) { true }
1505-
ast::expr_assign_op(_, _, _) { true }
1506-
ast::expr_ret(_) { true }
1507-
ast::expr_be(_) { true }
1508-
ast::expr_assert(_) { true }
1509-
ast::expr_check(_, _) { true }
1510-
ast::expr_log(_, _, _) { true }
1511-
_ { !parse::classify::expr_requires_semi_to_be_stmt(expr) }
1512-
}
1513-
}
1514-
15151494
fn print_op_maybe_parens(s: ps, expr: @ast::expr, outer_prec: int) {
15161495
let add_them = need_parens(expr, outer_prec);
15171496
if add_them { popen(s); }
@@ -1596,12 +1575,6 @@ fn print_remaining_comments(s: ps) {
15961575
}
15971576
}
15981577

1599-
fn in_cbox(s: ps) -> bool {
1600-
let len = vec::len(s.boxes);
1601-
if len == 0u { ret false; }
1602-
ret s.boxes[len - 1u] == pp::consistent;
1603-
}
1604-
16051578
fn print_literal(s: ps, &&lit: @ast::lit) {
16061579
maybe_print_comment(s, lit.span.lo);
16071580
alt next_lit(s, lit.span.lo) {
@@ -1839,25 +1812,6 @@ fn proto_to_str(p: ast::proto) -> str {
18391812
};
18401813
}
18411814

1842-
fn ends_in_lit_int(ex: @ast::expr) -> bool {
1843-
alt ex.node {
1844-
ast::expr_lit(@{node: ast::lit_int(_, ast::ty_i), _}) { true }
1845-
ast::expr_binary(_, _, sub) | ast::expr_unary(_, sub) |
1846-
ast::expr_move(_, sub) | ast::expr_copy(sub) |
1847-
ast::expr_assign(_, sub) | ast::expr_be(sub) |
1848-
ast::expr_assign_op(_, _, sub) | ast::expr_swap(_, sub) |
1849-
ast::expr_log(_, _, sub) | ast::expr_assert(sub) |
1850-
ast::expr_check(_, sub) { ends_in_lit_int(sub) }
1851-
ast::expr_fail(osub) | ast::expr_ret(osub) {
1852-
alt osub {
1853-
some(ex) { ends_in_lit_int(ex) }
1854-
_ { false }
1855-
}
1856-
}
1857-
_ { false }
1858-
}
1859-
}
1860-
18611815
//
18621816
// Local Variables:
18631817
// mode: rust

0 commit comments

Comments
 (0)