Skip to content

Commit

Permalink
keyword can be an object's property name
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed Jun 6, 2014
1 parent a8a8775 commit e587d5d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 15 deletions.
18 changes: 13 additions & 5 deletions dist/parser/es6/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@
this.declnode(node);
return node;
},
bindid: function(msg, noIn, noOf) {
bindid: function(msg, noIn, noOf, canKw) {
var node = new Node(Node.BINDID);
if(!this.look) {
this.error(msg);
Expand All @@ -422,7 +422,12 @@
if(noOf && this.look.content() == 'of') {
this.error();
}
node.add(this.match(Token.ID, msg));
if(canKw && this.look.type() == Token.KEYWORD) {
node.add(this.match(undefined, msg));
}
else {
node.add(this.match(Token.ID, msg));
}
return node;
},
bindpat: function() {
Expand Down Expand Up @@ -469,9 +474,9 @@
}
return node;
},
singlename: function() {
singlename: function(canKw) {
var node = new Node(Node.SINGLENAME);
node.add(this.bindid());
node.add(this.bindid(null, null, null, canKw));
if(this.look && this.look.content() == '=') {
node.add(this.initlz());
}
Expand Down Expand Up @@ -504,6 +509,7 @@
case Token.ID:
case Token.STRING:
case Token.NUMBER:
case Token.KEYWORD:
break;
default:
if(this.look.content() != '[') {
Expand Down Expand Up @@ -532,7 +538,7 @@
);
}
else {
node.add(this.singlename());
node.add(this.singlename(true));
}
return node;
}
Expand Down Expand Up @@ -2044,6 +2050,7 @@
}
case Token.STRING:
case Token.NUMBER:
case Token.KEYWORD:
node.add(
this.proptname(noIn, noOf),
this.match(':', 'missing : after property id'),
Expand Down Expand Up @@ -2096,6 +2103,7 @@
}
case Token.NUMBER:
case Token.STRING:
case Token.KEYWORD:
node.add(this.match());
return node;
default:
Expand Down
5 changes: 3 additions & 2 deletions dist/parser/js/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@
if(!this.look) {
this.error('missing : after property id');
}
if(this.look.type() == Token.ID) {
if(this.look.type() == Token.ID || this.look.type() == Token.KEYWORD) {
node.add(
this.match(),
this.match('(', 'missing ( before formal parameters'),
Expand All @@ -1082,7 +1082,7 @@
if(!this.look) {
this.error('missing : after property id');
}
if(this.look.type() == Token.ID) {
if(this.look.type() == Token.ID || this.look.type() == Token.KEYWORD) {
node.add(
this.match(),
this.match('(', 'missing ( before formal parameters'),
Expand All @@ -1103,6 +1103,7 @@
}
case Token.STRING:
case Token.NUMBER:
case Token.KEYWORD:
node.add(
this.match(),
this.match(':', 'missing : after property id'),
Expand Down
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.2.2",
"version": "0.2.3",
"description": "A lexer&parser by Javascript",
"maintainers": [
{
Expand Down
18 changes: 13 additions & 5 deletions src/parser/es6/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ var Parser = IParser.extend(function(lexer) {
this.declnode(node);
return node;
},
bindid: function(msg, noIn, noOf) {
bindid: function(msg, noIn, noOf, canKw) {
var node = new Node(Node.BINDID);
if(!this.look) {
this.error(msg);
Expand All @@ -414,7 +414,12 @@ var Parser = IParser.extend(function(lexer) {
if(noOf && this.look.content() == 'of') {
this.error();
}
node.add(this.match(Token.ID, msg));
if(canKw && this.look.type() == Token.KEYWORD) {
node.add(this.match(undefined, msg));
}
else {
node.add(this.match(Token.ID, msg));
}
return node;
},
bindpat: function() {
Expand Down Expand Up @@ -461,9 +466,9 @@ var Parser = IParser.extend(function(lexer) {
}
return node;
},
singlename: function() {
singlename: function(canKw) {
var node = new Node(Node.SINGLENAME);
node.add(this.bindid());
node.add(this.bindid(null, null, null, canKw));
if(this.look && this.look.content() == '=') {
node.add(this.initlz());
}
Expand Down Expand Up @@ -496,6 +501,7 @@ var Parser = IParser.extend(function(lexer) {
case Token.ID:
case Token.STRING:
case Token.NUMBER:
case Token.KEYWORD:
break;
default:
if(this.look.content() != '[') {
Expand Down Expand Up @@ -524,7 +530,7 @@ var Parser = IParser.extend(function(lexer) {
);
}
else {
node.add(this.singlename());
node.add(this.singlename(true));
}
return node;
}
Expand Down Expand Up @@ -2036,6 +2042,7 @@ var Parser = IParser.extend(function(lexer) {
}
case Token.STRING:
case Token.NUMBER:
case Token.KEYWORD:
node.add(
this.proptname(noIn, noOf),
this.match(':', 'missing : after property id'),
Expand Down Expand Up @@ -2088,6 +2095,7 @@ var Parser = IParser.extend(function(lexer) {
}
case Token.NUMBER:
case Token.STRING:
case Token.KEYWORD:
node.add(this.match());
return node;
default:
Expand Down
5 changes: 3 additions & 2 deletions src/parser/js/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ var Parser = IParser.extend(function(lexer) {
if(!this.look) {
this.error('missing : after property id');
}
if(this.look.type() == Token.ID) {
if(this.look.type() == Token.ID || this.look.type() == Token.KEYWORD) {
node.add(
this.match(),
this.match('(', 'missing ( before formal parameters'),
Expand All @@ -1074,7 +1074,7 @@ var Parser = IParser.extend(function(lexer) {
if(!this.look) {
this.error('missing : after property id');
}
if(this.look.type() == Token.ID) {
if(this.look.type() == Token.ID || this.look.type() == Token.KEYWORD) {
node.add(
this.match(),
this.match('(', 'missing ( before formal parameters'),
Expand All @@ -1095,6 +1095,7 @@ var Parser = IParser.extend(function(lexer) {
}
case Token.STRING:
case Token.NUMBER:
case Token.KEYWORD:
node.add(
this.match(),
this.match(':', 'missing : after property id'),
Expand Down
15 changes: 15 additions & 0 deletions tests/es6parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ describe('es6parser', function() {
var node = parser.parse('var {x, y = 1, "f": [z]} = {}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.VARSTMT,["var",JsNode.VARDECL,[JsNode.OBJBINDPAT,["{",JsNode.BINDPROPT,[JsNode.SINGLENAME,[JsNode.BINDID,["x"]]],",",JsNode.BINDPROPT,[JsNode.SINGLENAME,[JsNode.BINDID,["y"],JsNode.INITLZ,["=",JsNode.PRMREXPR,["1"]]]],",",JsNode.BINDPROPT,[JsNode.PROPTNAME,[JsNode.LTRPROPT,["\"f\""]],":",JsNode.BINDELEM,[JsNode.ARRBINDPAT,["[",JsNode.SINGLENAME,[JsNode.BINDID,["z"]],"]"]]],"}"],JsNode.INITLZ,["=",JsNode.PRMREXPR,[JsNode.OBJLTR,["{","}"]]]]]]]]);
});
it('destructuring object with keyword property', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('var {var, f,} = {}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.VARSTMT,["var",JsNode.VARDECL,[JsNode.OBJBINDPAT,["{",JsNode.BINDPROPT,[JsNode.SINGLENAME,[JsNode.BINDID,["var"]]],",",JsNode.BINDPROPT,[JsNode.SINGLENAME,[JsNode.BINDID,["f"]]],",","}"],JsNode.INITLZ,["=",JsNode.PRMREXPR,[JsNode.OBJLTR,["{","}"]]]]]]]]);
});
it('destructuring complex', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('var [x, {"a":[y=1,{z=2},...o]}] = []');
Expand Down Expand Up @@ -466,6 +471,16 @@ describe('es6parser', function() {
var node = parser.parse('~{x,y,}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.EXPRSTMT,[JsNode.UNARYEXPR,["~",JsNode.PRMREXPR,[JsNode.OBJLTR,["{",JsNode.PROPTDEF,["x"],",",JsNode.PROPTDEF,["y"],",","}"]]]]]]]);
});
it('keyword can be obj\'s property', function() {
var parser = homunculus.getParser('es6');
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 after get/set', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('~{get var(){}}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.EXPRSTMT,[JsNode.UNARYEXPR,["~",JsNode.PRMREXPR,[JsNode.OBJLTR,["{",JsNode.PROPTDEF,[JsNode.METHOD,["get",JsNode.PROPTNAME,[JsNode.LTRPROPT,["var"]],"(",")","{",JsNode.FNBODY,[],"}"]],"}"]]]]]]]);
});
it('newexpr 1', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('new A');
Expand Down
10 changes: 10 additions & 0 deletions tests/jsparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ describe('jsparser', function() {
var node = parser.parse('var o = {a: [], "b": 3, 5: {}}');
expect(tree(node)).to.eql([JsNode.PROGRAM,[JsNode.VARSTMT,["var",JsNode.VARDECL,["o",JsNode.ASSIGN,["=",JsNode.PRMREXPR,[JsNode.OBJLTR,["{",JsNode.PROPTASSIGN,["a",":",JsNode.PRMREXPR,[JsNode.ARRLTR,["[","]"]]],",",JsNode.PROPTASSIGN,["\"b\"",":",JsNode.PRMREXPR,["3"]],",",JsNode.PROPTASSIGN,["5",":",JsNode.PRMREXPR,[JsNode.OBJLTR,["{","}"]]],"}"]]]]]]]);
});
it('keyword can be obj\'s property', function() {
var parser = homunculus.getParser('js');
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 after get/set', function() {
var parser = homunculus.getParser('js');
var node = parser.parse('~{get var(){}}');
expect(tree(node)).to.eql([JsNode.PROGRAM,[JsNode.EXPRSTMT,[JsNode.UNARYEXPR,["~",JsNode.PRMREXPR,[JsNode.OBJLTR,["{",JsNode.PROPTASSIGN,["get","var","(",")","{",JsNode.FNBODY,[],"}"],"}"]]]]]]);
});
it('objltr error', function() {
var parser = homunculus.getParser('js');
expect(function() {
Expand Down

1 comment on commit e587d5d

@army8735
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#23

Please sign in to comment.