about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-11-12 20:31:12 +0000
committerbors <bors@rust-lang.org>2015-11-12 20:31:12 +0000
commit5a872880bd42f5fc56a3ae35aa05a4e239f5bd35 (patch)
tree9ba70793feeae9a817ffdad1b49a1b1ff260e0a5
parent15e7824f1ce2f622afc195e72469f3c01f88a1f3 (diff)
parent8c88308c68691b795815a776d41b3aa11717c146 (diff)
downloadrust-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.
-rw-r--r--src/libsyntax/ext/base.rs12
-rw-r--r--src/libsyntax/ext/quote.rs51
-rw-r--r--src/libsyntax/fold.rs1
-rw-r--r--src/libsyntax/parse/parser.rs18
-rw-r--r--src/libsyntax/parse/token.rs4
-rw-r--r--src/libsyntax/print/pprust.rs1
-rw-r--r--src/test/compile-fail-fulldeps/gated-quote.rs24
-rw-r--r--src/test/run-pass-fulldeps/qquote.rs37
8 files changed, 137 insertions, 11 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",
diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs
index 284bf46cc3f..66ee5aa12ca 100644
--- a/src/libsyntax/fold.rs
+++ b/src/libsyntax/fold.rs
@@ -684,6 +684,7 @@ pub fn noop_fold_interpolated<T: Folder>(nt: token::Nonterminal, fld: &mut T)
         token::NtGenerics(generics) => token::NtGenerics(fld.fold_generics(generics)),
         token::NtWhereClause(where_clause) =>
             token::NtWhereClause(fld.fold_where_clause(where_clause)),
+        token::NtArg(arg) => token::NtArg(fld.fold_arg(arg)),
     }
 }
 
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 1e38eebec5d..9219f05d99a 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -393,6 +393,22 @@ impl<'a> Parser<'a> {
         panictry!(self.parse_attribute(permit_inner))
     }
 
