diff --git a/dist/lexer/rule/EcmascriptRule.js b/dist/lexer/rule/EcmascriptRule.js index 7eb0ea1..9a5c62b 100644 --- a/dist/lexer/rule/EcmascriptRule.js +++ b/dist/lexer/rule/EcmascriptRule.js @@ -32,19 +32,19 @@ self.addMatch(new LineParse(Token.STRING, "'", "'", false, Lexer.IS_REG)); self.addMatch(new LineParse(Token.TEMPLATE, '`', '`', true, Lexer.IS_REG)); - var regMatch = new RegMatch(Token.ID, /^[$a-zA-Z_][$\w]*/, Lexer.SPECIAL, function() { + var id = new RegMatch(Token.ID, /^[$a-zA-Z_][$\w]*/, Lexer.SPECIAL, function() { return !!(self.keyWords().hasOwnProperty(this.content())); }, function() { return ['if', 'for', 'while'].indexOf(this.content()) != -1; }); - regMatch.callback = function(token) { + id.callback = function(token) { var s = token.content(); if(isProperty) { token.type(Token.ID); } isProperty = false; }; - self.addMatch(regMatch); + self.addMatch(id); self.addMatch(new RegMatch(Token.NUMBER, /^\.\d+(?:E[+-]?\d*)?/i, { 'SyntaxError: missing exponent': /E[+-]?$/i diff --git a/src/lexer/rule/EcmascriptRule.js b/src/lexer/rule/EcmascriptRule.js index a048e7e..7f5fbdd 100644 --- a/src/lexer/rule/EcmascriptRule.js +++ b/src/lexer/rule/EcmascriptRule.js @@ -24,19 +24,19 @@ var EcmascriptRule = Rule.extend(function() { self.addMatch(new LineParse(Token.STRING, "'", "'", false, Lexer.IS_REG)); self.addMatch(new LineParse(Token.TEMPLATE, '`', '`', true, Lexer.IS_REG)); - var regMatch = new RegMatch(Token.ID, /^[$a-zA-Z_][$\w]*/, Lexer.SPECIAL, function() { + var id = new RegMatch(Token.ID, /^[$a-zA-Z_][$\w]*/, Lexer.SPECIAL, function() { return !!(self.keyWords().hasOwnProperty(this.content())); }, function() { return ['if', 'for', 'while'].indexOf(this.content()) != -1; }); - regMatch.callback = function(token) { + id.callback = function(token) { var s = token.content(); if(isProperty) { token.type(Token.ID); } isProperty = false; }; - self.addMatch(regMatch); + self.addMatch(id); self.addMatch(new RegMatch(Token.NUMBER, /^\.\d+(?:E[+-]?\d*)?/i, { 'SyntaxError: missing exponent': /E[+-]?$/i diff --git a/tests/es6parser.js b/tests/es6parser.js index 2777e47..8200ed6 100644 --- a/tests/es6parser.js +++ b/tests/es6parser.js @@ -137,6 +137,18 @@ describe('es6parser', function() { parser.parse('var {a '); }).to.throwError(); }); + it('destructuring object error: kw can not in array 1', function() { + var parser = homunculus.getParser('es6'); + expect(function() { + parser.parse('var [var] = [1]'); + }).to.throwError(); + }); + it('destructuring object error: kw can not in array 2', function() { + var parser = homunculus.getParser('es6'); + expect(function() { + parser.parse('var [x, {"a":[var=1,{z=2},...o]}] = []'); + }).to.throwError(); + }); it('letdecl 1', function() { var parser = homunculus.getParser('es6'); var node = parser.parse('let a'); @@ -476,6 +488,12 @@ describe('es6parser', function() { var node = parser.parse('var o = {var:1}'); expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.VARSTMT,["var",JsNode.VARDECL,[JsNode.BINDID,["o"],JsNode.INITLZ,["=",JsNode.PRMREXPR,[JsNode.OBJLTR,["{",JsNode.PROPTDEF,[JsNode.PROPTNAME,[JsNode.LTRPROPT,["var"]],":",JsNode.PRMREXPR,["1"]],"}"]]]]]]]]); }); + it('keyword can not be label', function() { + var parser = homunculus.getParser('es6'); + expect(function() { + parser.parse('var:'); + }).to.throwError(); + }); it('keyword after get/set', function() { var parser = homunculus.getParser('es6'); var node = parser.parse('~{get var(){}}'); diff --git a/tests/jsparser.js b/tests/jsparser.js index f942dc3..dcab4a9 100644 --- a/tests/jsparser.js +++ b/tests/jsparser.js @@ -313,6 +313,12 @@ describe('jsparser', function() { var node = parser.parse('var o = {var:1}'); expect(tree(node)).to.eql([JsNode.PROGRAM,[JsNode.VARSTMT,["var",JsNode.VARDECL,["o",JsNode.ASSIGN,["=",JsNode.PRMREXPR,[JsNode.OBJLTR,["{",JsNode.PROPTASSIGN,["var",":",JsNode.PRMREXPR,["1"]],"}"]]]]]]]); }); + it('keyword can not be label', function() { + var parser = homunculus.getParser('js'); + expect(function() { + parser.parse('var:'); + }).to.throwError(); + }); it('keyword after get/set', function() { var parser = homunculus.getParser('js'); var node = parser.parse('~{get var(){}}');