diff options
| author | bors <bors@rust-lang.org> | 2015-11-12 20:31:12 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-11-12 20:31:12 +0000 |
| commit | 5a872880bd42f5fc56a3ae35aa05a4e239f5bd35 (patch) | |
| tree | 9ba70793feeae9a817ffdad1b49a1b1ff260e0a5 /src/libsyntax/ext | |
| parent | 15e7824f1ce2f622afc195e72469f3c01f88a1f3 (diff) | |
| parent | 8c88308c68691b795815a776d41b3aa11717c146 (diff) | |
| download | rust-5a872880bd42f5fc56a3ae35aa05a4e239f5bd35.tar.gz rust-5a872880bd42f5fc56a3ae35aa05a4e239f5bd35.zip | |
Auto merge of #29780 - KyleMayes:quote-ext, r=nrc
This is my first code contribution to Rust, so I'm sure there are some issues with the changes I've made. I've added the `quote_arg!`, `quote_block!`, `quote_path!`, and `quote_meta_item!` quasiquoting macros. From my experience trying to build AST in compiler plugins, I would like to be able to build any AST piece with a quasiquoting macro (e.g., `quote_struct_field!` or `quote_variant!`) and then use those AST pieces in other quasiquoting macros, but this pull request just adds some of the low-hanging fruit. I'm not sure if these additions are desirable, and I'm sure these macros can be implemented in an external crate if not.
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", |
