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

Lookahead groups #145

Merged
merged 4 commits into from May 8, 2021
Merged

Lookahead groups #145

merged 4 commits into from May 8, 2021

Conversation

petee-d
Copy link
Contributor

@petee-d petee-d commented May 8, 2021

Partially reverts my changes from #144 to change the negative lookahead syntax from !? <expr> to (?! <expr> ...), in order to have syntax consistent with PERL-like regular expressions. Also adds support for positive lookahead groups, because why not.

Relates to #134

@petee-d petee-d mentioned this pull request May 8, 2021
2 tasks
@petee-d
Copy link
Contributor Author

petee-d commented May 8, 2021

Oh and I also found some bugs where wrong grammar syntax in negation or captures could lead to the grammar building successfully but then failing during runtime with a nil dereference error.

@petee-d
Copy link
Contributor Author

petee-d commented May 8, 2021

Probably review by commits

Copy link
Owner

@alecthomas alecthomas left a comment

Choose a reason for hiding this comment

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

Looks really nice, and super useful, thanks for doing this.

@@ -155,7 +155,8 @@ The grammar format is:
- `<expr> <expr> ...` Match expressions.
- `<expr> | <expr> | ...` Match one of the alternatives. Each alternative is tried in order, with backtracking.
- `!<expr>` Match any token that is _not_ the start of the expression (eg: `@!";"` matches anything but the `;` character into the field).
- `!?<expr>` Negative lookahead - the sequence will not match if this expression matches (lookahead won't be consumed)
- `(?= ... )` Positive lookahead group - requires the contents to match further input, without consuming it
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe move these up under group, though not a big deal.

@@ -0,0 +1,40 @@
package participle_test
Copy link
Owner

Choose a reason for hiding this comment

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

Nice!

Int int `| @Int`
}
type op struct {
Op string `@('+' | '*' (?= @Int))`
Copy link
Owner

Choose a reason for hiding this comment

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

Very nice, this will be super useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be honest, I couldn't actually find a good example of what to use positive lookahead for, the best I could quickly come up with was this rather silly test. But it was easy to have positive lookahead supported when I was refactoring negative lookahead anyway, so I thought why not.

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah good point, with Participle's backtracking it's not super useful...

@alecthomas alecthomas merged commit 5b1aaca into alecthomas:master May 8, 2021
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.

None yet

2 participants