Skip to content

How to only match full token? #96

Answered by renggli
donny-dont asked this question in Q&A
Discussion options

You must be logged in to vote

This is a common problem when parsing keywords. One way to solve it is to add a negative lookahead make sure the keyword is not followed by more letter/digits:

Parser bufferRelatedType() =>
      ref(token, 'ArrayBuffer').seq(word().not()).pick(0) | ...

Potentially you want to integrate that code into your token-builder, but often it is not needed for all tokens (or at all, depending on the language to parse).

Here is an example where this is done for the true token, and to avoid running into a problem when trying to parse a variable like trueBinding.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by renggli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants