diff options
Diffstat (limited to 'src/libsyntax/ext')
| -rw-r--r-- | src/libsyntax/ext/base.rs | 12 | ||||
| -rw-r--r-- | src/libsyntax/ext/quote.rs | 51 |
2 files changed, 63 insertions, 0 deletions
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 3f925c9d7ba..3e854cd72ba 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -512,6 +512,18 @@ fn initial_syntax_expander_table<'feat>(ecfg: &expand::ExpansionConfig<'feat>) syntax_expanders.insert(intern("quote_attr"), builtin_normal_expander( ext::quote::expand_quote_attr)); + syntax_expanders.insert(intern("quote_arg"), + builtin_normal_expander( + ext::quote::expand_quote_arg)); + syntax_expanders.insert(intern("quote_block"), + builtin_normal_expander( + ext::quote::expand_quote_block)); + syntax_expanders.insert(intern("quote_meta_item"), + builtin_normal_expander( + ext::quote::expand_quote_meta_item)); + syntax_expanders.insert(intern("quote_path"), + builtin_normal_expander( + ext::quote::expand_quote_path)); } syntax_expanders.insert(intern("line"), diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 5e1d2339164..dc4a061ce78 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -158,6 +158,18 @@ pub mod rt { } } + impl ToTokens for ast::Arg { + fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> { + vec![TokenTree::Token(DUMMY_SP, token::Interpolated(token::NtArg(self.clone())))] + } + } + + impl ToTokens for P<ast::Block> { + fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> { + vec![TokenTree::Token(DUMMY_SP, token::Interpolated(token::NtBlock(self.clone())))] + } + } + macro_rules! impl_to_tokens_slice { ($t: ty, $sep: expr) => { impl ToTokens for [$t] { @@ -177,6 +189,7 @@ pub mod rt { impl_to_tokens_slice! { ast::Ty, [TokenTree::Token(DUMMY_SP, token::Comma)] } impl_to_tokens_slice! { P<ast::Item>, [] } + impl_to_tokens_slice! { ast::Arg, [TokenTree::Token(DUMMY_SP, token::Comma)] } impl ToTokens for P<ast::MetaItem> { fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> { @@ -383,6 +396,39 @@ pub fn expand_quote_attr(cx: &mut ExtCtxt, base::MacEager::expr(expanded) } +pub fn expand_quote_arg(cx: &mut ExtCtxt, + sp: Span, + tts: &[TokenTree]) + -> Box<base::MacResult+'static> { + let expanded = expand_parse_call(cx, sp, "parse_arg_panic", vec!(), tts); + base::MacEager::expr(expanded) +} + +pub fn expand_quote_block(cx: &mut ExtCtxt, + sp: Span, + tts: &[TokenTree]) + -> Box<base::MacResult+'static> { + let expanded = expand_parse_call(cx, sp, "parse_block_panic", vec!(), tts); + base::MacEager::expr(expanded) +} + +pub fn expand_quote_meta_item(cx: &mut ExtCtxt, + sp: Span, + tts: &[TokenTree]) + -> Box<base::MacResult+'static> { + let expanded = expand_parse_call(cx, sp, "parse_meta_item_panic", vec!(), tts); + base::MacEager::expr(expanded) +} + +pub fn expand_quote_path(cx: &mut ExtCtxt, + sp: Span, + tts: &[TokenTree]) + -> Box<base::MacResult+'static> { + let mode = mk_parser_path(cx, sp, "LifetimeAndTypesWithoutColons"); + let expanded = expand_parse_call(cx, sp, "parse_path_panic", vec!(mode), tts); + base::MacEager::expr(expanded) +} + pub fn expand_quote_matcher(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) @@ -440,6 +486,11 @@ fn mk_token_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> { cx.expr_path(cx.path_global(sp, idents)) } +fn mk_parser_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> { + let idents = vec!(id_ext("syntax"), id_ext("parse"), id_ext("parser"), id_ext(name)); + cx.expr_path(cx.path_global(sp, idents)) +} + fn mk_binop(cx: &ExtCtxt, sp: Span, bop: token::BinOpToken) -> P<ast::Expr> { let name = match bop { token::Plus => "Plus", |
