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 parsing even further #153

Merged
merged 38 commits into from
Nov 9, 2023

Conversation

nikomatsakis
Copy link
Contributor

Parsing now supports:

  • Optional delimited vectors, so you don't have to write Foo<>
  • Guard clauses, so you don't have to write struct Foo where [] { but can just write struct Foo {
  • Precedence and ambiguous grammars, so you can write Expr = Expr + Expr and things like that.

also adds a custom testing framework (parser-torture-tests) as well as an example language (formality-eg). The formality language is incomplete.

This permits `use foo::grammar::*`
The old code was kind of specific to `$ . F` where `.` is a mode.
This is going to allow us to accommodate new modes more easily.
We now support optional lists.
We are going to want to make recursive modes.
this will better support the new gated parsing mode
This will make it easier to support fixed point parsing.
This helps resolve some annoying ambiguities
while keeping the ones I think we want.
It is sometimes useful to see EXACTLY what is going on
behavior is still broken
We are going to rework the way that variables are given precedence
to use explicit rejection instead.
When one variant is tagged as `#[variable]`,
we now automatically reject variable names from
other variants. This means we can use `#[precedence]`
just to encode true *precedence* (i.e., of operators).
This was important since there was a conflict where
variables wanted to be the same *precedence* as other
identifiers in a logical sense.
We are going to need access to it when we recurse.
Besides, the code looks cleaner this way!
This makes us panic safe, but it also just reads much more nicely.
Are we in Left position, right position, etc. (see docs)

This will be used to guide parsing of variants so that we can
properly handle recursion.
We used to also consider precedence, but we are moving away from that
approach. Remove some tests that no longer test a relevant edge case.
We are going to start enforcing the invariant that an expression
with priority N can only directly things of higher priority.
In that case for something like this:

```
enum Expr {
    Id(Id),
    Literal(Literal),

    #[precedence(1, left)]
    Add(Expr, Expr),

    #[precedence(2, left)]
    Mul(Expr, Expr),
}
```

you want Id/Literal to be max priority so they can be embedded
into anything.
@nikomatsakis
Copy link
Contributor Author

@bors r+

@nikomatsakis
Copy link
Contributor Author

I guess we didn't enable bors yet. :)

@nikomatsakis nikomatsakis merged commit 2002ced into rust-lang:main Nov 9, 2023
3 checks passed
@nikomatsakis nikomatsakis deleted the parser-testing branch November 9, 2023 11:10
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.

1 participant