+    pub fn parse_arg_panic(&mut self) -> Arg {
+        panictry!(self.parse_arg())
+    }
+
+    pub fn parse_block_panic(&mut self) -> P<Block> {
+        panictry!(self.parse_block())
+    }
+
+    pub fn parse_meta_item_panic(&mut self) -> P<ast::MetaItem> {
+        panictry!(self.parse_meta_item())
+    }
+
+    pub fn parse_path_panic(&mut self, mode: PathParsingMode) -> ast::Path {
+        panictry!(self.parse_path(mode))
+    }
+
     /// Convert a token to a string using self's reader
     pub fn token_to_string(token: &token::Token) -> String {
         pprust::token_to_string(token)
@@ -1455,6 +1471,8 @@ impl<'a> Parser<'a> {
     /// This version of parse arg doesn't necessarily require
     /// identifier names.
     pub fn parse_arg_general(&mut self, require_name: bool) -> PResult<Arg> {
+        maybe_whole!(no_clone self, NtArg);
+
         let pat = if require_name || self.is_named_argument() {
             debug!("parse_arg_general parse_pat (require_name:{})",
                    require_name);
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs
index ba24dc3c0a7..5e4449af604 100644
--- a/src/libsyntax/parse/token.rs
+++ b/src/libsyntax/parse/token.rs
@@ -381,12 +381,13 @@ pub enum Nonterminal {
     NtMeta(P<ast::MetaItem>),
     NtPath(Box<ast::Path>),
     NtTT(P<ast::TokenTree>), // needs P'ed to break a circularity
-    // These is not exposed to macros, but is used by quasiquote.
+    // These are not exposed to macros, but are used by quasiquote.
     NtArm(ast::Arm),
     NtImplItem(P<ast::ImplItem>),
     NtTraitItem(P<ast::TraitItem>),
     NtGenerics(ast::Generics),
     NtWhereClause(ast::WhereClause),
+    NtArg(ast::Arg),
 }
 
 impl fmt::Debug for Nonterminal {
@@ -407,6 +408,7 @@ impl fmt::Debug for Nonterminal {
             NtTraitItem(..) => f.pad("NtTraitItem(..)"),
             NtGenerics(..) => f.pad("NtGenerics(..)"),
             NtWhereClause(..) => f.pad("NtWhereClause(..)"),
+            NtArg(..) => f.pad("NtArg(..)"),
         }
     }
 }
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index fad0b7869f0..5b8f5c0aef6 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -305,6 +305,7 @@ pub fn token_to_string(tok: &Token) -> String {
             token::NtTraitItem(ref e)   => trait_item_to_string(&**e),
             token::NtGenerics(ref e)    => generics_to_string(&*e),
             token::NtWhereClause(ref e) => where_clause_to_string(&*e),
+            token::NtArg(ref e)         => arg_to_string(&*e),
         }
     }
 }
diff --git a/src/test/compile-fail-fulldeps/gated-quote.rs b/src/test/compile-fail-fulldeps/gated-quote.rs
index 6a5cd88a591..cd801fbcd88 100644
--- a/src/test/compile-fail-fulldeps/gated-quote.rs
+++ b/src/test/compile-fail-fulldeps/gated-quote.rs
@@ -37,14 +37,18 @@ impl ParseSess {
 
 pub fn main() {
     let ecx = &ParseSess;
-    let x = quote_tokens!(ecx, 3);   //~ ERROR macro undefined: 'quote_tokens!'
-    let x = quote_expr!(ecx, 3);     //~ ERROR macro undefined: 'quote_expr!'
-    let x = quote_ty!(ecx, 3);       //~ ERROR macro undefined: 'quote_ty!'
-    let x = quote_method!(ecx, 3);   //~ ERROR macro undefined: 'quote_method!'
-    let x = quote_item!(ecx, 3);     //~ ERROR macro undefined: 'quote_item!'
-    let x = quote_pat!(ecx, 3);      //~ ERROR macro undefined: 'quote_pat!'
-    let x = quote_arm!(ecx, 3);      //~ ERROR macro undefined: 'quote_arm!'
-    let x = quote_stmt!(ecx, 3);     //~ ERROR macro undefined: 'quote_stmt!'
-    let x = quote_matcher!(ecx, 3);  //~ ERROR macro undefined: 'quote_matcher!'
-    let x = quote_attr!(ecx, 3);     //~ ERROR macro undefined: 'quote_attr!'
+    let x = quote_tokens!(ecx, 3);    //~ ERROR macro undefined: 'quote_tokens!'
+    let x = quote_expr!(ecx, 3);      //~ ERROR macro undefined: 'quote_expr!'
+    let x = quote_ty!(ecx, 3);        //~ ERROR macro undefined: 'quote_ty!'
+    let x = quote_method!(ecx, 3);    //~ ERROR macro undefined: 'quote_method!'
+    let x = quote_item!(ecx, 3);      //~ ERROR macro undefined: 'quote_item!'
+    let x = quote_pat!(ecx, 3);       //~ ERROR macro undefined: 'quote_pat!'
+    let x = quote_arm!(ecx, 3);       //~ ERROR macro undefined: 'quote_arm!'
+    let x = quote_stmt!(ecx, 3);      //~ ERROR macro undefined: 'quote_stmt!'
+    let x = quote_matcher!(ecx, 3);   //~ ERROR macro undefined: 'quote_matcher!'
+    let x = quote_attr!(ecx, 3);      //~ ERROR macro undefined: 'quote_attr!'
+    let x = quote_arg!(ecx, 3);       //~ ERROR macro undefined: 'quote_arg!'
+    let x = quote_block!(ecx, 3);     //~ ERROR macro undefined: 'quote_block!'
+    let x = quote_meta_item!(ecx, 3); //~ ERROR macro undefined: 'quote_meta_item!'
+    let x = quote_path!(ecx, 3);      //~ ERROR macro undefined: 'quote_path!'
 }
diff --git a/src/test/run-pass-fulldeps/qquote.rs b/src/test/run-pass-fulldeps/qquote.rs
index ba713bb98f8..edaa88452c4 100644
--- a/src/test/run-pass-fulldeps/qquote.rs
+++ b/src/test/run-pass-fulldeps/qquote.rs
@@ -63,4 +63,41 @@ fn main() {
 
     let attr = quote_attr!(cx, #![cfg(foo = "bar")]);
     check!(attribute_to_string, attr, quote_attr!(cx, $attr); r#"#![cfg(foo = "bar")]"#);
+
+    // quote_arg!
+
+    let arg = quote_arg!(cx, foo: i32);
+    check!(arg_to_string, arg, quote_arg!(cx, $arg); "foo: i32");
+
+    let function = quote_item!(cx, fn f($arg) { }).unwrap();
+    check!(item_to_string, function; "fn f(foo: i32) { }");
+
+    let args = vec![arg, quote_arg!(cx, bar: u32)];
+    let args = &args[..];
+    let function = quote_item!(cx, fn f($args) { }).unwrap();
+    check!(item_to_string, function; "fn f(foo: i32, bar: u32) { }");
+
+    // quote_block!
+
+    let block = quote_block!(cx, { $stmt let y = 40u32; });
+    check!(block_to_string, block, *quote_block!(cx, $block); "{ let x = 20u16; let y = 40u32; }");
+
+    let function = quote_item!(cx, fn f() $block).unwrap();
+    check!(item_to_string, function; "fn f() { let x = 20u16; let y = 40u32; }");
+
+    // quote_path!
+
+    let path = quote_path!(cx, ::syntax::ptr::P<MetaItem>);
+    check!(path_to_string, path, quote_path!(cx, $path); "::syntax::ptr::P<MetaItem>");
+
+    let ty = quote_ty!(cx, $path);
+    check!(ty_to_string, ty; "::syntax::ptr::P<MetaItem>");
+
+    // quote_meta_item!
+
+    let meta = quote_meta_item!(cx, cfg(foo = "bar"));
+    check!(meta_item_to_string, meta, *quote_meta_item!(cx, $meta); r#"cfg(foo = "bar")"#);
+
+    let attr = quote_attr!(cx, #![$meta]);
+    check!(attribute_to_string, attr; r#"#![cfg(foo = "bar")]"#);
 }