yhirose / cpp-peglib

A single file C++ header-only PEG (Parsing Expression Grammars) library
MIT License
880 stars 112 forks source link

feature request: prevent token identity decent #236

Closed kfsone closed 2 years ago

kfsone commented 2 years ago
p <- identifier

NAME_START <- [A-Za-z_]
NAME_CONTINUATION<- NAME_START / [0-9]

identifier <- < NAME_START NAME_CONTINUATION* >

given an invalid identifier, this does not report 'expected identifier' but instead exposes the internal terminal name NAME_START:

image

perhaps support for

identifier <- <= NAME_START NAME_CONTINUATION* =>

could be added to peglib to allow identifier to behave in reporting as though it was not a compound?

Bonus points if it could be used to produce:

syntax error, unexpected '2', expecting <identifier> could not match <NAME_START>

or if I used better labels:

program <- identifier
identifier <- <= AllowedIdentifierFirstCharacters AllowedIdentifierCharacters* =>
AllowedIdentifierFirstCharacters <- [A-Za-z_]
AllowedIdentifierCharacters <- AllowedIdentifierFirstCharacters / [0-9]

->

syntax error, unexpected '2', expecting <identifier>: could not match <AllowedIdentifierFirstCharacters>
yhirose commented 2 years ago

@kfsone, thanks for the report. I fixed this problem without introducing any new operator.