From d71bca46a125213388995e2c88383f9e159e676f Mon Sep 17 00:00:00 2001 From: David Majda Date: Sat, 31 Aug 2013 10:28:48 +0200 Subject: [PATCH] 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 --- examples/javascript.pegjs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/examples/javascript.pegjs b/examples/javascript.pegjs index 8a22b7d..62f0e5c 100644 --- a/examples/javascript.pegjs +++ b/examples/javascript.pegjs @@ -475,20 +475,26 @@ PrimaryExpression / "(" __ expression:Expression __ ")" { return expression; } ArrayLiteral - = "[" __ elements:ElementList? __ (Elision __)? "]" { + = "[" __ (Elision __)? "]" { return { type: "ArrayLiteral", - elements: elements !== "" ? elements : [] + elements: [] + }; + } + / "[" __ elements:ElementList __ ("," __ (Elision __)?)? "]" { + return { + type: "ArrayLiteral", + elements: elements }; } ElementList = (Elision __)? head:AssignmentExpression - tail:(__ "," __ Elision? __ AssignmentExpression)* { + tail:(__ "," __ (Elision __)? AssignmentExpression)* { var result = [head]; for (var i = 0; i < tail.length; i++) { - result.push(tail[i][5]); + result.push(tail[i][4]); } return result; }