I want to use PEG.js to recognize numbers in a few different formats:
number = integer
/ real
/ (integer/real) '^' (integer/real)
/ (integer/real) '^^' (integer/real)
real = [0-9]+ '.' [0-9]*
/ [0-9]* '.' [0-9]+
integer = [0-9]+
PEG.js does not look for alternatives if there is a partial match earlier in the sequence of alternatives. Most numbers start with an integer, and if they don't they start with a real number. How can I rewrite this so that it tests each alternative individually? (I am not interested in compounding the rules into fewer rules.)
Try matching more specific expressions first (because there's overlap between the rules, so you want PEG to match more specifically first to prevent partial matches):
number = ( real / integer ) '^' ( real / integer )
/ ( real / integer ) '^^' ( real / integer )
/ real
/ integer