ohm
ohm copied to clipboard
Odd behavior: Alternation fails instead of trying second alternative
Hi. I am just exploring Ohm in the online editor.
When using this grammar:
Demo {
// does not work as expected
Query = EmptyQuery | NotEmptyQuery
// works as expected
// Query = NotEmptyQuery | EmptyQuery
EmptyQuery = ""
NotEmptyQuery = any+
}
I am confused to as why it makes a difference in which order I put the alternations.
With Query = NotEmptyQuery | EmptyQuery:
- the empty string will match the
EmptyQuery - any non-empty string will match the
NotEmptyQuery
However, with Query = EmptyQuery | NotEmptyQuery :
- the empty string will match as
EmptyQuery - any non-empty string will fail as
EmptyQueryandExpected end of input.
Why?! The reference says about the Alternation
Matches the expression
expr1, and if that does not succeed, matches the expressionexpr2.
Should the parser not try the second alternative, i.e. NotEmptyQuery?
(When I alter the rule to EmptyQuery = "" end, it works as expected).
Is this a bug? Or am I misunderstanding something here?