Javascript example: Improve array literal rules
Makes the |ArrayLiteral| and |ElementList| rules more in line with the ECMAScript grammar. Based on a patch by @fpirsch: https://github.com/dmajda/pegjs/pull/177
This commit is contained in:
parent
b6ccad6695
commit
d71bca46a1
|
@ -475,20 +475,26 @@ PrimaryExpression
|
||||||
/ "(" __ expression:Expression __ ")" { return expression; }
|
/ "(" __ expression:Expression __ ")" { return expression; }
|
||||||
|
|
||||||
ArrayLiteral
|
ArrayLiteral
|
||||||
= "[" __ elements:ElementList? __ (Elision __)? "]" {
|
= "[" __ (Elision __)? "]" {
|
||||||
return {
|
return {
|
||||||
type: "ArrayLiteral",
|
type: "ArrayLiteral",
|
||||||
elements: elements !== "" ? elements : []
|
elements: []
|
||||||
|
};
|
||||||
|
}
|
||||||
|
/ "[" __ elements:ElementList __ ("," __ (Elision __)?)? "]" {
|
||||||
|
return {
|
||||||
|
type: "ArrayLiteral",
|
||||||
|
elements: elements
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
ElementList
|
ElementList
|
||||||
= (Elision __)?
|
= (Elision __)?
|
||||||
head:AssignmentExpression
|
head:AssignmentExpression
|
||||||
tail:(__ "," __ Elision? __ AssignmentExpression)* {
|
tail:(__ "," __ (Elision __)? AssignmentExpression)* {
|
||||||
var result = [head];
|
var result = [head];
|
||||||
for (var i = 0; i < tail.length; i++) {
|
for (var i = 0; i < tail.length; i++) {
|
||||||
result.push(tail[i][5]);
|
result.push(tail[i][4]);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue