accept naked exprs with commas in pattern arms
pretty printing will use them, but indentation is slightly off if the expr is long
This commit is contained in:
parent
a89ed49d3d
commit
c206d024eb
6 changed files with 76 additions and 6 deletions
|
@ -588,6 +588,13 @@ fn view_path_id(p: @view_path) -> node_id {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn lone_block_expr(blk: blk) -> option<@ast::expr> {
|
||||||
|
if blk.node.view_items.len() != 0 { ret none; }
|
||||||
|
if blk.node.stmts.len() != 0 { ret none; }
|
||||||
|
if blk.node.rules != default_blk { ret none; }
|
||||||
|
ret blk.node.expr;
|
||||||
|
}
|
||||||
|
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
// mode: rust
|
// mode: rust
|
||||||
// fill-column: 78;
|
// fill-column: 78;
|
||||||
|
|
|
@ -1511,8 +1511,25 @@ class parser {
|
||||||
let pats = self.parse_pats();
|
let pats = self.parse_pats();
|
||||||
let mut guard = none;
|
let mut guard = none;
|
||||||
if self.eat_keyword(~"if") { guard = some(self.parse_expr()); }
|
if self.eat_keyword(~"if") { guard = some(self.parse_expr()); }
|
||||||
if self.token == token::FAT_ARROW { self.bump(); }
|
let blk = if self.token != token::FAT_ARROW {
|
||||||
let blk = self.parse_block();
|
self.parse_block()
|
||||||
|
} else {
|
||||||
|
self.bump();
|
||||||
|
if self.token == token::LBRACE {
|
||||||
|
self.parse_block()
|
||||||
|
} else {
|
||||||
|
let expr = self.parse_expr();
|
||||||
|
if self.token != token::RBRACE {
|
||||||
|
self.expect(token::COMMA);
|
||||||
|
}
|
||||||
|
{node: {view_items: ~[],
|
||||||
|
stmts: ~[],
|
||||||
|
expr: some(expr),
|
||||||
|
id: self.get_id(),
|
||||||
|
rules: default_blk},
|
||||||
|
span: expr.span}
|
||||||
|
}
|
||||||
|
};
|
||||||
vec::push(arms, {pats: pats, guard: guard, body: blk});
|
vec::push(arms, {pats: pats, guard: guard, body: blk});
|
||||||
}
|
}
|
||||||
let mut hi = self.span.hi;
|
let mut hi = self.span.hi;
|
||||||
|
|
|
@ -6,7 +6,7 @@ import pp::{break_offset, word, printer,
|
||||||
inconsistent, eof};
|
inconsistent, eof};
|
||||||
import diagnostic;
|
import diagnostic;
|
||||||
import ast::{required, provided};
|
import ast::{required, provided};
|
||||||
import ast_util::operator_prec;
|
import ast_util::{operator_prec, lone_block_expr};
|
||||||
import dvec::{dvec, extensions};
|
import dvec::{dvec, extensions};
|
||||||
import parse::classify::*;
|
import parse::classify::*;
|
||||||
import util::interner;
|
import util::interner;
|
||||||
|
@ -1034,7 +1034,8 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
|
||||||
print_maybe_parens_discrim(s, expr);
|
print_maybe_parens_discrim(s, expr);
|
||||||
space(s.s);
|
space(s.s);
|
||||||
bopen(s);
|
bopen(s);
|
||||||
for arms.each |arm| {
|
let len = arms.len();
|
||||||
|
for arms.eachi |i, arm| {
|
||||||
space(s.s);
|
space(s.s);
|
||||||
cbox(s, alt_indent_unit);
|
cbox(s, alt_indent_unit);
|
||||||
ibox(s, 0u);
|
ibox(s, 0u);
|
||||||
|
@ -1050,9 +1051,20 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
|
||||||
some(e) { word_space(s, ~"if"); print_expr(s, e); space(s.s); }
|
some(e) { word_space(s, ~"if"); print_expr(s, e); space(s.s); }
|
||||||
none { }
|
none { }
|
||||||
}
|
}
|
||||||
|
word_space(s, ~"=>");
|
||||||
|
alt lone_block_expr(arm.body) {
|
||||||
|
some(expr) => {
|
||||||
|
end(s); // close the ibox for the pattern
|
||||||
|
print_expr(s, expr);
|
||||||
|
if i < len - 1 { word_space(s, ~","); }
|
||||||
|
end(s); // close enclosing cbox
|
||||||
|
}
|
||||||
|
none => {
|
||||||
print_possibly_embedded_block(s, arm.body, block_normal,
|
print_possibly_embedded_block(s, arm.body, block_normal,
|
||||||
alt_indent_unit);
|
alt_indent_unit);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
bclose_(s, expr.span, alt_indent_unit);
|
bclose_(s, expr.span, alt_indent_unit);
|
||||||
}
|
}
|
||||||
ast::expr_fn(proto, decl, body, cap_clause) {
|
ast::expr_fn(proto, decl, body, cap_clause) {
|
||||||
|
|
17
src/test/pretty/alt-naked-expr-long.rs
Normal file
17
src/test/pretty/alt-naked-expr-long.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// pretty-exact
|
||||||
|
|
||||||
|
// actually this doesn't quite look how I want it to, but I can't
|
||||||
|
// get the prettyprinter to indent the long expr
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = some(3);
|
||||||
|
let y =
|
||||||
|
alt x {
|
||||||
|
some(_) =>
|
||||||
|
"some" + "very" + "very" + "very" + "very" + "very" + "very" +
|
||||||
|
"very" + "very" + "long" + "string",
|
||||||
|
|
||||||
|
none => "none"
|
||||||
|
};
|
||||||
|
assert y == "some(_)";
|
||||||
|
}
|
10
src/test/pretty/alt-naked-expr-medium.rs
Normal file
10
src/test/pretty/alt-naked-expr-medium.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
// pretty-exact
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = some(3);
|
||||||
|
let _y =
|
||||||
|
alt x {
|
||||||
|
some(_) => ~[~"some(_)", ~"not", ~"SO", ~"long", ~"string"],
|
||||||
|
none => ~[~"none"]
|
||||||
|
};
|
||||||
|
}
|
7
src/test/pretty/alt-naked-expr.rs
Normal file
7
src/test/pretty/alt-naked-expr.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
// pretty-exact
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = some(3);
|
||||||
|
let y = alt x { some(_) => "some(_)", none => "none" };
|
||||||
|
assert y == "some(_)";
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue