ohmjs / ohm

A library and language for building parsers, interpreters, compilers, etc.
MIT License
5.01k stars 217 forks source link

maybe an infinite loop? #402

Closed guitarvydas closed 2 years ago

guitarvydas commented 2 years ago

https://github.com/guitarvydas/pt22.git

I'm developing a new grammar. I'm sure that there is a problem in my grammar.

Ohm-Editor gives me a red "parse fail" and appears to have gone into an infinite loop.

I've snapshotted this while it is still fresh in my mind.

See ISSUE1.md for the grammar and the test input. Branch "issue1".

alexwarth commented 2 years ago

Hi Paul,

Here's (part of) your grammar:

comment =
  | "{" dontcareChar* "}" -- nested
  | dontcareChar*         -- base

dontcareChar = ~"}" ~"{" any
space += comment

Note that your comment rule is nullable, i.e., it can succeed w/o consuming any input. Normally this isn't a problem, but by making it a kind of space (via space += comment) you created an infinite loop. This is because Ohm's syntactic rules (like Main and Def, in your grammar) implicitly apply the spaces rule, which is defined as space*.

The fix is to change comment so that it only succeeds if it actually consumes a comment. I would do that by changing dontcareChar* to dontcareChar+.

Hope that helps!

Best, Alex