-
-
Notifications
You must be signed in to change notification settings - Fork 188
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
Support lookaround assertions? #134
Comments
I'm not 100% sure we mean the same thing, but it sounds like I have a use case for something like that when writing a parser for Jinja (specifically, implicit test arguments). In the parser, I need to match some complex struct term only if the next token isn't one of certain 3 keywords. Here is a grossly oversimplified example: type Condition struct {
True Expression `@@`
Cond Test `'if' @@ 'else'`
False Expression `@@`
}
type Test struct {
Left Expression `@@`
Name string `'is' @Ident`
Argument *Expression `@@?` // Allowed to match only if next token isn't 'else' or a few other keywords
}
type Expression struct {
// Would be much more complex in reality (operators with 10+ precedence groups, a huge recursive mess)
Variable string `@Ident`
}
func TestNegativeLookahead(t *testing.T) {
parser := participle.MustBuild(&Condition{})
result := &Condition{}
assert.NoError(t, parser.ParseString("", "one if x is odd else two", result))
assert.NoError(t, parser.ParseString("", "small if x is below y else large", result))
} I basically would need some way of modifying the optional Do you think this can somehow be done with existing participle grammar options? I've tried If you look into this, thank you very much in advance! This seems to be about the only thing that my Jinja parser can't handle yet. |
@alecthomas is this something you would be able to look at any time soon? Please let me know, as I could give it a try myself, but would like to know what kind of a syntax you would like for the negative lookahead. I think the ideal choice would be the negative lookahead syntax in https://pythonhosted.org/pyrser/dsl.html, so for example:
I.e. within a sequence, any expression prefixed by
could be written as
But since breaking backwards compatibility would suck, I think we could do something like this for my first example:
I.e. What do you think? Do you have another idea for what the syntax should be? Would you like to do it yourself, or should I take a crack at it? |
Was kind of pressing me, so I just did it. :) Chose |
Mmm actually I did merge it, but I think I'd prefer it to be |
I think, regarding syntax, that it would be preferable to emulate Perl regex syntax. It should be part of the grouping construct, which will probably make this a bit more of a complex change than your one, but will be more consistent. |
If you'd like to take a crack at it, I would be most appreciative. |
Could try. How about I still use |
Actually, just tried it and it wasn't much good. I'll try something a bit different... :) |
I changed it up by separating the code from negation completely (as negation had some unnecessary restrictions anyway) and took the opportunity to also implement positive lookahead with the PERL-like |
Should be fairly straightforward to implement, but is it useful?
The text was updated successfully, but these errors were encountered: