Browse Source

Handle do/while statement with trailing semicolon in if statement

An if statement containing a do/while statement that was not enclosed in brackets, followed by an else statement, would result in an unexpected else token error. This commit handles reads the semicolon following the statement so that the next token is an else token, allowing the if statement to continue parsing as expected.
Adam Keys 7 years ago
parent
commit
03f457fabf
2 changed files with 7 additions and 0 deletions
  1. 4 0
      parser/parser_test.go
  2. 3 0
      parser/statement.go

+ 4 - 0
parser/parser_test.go

@@ -356,6 +356,10 @@ func TestParserErr(t *testing.T) {
 
 
 		test("abc: while (true) { abc: while (true) {} }", "(anonymous): Line 1:21 Label 'abc' already exists")
 		test("abc: while (true) { abc: while (true) {} }", "(anonymous): Line 1:21 Label 'abc' already exists")
 
 
+		test(`if(0) { do { } while(0) } else { do { } while(0) }`, nil)
+
+		test(`if(0) do { } while(0); else do { } while(0)`, nil)
+
 		if false {
 		if false {
 			// TODO When strict mode is implemented
 			// TODO When strict mode is implemented
 			test("(function () { 'use strict'; delete abc; }())", "")
 			test("(function () { 'use strict'; delete abc; }())", "")

+ 3 - 0
parser/statement.go

@@ -483,6 +483,9 @@ func (self *_parser) parseDoWhileStatement() ast.Statement {
 	self.expect(token.LEFT_PARENTHESIS)
 	self.expect(token.LEFT_PARENTHESIS)
 	node.Test = self.parseExpression()
 	node.Test = self.parseExpression()
 	self.expect(token.RIGHT_PARENTHESIS)
 	self.expect(token.RIGHT_PARENTHESIS)
+	if self.token == token.SEMICOLON {
+		self.next()
+	}
 
 
 	return node
 	return node
 }
 }