Comparing version 0.3.5 to 0.3.6
{ | ||
"name": "jsdc", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"description": "transform ecmascript6 to ecmascript5", | ||
@@ -31,3 +31,5 @@ "maintainers": [ | ||
"jsdc/src/Str.js", | ||
"jsdc/src/Obj.js" | ||
"jsdc/src/Obj.js", | ||
"jsdc/src/join.js", | ||
"jsdc/src/eventbus.js" | ||
] | ||
@@ -53,3 +55,3 @@ } | ||
"dependencies": { | ||
"homunculus": "^0.3.0-2" | ||
"homunculus": "^0.3.4" | ||
}, | ||
@@ -60,3 +62,3 @@ "devDependencies": { | ||
"expect.js": "^0.3.1", | ||
"gulp": "^3.6.0", | ||
"gulp": "^3.8.1", | ||
"gulp-clean": "^0.2.4", | ||
@@ -63,0 +65,0 @@ "gulp-util": "^2.2.14", |
@@ -226,3 +226,3 @@ # Javascript Downcast | ||
```js | ||
function method(a, b ) {if(b ===void 0) b = 1; | ||
function method(a, b ) {if(b ===void 0)b=1; | ||
} | ||
@@ -302,3 +302,3 @@ ``` | ||
```js | ||
for(a =b.next();!a.done;){ | ||
for(a =b.next();!a.done;a=b.next()){ | ||
} | ||
@@ -312,3 +312,3 @@ ``` | ||
```js | ||
for(a =b.next();!a.done;){a=a.value; | ||
for(a =b.next();!a.done;a=b.next()){a=a.value; | ||
} | ||
@@ -322,3 +322,3 @@ ``` | ||
```js | ||
for(var a =b.next();!a.done;){a=a.value; | ||
for(var a =b.next();!a.done;a=b.next()){a=a.value; | ||
} | ||
@@ -392,3 +392,3 @@ ``` | ||
```js | ||
function A(){}!function(){var _=Object.create(B.prototype);_.constructor=A;A.prototype=_;}(); | ||
!function(){var _=Object.create(B.prototype);_.constructor=A;A.prototype=_;}(); | ||
function A(){} | ||
@@ -420,3 +420,3 @@ Object.keys(B).forEach(function(k){A[k]=B[k]}); | ||
!function(){var _=Object.create(B.prototype);_.constructor=A;A.prototype=_;}(); | ||
function A(){B.a.call(this)} | ||
function A(){B.prototype.a.call(this)} | ||
Object.keys(B).forEach(function(k){A[k]=B[k]}); | ||
@@ -603,3 +603,3 @@ ``` | ||
语句前会加上`arguments[0]`,模拟`next()`带上的一个参数: | ||
赋值语句后会加上一个临时唯一id,模拟下次调用`next()`传入的一个参数: | ||
```js | ||
@@ -611,4 +611,4 @@ function *a() { | ||
```js | ||
function *a() { | ||
var a = arguments[0];return | ||
function *a(_0_) { | ||
var a = void 0;return;a=_0_ | ||
} | ||
@@ -623,4 +623,4 @@ ``` | ||
```js | ||
function *a() { | ||
var a = arguments[0];return {value:1} | ||
function *a(_0_) { | ||
var a = void 0;return {value:1};a=_0_ | ||
} | ||
@@ -639,4 +639,4 @@ ``` | ||
function a(){ | ||
arguments[0];return {value:1,done:false} | ||
arguments[0];return {value:2,done:false} | ||
return{value:1,done:false} | ||
return{value:2,done:false} | ||
} | ||
@@ -652,5 +652,5 @@ ``` | ||
```js | ||
var a=function(){return function(){return {next:a}};function a(){ | ||
arguments[0];return {value:1,done:false} | ||
arguments[0];return {value:2,done:false} | ||
var a=function(){return function(){return{next:a}};function a(){ | ||
return{value:1,done:false} | ||
return{value:2,done:false} | ||
}}(); | ||
@@ -668,5 +668,5 @@ ``` | ||
```js | ||
var a=function(){return function(){return {next:_0_}};function _0_(){ | ||
arguments[0];return {value:1,done:false} | ||
arguments[0];return {value:2,done:false} | ||
var a=function(){return function(){return{next:_0_}};function _0_(){ | ||
return{value:1,done:false} | ||
return{value:2,done:false} | ||
}}(); | ||
@@ -682,5 +682,5 @@ ``` | ||
```js | ||
var a=function(){var _1_=0;return function(){return {next:_0_}};function _0_(){ | ||
arguments[0];return {value:1,done:false} | ||
arguments[0];return {value:2,done:false} | ||
var a=function(){var _1_=0;return function(){return{next:_0_}};function _0_(){ | ||
return{value:1,done:false} | ||
return{value:2,done:false} | ||
}}(); | ||
@@ -696,5 +696,5 @@ ``` | ||
```js | ||
var a=function(){var _1_=0;return function(){return {next:_0_}};function _0_(){ | ||
switch(_1_++){case 0:arguments[0];return {value:1,done:false} | ||
case 1:arguments[0];return {value:1,done:false}} | ||
var a=function(){var _1_=0;return function(){return{next:_0_}};function _0_(){ | ||
switch(_1_){case 0:_1_++;return{value:1,done:false} | ||
case 1:return{value:1,done:false}} | ||
}}(); | ||
@@ -711,6 +711,6 @@ ``` | ||
```js | ||
var a=function(){var _1_=0;return function(){return {next:_0_}};var a;function _0_(){ | ||
switch(_1_++){case 0:a = 1; | ||
arguments[0];return {value:a++,done:false}; | ||
case 1:arguments[0];return {value:a++,done:false;} | ||
var a=function(){var _1_=0;return function(){return{next:_0_}};var a;function _0_(){ | ||
switch(_1_){case 0:a = 1; | ||
_1_++;return{value:a++,done:false}; | ||
case 1:_1_++;return{value:a++,done:false;} | ||
}}(); | ||
@@ -731,6 +731,6 @@ ``` | ||
```js | ||
var a=function(){var _6_=0;return function(){return {next:_7_}};var a;function _7_(){ | ||
switch(_6_++){case 0:a = 1; | ||
arguments[0];return {value:a++,done:false}; | ||
case 1:arguments[0];return {value:a++,done:true};default:;;return{done:true}} | ||
var a=function(){var _0_=0;return function(){return{next:_1_}};var a;function _1_(_2_){ | ||
switch(_0_){case 0:a = 1; | ||
_0_++;return{value:a++,done:false};case 1: | ||
_0_++;return{value:a++,done:true};default:return{done:true}} | ||
}}(); | ||
@@ -745,4 +745,4 @@ ``` | ||
```js | ||
var a=function(){var _0_=0;return function(){return{next:_1_}};function _1_(){ | ||
switch(_0_++){case 0:arguments[0];var _2_=b();if(!_2_.done)_0_--;return _2_;default:;return{done:true}} | ||
var a=function(){var _0_=0;return function(){return{next:_1_}};function _1_(_2_){ | ||
switch(_0_){case 0:_0_++;var _3_=b();if(!_3_.done)_0_--;return _3_;default:return{done:true}} | ||
}}(); | ||
@@ -756,3 +756,3 @@ ``` | ||
```js | ||
~function(){var _0_=0;return function (){return{next:_1_}};function _1_(){ | ||
~function(){var _0_=0;return function(){return{next:_1_}};function _1_(){ | ||
}}() | ||
@@ -806,3 +806,3 @@ ``` | ||
```js | ||
var a;!function(){var _0_= [];a=_0_[0];if(a===void 0)a=1}() | ||
var a;!function(){var _0_= [];a=_0_[0];if(_0_.indexOf(a)!=0)a=1}() | ||
``` | ||
@@ -814,3 +814,3 @@ 表达式赋值也可以: | ||
```js | ||
(!function(){var _0_= {};a=_0_["a"];if(a===void 0)a=1}()) | ||
(!function(){var _0_= {};a=_0_["a"];if(a===void 0)a=1;return _0_}()) | ||
``` | ||
@@ -817,0 +817,0 @@ 数组解构最后允许`rest`运算符: |
@@ -6,2 +6,3 @@ var homunculus = require('homunculus'); | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -20,4 +21,4 @@ var ArrayCmph = Class(function(jsdc) { | ||
}; | ||
self.jsdc.ignore(node.first()); | ||
self.jsdc.ignore(node.last()); | ||
self.jsdc.ignore(node.first(), 'arrcmph1'); | ||
self.jsdc.ignore(node.last(), 'arrcmph2'); | ||
self.jsdc.append('function(){'); | ||
@@ -55,3 +56,3 @@ //forbinding的变量要提出来声明 | ||
this.jsdc.appendBefore('='); | ||
this.jsdc.appendBefore(this.join(node.leaf(4))); | ||
this.jsdc.appendBefore(join(node.leaf(4))); | ||
this.jsdc.appendBefore('['); | ||
@@ -78,22 +79,5 @@ this.jsdc.appendBefore(id); | ||
if(node.parent().name() == JsNode.CMPHFOR) { | ||
this.jsdc.ignore(node); | ||
this.jsdc.ignore(node, 'arrcmph3'); | ||
this.jsdc.append('in '); | ||
} | ||
}, | ||
join: function(node, res) { | ||
res = res || { s: '' }; | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.join(leaf, res); | ||
}); | ||
} | ||
return res.s; | ||
} | ||
@@ -100,0 +84,0 @@ }); |
@@ -24,3 +24,3 @@ var homunculus = require('homunculus'); | ||
arrow: function(t) { | ||
this.jsdc.ignore(t); | ||
this.jsdc.ignore(t, 'arrow1'); | ||
}, | ||
@@ -27,0 +27,0 @@ body: function(node, start) { |
@@ -6,2 +6,3 @@ var homunculus = require('homunculus'); | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -20,3 +21,3 @@ var DefaultValue = Class(function(jsdc) { | ||
if(init.name() == JsNode.INITLZ) { | ||
self.jsdc.ignore(init); | ||
self.jsdc.ignore(init, 'default1'); | ||
(self.hash[fnbody] = self.hash[fnbody] || []).push(sgname); | ||
@@ -35,21 +36,6 @@ } | ||
self.jsdc.append('if(' + id + '===void 0)' + id); | ||
self.recursion(sgname.last()); | ||
self.jsdc.append(join(sgname.last())); | ||
self.jsdc.append(';'); | ||
}); | ||
} | ||
}, | ||
recursion: function(node) { | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
self.jsdc.append(' ' + node.token().content()); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf); | ||
}); | ||
} | ||
} | ||
@@ -56,0 +42,0 @@ }); |
@@ -6,2 +6,3 @@ var homunculus = require('homunculus'); | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -18,3 +19,3 @@ var Destruct = Class(function(jsdc) { | ||
} | ||
this.jsdc.ignore(node); | ||
this.jsdc.ignore(node, 'destruct1'); | ||
var res = { arr: [] }; | ||
@@ -74,2 +75,8 @@ this.recursion(node, res); | ||
} | ||
else if(first.name() == JsNode.PROPTNAME) { | ||
first = leaf.leaf(2); | ||
if(first.name() == JsNode.SINGLENAME) { | ||
res.push(first.first().first().token().content()); | ||
} | ||
} | ||
} | ||
@@ -109,4 +116,4 @@ }); | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -157,4 +164,4 @@ break; | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -169,2 +176,8 @@ break; | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
break; | ||
@@ -209,4 +222,4 @@ case JsNode.BINDELEM: | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -246,4 +259,4 @@ break; | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -258,2 +271,8 @@ break; | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
break; | ||
@@ -281,6 +300,6 @@ case JsNode.BINDELEM: | ||
if(start) { | ||
this.jsdc.ignore(first); | ||
this.jsdc.ignore(first, 'destruct2'); | ||
if(assignexpr.parent().name() == JsNode.ASSIGNEXPR) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().first().first().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1)); | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct3'); | ||
} | ||
@@ -292,3 +311,3 @@ else if(assignexpr.parent().name() == JsNode.INITLZ) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().prev().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1)); | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct4'); | ||
} | ||
@@ -306,3 +325,3 @@ else { | ||
self.hash[first.nid()] = temp; | ||
self.jsdc.append('!function(){var '); | ||
self.jsdc.append('function(){var '); | ||
self.jsdc.append(temp); | ||
@@ -335,4 +354,4 @@ } | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + '];'); | ||
self.jsdc.appendBefore('if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + '=' + self.join(leaf.next().next()) + (end ? '' : ';')); | ||
self.jsdc.appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + '=' + join(leaf.next().next()) + (end ? '' : ';')); | ||
break; | ||
@@ -343,2 +362,3 @@ case JsNode.ARRLTR: | ||
temp: temp, | ||
end: end, | ||
index: i | ||
@@ -353,5 +373,3 @@ }); | ||
} | ||
if(self.inAssign[first.nid()]) { | ||
self.jsdc.appendBefore(';return ' + temp); | ||
} | ||
self.jsdc.appendBefore(';return ' + temp); | ||
self.jsdc.appendBefore('}()'); | ||
@@ -362,6 +380,6 @@ } | ||
if(start) { | ||
self.jsdc.ignore(first); | ||
self.jsdc.ignore(first, 'destruct5'); | ||
if(assignexpr.parent().name() == JsNode.ASSIGNEXPR) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().first().first().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1)); | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct6'); | ||
return; | ||
@@ -374,3 +392,3 @@ } | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().prev().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1)); | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct7'); | ||
} | ||
@@ -386,3 +404,3 @@ else { | ||
else { | ||
self.jsdc.append('!function(){var '); | ||
self.jsdc.append('function(){var '); | ||
var temp = self.jsdc.uid(); | ||
@@ -406,4 +424,4 @@ self.hash[first.nid()] = temp; | ||
var init = leaf.next(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -418,3 +436,18 @@ break; | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.PRMREXPR: | ||
var id = last.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(join(last.parent()) + (end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.ARRLTR: | ||
@@ -424,2 +457,3 @@ case JsNode.OBJLTR: | ||
temp: temp, | ||
end: end, | ||
name: name, | ||
@@ -438,5 +472,3 @@ index: 0 | ||
} | ||
if(self.inAssign[first.nid()]) { | ||
self.jsdc.appendBefore(';return ' + temp); | ||
} | ||
self.jsdc.appendBefore(';return ' + temp); | ||
self.jsdc.appendBefore('}()'); | ||
@@ -464,3 +496,3 @@ } | ||
} | ||
var end = i == target.length - 1; | ||
var end = i == target.length - 1 && data.end; | ||
leaf = leaf.first(); | ||
@@ -481,4 +513,4 @@ switch(leaf.name()) { | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + '];'); | ||
self.jsdc.appendBefore('if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + '=' + self.join(leaf.next().next()) + (end ? '' : ';')); | ||
self.jsdc.appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + '=' + join(leaf.next().next()) + (end ? '' : ';')); | ||
break; | ||
@@ -489,2 +521,3 @@ case JsNode.ARRLTR: | ||
temp: temp, | ||
end: end, | ||
index: i | ||
@@ -506,3 +539,3 @@ }); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1; | ||
var end = i == target.length - 1 && data.end; | ||
leaf = leaf.first(); | ||
@@ -515,4 +548,4 @@ switch(leaf.name()) { | ||
var init = leaf.next(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -527,3 +560,18 @@ break; | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.PRMREXPR: | ||
var id = last.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(join(last.parent()) + (end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.ARRLTR: | ||
@@ -533,2 +581,3 @@ case JsNode.OBJLTR: | ||
temp: temp, | ||
end: end, | ||
name: name, | ||
@@ -566,19 +615,2 @@ index: 0 | ||
}); | ||
}, | ||
join: function(node, res) { | ||
res = res || { s: '' }; | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.join(leaf, res); | ||
}); | ||
} | ||
return res.s; | ||
} | ||
@@ -585,0 +617,0 @@ }); |
@@ -6,2 +6,3 @@ var homunculus = require('homunculus'); | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -19,3 +20,3 @@ var Forof = Class(function(jsdc) { | ||
this.hash[node.nid()] = true; | ||
this.jsdc.ignore(of); | ||
this.jsdc.ignore(of, 'forof1'); | ||
} | ||
@@ -46,2 +47,3 @@ } | ||
//forof的varstmt只能有一个id,其它为mmbexpr | ||
var v = join(parent.leaf(4)); | ||
if(k.name() == JsNode.VARSTMT) { | ||
@@ -51,5 +53,6 @@ k = k.last().first().first().token().content(); | ||
else { | ||
k = this.join(k); | ||
k = join(k); | ||
} | ||
this.jsdc.append(k + '.done;') | ||
this.jsdc.append(k + '.done;'); | ||
this.jsdc.append(k + '=' + v + '.next()'); | ||
} | ||
@@ -85,25 +88,5 @@ else { | ||
else { | ||
k = this.join(k); | ||
k = join(k); | ||
} | ||
this.jsdc.append(k + '=' + k + '.value;'); | ||
}, | ||
join: function(node) { | ||
var res = { s: '' }; | ||
this.recursion(node, res); | ||
return res.s; | ||
}, | ||
recursion: function(node, res) { | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf, res); | ||
}); | ||
} | ||
} | ||
@@ -110,0 +93,0 @@ }); |
var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Token = homunculus.getClass('token'); | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
var eventbus = require('./eventbus'); | ||
@@ -10,71 +13,112 @@ var Generator = Class(function(jsdc) { | ||
this.star = {}; | ||
this.stmt = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
var self = this; | ||
if(start) { | ||
this.jsdc.ignore(node.leaf(1)); | ||
self.jsdc.ignore(node.leaf(1), 'gen1'); | ||
var token = node.leaf(2).first().token(); | ||
//有可能被scope前置过 | ||
var hasPre = token.ignore; | ||
//忽略本身 | ||
this.jsdc.ignore(node.first()); | ||
this.jsdc.ignore(token); | ||
self.jsdc.ignore(token, 'gen2'); | ||
if(!hasPre) { | ||
this.jsdc.append('var '); | ||
this.jsdc.append(node.leaf(2).first().token().content()); | ||
this.jsdc.append('='); | ||
self.jsdc.append('var '); | ||
self.jsdc.append(node.leaf(2).first().token().content()); | ||
self.jsdc.append('='); | ||
} | ||
var state = this.jsdc.uid(); | ||
var temp = this.jsdc.uid(); | ||
var o = this.hash[node.nid()] = { | ||
state: state, | ||
index: 0, | ||
temp: temp, | ||
yield: [] | ||
}; | ||
this.jsdc.append('function(){'); | ||
this.jsdc.append('var ' + state + '=0;'); | ||
this.jsdc.append('return '); | ||
this.jsdc.append('function(){return{next:' + temp + '}};'); | ||
o.pos = this.jsdc.res.length; | ||
this.jsdc.append('function ' + temp); | ||
self.gen(node, start); | ||
} | ||
else { | ||
this.jsdc.appendBefore('}();'); | ||
self.gen(node, start); | ||
self.jsdc.appendBefore('}();'); | ||
} | ||
}, | ||
expr: function(node, start) { | ||
var self = this; | ||
if(start) { | ||
this.jsdc.ignore(node.first()); | ||
this.jsdc.ignore(node.leaf(1)); | ||
self.jsdc.ignore(node.first(), 'gen3'); | ||
self.jsdc.ignore(node.leaf(1), 'gen4'); | ||
if(node.leaf(2).name() == JsNode.BINDID) { | ||
this.jsdc.ignore(node.leaf(2)); | ||
self.jsdc.ignore(node.leaf(2), 'gen5'); | ||
} | ||
var state = this.jsdc.uid(); | ||
var temp = this.jsdc.uid(); | ||
var o = this.hash[node.nid()] = { | ||
self.gen(node, start); | ||
} | ||
else { | ||
self.gen(node, start); | ||
self.jsdc.appendBefore('}()'); | ||
} | ||
}, | ||
gen: function(node, start) { | ||
var self = this; | ||
if(start) { | ||
self.jsdc.ignore(node.first(), 'gen6'); | ||
var state = self.jsdc.uid(); | ||
var temp = self.jsdc.uid(); | ||
var param = node.leaf(4).first(); | ||
var res = self.count(node.last().prev(), node); | ||
var count = res.count; | ||
var ret = res.return; | ||
if(res.pre) { | ||
self.pre(node.last().prev(), node.nid(), node.last().prev().nid()); | ||
} | ||
if(!param) { | ||
if(count) { | ||
param = self.jsdc.uid(); | ||
eventbus.on(node.leaf(4).nid(), function(node, start) { | ||
start && self.jsdc.append(param); | ||
}); | ||
} | ||
else { | ||
param = ''; | ||
} | ||
} | ||
else if(param.name() == JsNode.SINGLENAME) { | ||
param = param.first().first().token().content(); | ||
} | ||
else if(param.name() == JsNode.BINDREST) { | ||
param = param.last().first().token().content() + '[0]'; | ||
} | ||
var o = self.hash[node.nid()] = { | ||
state: state, | ||
index: 0, | ||
index2: 0, | ||
count: count, | ||
return: ret, | ||
if: 0, | ||
temp: temp, | ||
yield: [] | ||
param: param, | ||
last: null, | ||
yield: [], | ||
pre: res.pre | ||
}; | ||
this.jsdc.append('function(){'); | ||
this.jsdc.append('var ' + state + '=0;'); | ||
this.jsdc.append('return '); | ||
this.jsdc.append('function(){return{next:' + temp + '}};'); | ||
o.pos = this.jsdc.res.length; | ||
this.jsdc.append('function ' + temp); | ||
self.jsdc.append('function(){'); | ||
self.jsdc.append('var ' + state + '=0;'); | ||
self.jsdc.append('return function(){return{next:' + temp + '}};'); | ||
o.pos = self.jsdc.res.length; | ||
self.jsdc.append('function ' + temp); | ||
} | ||
else { | ||
this.jsdc.appendBefore('}()'); | ||
} | ||
}, | ||
yield: function(node, start) { | ||
var top = this.closest(node); | ||
var o = this.hash[top.nid()]; | ||
var self = this; | ||
var top = self.closest(node); | ||
var o = self.hash[top.nid()]; | ||
if(start) { | ||
if(o.index++ != 0) { | ||
this.jsdc.append('case ' + (o.index - 1) + ':'); | ||
self.jsdc.ignore(node.first(), 'gen7'); | ||
var parent = node.parent(); | ||
//赋值语句需要添加上参数,先默认undefined,并记录在变量中为下次添加做标记 | ||
var parent = node.parent(); | ||
switch(parent.name()) { | ||
case JsNode.INITLZ: | ||
o.last = join(parent.prev()); | ||
break; | ||
case JsNode.ASSIGNEXPR: | ||
o.last = join(parent.first()); | ||
break; | ||
default: | ||
o.last = null; | ||
} | ||
this.jsdc.ignore(node.first()); | ||
this.jsdc.append('arguments[0];'); | ||
//加上状态变更 | ||
o.index++; | ||
self.jsdc.append(o.state + '=' + ++o.index2 + ';'); | ||
//yield * | ||
@@ -84,16 +128,17 @@ if(node.size() > 2 | ||
&& node.leaf(1).token().content() == '*') { | ||
this.jsdc.ignore(node.leaf(1)); | ||
var temp = this.star[node.nid()] = this.jsdc.uid(); | ||
this.jsdc.append('var ' + temp + '='); | ||
self.jsdc.ignore(node.leaf(1), 'gen8'); | ||
var temp = this.star[node.nid()] = self.jsdc.uid(); | ||
self.jsdc.append('var ' + temp + '='); | ||
} | ||
else { | ||
this.jsdc.append('return{value:'); | ||
self.jsdc.append('return{value:'); | ||
} | ||
} | ||
else { | ||
if(this.star.hasOwnProperty(node.nid())) { | ||
var temp = this.star[node.nid()]; | ||
this.jsdc.appendBefore('();if(!' + temp + '.done)' + o.state + '--;return ' + temp); | ||
//yield * | ||
if(self.star.hasOwnProperty(node.nid())) { | ||
var temp = self.star[node.nid()]; | ||
self.jsdc.appendBefore('.next();return{done:(!' + temp + '.done&&' + o.state + '--),value:' + temp + '}'); | ||
o.yield.push({ | ||
i: this.jsdc.i, | ||
i: self.jsdc.i, | ||
star: temp | ||
@@ -103,7 +148,19 @@ }); | ||
else { | ||
this.jsdc.appendBefore(',done:false}'); | ||
self.jsdc.appendBefore(',done:' + (o.index == o.count) + '}'); | ||
o.yield.push({ | ||
i: this.jsdc.i | ||
i: self.jsdc.i | ||
}); | ||
} | ||
self.ignoreNext(node, ';'); | ||
if(o.index < o.count) { | ||
self.jsdc.appendBefore(';case ' + o.index2 + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';case ' + o.index2 + ':'); | ||
} | ||
//有赋值需要先赋值 | ||
if(o.last) { | ||
self.jsdc.appendBefore(o.last + '=' + o.param + ';'); | ||
} | ||
o.last = null; | ||
} | ||
@@ -116,18 +173,14 @@ }, | ||
if(start) { | ||
this.jsdc.append('switch(' + o.state + '++){case 0:'); | ||
if(o.count) { | ||
this.jsdc.append('while(1){switch(' + o.state + '){case 0:'); | ||
} | ||
} | ||
else { | ||
if(o.index) { | ||
var yie = o.yield[o.yield.length - 1]; | ||
var i; | ||
if(yie.star) { | ||
i = this.jsdc.res.lastIndexOf(yie.star, i); | ||
this.jsdc.replace(';default:', i + yie.star.length, 0); | ||
} | ||
else { | ||
i = this.jsdc.res.lastIndexOf(',done:false}', i); | ||
this.jsdc.replace('true};default:', i + 6, 6); | ||
} | ||
else if(!o.return) { | ||
if(o.count) { | ||
this.jsdc.appendBefore(';' + o.state + '=-1'); | ||
this.jsdc.appendBefore(';default:return{done:true}}}'); | ||
} | ||
this.jsdc.appendBefore(';return{done:true}}'); | ||
else { | ||
this.jsdc.appendBefore('return{done:true}'); | ||
} | ||
} | ||
@@ -148,8 +201,193 @@ } | ||
if(top) { | ||
this.jsdc.ignore(varstmt.first()); | ||
this.jsdc.ignore(varstmt.first(), 'gen9'); | ||
this.jsdc.insert('var ' + varstmt.leaf(1).first().first().token().content() + ';', this.hash[top.nid()].pos); | ||
} | ||
}, | ||
count: function(node, top, res) { | ||
res = res || { count: 0, return: false, pre: false }; | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
if(!isToken) { | ||
switch(node.name()) { | ||
case JsNode.YIELDEXPR: | ||
res.count++; | ||
//赋值语句忽略 | ||
var parent = node.parent(); | ||
switch(parent.name()) { | ||
case JsNode.INITLZ: | ||
self.jsdc.ignore(parent.prev(), 'gen10'); | ||
self.jsdc.ignore(node.prev(), 'gen11'); | ||
break; | ||
case JsNode.ASSIGNEXPR: | ||
self.jsdc.ignore(parent.first(), 'gen12'); | ||
self.jsdc.ignore(node.prev(), 'gen13'); | ||
break; | ||
} | ||
var belong = self.belong(node); | ||
belong.forEach(function(f) { | ||
self.stmt[f.nid()] = true; | ||
res.pre = true; | ||
}); | ||
break; | ||
case JsNode.RETSTMT: | ||
res.return = node; | ||
eventbus.on(node.nid(), function(node, start) { | ||
if(start) { | ||
var o = self.hash[top.nid()]; | ||
self.jsdc.appendBefore(';' + o.state + '=-1;default:'); | ||
} | ||
}); | ||
eventbus.on(node.leaf(1).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('{value:'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(',done:true}'); | ||
} | ||
}); | ||
break; | ||
//忽略这些节点中的yield语句 | ||
case JsNode.CLASSDECL: | ||
case JsNode.CLASSEXPR: | ||
case JsNode.FNDECL: | ||
case JsNode.FNEXPR: | ||
case JsNode.GENDECL: | ||
case JsNode.GENEXPR: | ||
case JsNode.METHOD: | ||
return; | ||
} | ||
node.leaves().forEach(function(leaf) { | ||
self.count(leaf, top, res); | ||
}); | ||
} | ||
return res; | ||
}, | ||
pre: function(node, nid, bid, res) { | ||
res = res || { index: 0 }; | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
if(!isToken) { | ||
switch(node.name()) { | ||
case JsNode.IFSTMT: | ||
if(self.stmt.hasOwnProperty(node.nid())) { | ||
res.index++; | ||
var block = node.leaf(4); | ||
//改写if语句 | ||
self.jsdc.ignore(node.first(), 'gen14'); | ||
self.jsdc.ignore(node.leaf(1), 'gen15'); | ||
self.jsdc.ignore(node.leaf(2), 'gen16'); | ||
self.jsdc.ignore(node.leaf(3), 'gen17'); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen18'); | ||
self.jsdc.ignore(block.first().last(), 'gen19'); | ||
} | ||
var temp; | ||
var ifEndTemp; | ||
var top; | ||
var ifstmt = node; | ||
//if结束后的状态 | ||
eventbus.on(node.nid(), function(node, start) { | ||
if(!start) { | ||
self.jsdc.append('case ' + ifEndTemp + ':'); | ||
} | ||
}); | ||
//根据表达式true/false分2个state | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
top = self.hash[nid]; | ||
index = top.index2; | ||
self.jsdc.append(top.state + '='); | ||
self.jsdc.append(join(ifstmt.leaf(2))); | ||
self.jsdc.append('?'); | ||
self.jsdc.append(++top.index2 + ':' + ++top.index2 + ';break;'); | ||
self.jsdc.append('case ' + (top.index2 - 1) + ':'); | ||
temp = top.index2; | ||
ifEndTemp = ++top.index2; | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(ifEndTemp); | ||
self.jsdc.appendBefore(';break;'); | ||
} | ||
}); | ||
//else语句忽略{} | ||
var elset = block.next(); | ||
if(elset && elset.name() == JsNode.TOKEN) { | ||
self.jsdc.ignore(elset, 'gen20'); | ||
block = elset.next(); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen21'); | ||
self.jsdc.ignore(block.first().last(), 'gen22'); | ||
} | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + temp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(ifEndTemp); | ||
self.jsdc.appendBefore('break;'); | ||
} | ||
}); | ||
} | ||
} | ||
break; | ||
//忽略这些节点中的所有逻辑 | ||
case JsNode.CLASSDECL: | ||
case JsNode.CLASSEXPR: | ||
case JsNode.FNDECL: | ||
case JsNode.FNEXPR: | ||
case JsNode.GENDECL: | ||
case JsNode.GENEXPR: | ||
case JsNode.METHOD: | ||
return; | ||
} | ||
node.leaves().forEach(function(leaf) { | ||
self.pre(leaf, nid, bid, res); | ||
}); | ||
} | ||
}, | ||
getLast: function(node) { | ||
while(node = node.last()) { | ||
if(node.name() == JsNode.TOKEN) { | ||
return node; | ||
} | ||
} | ||
}, | ||
ignoreNext: function(node, value) { | ||
node = this.getLast(node); | ||
if(node) { | ||
var token = node.token(); | ||
while(token = token.next()) { | ||
if(token.type() == Token.COMMENT | ||
|| token.type() == Token.BLANK | ||
|| token.type() == Token.LINE) { | ||
continue; | ||
} | ||
if(token.content() == value) { | ||
this.jsdc.ignore(token, 'gen23'); | ||
return; | ||
} | ||
else { | ||
return; | ||
} | ||
} | ||
} | ||
}, | ||
belong: function(node) { | ||
var res = []; | ||
while(node = node.parent()) { | ||
switch(node.name()) { | ||
case JsNode.IFSTMT: | ||
case JsNode.ITERSTMT: | ||
res.push(node); | ||
case JsNode.GENDECL: | ||
case JsNode.GENEXPR: | ||
break; | ||
} | ||
} | ||
return res; | ||
} | ||
}); | ||
module.exports = Generator; | ||
module.exports = Generator; |
@@ -7,2 +7,3 @@ var homunculus = require('homunculus'); | ||
var Class = require('./util/Class'); | ||
var eventbus = require('./eventbus'); | ||
@@ -182,4 +183,2 @@ var Scope = require('./Scope'); | ||
var ignore = token.ignore; | ||
//firefox的奇怪bug,调试时不显示,重新赋值就好了 | ||
token.ignore = token.ignore; | ||
this.i = this.res.length; | ||
@@ -291,2 +290,3 @@ //加上ignore | ||
} | ||
eventbus.emit(node.nid(), [node, true]); | ||
}, | ||
@@ -355,14 +355,15 @@ after: function(node) { | ||
} | ||
eventbus.emit(node.nid(), [node]); | ||
}, | ||
ignore: function(node) { | ||
ignore: function(node, msg) { | ||
var self = this; | ||
if(node instanceof Token) { | ||
node.ignore = true; | ||
node.ignore = msg || true; | ||
} | ||
else if(node.name() == JsNode.TOKEN) { | ||
node.token().ignore = true; | ||
this.ignore(node.token()); | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.ignore(leaf); | ||
self.ignore(leaf, msg); | ||
}); | ||
@@ -369,0 +370,0 @@ } |
@@ -6,2 +6,3 @@ var homunculus = require('homunculus'); | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -19,13 +20,13 @@ var Klass = Class(function(jsdc) { | ||
o.name = node.leaf(1).first().token().content(); | ||
this.jsdc.ignore(node.leaf(0)); | ||
this.jsdc.ignore(node.leaf(1)); | ||
this.jsdc.ignore(node.leaf(2)); | ||
this.jsdc.ignore(node.leaf(0), 'klass1'); | ||
this.jsdc.ignore(node.leaf(1), 'klass2'); | ||
this.jsdc.ignore(node.leaf(2), 'klass3'); | ||
if(node.leaf(3).name() == JsNode.CLASSBODY) { | ||
this.jsdc.ignore(node.leaf(4)); | ||
this.jsdc.ignore(node.leaf(4), 'klass4'); | ||
this.body(node.last().prev(), o.name); | ||
} | ||
else { | ||
this.jsdc.ignore(node.leaf(3)); | ||
this.jsdc.ignore(node.leaf(5)); | ||
o.extend = this.join(node.leaf(2).last()); | ||
this.jsdc.ignore(node.leaf(3), 'klass5'); | ||
this.jsdc.ignore(node.leaf(5), 'klass6'); | ||
o.extend = join(node.leaf(2).last()); | ||
this.body(node.last().prev(), o.name, o.extend); | ||
@@ -52,9 +53,9 @@ this.jsdc.append('!function(){'); | ||
var o = {}; | ||
this.jsdc.ignore(node.leaf(0)); | ||
this.jsdc.ignore(node.leaf(1)); | ||
this.jsdc.ignore(node.leaf(0), 'klass7'); | ||
this.jsdc.ignore(node.leaf(1), 'klass8'); | ||
if(node.leaf(2).name() == JsNode.TOKEN | ||
&& node.leaf(2).token().content() == '{') { | ||
this.jsdc.ignore(node.leaf(2)); | ||
this.jsdc.ignore(node.leaf(2), 'klass9'); | ||
if(node.leaf(1).name() == JsNode.HERITAGE) { | ||
o.extend = this.join(node.leaf(1).last()); | ||
o.extend = join(node.leaf(1).last()); | ||
o.name = this.jsdc.uid(); | ||
@@ -68,11 +69,11 @@ } | ||
&& node.leaf(3).token().content() == '{') { | ||
this.jsdc.ignore(node.leaf(3)); | ||
this.jsdc.ignore(node.leaf(3), 'klass10'); | ||
o.name = node.leaf(1).first().token().content(); | ||
o.extend = this.join(node.leaf(2).last()); | ||
o.extend = join(node.leaf(2).last()); | ||
} | ||
else { | ||
o.name = this.jsdc.uid(); | ||
this.jsdc.ignore(node.leaf(1)); | ||
this.jsdc.ignore(node.leaf(1), 'klass11'); | ||
} | ||
this.jsdc.ignore(node.last()); | ||
this.jsdc.ignore(node.last(), 'klass12'); | ||
var classbody = node.last().prev(); | ||
@@ -109,3 +110,3 @@ this.body(classbody, o.name, o.extend); | ||
var token = first.first().first().token(); | ||
this.jsdc.ignore(token); | ||
this.jsdc.ignore(token, 'klass13'); | ||
if(token.content() == 'constructor') { | ||
@@ -126,3 +127,3 @@ this.jsdc.append('function '); | ||
this.gs[prptn.nid()] = true; | ||
this.jsdc.ignore(prptn); | ||
this.jsdc.ignore(prptn, 'klass14'); | ||
this.jsdc.append('Object.defineProperty('); | ||
@@ -151,3 +152,3 @@ this.jsdc.append(o.name); | ||
if(start) { | ||
this.jsdc.ignore(first.token()); | ||
this.jsdc.ignore(first.token(), 'klass15'); | ||
this.jsdc.append(o.name + '.'); | ||
@@ -163,3 +164,3 @@ } | ||
super: function(node) { | ||
this.jsdc.ignore(node); | ||
this.jsdc.ignore(node, 'klass16'); | ||
var top = this.closest(node); | ||
@@ -247,22 +248,2 @@ if(this.hash.hasOwnProperty(top.nid())) { | ||
} | ||
}, | ||
join: function(node) { | ||
var res = { s: '' }; | ||
this.recursion(node, res); | ||
return res.s; | ||
}, | ||
recursion: function(node, res) { | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf, res); | ||
}); | ||
} | ||
} | ||
@@ -269,0 +250,0 @@ }); |
@@ -13,3 +13,3 @@ var homunculus = require('homunculus'); | ||
module: function(node) { | ||
this.jsdc.ignore(node); | ||
this.jsdc.ignore(node, 'module1'); | ||
this.jsdc.append('var '); | ||
@@ -23,3 +23,3 @@ this.jsdc.append(node.leaf(1).first().token().content()); | ||
var self = this; | ||
self.jsdc.ignore(node); | ||
self.jsdc.ignore(node, 'module2'); | ||
var one = node.leaf(1); | ||
@@ -70,8 +70,8 @@ //import "string" | ||
this.jsdc.append('});}();'); | ||
this.jsdc.ignore(node); | ||
this.jsdc.ignore(node, 'module3'); | ||
} | ||
else if(s == 'default') { | ||
this.jsdc.append('module.exports='); | ||
this.jsdc.ignore(node.leaf(0)); | ||
this.jsdc.ignore(node.leaf(1)); | ||
this.jsdc.ignore(node.leaf(0), 'module4'); | ||
this.jsdc.ignore(node.leaf(1), 'module5'); | ||
} | ||
@@ -87,4 +87,4 @@ break; | ||
this.jsdc.append(';exports.' + id + '='); | ||
this.jsdc.ignore(varstmt.first()); | ||
this.jsdc.ignore(node.first()); | ||
this.jsdc.ignore(varstmt.first(), 'module6'); | ||
this.jsdc.ignore(node.first(), 'module7'); | ||
break; | ||
@@ -97,3 +97,3 @@ case JsNode.FNDECL: | ||
this.jsdc.append(';'); | ||
this.jsdc.ignore(node.first()); | ||
this.jsdc.ignore(node.first(), 'module8'); | ||
break; | ||
@@ -100,0 +100,0 @@ } |
@@ -13,7 +13,7 @@ var homunculus = require('homunculus'); | ||
if(s.toLowerCase().indexOf('0b') == 0) { | ||
this.jsdc.ignore(t); | ||
this.jsdc.ignore(t, 'num1'); | ||
this.jsdc.append('parseInt("' + t.content().slice(2) + '", 2)'); | ||
} | ||
else if(s.toLowerCase().indexOf('0o') == 0) { | ||
this.jsdc.ignore(t); | ||
this.jsdc.ignore(t, 'num2'); | ||
this.jsdc.append('parseInt("' + t.content().slice(2) + '", 8)'); | ||
@@ -20,0 +20,0 @@ } |
@@ -6,2 +6,3 @@ var homunculus = require('homunculus'); | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -43,6 +44,6 @@ var Obj = Class(function(jsdc) { | ||
temp: temp, | ||
id: this.join(first) | ||
id: join(first) | ||
}); | ||
this.jsdc.append(temp); | ||
this.jsdc.ignore(first); | ||
this.jsdc.ignore(first, 'obj1'); | ||
} | ||
@@ -91,19 +92,2 @@ } | ||
} | ||
}, | ||
join: function(node, res) { | ||
res = res || { s: '' }; | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.join(leaf, res); | ||
}); | ||
} | ||
return res.s; | ||
} | ||
@@ -110,0 +94,0 @@ }); |
@@ -6,2 +6,3 @@ var homunculus = require('homunculus'); | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -19,3 +20,3 @@ var Rest = Class(function(jsdc) { | ||
var rest = last.first(); | ||
this.jsdc.ignore(rest); | ||
this.jsdc.ignore(rest, 'rest1'); | ||
this.hash[fnbody.nid()] = { | ||
@@ -45,3 +46,3 @@ index: fmparams.size() - 1, | ||
this.hash2[node.nid()] = first; | ||
this.jsdc.ignore(arglist); | ||
this.jsdc.ignore(arglist, 'rest2'); | ||
} | ||
@@ -61,7 +62,7 @@ } | ||
//主表达式无需设置apply的context,成员需设 | ||
this.jsdc.append(mmb.name() == JsNode.MMBEXPR ? this.join(mmb.first()) : 'this'); | ||
this.jsdc.append(mmb.name() == JsNode.MMBEXPR ? join(mmb.first()) : 'this'); | ||
this.jsdc.append(', ['); | ||
var leaves = node.leaves(); | ||
for(var i = 0; i < leaves.length - 3; i++) { | ||
this.jsdc.append(this.join(leaves[i])); | ||
this.jsdc.append(join(leaves[i])); | ||
} | ||
@@ -73,22 +74,2 @@ this.jsdc.append(']'); | ||
} | ||
}, | ||
join: function(node) { | ||
var res = { s: '' }; | ||
this.recursion(node, res); | ||
return res.s; | ||
}, | ||
recursion: function(node, res) { | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf, res); | ||
}); | ||
} | ||
} | ||
@@ -95,0 +76,0 @@ }); |
@@ -26,6 +26,7 @@ var homunculus = require('homunculus'); | ||
var name = node.name(); | ||
//每个{}作用域记录是否有lexdecl或fn或generator | ||
//每个{}作用域记录是否有lexdecl或fn或generator或classdecl | ||
if(name == JsNode.LEXDECL | ||
|| name == JsNode.FNDECL | ||
|| name == JsNode.GENDECL) { | ||
|| name == JsNode.GENDECL | ||
|| name == JsNode.CLASSDECL) { | ||
var parent = self.closest(node); | ||
@@ -73,10 +74,10 @@ //全局lexdecl没有作用域无需记录,fnbody的也无需记录 | ||
if(vardecl.first().name() == JsNode.BINDID) { | ||
self.jsdc.ignore(varstmt.first().token()); | ||
self.jsdc.ignore(varstmt.first().token(), 'scope1'); | ||
} | ||
else { | ||
//destruct需忽略前后可能的,再改为; var也需忽略 | ||
self.jsdc.ignore(vardecl.prev()); | ||
self.jsdc.ignore(vardecl.prev(), 'scope2'); | ||
var next = vardecl.next(); | ||
if(next.token().content() == ',') { | ||
self.jsdc.ignore(next); | ||
self.jsdc.ignore(next, 'scope3'); | ||
} | ||
@@ -159,2 +160,3 @@ } | ||
case JsNode.GENDECL: | ||
case JsNode.GENEXPR: | ||
return parent; | ||
@@ -161,0 +163,0 @@ case JsNode.FNDECL: |
@@ -34,3 +34,3 @@ var homunculus = require('homunculus'); | ||
if(res != s) { | ||
this.jsdc.ignore(t); | ||
this.jsdc.ignore(t, 'str1'); | ||
this.jsdc.append(res); | ||
@@ -37,0 +37,0 @@ } |
@@ -11,3 +11,3 @@ var homunculus = require('homunculus'); | ||
parse: function(t) { | ||
this.jsdc.ignore(t); | ||
this.jsdc.ignore(t, 'template1'); | ||
var s = t.content(); | ||
@@ -14,0 +14,0 @@ var res = '"'; |
@@ -7,2 +7,3 @@ define(function(require, exports, module) { | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -21,4 +22,4 @@ var ArrayCmph = Class(function(jsdc) { | ||
}; | ||
self.jsdc.ignore(node.first()); | ||
self.jsdc.ignore(node.last()); | ||
self.jsdc.ignore(node.first(), 'arrcmph1'); | ||
self.jsdc.ignore(node.last(), 'arrcmph2'); | ||
self.jsdc.append('function(){'); | ||
@@ -56,3 +57,3 @@ //forbinding的变量要提出来声明 | ||
this.jsdc.appendBefore('='); | ||
this.jsdc.appendBefore(this.join(node.leaf(4))); | ||
this.jsdc.appendBefore(join(node.leaf(4))); | ||
this.jsdc.appendBefore('['); | ||
@@ -79,22 +80,5 @@ this.jsdc.appendBefore(id); | ||
if(node.parent().name() == JsNode.CMPHFOR) { | ||
this.jsdc.ignore(node); | ||
this.jsdc.ignore(node, 'arrcmph3'); | ||
this.jsdc.append('in '); | ||
} | ||
}, | ||
join: function(node, res) { | ||
res = res || { s: '' }; | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.join(leaf, res); | ||
}); | ||
} | ||
return res.s; | ||
} | ||
@@ -101,0 +85,0 @@ }); |
@@ -25,3 +25,3 @@ define(function(require, exports, module) { | ||
arrow: function(t) { | ||
this.jsdc.ignore(t); | ||
this.jsdc.ignore(t, 'arrow1'); | ||
}, | ||
@@ -28,0 +28,0 @@ body: function(node, start) { |
@@ -7,2 +7,3 @@ define(function(require, exports, module) { | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -21,3 +22,3 @@ var DefaultValue = Class(function(jsdc) { | ||
if(init.name() == JsNode.INITLZ) { | ||
self.jsdc.ignore(init); | ||
self.jsdc.ignore(init, 'default1'); | ||
(self.hash[fnbody] = self.hash[fnbody] || []).push(sgname); | ||
@@ -36,21 +37,6 @@ } | ||
self.jsdc.append('if(' + id + '===void 0)' + id); | ||
self.recursion(sgname.last()); | ||
self.jsdc.append(join(sgname.last())); | ||
self.jsdc.append(';'); | ||
}); | ||
} | ||
}, | ||
recursion: function(node) { | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
self.jsdc.append(' ' + node.token().content()); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf); | ||
}); | ||
} | ||
} | ||
@@ -57,0 +43,0 @@ }); |
@@ -7,2 +7,3 @@ define(function(require, exports, module) { | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -19,3 +20,3 @@ var Destruct = Class(function(jsdc) { | ||
} | ||
this.jsdc.ignore(node); | ||
this.jsdc.ignore(node, 'destruct1'); | ||
var res = { arr: [] }; | ||
@@ -75,2 +76,8 @@ this.recursion(node, res); | ||
} | ||
else if(first.name() == JsNode.PROPTNAME) { | ||
first = leaf.leaf(2); | ||
if(first.name() == JsNode.SINGLENAME) { | ||
res.push(first.first().first().token().content()); | ||
} | ||
} | ||
} | ||
@@ -110,4 +117,4 @@ }); | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -158,4 +165,4 @@ break; | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -170,2 +177,8 @@ break; | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
break; | ||
@@ -210,4 +223,4 @@ case JsNode.BINDELEM: | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -247,4 +260,4 @@ break; | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -259,2 +272,8 @@ break; | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
break; | ||
@@ -282,6 +301,6 @@ case JsNode.BINDELEM: | ||
if(start) { | ||
this.jsdc.ignore(first); | ||
this.jsdc.ignore(first, 'destruct2'); | ||
if(assignexpr.parent().name() == JsNode.ASSIGNEXPR) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().first().first().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1)); | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct3'); | ||
} | ||
@@ -293,3 +312,3 @@ else if(assignexpr.parent().name() == JsNode.INITLZ) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().prev().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1)); | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct4'); | ||
} | ||
@@ -307,3 +326,3 @@ else { | ||
self.hash[first.nid()] = temp; | ||
self.jsdc.append('!function(){var '); | ||
self.jsdc.append('function(){var '); | ||
self.jsdc.append(temp); | ||
@@ -336,4 +355,4 @@ } | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + '];'); | ||
self.jsdc.appendBefore('if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + '=' + self.join(leaf.next().next()) + (end ? '' : ';')); | ||
self.jsdc.appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + '=' + join(leaf.next().next()) + (end ? '' : ';')); | ||
break; | ||
@@ -344,2 +363,3 @@ case JsNode.ARRLTR: | ||
temp: temp, | ||
end: end, | ||
index: i | ||
@@ -354,5 +374,3 @@ }); | ||
} | ||
if(self.inAssign[first.nid()]) { | ||
self.jsdc.appendBefore(';return ' + temp); | ||
} | ||
self.jsdc.appendBefore(';return ' + temp); | ||
self.jsdc.appendBefore('}()'); | ||
@@ -363,6 +381,6 @@ } | ||
if(start) { | ||
self.jsdc.ignore(first); | ||
self.jsdc.ignore(first, 'destruct5'); | ||
if(assignexpr.parent().name() == JsNode.ASSIGNEXPR) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().first().first().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1)); | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct6'); | ||
return; | ||
@@ -375,3 +393,3 @@ } | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().prev().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1)); | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct7'); | ||
} | ||
@@ -387,3 +405,3 @@ else { | ||
else { | ||
self.jsdc.append('!function(){var '); | ||
self.jsdc.append('function(){var '); | ||
var temp = self.jsdc.uid(); | ||
@@ -407,4 +425,4 @@ self.hash[first.nid()] = temp; | ||
var init = leaf.next(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -419,3 +437,18 @@ break; | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.PRMREXPR: | ||
var id = last.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(join(last.parent()) + (end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.ARRLTR: | ||
@@ -425,2 +458,3 @@ case JsNode.OBJLTR: | ||
temp: temp, | ||
end: end, | ||
name: name, | ||
@@ -439,5 +473,3 @@ index: 0 | ||
} | ||
if(self.inAssign[first.nid()]) { | ||
self.jsdc.appendBefore(';return ' + temp); | ||
} | ||
self.jsdc.appendBefore(';return ' + temp); | ||
self.jsdc.appendBefore('}()'); | ||
@@ -465,3 +497,3 @@ } | ||
} | ||
var end = i == target.length - 1; | ||
var end = i == target.length - 1 && data.end; | ||
leaf = leaf.first(); | ||
@@ -482,4 +514,4 @@ switch(leaf.name()) { | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + '];'); | ||
self.jsdc.appendBefore('if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + '=' + self.join(leaf.next().next()) + (end ? '' : ';')); | ||
self.jsdc.appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + '=' + join(leaf.next().next()) + (end ? '' : ';')); | ||
break; | ||
@@ -490,2 +522,3 @@ case JsNode.ARRLTR: | ||
temp: temp, | ||
end: end, | ||
index: i | ||
@@ -507,3 +540,3 @@ }); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1; | ||
var end = i == target.length - 1 && data.end; | ||
leaf = leaf.first(); | ||
@@ -516,4 +549,4 @@ switch(leaf.name()) { | ||
var init = leaf.next(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + id + '===void 0)') | ||
self.jsdc.appendBefore(id + self.join(init) + (end ? '' : ';')); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
@@ -528,3 +561,18 @@ break; | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.PRMREXPR: | ||
var id = last.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(join(last.parent()) + (end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.ARRLTR: | ||
@@ -534,2 +582,3 @@ case JsNode.OBJLTR: | ||
temp: temp, | ||
end: end, | ||
name: name, | ||
@@ -567,19 +616,2 @@ index: 0 | ||
}); | ||
}, | ||
join: function(node, res) { | ||
res = res || { s: '' }; | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.join(leaf, res); | ||
}); | ||
} | ||
return res.s; | ||
} | ||
@@ -586,0 +618,0 @@ }); |
@@ -7,2 +7,3 @@ define(function(require, exports, module) { | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -20,3 +21,3 @@ var Forof = Class(function(jsdc) { | ||
this.hash[node.nid()] = true; | ||
this.jsdc.ignore(of); | ||
this.jsdc.ignore(of, 'forof1'); | ||
} | ||
@@ -47,2 +48,3 @@ } | ||
//forof的varstmt只能有一个id,其它为mmbexpr | ||
var v = join(parent.leaf(4)); | ||
if(k.name() == JsNode.VARSTMT) { | ||
@@ -52,5 +54,6 @@ k = k.last().first().first().token().content(); | ||
else { | ||
k = this.join(k); | ||
k = join(k); | ||
} | ||
this.jsdc.append(k + '.done;') | ||
this.jsdc.append(k + '.done;'); | ||
this.jsdc.append(k + '=' + v + '.next()'); | ||
} | ||
@@ -86,25 +89,5 @@ else { | ||
else { | ||
k = this.join(k); | ||
k = join(k); | ||
} | ||
this.jsdc.append(k + '=' + k + '.value;'); | ||
}, | ||
join: function(node) { | ||
var res = { s: '' }; | ||
this.recursion(node, res); | ||
return res.s; | ||
}, | ||
recursion: function(node, res) { | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf, res); | ||
}); | ||
} | ||
} | ||
@@ -111,0 +94,0 @@ }); |
define(function(require, exports, module) { | ||
var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Token = homunculus.getClass('token'); | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
var eventbus = require('./eventbus'); | ||
@@ -11,71 +14,112 @@ var Generator = Class(function(jsdc) { | ||
this.star = {}; | ||
this.stmt = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
var self = this; | ||
if(start) { | ||
this.jsdc.ignore(node.leaf(1)); | ||
self.jsdc.ignore(node.leaf(1), 'gen1'); | ||
var token = node.leaf(2).first().token(); | ||
//有可能被scope前置过 | ||
var hasPre = token.ignore; | ||
//忽略本身 | ||
this.jsdc.ignore(node.first()); | ||
this.jsdc.ignore(token); | ||
self.jsdc.ignore(token, 'gen2'); | ||
if(!hasPre) { | ||
this.jsdc.append('var '); | ||
this.jsdc.append(node.leaf(2).first().token().content()); | ||
this.jsdc.append('='); | ||
self.jsdc.append('var '); | ||
self.jsdc.append(node.leaf(2).first().token().content()); | ||
self.jsdc.append('='); | ||
} | ||
var state = this.jsdc.uid(); | ||
var temp = this.jsdc.uid(); | ||
var o = this.hash[node.nid()] = { | ||
state: state, | ||
index: 0, | ||
temp: temp, | ||
yield: [] | ||
}; | ||
this.jsdc.append('function(){'); | ||
this.jsdc.append('var ' + state + '=0;'); | ||
this.jsdc.append('return '); | ||
this.jsdc.append('function(){return{next:' + temp + '}};'); | ||
o.pos = this.jsdc.res.length; | ||
this.jsdc.append('function ' + temp); | ||
self.gen(node, start); | ||
} | ||
else { | ||
this.jsdc.appendBefore('}();'); | ||
self.gen(node, start); | ||
self.jsdc.appendBefore('}();'); | ||
} | ||
}, | ||
expr: function(node, start) { | ||
var self = this; | ||
if(start) { | ||
this.jsdc.ignore(node.first()); | ||
this.jsdc.ignore(node.leaf(1)); | ||
self.jsdc.ignore(node.first(), 'gen3'); | ||
self.jsdc.ignore(node.leaf(1), 'gen4'); | ||
if(node.leaf(2).name() == JsNode.BINDID) { | ||
this.jsdc.ignore(node.leaf(2)); | ||
self.jsdc.ignore(node.leaf(2), 'gen5'); | ||
} | ||
var state = this.jsdc.uid(); | ||
var temp = this.jsdc.uid(); | ||
var o = this.hash[node.nid()] = { | ||
self.gen(node, start); | ||
} | ||
else { | ||
self.gen(node, start); | ||
self.jsdc.appendBefore('}()'); | ||
} | ||
}, | ||
gen: function(node, start) { | ||
var self = this; | ||
if(start) { | ||
self.jsdc.ignore(node.first(), 'gen6'); | ||
var state = self.jsdc.uid(); | ||
var temp = self.jsdc.uid(); | ||
var param = node.leaf(4).first(); | ||
var res = self.count(node.last().prev(), node); | ||
var count = res.count; | ||
var ret = res.return; | ||
if(res.pre) { | ||
self.pre(node.last().prev(), node.nid(), node.last().prev().nid()); | ||
} | ||
if(!param) { | ||
if(count) { | ||
param = self.jsdc.uid(); | ||
eventbus.on(node.leaf(4).nid(), function(node, start) { | ||
start && self.jsdc.append(param); | ||
}); | ||
} | ||
else { | ||
param = ''; | ||
} | ||
} | ||
else if(param.name() == JsNode.SINGLENAME) { | ||
param = param.first().first().token().content(); | ||
} | ||
else if(param.name() == JsNode.BINDREST) { | ||
param = param.last().first().token().content() + '[0]'; | ||
} | ||
var o = self.hash[node.nid()] = { | ||
state: state, | ||
index: 0, | ||
index2: 0, | ||
count: count, | ||
return: ret, | ||
if: 0, | ||
temp: temp, | ||
yield: [] | ||
param: param, | ||
last: null, | ||
yield: [], | ||
pre: res.pre | ||
}; | ||
this.jsdc.append('function(){'); | ||
this.jsdc.append('var ' + state + '=0;'); | ||
this.jsdc.append('return '); | ||
this.jsdc.append('function(){return{next:' + temp + '}};'); | ||
o.pos = this.jsdc.res.length; | ||
this.jsdc.append('function ' + temp); | ||
self.jsdc.append('function(){'); | ||
self.jsdc.append('var ' + state + '=0;'); | ||
self.jsdc.append('return function(){return{next:' + temp + '}};'); | ||
o.pos = self.jsdc.res.length; | ||
self.jsdc.append('function ' + temp); | ||
} | ||
else { | ||
this.jsdc.appendBefore('}()'); | ||
} | ||
}, | ||
yield: function(node, start) { | ||
var top = this.closest(node); | ||
var o = this.hash[top.nid()]; | ||
var self = this; | ||
var top = self.closest(node); | ||
var o = self.hash[top.nid()]; | ||
if(start) { | ||
if(o.index++ != 0) { | ||
this.jsdc.append('case ' + (o.index - 1) + ':'); | ||
self.jsdc.ignore(node.first(), 'gen7'); | ||
var parent = node.parent(); | ||
//赋值语句需要添加上参数,先默认undefined,并记录在变量中为下次添加做标记 | ||
var parent = node.parent(); | ||
switch(parent.name()) { | ||
case JsNode.INITLZ: | ||
o.last = join(parent.prev()); | ||
break; | ||
case JsNode.ASSIGNEXPR: | ||
o.last = join(parent.first()); | ||
break; | ||
default: | ||
o.last = null; | ||
} | ||
this.jsdc.ignore(node.first()); | ||
this.jsdc.append('arguments[0];'); | ||
//加上状态变更 | ||
o.index++; | ||
self.jsdc.append(o.state + '=' + ++o.index2 + ';'); | ||
//yield * | ||
@@ -85,16 +129,17 @@ if(node.size() > 2 | ||
&& node.leaf(1).token().content() == '*') { | ||
this.jsdc.ignore(node.leaf(1)); | ||
var temp = this.star[node.nid()] = this.jsdc.uid(); | ||
this.jsdc.append('var ' + temp + '='); | ||
self.jsdc.ignore(node.leaf(1), 'gen8'); | ||
var temp = this.star[node.nid()] = self.jsdc.uid(); | ||
self.jsdc.append('var ' + temp + '='); | ||
} | ||
else { | ||
this.jsdc.append('return{value:'); | ||
self.jsdc.append('return{value:'); | ||
} | ||
} | ||
else { | ||
if(this.star.hasOwnProperty(node.nid())) { | ||
var temp = this.star[node.nid()]; | ||
this.jsdc.appendBefore('();if(!' + temp + '.done)' + o.state + '--;return ' + temp); | ||
//yield * | ||
if(self.star.hasOwnProperty(node.nid())) { | ||
var temp = self.star[node.nid()]; | ||
self.jsdc.appendBefore('.next();return{done:(!' + temp + '.done&&' + o.state + '--),value:' + temp + '}'); | ||
o.yield.push({ | ||
i: this.jsdc.i, | ||
i: self.jsdc.i, | ||
star: temp | ||
@@ -104,7 +149,19 @@ }); | ||
else { | ||
this.jsdc.appendBefore(',done:false}'); | ||
self.jsdc.appendBefore(',done:' + (o.index == o.count) + '}'); | ||
o.yield.push({ | ||
i: this.jsdc.i | ||
i: self.jsdc.i | ||
}); | ||
} | ||
self.ignoreNext(node, ';'); | ||
if(o.index < o.count) { | ||
self.jsdc.appendBefore(';case ' + o.index2 + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';case ' + o.index2 + ':'); | ||
} | ||
//有赋值需要先赋值 | ||
if(o.last) { | ||
self.jsdc.appendBefore(o.last + '=' + o.param + ';'); | ||
} | ||
o.last = null; | ||
} | ||
@@ -117,18 +174,14 @@ }, | ||
if(start) { | ||
this.jsdc.append('switch(' + o.state + '++){case 0:'); | ||
if(o.count) { | ||
this.jsdc.append('while(1){switch(' + o.state + '){case 0:'); | ||
} | ||
} | ||
else { | ||
if(o.index) { | ||
var yie = o.yield[o.yield.length - 1]; | ||
var i; | ||
if(yie.star) { | ||
i = this.jsdc.res.lastIndexOf(yie.star, i); | ||
this.jsdc.replace(';default:', i + yie.star.length, 0); | ||
} | ||
else { | ||
i = this.jsdc.res.lastIndexOf(',done:false}', i); | ||
this.jsdc.replace('true};default:', i + 6, 6); | ||
} | ||
else if(!o.return) { | ||
if(o.count) { | ||
this.jsdc.appendBefore(';' + o.state + '=-1'); | ||
this.jsdc.appendBefore(';default:return{done:true}}}'); | ||
} | ||
this.jsdc.appendBefore(';return{done:true}}'); | ||
else { | ||
this.jsdc.appendBefore('return{done:true}'); | ||
} | ||
} | ||
@@ -149,5 +202,190 @@ } | ||
if(top) { | ||
this.jsdc.ignore(varstmt.first()); | ||
this.jsdc.ignore(varstmt.first(), 'gen9'); | ||
this.jsdc.insert('var ' + varstmt.leaf(1).first().first().token().content() + ';', this.hash[top.nid()].pos); | ||
} | ||
}, | ||
count: function(node, top, res) { | ||
res = res || { count: 0, return: false, pre: false }; | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
if(!isToken) { | ||
switch(node.name()) { | ||
case JsNode.YIELDEXPR: | ||
res.count++; | ||
//赋值语句忽略 | ||
var parent = node.parent(); | ||
switch(parent.name()) { | ||
case JsNode.INITLZ: | ||
self.jsdc.ignore(parent.prev(), 'gen10'); | ||
self.jsdc.ignore(node.prev(), 'gen11'); | ||
break; | ||
case JsNode.ASSIGNEXPR: | ||
self.jsdc.ignore(parent.first(), 'gen12'); | ||
self.jsdc.ignore(node.prev(), 'gen13'); | ||
break; | ||
} | ||
var belong = self.belong(node); | ||
belong.forEach(function(f) { | ||
self.stmt[f.nid()] = true; | ||
res.pre = true; | ||
}); | ||
break; | ||
case JsNode.RETSTMT: | ||
res.return = node; | ||
eventbus.on(node.nid(), function(node, start) { | ||
if(start) { | ||
var o = self.hash[top.nid()]; | ||
self.jsdc.appendBefore(';' + o.state + '=-1;default:'); | ||
} | ||
}); | ||
eventbus.on(node.leaf(1).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('{value:'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(',done:true}'); | ||
} | ||
}); | ||
break; | ||
//忽略这些节点中的yield语句 | ||
case JsNode.CLASSDECL: | ||
case JsNode.CLASSEXPR: | ||
case JsNode.FNDECL: | ||
case JsNode.FNEXPR: | ||
case JsNode.GENDECL: | ||
case JsNode.GENEXPR: | ||
case JsNode.METHOD: | ||
return; | ||
} | ||
node.leaves().forEach(function(leaf) { | ||
self.count(leaf, top, res); | ||
}); | ||
} | ||
return res; | ||
}, | ||
pre: function(node, nid, bid, res) { | ||
res = res || { index: 0 }; | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
if(!isToken) { | ||
switch(node.name()) { | ||
case JsNode.IFSTMT: | ||
if(self.stmt.hasOwnProperty(node.nid())) { | ||
res.index++; | ||
var block = node.leaf(4); | ||
//改写if语句 | ||
self.jsdc.ignore(node.first(), 'gen14'); | ||
self.jsdc.ignore(node.leaf(1), 'gen15'); | ||
self.jsdc.ignore(node.leaf(2), 'gen16'); | ||
self.jsdc.ignore(node.leaf(3), 'gen17'); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen18'); | ||
self.jsdc.ignore(block.first().last(), 'gen19'); | ||
} | ||
var temp; | ||
var ifEndTemp; | ||
var top; | ||
var ifstmt = node; | ||
//if结束后的状态 | ||
eventbus.on(node.nid(), function(node, start) { | ||
if(!start) { | ||
self.jsdc.append('case ' + ifEndTemp + ':'); | ||
} | ||
}); | ||
//根据表达式true/false分2个state | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
top = self.hash[nid]; | ||
index = top.index2; | ||
self.jsdc.append(top.state + '='); | ||
self.jsdc.append(join(ifstmt.leaf(2))); | ||
self.jsdc.append('?'); | ||
self.jsdc.append(++top.index2 + ':' + ++top.index2 + ';break;'); | ||
self.jsdc.append('case ' + (top.index2 - 1) + ':'); | ||
temp = top.index2; | ||
ifEndTemp = ++top.index2; | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(ifEndTemp); | ||
self.jsdc.appendBefore(';break;'); | ||
} | ||
}); | ||
//else语句忽略{} | ||
var elset = block.next(); | ||
if(elset && elset.name() == JsNode.TOKEN) { | ||
self.jsdc.ignore(elset, 'gen20'); | ||
block = elset.next(); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen21'); | ||
self.jsdc.ignore(block.first().last(), 'gen22'); | ||
} | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + temp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(ifEndTemp); | ||
self.jsdc.appendBefore('break;'); | ||
} | ||
}); | ||
} | ||
} | ||
break; | ||
//忽略这些节点中的所有逻辑 | ||
case JsNode.CLASSDECL: | ||
case JsNode.CLASSEXPR: | ||
case JsNode.FNDECL: | ||
case JsNode.FNEXPR: | ||
case JsNode.GENDECL: | ||
case JsNode.GENEXPR: | ||
case JsNode.METHOD: | ||
return; | ||
} | ||
node.leaves().forEach(function(leaf) { | ||
self.pre(leaf, nid, bid, res); | ||
}); | ||
} | ||
}, | ||
getLast: function(node) { | ||
while(node = node.last()) { | ||
if(node.name() == JsNode.TOKEN) { | ||
return node; | ||
} | ||
} | ||
}, | ||
ignoreNext: function(node, value) { | ||
node = this.getLast(node); | ||
if(node) { | ||
var token = node.token(); | ||
while(token = token.next()) { | ||
if(token.type() == Token.COMMENT | ||
|| token.type() == Token.BLANK | ||
|| token.type() == Token.LINE) { | ||
continue; | ||
} | ||
if(token.content() == value) { | ||
this.jsdc.ignore(token, 'gen23'); | ||
return; | ||
} | ||
else { | ||
return; | ||
} | ||
} | ||
} | ||
}, | ||
belong: function(node) { | ||
var res = []; | ||
while(node = node.parent()) { | ||
switch(node.name()) { | ||
case JsNode.IFSTMT: | ||
case JsNode.ITERSTMT: | ||
res.push(node); | ||
case JsNode.GENDECL: | ||
case JsNode.GENEXPR: | ||
break; | ||
} | ||
} | ||
return res; | ||
} | ||
@@ -157,3 +395,2 @@ }); | ||
module.exports = Generator; | ||
}); |
@@ -8,2 +8,3 @@ define(function(require, exports, module) { | ||
var Class = require('./util/Class'); | ||
var eventbus = require('./eventbus'); | ||
@@ -183,4 +184,2 @@ var Scope = require('./Scope'); | ||
var ignore = token.ignore; | ||
//firefox的奇怪bug,调试时不显示,重新赋值就好了 | ||
token.ignore = token.ignore; | ||
this.i = this.res.length; | ||
@@ -292,2 +291,3 @@ //加上ignore | ||
} | ||
eventbus.emit(node.nid(), [node, true]); | ||
}, | ||
@@ -356,14 +356,15 @@ after: function(node) { | ||
} | ||
eventbus.emit(node.nid(), [node]); | ||
}, | ||
ignore: function(node) { | ||
ignore: function(node, msg) { | ||
var self = this; | ||
if(node instanceof Token) { | ||
node.ignore = true; | ||
node.ignore = msg || true; | ||
} | ||
else if(node.name() == JsNode.TOKEN) { | ||
node.token().ignore = true; | ||
this.ignore(node.token()); | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.ignore(leaf); | ||
self.ignore(leaf, msg); | ||
}); | ||
@@ -370,0 +371,0 @@ } |
@@ -7,2 +7,3 @@ define(function(require, exports, module) { | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -20,13 +21,13 @@ var Klass = Class(function(jsdc) { | ||
o.name = node.leaf(1).first().token().content(); | ||
this.jsdc.ignore(node.leaf(0)); | ||
this.jsdc.ignore(node.leaf(1)); | ||
this.jsdc.ignore(node.leaf(2)); | ||
this.jsdc.ignore(node.leaf(0), 'klass1'); | ||
this.jsdc.ignore(node.leaf(1), 'klass2'); | ||
this.jsdc.ignore(node.leaf(2), 'klass3'); | ||
if(node.leaf(3).name() == JsNode.CLASSBODY) { | ||
this.jsdc.ignore(node.leaf(4)); | ||
this.jsdc.ignore(node.leaf(4), 'klass4'); | ||
this.body(node.last().prev(), o.name); | ||
} | ||
else { | ||
this.jsdc.ignore(node.leaf(3)); | ||
this.jsdc.ignore(node.leaf(5)); | ||
o.extend = this.join(node.leaf(2).last()); | ||
this.jsdc.ignore(node.leaf(3), 'klass5'); | ||
this.jsdc.ignore(node.leaf(5), 'klass6'); | ||
o.extend = join(node.leaf(2).last()); | ||
this.body(node.last().prev(), o.name, o.extend); | ||
@@ -53,9 +54,9 @@ this.jsdc.append('!function(){'); | ||
var o = {}; | ||
this.jsdc.ignore(node.leaf(0)); | ||
this.jsdc.ignore(node.leaf(1)); | ||
this.jsdc.ignore(node.leaf(0), 'klass7'); | ||
this.jsdc.ignore(node.leaf(1), 'klass8'); | ||
if(node.leaf(2).name() == JsNode.TOKEN | ||
&& node.leaf(2).token().content() == '{') { | ||
this.jsdc.ignore(node.leaf(2)); | ||
this.jsdc.ignore(node.leaf(2), 'klass9'); | ||
if(node.leaf(1).name() == JsNode.HERITAGE) { | ||
o.extend = this.join(node.leaf(1).last()); | ||
o.extend = join(node.leaf(1).last()); | ||
o.name = this.jsdc.uid(); | ||
@@ -69,11 +70,11 @@ } | ||
&& node.leaf(3).token().content() == '{') { | ||
this.jsdc.ignore(node.leaf(3)); | ||
this.jsdc.ignore(node.leaf(3), 'klass10'); | ||
o.name = node.leaf(1).first().token().content(); | ||
o.extend = this.join(node.leaf(2).last()); | ||
o.extend = join(node.leaf(2).last()); | ||
} | ||
else { | ||
o.name = this.jsdc.uid(); | ||
this.jsdc.ignore(node.leaf(1)); | ||
this.jsdc.ignore(node.leaf(1), 'klass11'); | ||
} | ||
this.jsdc.ignore(node.last()); | ||
this.jsdc.ignore(node.last(), 'klass12'); | ||
var classbody = node.last().prev(); | ||
@@ -110,3 +111,3 @@ this.body(classbody, o.name, o.extend); | ||
var token = first.first().first().token(); | ||
this.jsdc.ignore(token); | ||
this.jsdc.ignore(token, 'klass13'); | ||
if(token.content() == 'constructor') { | ||
@@ -127,3 +128,3 @@ this.jsdc.append('function '); | ||
this.gs[prptn.nid()] = true; | ||
this.jsdc.ignore(prptn); | ||
this.jsdc.ignore(prptn, 'klass14'); | ||
this.jsdc.append('Object.defineProperty('); | ||
@@ -152,3 +153,3 @@ this.jsdc.append(o.name); | ||
if(start) { | ||
this.jsdc.ignore(first.token()); | ||
this.jsdc.ignore(first.token(), 'klass15'); | ||
this.jsdc.append(o.name + '.'); | ||
@@ -164,3 +165,3 @@ } | ||
super: function(node) { | ||
this.jsdc.ignore(node); | ||
this.jsdc.ignore(node, 'klass16'); | ||
var top = this.closest(node); | ||
@@ -248,22 +249,2 @@ if(this.hash.hasOwnProperty(top.nid())) { | ||
} | ||
}, | ||
join: function(node) { | ||
var res = { s: '' }; | ||
this.recursion(node, res); | ||
return res.s; | ||
}, | ||
recursion: function(node, res) { | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf, res); | ||
}); | ||
} | ||
} | ||
@@ -270,0 +251,0 @@ }); |
@@ -14,3 +14,3 @@ define(function(require, exports, module) { | ||
module: function(node) { | ||
this.jsdc.ignore(node); | ||
this.jsdc.ignore(node, 'module1'); | ||
this.jsdc.append('var '); | ||
@@ -24,3 +24,3 @@ this.jsdc.append(node.leaf(1).first().token().content()); | ||
var self = this; | ||
self.jsdc.ignore(node); | ||
self.jsdc.ignore(node, 'module2'); | ||
var one = node.leaf(1); | ||
@@ -71,8 +71,8 @@ //import "string" | ||
this.jsdc.append('});}();'); | ||
this.jsdc.ignore(node); | ||
this.jsdc.ignore(node, 'module3'); | ||
} | ||
else if(s == 'default') { | ||
this.jsdc.append('module.exports='); | ||
this.jsdc.ignore(node.leaf(0)); | ||
this.jsdc.ignore(node.leaf(1)); | ||
this.jsdc.ignore(node.leaf(0), 'module4'); | ||
this.jsdc.ignore(node.leaf(1), 'module5'); | ||
} | ||
@@ -88,4 +88,4 @@ break; | ||
this.jsdc.append(';exports.' + id + '='); | ||
this.jsdc.ignore(varstmt.first()); | ||
this.jsdc.ignore(node.first()); | ||
this.jsdc.ignore(varstmt.first(), 'module6'); | ||
this.jsdc.ignore(node.first(), 'module7'); | ||
break; | ||
@@ -98,3 +98,3 @@ case JsNode.FNDECL: | ||
this.jsdc.append(';'); | ||
this.jsdc.ignore(node.first()); | ||
this.jsdc.ignore(node.first(), 'module8'); | ||
break; | ||
@@ -101,0 +101,0 @@ } |
@@ -14,7 +14,7 @@ define(function(require, exports, module) { | ||
if(s.toLowerCase().indexOf('0b') == 0) { | ||
this.jsdc.ignore(t); | ||
this.jsdc.ignore(t, 'num1'); | ||
this.jsdc.append('parseInt("' + t.content().slice(2) + '", 2)'); | ||
} | ||
else if(s.toLowerCase().indexOf('0o') == 0) { | ||
this.jsdc.ignore(t); | ||
this.jsdc.ignore(t, 'num2'); | ||
this.jsdc.append('parseInt("' + t.content().slice(2) + '", 8)'); | ||
@@ -21,0 +21,0 @@ } |
@@ -7,2 +7,3 @@ define(function(require, exports, module) { | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -44,6 +45,6 @@ var Obj = Class(function(jsdc) { | ||
temp: temp, | ||
id: this.join(first) | ||
id: join(first) | ||
}); | ||
this.jsdc.append(temp); | ||
this.jsdc.ignore(first); | ||
this.jsdc.ignore(first, 'obj1'); | ||
} | ||
@@ -92,19 +93,2 @@ } | ||
} | ||
}, | ||
join: function(node, res) { | ||
res = res || { s: '' }; | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.join(leaf, res); | ||
}); | ||
} | ||
return res.s; | ||
} | ||
@@ -111,0 +95,0 @@ }); |
@@ -7,2 +7,3 @@ define(function(require, exports, module) { | ||
var Class = require('./util/Class'); | ||
var join = require('./join'); | ||
@@ -20,3 +21,3 @@ var Rest = Class(function(jsdc) { | ||
var rest = last.first(); | ||
this.jsdc.ignore(rest); | ||
this.jsdc.ignore(rest, 'rest1'); | ||
this.hash[fnbody.nid()] = { | ||
@@ -46,3 +47,3 @@ index: fmparams.size() - 1, | ||
this.hash2[node.nid()] = first; | ||
this.jsdc.ignore(arglist); | ||
this.jsdc.ignore(arglist, 'rest2'); | ||
} | ||
@@ -62,7 +63,7 @@ } | ||
//主表达式无需设置apply的context,成员需设 | ||
this.jsdc.append(mmb.name() == JsNode.MMBEXPR ? this.join(mmb.first()) : 'this'); | ||
this.jsdc.append(mmb.name() == JsNode.MMBEXPR ? join(mmb.first()) : 'this'); | ||
this.jsdc.append(', ['); | ||
var leaves = node.leaves(); | ||
for(var i = 0; i < leaves.length - 3; i++) { | ||
this.jsdc.append(this.join(leaves[i])); | ||
this.jsdc.append(join(leaves[i])); | ||
} | ||
@@ -74,22 +75,2 @@ this.jsdc.append(']'); | ||
} | ||
}, | ||
join: function(node) { | ||
var res = { s: '' }; | ||
this.recursion(node, res); | ||
return res.s; | ||
}, | ||
recursion: function(node, res) { | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
if(!isVirtual) { | ||
res.s += node.token().content(); | ||
} | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf, res); | ||
}); | ||
} | ||
} | ||
@@ -96,0 +77,0 @@ }); |
@@ -27,6 +27,7 @@ define(function(require, exports, module) { | ||
var name = node.name(); | ||
//每个{}作用域记录是否有lexdecl或fn或generator | ||
//每个{}作用域记录是否有lexdecl或fn或generator或classdecl | ||
if(name == JsNode.LEXDECL | ||
|| name == JsNode.FNDECL | ||
|| name == JsNode.GENDECL) { | ||
|| name == JsNode.GENDECL | ||
|| name == JsNode.CLASSDECL) { | ||
var parent = self.closest(node); | ||
@@ -74,10 +75,10 @@ //全局lexdecl没有作用域无需记录,fnbody的也无需记录 | ||
if(vardecl.first().name() == JsNode.BINDID) { | ||
self.jsdc.ignore(varstmt.first().token()); | ||
self.jsdc.ignore(varstmt.first().token(), 'scope1'); | ||
} | ||
else { | ||
//destruct需忽略前后可能的,再改为; var也需忽略 | ||
self.jsdc.ignore(vardecl.prev()); | ||
self.jsdc.ignore(vardecl.prev(), 'scope2'); | ||
var next = vardecl.next(); | ||
if(next.token().content() == ',') { | ||
self.jsdc.ignore(next); | ||
self.jsdc.ignore(next, 'scope3'); | ||
} | ||
@@ -160,2 +161,3 @@ } | ||
case JsNode.GENDECL: | ||
case JsNode.GENEXPR: | ||
return parent; | ||
@@ -162,0 +164,0 @@ case JsNode.FNDECL: |
@@ -35,3 +35,3 @@ define(function(require, exports, module) { | ||
if(res != s) { | ||
this.jsdc.ignore(t); | ||
this.jsdc.ignore(t, 'str1'); | ||
this.jsdc.append(res); | ||
@@ -38,0 +38,0 @@ } |
@@ -12,3 +12,3 @@ define(function(require, exports, module) { | ||
parse: function(t) { | ||
this.jsdc.ignore(t); | ||
this.jsdc.ignore(t, 'template1'); | ||
var s = t.content(); | ||
@@ -15,0 +15,0 @@ var res = '"'; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
239462
56
5392
0
Updatedhomunculus@^0.3.4