Skip to content

Commit

Permalink
增加classbody内直接声明变量语法
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed Dec 29, 2016
1 parent 705e0db commit a9225c8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
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": "1.0.1",
"version": "1.1.0",
"description": "A lexer&parser by Javascript",
"maintainers": [
{
Expand Down
18 changes: 18 additions & 0 deletions src/parser/es6/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,24 @@ var Parser = IParser.extend(function(lexer) {
else if(this.look.type() == Token.ANNOT) {
node.add(this.annot());
}
else if(['[', '{'].indexOf(this.look.content()) != -1) {
node.add(this.lexbind());
}
else if(this.look.type() == Token.ID && ['get', 'set'].indexOf(this.look.content()) == -1) {
//LL2区分新增语法classbody内赋值
for(var i = this.index; i < this.length; i++) {
var next = this.tokens[i];
if(!S[next.type()]) {
if(next.content() == '(') {
node.add(this.method(noIn, noOf));
}
else {
node.add(this.lexbind());
}
return node;
}
}
}
else {
node.add(this.method(noIn, noOf));
}
Expand Down
15 changes: 15 additions & 0 deletions tests/es6parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,21 @@ describe('es6parser', function() {
var node = parser.parse('class A{@bind test(){} @link(a,b) get v(){}}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.CLASSDECL,["class",JsNode.BINDID,["A"],"{",JsNode.CLASSBODY,[JsNode.CLASSELEM,[JsNode.ANNOT,["@bind"]],JsNode.CLASSELEM,[JsNode.METHOD,[JsNode.PROPTNAME,[JsNode.LTRPROPT,["test"]],"(",JsNode.FMPARAMS,[],")","{",JsNode.FNBODY,[],"}"]],JsNode.CLASSELEM,[JsNode.ANNOT,["@link","(",JsNode.FMPARAMS,[JsNode.SINGLENAME,[JsNode.BINDID,["a"]],",",JsNode.SINGLENAME,[JsNode.BINDID,["b"]]],")"]],JsNode.CLASSELEM,[JsNode.METHOD,["get",JsNode.PROPTNAME,[JsNode.LTRPROPT,["v"]],"(",JsNode.FMPARAMS,[],")","{",JsNode.FNBODY,[],"}"]]],"}"]]]]);
});
it('class lexbind', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('class A{b=1}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.CLASSDECL,["class",JsNode.BINDID,["A"],"{",JsNode.CLASSBODY,[JsNode.CLASSELEM,[JsNode.LEXBIND,[JsNode.BINDID,["b"],JsNode.INITLZ,["=",JsNode.PRMREXPR,["1"]]]]],"}"]]]]);
});
it('class lexbind 2', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('class A{[b]=[1]}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.CLASSDECL,["class",JsNode.BINDID,["A"],"{",JsNode.CLASSBODY,[JsNode.CLASSELEM,[JsNode.LEXBIND,[JsNode.ARRBINDPAT,["[",JsNode.SINGLENAME,[JsNode.BINDID,["b"]],"]"],JsNode.INITLZ,["=",JsNode.PRMREXPR,[JsNode.ARRLTR,["[",JsNode.PRMREXPR,["1"],"]"]]]]]],"}"]]]]);
});
it('class lexbind 3', function() {
var parser = homunculus.getParser('es6');
var node = parser.parse('class A{{b}={b:1}}');
expect(tree(node)).to.eql([JsNode.SCRIPT,[JsNode.SCRIPTBODY,[JsNode.CLASSDECL,["class",JsNode.BINDID,["A"],"{",JsNode.CLASSBODY,[JsNode.CLASSELEM,[JsNode.LEXBIND,[JsNode.OBJBINDPAT,["{",JsNode.BINDPROPT,[JsNode.SINGLENAME,[JsNode.BINDID,["b"]]],"}"],JsNode.INITLZ,["=",JsNode.PRMREXPR,[JsNode.OBJLTR,["{",JsNode.PROPTDEF,[JsNode.PROPTNAME,[JsNode.LTRPROPT,["b"]],":",JsNode.PRMREXPR,["1"]],"}"]]]]]],"}"]]]]);
});
});
describe('js lib exec test', function() {
it('jquery 1.11.0', function() {
Expand Down
18 changes: 18 additions & 0 deletions web/parser/es6/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,24 @@ var Parser = IParser.extend(function(lexer) {
else if(this.look.type() == Token.ANNOT) {
node.add(this.annot());
}
else if(['[', '{'].indexOf(this.look.content()) != -1) {
node.add(this.lexbind());
}
else if(this.look.type() == Token.ID && ['get', 'set'].indexOf(this.look.content()) == -1) {
//LL2区分新增语法classbody内赋值
for(var i = this.index; i < this.length; i++) {
var next = this.tokens[i];
if(!S[next.type()]) {
if(next.content() == '(') {
node.add(this.method(noIn, noOf));
}
else {
node.add(this.lexbind());
}
return node;
}
}
}
else {
node.add(this.method(noIn, noOf));
}
Expand Down

0 comments on commit a9225c8

Please sign in to comment.