dmajda/pegjs — Issue #70: join feature for * zero_or_more
Author: jaekwon
Created: 2012-02-12 01:03:58+00:00
State: open

First of all, this project is great. I was working on my own parser generator and it turns out I was trying to implement PegJS.

I see a lot of head/tail conventions in programming language grammars, like examples/javascript.pegjs. I think there are more than a dozen of these cases in javascript.pegjs.
```javascript
head:AssignmentExpression tail:(__ "," __ Elision? __ AssignmentExpression)* {
var result = [head];
for (var i = 0; i < tail.length; i++) {
result.push(tail[i][5]);
}
}
```

How about this?
```javascript
assignments:(AssignmentExpression)*{__ "," __ Elision?; 1, } {
var result = assignments
}
```

The proposed syntax (modeled after regex) is
```
expression:primary star ("{" join:primary? (";" min:integer? ("," max:integer? )? )? "}")?
```

This would make the pegjs file easier to comprehend, IMO.

See my untested implementation here: https://github.com/jaekwon/pegjs/commit/4a979356546ee352d334df3d2dc483f69261817a

BTW, I had to make an edit to allow labels in optional nodes. That could be a separate issue.