Consider the following grammar:
list
= head:item (',' tail:item)*
{ return [head].concat(tail); }
item
= $ ([0-9]*)
It should describe lists of positive integers.
The problem is that tail
is undefined as it is inside parens.
So I'm forced to do the following:
list
= head:item tail:tail*
{ return [head].concat(tail); }
tail
= ',' item:item
{ return item; }
item
= $ ([0-9]*)
This can be quite cumbersome in longer grammars.
Is there any way to label what's inside the (',' item)
regex group?
You can indeed label what's inside the group (i:
below), tell PEGJS exactly what to return ({return i;}
), and label the result as well (tail:
).
list
= head:item tail:(',' i:item {return i;})*
{ return [head].concat(tail); }