Why does pegjs fail to process the whitespace rule " "*

298 views Asked by At

The following simple pegjs grammar works fine:

start
  = sentence

sentence
  = word ws sentence
  / word

word
  = [a-z]*

ws
  = " "

It is available at http://jsfiddle.net/4V3Zt/ . The gramar can be also pasted into http://pegjs.majda.cz/online .

If I change the ws rule to allow whitespaces arbitrary spaces:

ws
  = " "*    // add an asterisk to allow " ", "  ", "   ", ...

parsing fails with a maximum callstack exception thrown. (If using jsfiddle, you see the exception in browser tools.The exception comes up in a node.js environment as well, so it is certainly related to pegjs).

What is wrong with the * in this ws rule?

1

There are 1 answers

0
rici On BEST ANSWER

Use + instead of * (for word as well).

* can match the empty string; + requires at least one instance.