Skip to content

Commit eef1887

Browse files
rhendricerikd
authored andcommitted
Add support for super keyword
1 parent 5f6995a commit eef1887

File tree

5 files changed

+14
-1
lines changed

5 files changed

+14
-1
lines changed

src/Language/JavaScript/Parser/Grammar7.y

+9
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ import qualified Language.JavaScript.Parser.AST as AST
117117
'return' { ReturnToken {} }
118118
'set' { SetToken {} }
119119
'static' { StaticToken {} }
120+
'super' { SuperToken {} }
120121
'switch' { SwitchToken {} }
121122
'this' { ThisToken {} }
122123
'throw' { ThrowToken {} }
@@ -357,6 +358,7 @@ IdentifierName : Identifier {$1}
357358
| 'of' { AST.JSIdentifier (mkJSAnnot $1) "of" }
358359
| 'return' { AST.JSIdentifier (mkJSAnnot $1) "return" }
359360
| 'static' { AST.JSIdentifier (mkJSAnnot $1) "static" }
361+
| 'super' { AST.JSIdentifier (mkJSAnnot $1) "super" }
360362
| 'switch' { AST.JSIdentifier (mkJSAnnot $1) "switch" }
361363
| 'this' { AST.JSIdentifier (mkJSAnnot $1) "this" }
362364
| 'throw' { AST.JSIdentifier (mkJSAnnot $1) "throw" }
@@ -450,6 +452,9 @@ Extends : 'extends' { mkJSAnnot $1 }
450452
Static :: { AST.JSAnnot }
451453
Static : 'static' { mkJSAnnot $1 }
452454

455+
Super :: { AST.JSExpression }
456+
Super : 'super' { AST.JSLiteral (mkJSAnnot $1) "super" }
457+
453458

454459
Eof :: { AST.JSAnnot }
455460
Eof : 'tail' { mkJSAnnot $1 {- 'Eof' -} }
@@ -641,6 +646,8 @@ MemberExpression : PrimaryExpression { $1 {- 'MemberExpression1' -} }
641646
| MemberExpression LSquare Expression RSquare { AST.JSMemberSquare $1 $2 $3 $4 {- 'MemberExpression3' -} }
642647
| MemberExpression Dot IdentifierName { AST.JSMemberDot $1 $2 $3 {- 'MemberExpression4' -} }
643648
| MemberExpression TemplateLiteral { mkJSTemplateLiteral (Just $1) $2 }
649+
| Super LSquare Expression RSquare { AST.JSMemberSquare $1 $2 $3 $4 }
650+
| Super Dot IdentifierName { AST.JSMemberDot $1 $2 $3 }
644651
| New MemberExpression Arguments { mkJSMemberNew $1 $2 $3 {- 'MemberExpression5' -} }
645652

646653
-- NewExpression : See 11.2
@@ -658,6 +665,8 @@ NewExpression : MemberExpression { $1 {- 'NewExpressio
658665
CallExpression :: { AST.JSExpression }
659666
CallExpression : MemberExpression Arguments
660667
{ mkJSMemberExpression $1 $2 {- 'CallExpression1' -} }
668+
| Super Arguments
669+
{ mkJSCallExpression $1 $2 }
661670
| CallExpression Arguments
662671
{ mkJSCallExpression $1 $2 {- 'CallExpression2' -} }
663672
| CallExpression LSquare Expression RSquare

src/Language/JavaScript/Parser/Lexer.x

+2-1
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ keywordNames =
565565
, ( "of", OfToken )
566566
, ( "return", ReturnToken )
567567
, ( "static", StaticToken )
568+
, ( "super", SuperToken )
568569
, ( "switch", SwitchToken )
569570
, ( "this", ThisToken )
570571
, ( "throw", ThrowToken )
@@ -599,7 +600,7 @@ keywordNames =
599600
-- ( "const", FutureToken ) **** an actual token, used in productions
600601
-- enum **** an actual token, used in productions
601602
-- ( "extends", FutureToken ) **** an actual token, used in productions
602-
, ( "super", FutureToken )
603+
-- ( "super", FutureToken ) **** an actual token, used in productions
603604
604605
605606
-- Strict mode FutureReservedWords

src/Language/JavaScript/Parser/Token.hs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ data Token
8383
| OfToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
8484
| ReturnToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
8585
| StaticToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
86+
| SuperToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
8687
| SwitchToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
8788
| ThisToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
8889
| ThrowToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }

test/Test/Language/Javascript/ExpressionParser.hs

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ testExpressionParser = describe "Parse expressions:" $ do
193193
it "class expression" $ do
194194
testExpr "class Foo extends Bar { a(x,y) {} *b() {} }" `shouldBe` "Right (JSAstExpression (JSClassExpression 'Foo' (JSIdentifier 'Bar') [JSMethodDefinition (JSIdentifier 'a') (JSIdentifier 'x',JSIdentifier 'y') (JSBlock []),JSGeneratorMethodDefinition (JSIdentifier 'b') () (JSBlock [])]))"
195195
testExpr "class { static get [a]() {}; }" `shouldBe` "Right (JSAstExpression (JSClassExpression '' () [JSClassStaticMethod (JSPropertyAccessor JSAccessorGet (JSPropertyComputed (JSIdentifier 'a')) () (JSBlock [])),JSClassSemi]))"
196+
testExpr "class Foo extends Bar { a(x,y) { super(x); } }" `shouldBe` "Right (JSAstExpression (JSClassExpression 'Foo' (JSIdentifier 'Bar') [JSMethodDefinition (JSIdentifier 'a') (JSIdentifier 'x',JSIdentifier 'y') (JSBlock [JSCallExpression (JSLiteral 'super',JSArguments (JSIdentifier 'x')),JSSemicolon])]))"
196197

197198

198199
testExpr :: String -> String

test/Test/Language/Javascript/StatementParser.hs

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ testStatementParser = describe "Parse statements:" $ do
128128
it "class" $ do
129129
testStmt "class Foo extends Bar { a(x,y) {} *b() {} }" `shouldBe` "Right (JSAstStatement (JSClass 'Foo' (JSIdentifier 'Bar') [JSMethodDefinition (JSIdentifier 'a') (JSIdentifier 'x',JSIdentifier 'y') (JSBlock []),JSGeneratorMethodDefinition (JSIdentifier 'b') () (JSBlock [])]))"
130130
testStmt "class Foo { static get [a]() {}; }" `shouldBe` "Right (JSAstStatement (JSClass 'Foo' () [JSClassStaticMethod (JSPropertyAccessor JSAccessorGet (JSPropertyComputed (JSIdentifier 'a')) () (JSBlock [])),JSClassSemi]))"
131+
testStmt "class Foo extends Bar { a(x,y) { super[x](y); } }" `shouldBe` "Right (JSAstStatement (JSClass 'Foo' (JSIdentifier 'Bar') [JSMethodDefinition (JSIdentifier 'a') (JSIdentifier 'x',JSIdentifier 'y') (JSBlock [JSMethodCall (JSMemberSquare (JSLiteral 'super',JSIdentifier 'x'),JSArguments (JSIdentifier 'y')),JSSemicolon])]))"
131132

132133

133134
testStmt :: String -> String

0 commit comments

Comments
 (0)