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

Token macros #241

Merged
merged 1 commit into from
Nov 12, 2017
Merged

Token macros #241

merged 1 commit into from
Nov 12, 2017

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Nov 12, 2017

I am not dead set on this, but I think it is an improvement. This commit introduces a type macro Token! and parser macros punct! and keyword! used as follows.

ItemExternCrate {
    pub attrs: Vec,
    pub vis: Visibility,
    pub extern_token: Token![extern],
    pub crate_token: Token![crate],
    pub ident: Ident,
    pub rename: Option<(Token![as], Ident)>,
    pub semi_token: Token![;],
}

impl_synom!(ItemExternCrate "extern crate item" do_parse!(
    attrs: many0!(call!(Attribute::parse_outer)) >>
    vis: syn!(Visibility) >>
    extern_: keyword!(extern) >>
    crate_: keyword!(crate) >>
    ident: syn!(Ident) >>
    rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
    semi: punct!(;) >>
    (ItemExternCrate {
        attrs: attrs,
        vis: vis,
        extern_token: extern_,
        crate_token: crate_,
        ident: ident,
        rename: rename,
        semi_token: semi,
    })
));

There are a few advantages.

  • I learn this scheme once and then I can be productive. In the previous scheme I frequently have to look up the names of RArrow, Colon2, Star, etc.

  • No longer misleading for punctuation that has overloaded meanings. In the previous scheme for example TyRptr, which represents &T and &mut T, contained a tokens::And. But in this context the & symbol means "reference", not "and".

  • No longer need to glob import synom::tokens::* which means token names no longer conflict with prelude types or syn types. As part of this commit, I have renamed tokens::Box_, tokens::Default_, and tokens::Fn_ to remove the underscores. I kept tokens::Self_ because that one would be a reserved word without underscore. This also frees up the name Type for Standardize on usage of abbreviations #224.

@dtolnay dtolnay merged commit 88e1feb into master Nov 12, 2017
@dtolnay dtolnay deleted the token branch November 12, 2017 17:32
@mystor
Copy link
Collaborator

mystor commented Nov 12, 2017

I like this a lot. I'd say that it's definitely a usability improvement over the old system.

@alexcrichton
Copy link
Collaborator

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants