Skip to content

Commit 4229c1b

Browse files
committed
Allow trailing comma in object literal
1 parent 689e548 commit 4229c1b

File tree

4 files changed

+8
-1
lines changed

4 files changed

+8
-1
lines changed

README

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ $ cabal install --reinstall alex
4545

4646
Changes
4747

48+
0.2.1 Allow trailing comma in object literal
49+
4850
0.2.0 ECMAScript 3 allows function expressions to have names, AST.JSFunctionExpression now reflects this
4951

5052
0.1.0 Simplified AST by removing JSElement and JSElementList components

language-javascript.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Name: language-javascript
2-
Version: 0.2.0
2+
Version: 0.2.1
33
Synopsis: Parser for JavaScript
44
Description: Parses Javascript into an Abstract Syntax Tree (AST). Initially intended as frontend to hjsmin.
55
Homepage: https://github.com/alanz/language-javascript

runtests.hs

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ testSuite = testGroup "Parser"
5656

5757
, testCase "ObjectLiteral4" (testPE "{evaluate:evaluate,load:function load(s){if(x)return s;1}}" "Right (JSObjectLiteral [JSPropertyNameandValue (JSIdentifier \"evaluate\") [JSIdentifier \"evaluate\"],JSPropertyNameandValue (JSIdentifier \"load\") [JSFunction (JSIdentifier \"load\") [JSIdentifier \"s\"] (JSFunctionBody [JSSourceElements [JSIf (JSExpression [JSIdentifier \"x\"]) (JSReturn [JSExpression [JSIdentifier \"s\"],JSLiteral \";\"]),JSExpression [JSDecimal \"1\"]]])]])")
5858

59+
, testCase "ObjectLiteral5" (testPE "{x:1,}" "Right (JSObjectLiteral [JSPropertyNameandValue (JSIdentifier \"x\") [JSDecimal \"1\"],JSLiteral \",\"])")
60+
5961
, testCase "ExpressionParen" (testPE "(56)" "Right (JSExpressionParen (JSExpression [JSDecimal \"56\"]))")
6062

6163
, testCase "Statement1" (testStmt "x" "Right (JSExpression [JSIdentifier \"x\"])")

src/Language/JavaScript/Parser/Grammar.y

+3
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,16 @@ ObjectLiteral : '{' PropertyNameandValueList '}' { AST.JSObjectLiteral $2 }
237237
-- | <Property Name and Value List> ',' <Property Name> ':' <Assignment Expression>
238238

239239
-- Seems we can have function declarations in the value part too
240+
-- TODO: And can end with a comma
240241
PropertyNameandValueList :: { [ AST.JSNode ] }
241242
PropertyNameandValueList : PropertyName ':' AssignmentExpression { [(AST.JSPropertyNameandValue $1 $3)] }
242243
| PropertyName ':' FunctionDeclaration { [(AST.JSPropertyNameandValue $1 [$3])] }
243244
| PropertyNameandValueList ',' PropertyName ':' AssignmentExpression
244245
{ ($1 ++ [(AST.JSPropertyNameandValue $3 $5)]) }
245246
| PropertyNameandValueList ',' PropertyName ':' FunctionDeclaration
246247
{ ($1 ++ [(AST.JSPropertyNameandValue $3 [$5])]) }
248+
| PropertyNameandValueList ','
249+
{ ($1 ++ [(AST.JSLiteral ",")]) }
247250
| { [] }
248251

249252

0 commit comments

Comments
 (0)