yhirose / go-peg

Yet another PEG (Parsing Expression Grammars) parser generator for Go
MIT License
63 stars 8 forks source link

Negative lookahead predicate. #4

Closed Kyras closed 6 years ago

Kyras commented 7 years ago

I would need to create predicate to match every symbol but newline symbol. Something like [^\n] in regex or negative lookahead operator !"\n". Would that be possible ?

yhirose commented 7 years ago

@Kyras, sorry for the late replay.

We can use the negative lookahead operator.

# a.peg
START <- TEXT '\n'
TEXT <- (!'\n' .)*
$ peglint -ast -s $'hello' a.peg
1:6 syntax error

$ peglint -ast -s $'hello\n' a.peg
+ START
  - TEXT ("hello")

Hope it helps

yhirose commented 6 years ago

@Kyras, please let me know if the above solution doesn't work for you.