Skip to content

Commit 8fc43cd

Browse files
committed
Parser: Fix another break/continue/return issue
After the previous lexer/parser fix, another problem showed up with these keywords enclosed in curly braces. The solution was to update the AutoSemi parser rule to accept any of the following: - A real semicolon - An AutoSemiToken inserted by the lexer - Nothing (which fixes this new case) Tests added as well.
1 parent 6ebef86 commit 8fc43cd

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

runtests.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ parserSuite = testGroup "Parser"
333333
, testCase "AutoSemiBreak" (testProg "if(true)break \nfoo();" "Right (JSSourceElementsTop [JSIf (JSExpression [JSLiteral \"true\"]) ([JSBreak [] JSLiteral \"\"]) ([]),JSExpression [JSIdentifier \"foo\",JSArguments []],JSLiteral \";\",JSLiteral \"\"])")
334334
, testCase "AutoSemiContinue" (testProg "if(true)continue \nfoo();" "Right (JSSourceElementsTop [JSIf (JSExpression [JSLiteral \"true\"]) ([JSContinue [] JSLiteral \"\"]) ([]),JSExpression [JSIdentifier \"foo\",JSArguments []],JSLiteral \";\",JSLiteral \"\"])")
335335
, testCase "AutoSemiReturn" (testProg "if(true)break \nfoo();" "Right (JSSourceElementsTop [JSIf (JSExpression [JSLiteral \"true\"]) ([JSBreak [] JSLiteral \"\"]) ([]),JSExpression [JSIdentifier \"foo\",JSArguments []],JSLiteral \";\",JSLiteral \"\"])")
336+
337+
, testCase "BreakBlock" (testProg "{break}" "Right (JSSourceElementsTop [JSBlock ([JSBreak [] JSLiteral \"\"]),JSLiteral \"\"])")
338+
, testCase "ContinueBlock" (testProg "{continue}" "Right (JSSourceElementsTop [JSBlock ([JSContinue [] JSLiteral \"\"]),JSLiteral \"\"])")
339+
, testCase "ReturnBlock" (testProg "{return}" "Right (JSSourceElementsTop [JSBlock ([JSReturn [] JSLiteral \"\"]),JSLiteral \"\"])")
336340
]
337341

338342
caseHelloWorld :: Assertion

src/Language/JavaScript/Parser/Grammar5.y

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ import qualified Language.JavaScript.Parser.AST as AST
127127
-- Sort out automatically inserted semi-colons.
128128
-- A MaybeSemi is an actual semi-colon or nothing.
129129
-- An AutoSemu is either an actual semi-colon or 'virtual' semi-colon inserted
130-
-- by the Alex lexer.
130+
-- by the Alex lexer or nothing.
131131

132132
MaybeSemi :: { AST.JSNode }
133133
MaybeSemi : ';' { AST.NT (AST.JSLiteral ";") (ss $1) (gc $1)}
@@ -136,6 +136,7 @@ MaybeSemi : ';' { AST.NT (AST.JSLiteral ";") (ss $1) (gc $1)}
136136
AutoSemi :: { AST.JSNode }
137137
AutoSemi : ';' { AST.NT (AST.JSLiteral ";") (ss $1) (gc $1)}
138138
| 'autosemi' { AST.NT (AST.JSLiteral "") (ss $1) (gc $1)}
139+
| { AST.NT (AST.JSLiteral "") tokenPosnEmpty []}
139140

140141
-- ---------------------------------------------------------------------
141142

0 commit comments

Comments
 (0)