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

Improve the ergonomics of parsing and peeking #138

Merged
merged 1 commit into from
Oct 7, 2020
Merged

Conversation

udoprog
Copy link
Collaborator

@udoprog udoprog commented Oct 7, 2020

This is accomplished through a bunch of new codegen in tools/generate, a couple of token markers and improved parser buffering.

The two primary macros added are:

  • T, which will produce the ast type for the given fragment. For example, T![==] gives ast::EqEq.
  • K, which produces the token kind of the given fragment. For examples, K![==] gives ast::Kind::EqEq.

These are less error prone and more compact than their ast::* equivalents because they visualize what the produced ast means, like:

p.parse::<T![async]>()?;
p.parse::<T!['(']>()?;

Instead of:

p.parse::<ast::Async>()?;
p.parse::<ast::OpenBrace>()?;

The Parser has also been modified to for improved peeking using Peeker and its associated nth method, which allows for peeking as many tokens as necessary forward using a circular buffer.

New Peek impls look like this:

impl Peek for Expr {
    fn peek(p: &mut Peeker<'_>) -> bool {
        match p.nth(0) {
            K![async] => true,
            K![self] => true,
            K![select] => true,
            ast::Kind::Label(..) => matches!(p.nth(1), K![:]),
            K![#] => true,
            K![-] => true,
            K![!] => true,
            K![&] => true,
            // and so forth...
        }
    }
}

@udoprog udoprog added the enhancement New feature or request label Oct 7, 2020
@udoprog udoprog merged commit c0b45b6 into master Oct 7, 2020
@udoprog udoprog deleted the macro-ergonomics branch October 7, 2020 16:04
@udoprog udoprog added the changelog Issue has been added to the changelog label Oct 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog Issue has been added to the changelog enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant