[Suggestion] Anchors for grammar rules? #602
DoctorBracewell
started this conversation in
General
Replies: 1 comment
-
That regex doesn't do what you want it to do: https://regex101.com/r/W2UjRZ/1 The way to accomplish "identifier which is not a keyword" in pest is to use pest's lookahead functionality.
input:
output: - list
- ident: "ab"
- WHITESPACE: " "
- kw: "abc"
- WHITESPACE: " "
- ident: "abcd" Here An alternative that I personally prefer is to keep the guard on keywords that they don't subslice an identifier, but to allow the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Regular Expressions have the anchor characters
^
and$
, which allow you to anchor the match at a certain position.I'm currently running into an issue that could be solved with these, described below:
I want to use an arbitrary matching rule (let's say its
ASCII_ALPHA_LOWER+
), so there can be any non-zero number of lowercase letters one after each other, but I want to the result to not be equal to a specific string (say,"abc"
).In regex, this can be achieved by using a negative lookahead combined with the anchor characters -
^(?!abc$)[a-z]+
because the anchors stop the negative lookahead as soon as it matches theabc
, which means that a string likeabcd
would be matched.This is important when, for example, defining "identifiers" in languages. You may want to define an identifier as a sequence of any characters that isn't a string from a set of reserved keywords.
Please correct me if I'm wrong and point out any potential methods of doing this with pest's grammar syntax, but currently I don't think this is possible because pest does not have a way of matching the "end of a rule", so I think these anchor characters could be a good addition.
Beta Was this translation helpful? Give feedback.
All reactions