Skip to content

Consume token only when it matches a predicate? #1931

Answered by msujew
thejohnfreeman asked this question in Q&A
Discussion options

You must be logged in to vote

@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.

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@msujew
Comment options

@thejohnfreeman
Comment options

@bd82
Comment options

@thejohnfreeman
Comment options

@bd82
Comment options

Answer selected by thejohnfreeman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #1930 on March 13, 2023 15:44.