0

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?

Pierre Arlaud
  • 4,040
  • 3
  • 28
  • 42
  • 1
    I think that's how it's supposed to work. It's cumbersome but it helps the grammar optimize since the "tail" rule can be cached between calls. – Josh Voigts Jan 22 '19 at 18:49

1 Answers1

3

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); }
Marc Q.
  • 561
  • 3
  • 11