yhirose / cpp-peglib

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

Whitespace is ignored before it can be look-ahead tested #204

Closed kfsone closed 2 years ago

kfsone commented 2 years ago

We're using a quasi-ini styled file format, and one of the conditions I'd like to assert is that the section header lines have nothing but a newline after the close ']'.

section <- "[" identifier "]" &'\n'

Almost nowhere else do I care about newlines, or the grammar gets difficult, unfortunately the discard of whitespace appears to prevent doing this kind of lookahead test:

image

yhirose commented 2 years ago

@kfsone, you are correct since %whitespace has higher priority and eats white spaces after tokens before a following operator gets evaluated. If you need to fully control white space behavior, you can't use %whitespace, rather you have to do it manually. %whitespace and %word can be used for common situations, but don't cover all the possible use cases like yours.