Consume token only when it matches a predicate? #1931
Answered
by
msujew
thejohnfreeman
asked this question in
Q&A
-
Is there a way to consume a token only when its image matches a predicate? |
Beta Was this translation helpful? Give feedback.
Answered by
msujew
Mar 13, 2023
Replies: 1 comment 6 replies
-
@thejohnfreeman You can probably accomplish this using a structure like this: $.OR([
{
GATE: () => predicate(),
ALT: () => $.CONSUME(<token>)
},
{
GATE: () => !predicate(),
ALT: EMPTY_ALT
}
]); Note that for specific use cases such as "does the image match a predicate" you can probably also use custom token patterns. They're probably more efficient, but they operate on the lexer level, not the parser level. |
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
thejohnfreeman
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@thejohnfreeman You can probably accomplish this using a structure like this:
Note that for specific use cases such as "does the image match a predicate" you can probably also use custom token patterns. They're probably more efficient, but they operate on the lexer level, not the parser level.