Skip to content

Commit

Permalink
Merge pull request #241 from dtolnay/token
Browse files Browse the repository at this point in the history
Token macros
  • Loading branch information
dtolnay authored Nov 12, 2017
2 parents 307a276 + f8db7ba commit 88e1feb
Show file tree
Hide file tree
Showing 16 changed files with 800 additions and 641 deletions.
29 changes: 14 additions & 15 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ast_struct! {
/// Doc-comments are promoted to attributes that have `is_sugared_doc` = true
pub struct Attribute {
pub style: AttrStyle,
pub pound_token: tokens::Pound,
pub pound_token: Token![#],
pub bracket_token: tokens::Bracket,

/// The path of the attribute.
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Attribute {
if let TokenNode::Literal(ref lit) = self.tts[1].0.kind {
return Some(MetaItem::NameValue(MetaNameValue {
ident: name.clone(),
eq_token: tokens::Eq([Span(self.tts[0].0.span)]),
eq_token: Token![=]([Span(self.tts[0].0.span)]),
lit: Lit {
value: LitKind::Other(lit.clone()),
span: Span(self.tts[1].0.span),
Expand Down Expand Up @@ -94,7 +94,7 @@ fn nested_meta_item_from_tokens(tts: &[proc_macro2::TokenTree])
if let TokenNode::Literal(ref lit) = tts[2].kind {
let pair = MetaNameValue {
ident: Ident::new(sym, Span(tts[0].span)),
eq_token: tokens::Eq([Span(tts[1].span)]),
eq_token: Token![=]([Span(tts[1].span)]),
lit: Lit {
value: LitKind::Other(lit.clone()),
span: Span(tts[2].span),
Expand Down Expand Up @@ -131,7 +131,7 @@ fn nested_meta_item_from_tokens(tts: &[proc_macro2::TokenTree])
}

fn list_of_nested_meta_items_from_tokens(mut tts: &[proc_macro2::TokenTree])
-> Option<Delimited<NestedMetaItem, tokens::Comma>>
-> Option<Delimited<NestedMetaItem, Token![,]>>
{
let mut delimited = Delimited::new();
let mut first = true;
Expand All @@ -141,7 +141,7 @@ fn list_of_nested_meta_items_from_tokens(mut tts: &[proc_macro2::TokenTree])
first = false;
None
} else if let TokenNode::Op(',', Spacing::Alone) = tts[0].kind {
let tok = tokens::Comma([Span(tts[0].span)]);
let tok = Token![,]([Span(tts[0].span)]);
tts = &tts[1..];
if tts.is_empty() {
break
Expand Down Expand Up @@ -175,7 +175,7 @@ ast_enum! {
Outer,

/// Attribute of the form `#![...]`.
Inner(tokens::Bang),
Inner(Token![!]),
}
}

Expand Down Expand Up @@ -203,7 +203,7 @@ ast_enum_of_structs! {
/// Arguments to this attribute
///
/// E.g. `..` in `#[derive(..)]`
pub nested: Delimited<NestedMetaItem, tokens::Comma>,
pub nested: Delimited<NestedMetaItem, Token![,]>,
}),

/// Name-value meta item.
Expand All @@ -215,7 +215,7 @@ ast_enum_of_structs! {
/// E.g. `feature` in `#[feature = "foo"]`
pub ident: Ident,

pub eq_token: tokens::Eq,
pub eq_token: Token![=],

/// Arguments to this attribute
///
Expand Down Expand Up @@ -293,7 +293,6 @@ impl<'a, T> FilterAttrs<'a> for T
pub mod parsing {
use super::*;
use synom::{PResult, Cursor, parse_error};
use synom::tokens::*;
use proc_macro2::{TokenNode, Spacing, TokenTree};

fn eq() -> TokenTree {
Expand All @@ -307,8 +306,8 @@ pub mod parsing {
#[cfg(feature = "full")]
named!(pub parse_inner -> Self, alt!(
do_parse!(
pound: syn!(Pound) >>
bang: syn!(Bang) >>
pound: punct!(#) >>
bang: punct!(!) >>
path_and_tts: brackets!(tuple!(
call!(::Path::parse_mod_style),
call!(::TokenTree::parse_list)
Expand All @@ -330,22 +329,22 @@ pub mod parsing {
map!(
lit_doc_comment,
|lit| Attribute {
style: AttrStyle::Inner(tokens::Bang::default()),
style: AttrStyle::Inner(<Token![!]>::default()),
path: "doc".into(),
tts: vec![
::TokenTree(eq()),
::TokenTree(lit),
],
is_sugared_doc: true,
pound_token: tokens::Pound::default(),
pound_token: <Token![#]>::default(),
bracket_token: tokens::Bracket::default(),
}
)
));

named!(pub parse_outer -> Self, alt!(
do_parse!(
pound: syn!(Pound) >>
pound: punct!(#) >>
path_and_tts: brackets!(tuple!(
call!(::Path::parse_mod_style),
call!(::TokenTree::parse_list)
Expand Down Expand Up @@ -374,7 +373,7 @@ pub mod parsing {
::TokenTree(lit),
],
is_sugared_doc: true,
pound_token: tokens::Pound::default(),
pound_token: <Token![#]>::default(),
bracket_token: tokens::Bracket::default(),
}
)
Expand Down
40 changes: 19 additions & 21 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ ast_struct! {
/// Explicit discriminant, e.g. `Foo = 1`
pub discriminant: Option<Expr>,

pub eq_token: Option<tokens::Eq>,
pub eq_token: Option<Token![=]>,
}
}

ast_enum! {
/// Data stored within an enum variant or struct.
pub enum VariantData {
/// Struct variant, e.g. `Point { x: f64, y: f64 }`.
Struct(Delimited<Field, tokens::Comma>, tokens::Brace),
Struct(Delimited<Field, Token![,]>, tokens::Brace),

/// Tuple variant, e.g. `Some(T)`.
Tuple(Delimited<Field, tokens::Comma>, tokens::Paren),
Tuple(Delimited<Field, Token![,]>, tokens::Paren),

/// Unit variant, e.g. `None`.
Unit,
Expand Down Expand Up @@ -72,7 +72,7 @@ ast_struct! {
/// Type of the field.
pub ty: Ty,

pub colon_token: Option<tokens::Colon>,
pub colon_token: Option<Token![:]>,
}
}

Expand All @@ -81,21 +81,21 @@ ast_enum_of_structs! {
pub enum Visibility {
/// Public, i.e. `pub`.
pub Public(VisPublic {
pub pub_token: tokens::Pub,
pub pub_token: Token![pub],
}),

/// Crate-visible, i.e. `pub(crate)`.
pub Crate(VisCrate {
pub pub_token: tokens::Pub,
pub pub_token: Token![pub],
pub paren_token: tokens::Paren,
pub crate_token: tokens::Crate,
pub crate_token: Token![crate],
}),

/// Restricted, e.g. `pub(self)` or `pub(super)` or `pub(in some::module)`.
pub Restricted(VisRestricted {
pub pub_token: tokens::Pub,
pub pub_token: Token![pub],
pub paren_token: tokens::Paren,
pub in_token: Option<tokens::In>,
pub in_token: Option<Token![in]>,
pub path: Box<Path>,
}),

Expand All @@ -109,15 +109,13 @@ pub mod parsing {
use super::*;

use synom::Synom;
use synom::tokens;
use synom::tokens::*;

impl Field {
named!(pub parse_struct -> Self, do_parse!(
attrs: many0!(call!(Attribute::parse_outer)) >>
vis: syn!(Visibility) >>
id: syn!(Ident) >>
colon: syn!(Colon) >>
colon: punct!(:) >>
ty: syn!(Ty) >>
(Field {
ident: Some(id),
Expand Down Expand Up @@ -145,8 +143,8 @@ pub mod parsing {
impl Synom for Visibility {
named!(parse -> Self, alt!(
do_parse!(
pub_token: syn!(Pub) >>
other: parens!(syn!(tokens::Crate)) >>
pub_token: keyword!(pub) >>
other: parens!(keyword!(crate)) >>
(Visibility::Crate(VisCrate {
crate_token: other.0,
paren_token: other.1,
Expand All @@ -155,8 +153,8 @@ pub mod parsing {
)
|
do_parse!(
pub_token: syn!(Pub) >>
other: parens!(syn!(Self_)) >>
pub_token: keyword!(pub) >>
other: parens!(keyword!(self)) >>
(Visibility::Restricted(VisRestricted {
path: Box::new(other.0.into()),
in_token: None,
Expand All @@ -166,8 +164,8 @@ pub mod parsing {
)
|
do_parse!(
pub_token: syn!(Pub) >>
other: parens!(syn!(Super)) >>
pub_token: keyword!(pub) >>
other: parens!(keyword!(super)) >>
(Visibility::Restricted(VisRestricted {
path: Box::new(other.0.into()),
in_token: None,
Expand All @@ -177,9 +175,9 @@ pub mod parsing {
)
|
do_parse!(
pub_token: syn!(Pub) >>
pub_token: keyword!(pub) >>
other: parens!(do_parse!(
in_tok: syn!(In) >>
in_tok: keyword!(in) >>
restricted: call!(Path::parse_mod_style) >>
(in_tok, restricted)
)) >>
Expand All @@ -191,7 +189,7 @@ pub mod parsing {
}))
)
|
syn!(Pub) => { |tok| {
keyword!(pub) => { |tok| {
Visibility::Public(VisPublic {
pub_token: tok,
})
Expand Down
29 changes: 14 additions & 15 deletions src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ ast_enum_of_structs! {
pub enum Body {
/// It's an enum.
pub Enum(BodyEnum {
pub enum_token: tokens::Enum,
pub enum_token: Token![enum],
pub brace_token: tokens::Brace,
pub variants: Delimited<Variant, tokens::Comma>,
pub variants: Delimited<Variant, Token![,]>,
}),

/// It's a struct.
pub Struct(BodyStruct {
pub data: VariantData,
pub struct_token: tokens::Struct,
pub semi_token: Option<tokens::Semi>,
pub struct_token: Token![struct],
pub semi_token: Option<Token![;]>,
}),
}

Expand All @@ -48,16 +48,15 @@ pub mod parsing {
use super::*;

use synom::Synom;
use synom::tokens::*;

impl Synom for DeriveInput {
named!(parse -> Self, do_parse!(
attrs: many0!(call!(Attribute::parse_outer)) >>
vis: syn!(Visibility) >>
which: alt!(
syn!(Struct) => { Ok }
keyword!(struct) => { Ok }
|
syn!(Enum) => { Err }
keyword!(enum) => { Err }
) >>
id: syn!(Ident) >>
generics: syn!(Generics) >>
Expand Down Expand Up @@ -101,7 +100,7 @@ pub mod parsing {
}


named!(struct_body -> (WhereClause, VariantData, Option<tokens::Semi>), alt!(
named!(struct_body -> (WhereClause, VariantData, Option<Token![;]>), alt!(
do_parse!(
wh: syn!(WhereClause) >>
body: struct_like_body >>
Expand All @@ -111,18 +110,18 @@ pub mod parsing {
do_parse!(
body: tuple_like_body >>
wh: syn!(WhereClause) >>
semi: syn!(Semi) >>
semi: punct!(;) >>
(wh, VariantData::Tuple(body.0, body.1), Some(semi))
)
|
do_parse!(
wh: syn!(WhereClause) >>
semi: syn!(Semi) >>
semi: punct!(;) >>
(wh, VariantData::Unit, Some(semi))
)
));

named!(enum_body -> (WhereClause, Delimited<Variant, tokens::Comma>, tokens::Brace), do_parse!(
named!(enum_body -> (WhereClause, Delimited<Variant, Token![,]>, tokens::Brace), do_parse!(
wh: syn!(WhereClause) >>
data: braces!(Delimited::parse_terminated) >>
(wh, data.0, data.1)
Expand All @@ -140,24 +139,24 @@ pub mod parsing {
epsilon!() => { |_| VariantData::Unit }
) >>
disr: option!(do_parse!(
eq: syn!(Eq) >>
eq: punct!(=) >>
disr: syn!(Expr) >>
(eq, disr)
)) >>
(Variant {
ident: id,
attrs: attrs,
data: data,
eq_token: disr.as_ref().map(|p| tokens::Eq((p.0).0)),
eq_token: disr.as_ref().map(|p| Token![=]((p.0).0)),
discriminant: disr.map(|p| p.1),
})
));
}

named!(struct_like_body -> (Delimited<Field, tokens::Comma>, tokens::Brace),
named!(struct_like_body -> (Delimited<Field, Token![,]>, tokens::Brace),
braces!(call!(Delimited::parse_terminated_with, Field::parse_struct)));

named!(tuple_like_body -> (Delimited<Field, tokens::Comma>, tokens::Paren),
named!(tuple_like_body -> (Delimited<Field, Token![,]>, tokens::Paren),
parens!(call!(Delimited::parse_terminated_with, Field::parse_tuple)));
}

Expand Down
Loading

0 comments on commit 88e1feb

Please sign in to comment.