>>> g = MatchFirst( Literal("scoobydoo"), Literal("scooby") )
>>> g.parseString( "scooby" )
pyparsing.ParseException: Expected "scoobydoo" (at char 0), (line:1, col:1)
Is the ParseException thrown because the scooby
has already been consumed in the character stream & thus the parser cannot backtrack ? I'm looking for a detailed implementation explanation for this.
At the moment, I consider this a bug because why would the parser short-circuit the matching since it has not search all the choices in production rule.
UPDATE:
Seems like MatchFirst
is not exactly equivalent to |
operator. Why ?
>>> g = Literal("scoobydoo") | Literal("scooby")
>>> g.parseString("scooby").asList()
['scooby']
>>> g.parseString("scoobydoo").asList()
['scoobydoo']
MatchFirst
(or '|') does short-circuiting by design. To force all alternatives to be checked, useOr
(or '^').oneOf("scooby scoobydoo")
will also work, sinceoneOf
will short-circuit, but only after rearranging alternative words that have leading overlaps.