Skip to content
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

Support parsing attributes with arbitrary tokens. #111

Merged
merged 12 commits into from
Apr 21, 2017
32 changes: 31 additions & 1 deletion src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub enum MetaItem {
///
/// E.g. `feature = "foo"` as in `#[feature = "foo"]`
NameValue(Ident, Lit),

/// Tokens meta item.
///
/// E.g. `test foo bar` as in `#[test foo bar]`
Tokens(Ident, Vec<TokenTree>),
}

impl MetaItem {
Expand All @@ -58,7 +63,8 @@ impl MetaItem {
match *self {
MetaItem::Word(ref name) |
MetaItem::List(ref name, _) |
MetaItem::NameValue(ref name, _) => name.as_ref(),
MetaItem::NameValue(ref name, _) |
MetaItem::Tokens(ref name, _) => name.as_ref(),
}
}
}
Expand Down Expand Up @@ -111,6 +117,7 @@ pub mod parsing {
use super::*;
use ident::parsing::ident;
use lit::parsing::lit;
use mac::parsing::token_trees;
use synom::space::{block_comment, whitespace};

#[cfg(feature = "full")]
Expand Down Expand Up @@ -169,6 +176,25 @@ pub mod parsing {
})
)
|
do_parse!(
name_and_token_trees: preceded!(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to implement this here instead of in the meta_item rule because I needed to use delimited! to limit how much token_trees parses. Is there a better way?

punct!("#"),
delimited!(
punct!("["),
tuple!(ident, token_trees),
punct!("]")
)
) >>
(Attribute {
style: AttrStyle::Outer,
value: {
let (name, token_trees) = name_and_token_trees;
MetaItem::Tokens(name, token_trees)
},
is_sugared_doc: false,
})
)
|
do_parse!(
punct!("///") >>
not!(tag!("/")) >>
Expand Down Expand Up @@ -286,6 +312,10 @@ mod printing {
tokens.append("=");
value.to_tokens(tokens);
}
MetaItem::Tokens(ref name, ref token_trees) => {
name.to_tokens(tokens);
tokens.append_all(token_trees);
}
}
}
}
Expand Down