Skip to content

Add and generalize ToTokens impls #15732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
35 changes: 32 additions & 3 deletions src/libsyntax/ext/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -222,13 +239,15 @@ 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])
impl_to_tokens!(Generics)
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)
Expand Down Expand Up @@ -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])
Expand Down