I am currently working on creating a basic PEG (pegjs) grammar to analyze either a space separated list or a comma separated list of numbers. However, it seems like I am overlooking a crucial element in my implementation. Essentially, I aim to identify patterns such as "1 2 3" or "1,2,3", but not a combination like "1 2,3".
Here is the grammar I have put together (which can be tested at ):
start = seq
seq = num (" " n:num {return n})*
/ num ("," n:num {return n})*
num = a:$[0-9]+ {return parseInt(a, 10)}
EOL = !.
Nevertheless, this grammar only interprets a space separated list. If I adjust it to:
start = seq
seq = num (" " n:num {return n})* EOL
/ num ("," n:num {return n})* EOL
num = a:$[0-9]+ {return parseInt(a, 10)}
EOL = !.
it will now handle both space separated and comma separated lists. Yet, I sense that having to append EOL
at the end of each expression might not be ideal... I assumed that when confronted with a comma separated list, pegjs would first attempt to match it against a space separated list, fail, and then proceed to the comma separated list rule.
What could I be overlooking?