diff options
| author | bors <bors@rust-lang.org> | 2014-07-18 09:31:22 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-07-18 09:31:22 +0000 |
| commit | d9f1d6b7f69f293ba5f060fd9e179de228d9497b (patch) | |
| tree | 58ea2bf65c6eb86048034eaaaa2f24083dd7cbbe | |
| parent | 5980aa0f22849b8d4f25c8d30584e0e2bc82a9b8 (diff) | |
| parent | e4f8cec416d727a7ca2d732cbbae99448228f76d (diff) | |
| download | rust-d9f1d6b7f69f293ba5f060fd9e179de228d9497b.tar.gz rust-d9f1d6b7f69f293ba5f060fd9e179de228d9497b.zip | |
auto merge of #15732 : bgamari/rust/to-tokens, r=alexcrichton
Here I add a `ToTokens` impl for `Attribute_` and `Option<T>`, as well as generalize the impl for `Vec<T>`
| -rw-r--r-- | src/libsyntax/ext/base.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax/ext/quote.rs | 35 |
2 files changed, 35 insertions, 3 deletions
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 56484c4ba59..49bd3697884 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -371,6 +371,9 @@ pub fn syntax_expander_table() -> SyntaxEnv { syntax_expanders.insert(intern("quote_ty"), builtin_normal_expander( ext::quote::expand_quote_ty)); + syntax_expanders.insert(intern("quote_method"), + builtin_normal_expander( + ext::quote::expand_quote_method)); syntax_expanders.insert(intern("quote_item"), builtin_normal_expander( ext::quote::expand_quote_item)); diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 696d62838ba..a7ede6f742d 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -54,9 +54,10 @@ pub mod rt { } } - impl ToTokens for Vec<TokenTree> { - fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> { - (*self).clone() + impl<T: ToTokens> ToTokens for Vec<T> { + fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> { + let a = self.iter().flat_map(|t| t.to_tokens(cx).move_iter()); + FromIterator::from_iter(a) } } @@ -67,6 +68,15 @@ pub mod rt { } } + impl<T: ToTokens> ToTokens for Option<T> { + fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> { + match self { + &Some(ref t) => t.to_tokens(cx), + &None => Vec::new(), + } + } + } + /* Should be (when bugs in default methods are fixed): trait ToSource : ToTokens { @@ -133,11 +143,18 @@ pub mod rt { impl_to_source!(ast::Arg, arg_to_string) impl_to_source!(Generics, generics_to_string) impl_to_source!(Gc<ast::Item>, item_to_string) + impl_to_source!(Gc<ast::Method>, method_to_string) impl_to_source!(Gc<ast::Expr>, expr_to_string) impl_to_source!(Gc<ast::Pat>, pat_to_string) impl_to_source_slice!(ast::Ty, ", ") impl_to_source_slice!(Gc<ast::Item>, "\n\n") + impl ToSource for ast::Attribute_ { + fn to_source(&self) -> String { + pprust::attribute_to_string(&dummy_spanned(*self)) + } + } + impl<'a> ToSource for &'a str { fn to_source(&self) -> String { let lit = dummy_spanned(ast::LitStr( @@ -222,6 +239,7 @@ pub mod rt { impl_to_tokens!(ast::Ident) impl_to_tokens!(Gc<ast::Item>) impl_to_tokens!(Gc<ast::Pat>) + impl_to_tokens!(Gc<ast::Method>) impl_to_tokens_lifetime!(&'a [Gc<ast::Item>]) impl_to_tokens!(ast::Ty) impl_to_tokens_lifetime!(&'a [ast::Ty]) @@ -229,6 +247,7 @@ pub mod rt { impl_to_tokens!(Gc<ast::Expr>) impl_to_tokens!(ast::Block) impl_to_tokens!(ast::Arg) + impl_to_tokens!(ast::Attribute_) impl_to_tokens_lifetime!(&'a str) impl_to_tokens!(()) impl_to_tokens!(char) @@ -336,6 +355,16 @@ pub fn expand_quote_ty(cx: &mut ExtCtxt, base::MacExpr::new(expanded) } +pub fn expand_quote_method(cx: &mut ExtCtxt, + sp: Span, + tts: &[ast::TokenTree]) + -> Box<base::MacResult> { + let e_param_colons = cx.expr_none(sp); + let expanded = expand_parse_call(cx, sp, "parse_method", + vec!(e_param_colons), tts); + base::MacExpr::new(expanded) +} + pub fn expand_quote_stmt(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) |
