Skip to content

Commit

Permalink
let/const语句在forstmt中缺陷 fix #41
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed Feb 6, 2015
1 parent 7168f1a commit a52c14e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homunculus",
"version": "0.7.5",
"version": "0.7.6",
"description": "A lexer&parser by Javascript",
"maintainers": [
{
Expand Down
63 changes: 51 additions & 12 deletions src/parser/es6/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ var Parser = IParser.extend(function(lexer) {
var end2 = false;
for(var j = i + 1; j < this.length; j++) {
var next = this.tokens[j];
if(!S[token.type()]) {
if(!S[next.type()]) {
if(['in', 'of'].indexOf(next.content()) > -1) {
node.add(
this.match(),
Expand Down Expand Up @@ -683,18 +683,57 @@ var Parser = IParser.extend(function(lexer) {
}
}
else if(['let', 'const'].indexOf(this.look.content()) > -1) {
node.add(
this.match(),
this.forbind()
);
if(!this.look || ['in', 'of'].indexOf(this.look.content()) == -1) {
this.error();
outer:
for(var i = this.index; i < this.length; i++) {
var token = this.tokens[i];
if(!S[token.type()]) {
//直接指向LexicalDeclaration
if(['{', '['].indexOf(token.content()) > -1) {
node.add(this.lexdecl(yYield));
if(this.look && this.look.content() != ';') {
node.add(this.expr());
}
node.add(this.match(';'));
if(this.look && this.look.content() != ')') {
node.add(this.expr());
}
break;
}
//仅一个id之后跟着of或in也是LexicalDeclaration
else if(token.type() == Token.ID) {
for(var j = i + 1; j < this.length; j++) {
var next = this.tokens[j];
if(!S[next.type()]) {
if(['in', 'of'].indexOf(next.content()) > -1) {
node.add(
this.match(),
this.forbind()
);
var isOf = next.content() == 'of';
node.add(
this.match(),
isOf ? this.assignexpr() : this.expr()
);
}
else {
node.add(this.lexdecl(yYield));
if(this.look && this.look.content() != ';') {
node.add(this.expr());
}
node.add(this.match(';'));
if(this.look && this.look.content() != ')') {
node.add(this.expr());
}
}
break outer;
}
}
}
else {
this.error();
}
}
}
var isOf = this.look.content() == 'of';
node.add(
this.match(),
isOf ? this.assignexpr() : this.expr()
);
}
else {
if(['in', 'of'].indexOf(this.look.content()) > -1) {
Expand Down
24 changes: 22 additions & 2 deletions tests/es6parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ describe('es6parser', function() {
it('forstmt 4', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('for(var k in {}){}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.ITERSTMT,["for","(",JsNode.VARSTMT,["var",JsNode.VARDECL,[JsNode.BINDID,["k"]]],"in",JsNode.PRMREXPR,[JsNode.OBJLTR,["{","}"]],")",JsNode.BLOCKSTMT,[JsNode.BLOCK,["{","}"]]]]]]);
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.ITERSTMT,["for","(","var",JsNode.BINDID,["k"],"in",JsNode.PRMREXPR,[JsNode.OBJLTR,["{","}"]],")",JsNode.BLOCKSTMT,[JsNode.BLOCK,["{","}"]]]]]]);
});
it('forstmt 5', function() {
var parser = homunculus.getParser('es6');
Expand Down Expand Up @@ -1128,6 +1128,26 @@ describe('es6parser', function() {
var node = parser.parse('for({a,b} of o){}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.ITERSTMT,["for","(",JsNode.PRMREXPR,[JsNode.OBJLTR,["{",JsNode.PROPTDEF,["a"],",",JsNode.PROPTDEF,["b"],"}"]],"of",JsNode.PRMREXPR,["o"],")",JsNode.BLOCKSTMT,[JsNode.BLOCK,["{","}"]]]]]]);
});
it('forstmt 12', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('for(let a of {});');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.ITERSTMT,["for","(","let",JsNode.BINDID,["a"],"of",JsNode.PRMREXPR,[JsNode.OBJLTR,["{","}"]],")",JsNode.EMPTSTMT,[";"]]]]]);
});
it('forstmt 13', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('for(let i = 0; i < 2; i++){}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.ITERSTMT,["for","(",JsNode.LEXDECL,["let",JsNode.LEXBIND,[JsNode.BINDID,["i"],JsNode.INITLZ,["=",JsNode.PRMREXPR,["0"]]],";"],JsNode.RELTEXPR,[JsNode.PRMREXPR,["i"],"<",JsNode.PRMREXPR,["2"]],";",JsNode.POSTFIXEXPR,[JsNode.PRMREXPR,["i"],"++"],")",JsNode.BLOCKSTMT,[JsNode.BLOCK,["{","}"]]]]]]);
});
it('forstmt 14', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('for(let [a] = [1];;){}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.ITERSTMT,["for","(",JsNode.LEXDECL,["let",JsNode.LEXBIND,[JsNode.ARRBINDPAT,["[",JsNode.SINGLENAME,[JsNode.BINDID,["a"]],"]"],JsNode.INITLZ,["=",JsNode.PRMREXPR,[JsNode.ARRLTR,["[",JsNode.PRMREXPR,["1"],"]"]]]],";"],";",")",JsNode.BLOCKSTMT,[JsNode.BLOCK,["{","}"]]]]]]);
});
it('forstmt 15', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('for(let a = 1;;);');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.ITERSTMT,["for","(",JsNode.LEXDECL,["let",JsNode.LEXBIND,[JsNode.BINDID,["a"],JsNode.INITLZ,["=",JsNode.PRMREXPR,["1"]]],";"],";",")",JsNode.EMPTSTMT,[";"]]]]]);
});
it('forstmt missing expr', function() {
var parser = homunculus.getParser('es6');
expect(function() {
Expand All @@ -1149,7 +1169,7 @@ describe('es6parser', function() {
it('forstmt let error', function() {
var parser = homunculus.getParser('js');
expect(function() {
parser.parse('for(left a = 1;;);');
parser.parse('for(let ;;);');
}).to.throwError();
});
it('forstmt missing ; 1', function() {
Expand Down
63 changes: 51 additions & 12 deletions web/parser/es6/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ var Parser = IParser.extend(function(lexer) {
var end2 = false;
for(var j = i + 1; j < this.length; j++) {
var next = this.tokens[j];
if(!S[token.type()]) {
if(!S[next.type()]) {
if(['in', 'of'].indexOf(next.content()) > -1) {
node.add(
this.match(),
Expand Down Expand Up @@ -683,18 +683,57 @@ var Parser = IParser.extend(function(lexer) {
}
}
else if(['let', 'const'].indexOf(this.look.content()) > -1) {
node.add(
this.match(),
this.forbind()
);
if(!this.look || ['in', 'of'].indexOf(this.look.content()) == -1) {
this.error();
outer:
for(var i = this.index; i < this.length; i++) {
var token = this.tokens[i];
if(!S[token.type()]) {
//直接指向LexicalDeclaration
if(['{', '['].indexOf(token.content()) > -1) {
node.add(this.lexdecl(yYield));
if(this.look && this.look.content() != ';') {
node.add(this.expr());
}
node.add(this.match(';'));
if(this.look && this.look.content() != ')') {
node.add(this.expr());
}
break;
}
//仅一个id之后跟着of或in也是LexicalDeclaration
else if(token.type() == Token.ID) {
for(var j = i + 1; j < this.length; j++) {
var next = this.tokens[j];
if(!S[next.type()]) {
if(['in', 'of'].indexOf(next.content()) > -1) {
node.add(
this.match(),
this.forbind()
);
var isOf = next.content() == 'of';
node.add(
this.match(),
isOf ? this.assignexpr() : this.expr()
);
}
else {
node.add(this.lexdecl(yYield));
if(this.look && this.look.content() != ';') {
node.add(this.expr());
}
node.add(this.match(';'));
if(this.look && this.look.content() != ')') {
node.add(this.expr());
}
}
break outer;
}
}
}
else {
this.error();
}
}
}
var isOf = this.look.content() == 'of';
node.add(
this.match(),
isOf ? this.assignexpr() : this.expr()
);
}
else {
if(['in', 'of'].indexOf(this.look.content()) > -1) {
Expand Down

0 comments on commit a52c14e

Please sign in to comment.