Skip to content

Commit e841a88

Browse files
committed
syntax: add support for quoting arms
1 parent 1200ad0 commit e841a88

File tree

7 files changed

+47
-29
lines changed

7 files changed

+47
-29
lines changed

src/libsyntax/ext/base.rs

+3
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ fn initial_syntax_expander_table() -> SyntaxEnv {
382382
syntax_expanders.insert(intern("quote_pat"),
383383
builtin_normal_expander(
384384
ext::quote::expand_quote_pat));
385+
syntax_expanders.insert(intern("quote_arm"),
386+
builtin_normal_expander(
387+
ext::quote::expand_quote_arm));
385388
syntax_expanders.insert(intern("quote_stmt"),
386389
builtin_normal_expander(
387390
ext::quote::expand_quote_stmt));

src/libsyntax/ext/quote.rs

+8
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,14 @@ pub fn expand_quote_pat(cx: &mut ExtCtxt,
349349
base::MacExpr::new(expanded)
350350
}
351351

352+
pub fn expand_quote_arm(cx: &mut ExtCtxt,
353+
sp: Span,
354+
tts: &[ast::TokenTree])
355+
-> Box<base::MacResult> {
356+
let expanded = expand_parse_call(cx, sp, "parse_arm", vec!(), tts);
357+
base::MacExpr::new(expanded)
358+
}
359+
352360
pub fn expand_quote_ty(cx: &mut ExtCtxt,
353361
sp: Span,
354362
tts: &[ast::TokenTree])

src/libsyntax/parse/parser.rs

+29-25
Original file line numberDiff line numberDiff line change
@@ -2727,37 +2727,41 @@ impl<'a> Parser<'a> {
27272727
self.commit_expr_expecting(discriminant, token::LBRACE);
27282728
let mut arms: Vec<Arm> = Vec::new();
27292729
while self.token != token::RBRACE {
2730-
let attrs = self.parse_outer_attributes();
2731-
let pats = self.parse_pats();
2732-
let mut guard = None;
2733-
if self.eat_keyword(keywords::If) {
2734-
guard = Some(self.parse_expr());
2735-
}
2736-
self.expect(&token::FAT_ARROW);
2737-
let expr = self.parse_expr_res(RESTRICT_STMT_EXPR);
2738-
2739-
let require_comma =
2740-
!classify::expr_is_simple_block(expr)
2741-
&& self.token != token::RBRACE;
2742-
2743-
if require_comma {
2744-
self.commit_expr(expr, &[token::COMMA], &[token::RBRACE]);
2745-
} else {
2746-
self.eat(&token::COMMA);
2747-
}
2748-
2749-
arms.push(ast::Arm {
2750-
attrs: attrs,
2751-
pats: pats,
2752-
guard: guard,
2753-
body: expr
2754-
});
2730+
arms.push(self.parse_arm());
27552731
}
27562732
let hi = self.span.hi;
27572733
self.bump();
27582734
return self.mk_expr(lo, hi, ExprMatch(discriminant, arms));
27592735
}
27602736

2737+
pub fn parse_arm(&mut self) -> Arm {
2738+
let attrs = self.parse_outer_attributes();
2739+
let pats = self.parse_pats();
2740+
let mut guard = None;
2741+
if self.eat_keyword(keywords::If) {
2742+
guard = Some(self.parse_expr());
2743+
}
2744+
self.expect(&token::FAT_ARROW);
2745+
let expr = self.parse_expr_res(RESTRICT_STMT_EXPR);
2746+
2747+
let require_comma =
2748+
!classify::expr_is_simple_block(expr)
2749+
&& self.token != token::RBRACE;
2750+
2751+
if require_comma {
2752+
self.commit_expr(expr, &[token::COMMA], &[token::RBRACE]);
2753+
} else {
2754+
self.eat(&token::COMMA);
2755+
}
2756+
2757+
ast::Arm {
2758+
attrs: attrs,
2759+
pats: pats,
2760+
guard: guard,
2761+
body: expr,
2762+
}
2763+
}
2764+
27612765
/// Parse an expression
27622766
pub fn parse_expr(&mut self) -> Gc<Expr> {
27632767
return self.parse_expr_res(UNRESTRICTED);

src/test/run-make/graphviz-flowgraph/f07.dot-expected.dot

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ digraph block {
66
N4[label="expr 777i"];
77
N5[label="expr 7777i"];
88
N6[label="expr [7i, 77i, 777i, 7777i]"];
9-
N7[label="expr match [7i, 77i, 777i, 7777i] { [x, y, ..] => x + y }"];
9+
N7[label="expr match [7i, 77i, 777i, 7777i] { [x, y, ..] => x + y, }"];
1010
N8[label="(dummy_node)"];
1111
N9[label="local x"];
1212
N10[label="local y"];
@@ -15,7 +15,7 @@ digraph block {
1515
N13[label="expr x"];
1616
N14[label="expr y"];
1717
N15[label="expr x + y"];
18-
N16[label="block { match [7i, 77i, 777i, 7777i] { [x, y, ..] => x + y }; }"];
18+
N16[label="block { match [7i, 77i, 777i, 7777i] { [x, y, ..] => x + y, }; }"];
1919
N0 -> N2;
2020
N2 -> N3;
2121
N3 -> N4;

src/test/run-make/graphviz-flowgraph/f13.dot-expected.dot

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ digraph block {
77
N5[label="local x"];
88
N6[label="local _y"];
99
N7[label="expr x"];
10-
N8[label="expr match x { E13a => _y = 1, E13b(v) => _y = v + 1 }"];
10+
N8[label="expr match x { E13a => _y = 1, E13b(v) => _y = v + 1, }"];
1111
N9[label="(dummy_node)"];
1212
N10[label="local E13a"];
1313
N11[label="expr 1"];
@@ -21,7 +21,7 @@ digraph block {
2121
N19[label="expr v + 1"];
2222
N20[label="expr _y"];
2323
N21[label="expr _y = v + 1"];
24-
N22[label="block {\l let x = E13b(13);\l let _y;\l match x { E13a => _y = 1, E13b(v) => _y = v + 1 }\l}\l"];
24+
N22[label="block {\l let x = E13b(13);\l let _y;\l match x { E13a => _y = 1, E13b(v) => _y = v + 1, }\l}\l"];
2525
N0 -> N2;
2626
N2 -> N3;
2727
N3 -> N4;

src/test/run-pass-fulldeps/qquote.rs

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ fn main() {
7272
let pat = quote_pat!(cx, Some(_));
7373
check_pp(ext_cx, pat, pprust::print_pat, "Some(_)".to_string());
7474

75+
let arm = quote_arm!(cx, (ref x, ref y) => (x, y));
76+
check_pp(ext_cx, arm, pprust::print_stmt, "(ref x, ref y) = (x, y)".to_string());
7577
}
7678

7779
fn check_pp<T>(cx: fake_ext_ctxt,

src/test/run-pass-fulldeps/quote-tokens.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn syntax_extension(cx: &ExtCtxt) {
2626
let _b: Option<Gc<syntax::ast::Item>> = quote_item!(cx, static foo : int = $e_toks; );
2727
let _c: Gc<syntax::ast::Pat> = quote_pat!(cx, (x, 1 .. 4, *) );
2828
let _d: Gc<syntax::ast::Stmt> = quote_stmt!(cx, let x = $a; );
29+
let _d: syntax::ast::Arm = quote_arm!(cx, (ref x, ref y) = (x, y) );
2930
let _e: Gc<syntax::ast::Expr> = quote_expr!(cx, match foo { $p_toks => 10 } );
3031

3132
let _f: Gc<syntax::ast::Expr> = quote_expr!(cx, ());

0 commit comments

Comments
 (0)