I defined the following minimal Peg.js grammar:
start = "A1" / "A123"
which you can try in the sandbox .
I would expect it to match โA1โ as well as โA123โ (according to my idea of โโhow the rollback works). But this is not so: the grammar recognizes "A1", but not "A123".
Note I am not looking for advice "to cancel the order of your conditions", as in the related question How to turn a simple grammar into something that works in PEG.js (expected "a" but "found" ). Rather, I am looking to understand the behavior that I see, and why Peg.js backtracking does not apply in this case. For an explanation of why reordering my terms doesn't help, see a More Realistic Example Below.
For a more realistic example, consider parsing units. The grammar must recognize metric units (for example, "m", "mol") with additional prefixes such as "mm", "mmol", as well as non-metric units such as "yr", "week" or "mo".
The following Peg.js grammar will not recognize moles because it receives spontaneous consumption of mo and does not back down. (Changing the order of the terms does not help, or, rather, leads to the recognition of "mo" due to "mole" or "mmol."
start = nonmetric / metric / prefix metric metric = "mol" / "l" / "m" / "g" nonmetric = "yr" / "mo" / "week" / "day" / "hour" prefix = "m" / "k" / "c"
I can do a similar thing in Antlr with good success:
grammar units; start : nonmetric | metric | prefix metric; metric : 'mol' | 'l' | 'm' | 'g'; nonmetric : 'yr' | 'mo' | 'week' | 'day' | 'hour'; prefix : 'm' | 'k' | 'c';
source share