ohm icon indicating copy to clipboard operation
ohm copied to clipboard

Odd behavior: Alternation fails instead of trying second alternative

Open theHacker opened this issue 1 year ago • 0 comments

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 EmptyQuery and Expected end of input.

Why?! The reference says about the Alternation

Matches the expression expr1, and if that does not succeed, matches the expression expr2.

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?

theHacker avatar Aug 19 '24 16:08 theHacker