Parsing a reserved word in Parsing Expressive Grammar (PEG.js)
Question
The similar question and the author's website give me solutions like this:
Identifier "identifier"
= !ReservedWord [A-Za-z_]+
ReservedWord
= "test"
/ "abc"
This solution can't parse an identifier like this "test_var".
In this example, the grammar !ReservedWord will exclusive all variables starting with "test" or "abc".
Thanks in advance.
Solution
ReservedWord
succeeds, when a reserved word is complete, regardless of what follows. So the solution is to make it fail when there is a longer match that includes more identifier characters:
Identifier "identifier"
= !ReservedWord [A-Za-z_]+
ReservedWord
= ( "test" / "abc" ) ![A-Za-z_]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow