1
Fork 0

syntax: add support for quoting arms

This commit is contained in:
Erick Tryzelaar 2014-07-28 17:32:51 -07:00
parent 1200ad0f06
commit e841a88b92
7 changed files with 47 additions and 29 deletions

View file

@ -382,6 +382,9 @@ fn initial_syntax_expander_table() -> SyntaxEnv {
syntax_expanders.insert(intern("quote_pat"), syntax_expanders.insert(intern("quote_pat"),
builtin_normal_expander( builtin_normal_expander(
ext::quote::expand_quote_pat)); ext::quote::expand_quote_pat));
syntax_expanders.insert(intern("quote_arm"),
builtin_normal_expander(
ext::quote::expand_quote_arm));
syntax_expanders.insert(intern("quote_stmt"), syntax_expanders.insert(intern("quote_stmt"),
builtin_normal_expander( builtin_normal_expander(
ext::quote::expand_quote_stmt)); ext::quote::expand_quote_stmt));

View file

@ -349,6 +349,14 @@ pub fn expand_quote_pat(cx: &mut ExtCtxt,
base::MacExpr::new(expanded) base::MacExpr::new(expanded)
} }
pub fn expand_quote_arm(cx: &mut ExtCtxt,
sp: Span,
tts: &[ast::TokenTree])
-> Box<base::MacResult> {
let expanded = expand_parse_call(cx, sp, "parse_arm", vec!(), tts);
base::MacExpr::new(expanded)
}
pub fn expand_quote_ty(cx: &mut ExtCtxt, pub fn expand_quote_ty(cx: &mut ExtCtxt,
sp: Span, sp: Span,
tts: &[ast::TokenTree]) tts: &[ast::TokenTree])

View file

@ -2727,6 +2727,14 @@ impl<'a> Parser<'a> {
self.commit_expr_expecting(discriminant, token::LBRACE); self.commit_expr_expecting(discriminant, token::LBRACE);
let mut arms: Vec<Arm> = Vec::new(); let mut arms: Vec<Arm> = Vec::new();
while self.token != token::RBRACE { while self.token != token::RBRACE {
arms.push(self.parse_arm());
}
let hi = self.span.hi;
self.bump();
return self.mk_expr(lo, hi, ExprMatch(discriminant, arms));
}
pub fn parse_arm(&mut self) -> Arm {
let attrs = self.parse_outer_attributes(); let attrs = self.parse_outer_attributes();
let pats = self.parse_pats(); let pats = self.parse_pats();
let mut guard = None; let mut guard = None;
@ -2746,16 +2754,12 @@ impl<'a> Parser<'a> {
self.eat(&token::COMMA); self.eat(&token::COMMA);
} }
arms.push(ast::Arm { ast::Arm {
attrs: attrs, attrs: attrs,
pats: pats, pats: pats,
guard: guard, guard: guard,
body: expr body: expr,
});
} }
let hi = self.span.hi;
self.bump();
return self.mk_expr(lo, hi, ExprMatch(discriminant, arms));
} }
/// Parse an expression /// Parse an expression

View file

@ -6,7 +6,7 @@ digraph block {
N4[label="expr 777i"]; N4[label="expr 777i"];
N5[label="expr 7777i"]; N5[label="expr 7777i"];
N6[label="expr [7i, 77i, 777i, 7777i]"]; N6[label="expr [7i, 77i, 777i, 7777i]"];
N7[label="expr match [7i, 77i, 777i, 7777i] { [x, y, ..] => x + y }"]; N7[label="expr match [7i, 77i, 777i, 7777i] { [x, y, ..] => x + y, }"];
N8[label="(dummy_node)"]; N8[label="(dummy_node)"];
N9[label="local x"]; N9[label="local x"];
N10[label="local y"]; N10[label="local y"];
@ -15,7 +15,7 @@ digraph block {
N13[label="expr x"]; N13[label="expr x"];
N14[label="expr y"]; N14[label="expr y"];
N15[label="expr x + y"]; N15[label="expr x + y"];
N16[label="block { match [7i, 77i, 777i, 7777i] { [x, y, ..] => x + y }; }"]; N16[label="block { match [7i, 77i, 777i, 7777i] { [x, y, ..] => x + y, }; }"];
N0 -> N2; N0 -> N2;
N2 -> N3; N2 -> N3;
N3 -> N4; N3 -> N4;

View file

@ -7,7 +7,7 @@ digraph block {
N5[label="local x"]; N5[label="local x"];
N6[label="local _y"]; N6[label="local _y"];
N7[label="expr x"]; N7[label="expr x"];
N8[label="expr match x { E13a => _y = 1, E13b(v) => _y = v + 1 }"]; N8[label="expr match x { E13a => _y = 1, E13b(v) => _y = v + 1, }"];
N9[label="(dummy_node)"]; N9[label="(dummy_node)"];
N10[label="local E13a"]; N10[label="local E13a"];
N11[label="expr 1"]; N11[label="expr 1"];
@ -21,7 +21,7 @@ digraph block {
N19[label="expr v + 1"]; N19[label="expr v + 1"];
N20[label="expr _y"]; N20[label="expr _y"];
N21[label="expr _y = v + 1"]; N21[label="expr _y = v + 1"];
N22[label="block {\l let x = E13b(13);\l let _y;\l match x { E13a => _y = 1, E13b(v) => _y = v + 1 }\l}\l"]; N22[label="block {\l let x = E13b(13);\l let _y;\l match x { E13a => _y = 1, E13b(v) => _y = v + 1, }\l}\l"];
N0 -> N2; N0 -> N2;
N2 -> N3; N2 -> N3;
N3 -> N4; N3 -> N4;

View file

@ -72,6 +72,8 @@ fn main() {
let pat = quote_pat!(cx, Some(_)); let pat = quote_pat!(cx, Some(_));
check_pp(ext_cx, pat, pprust::print_pat, "Some(_)".to_string()); check_pp(ext_cx, pat, pprust::print_pat, "Some(_)".to_string());
let arm = quote_arm!(cx, (ref x, ref y) => (x, y));
check_pp(ext_cx, arm, pprust::print_stmt, "(ref x, ref y) = (x, y)".to_string());
} }
fn check_pp<T>(cx: fake_ext_ctxt, fn check_pp<T>(cx: fake_ext_ctxt,

View file

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