Comparing version 0.4.14 to 0.4.15-1
{ | ||
"name": "jsdc", | ||
"version": "0.4.14", | ||
"version": "0.4.15-1", | ||
"description": "compiler ecmascript6 to ecmascript5", | ||
@@ -55,3 +55,3 @@ "maintainers": [ | ||
"dependencies": { | ||
"homunculus": "^0.5.2" | ||
"homunculus": "^0.5.6" | ||
}, | ||
@@ -58,0 +58,0 @@ "devDependencies": { |
@@ -26,2 +26,5 @@ var homunculus = require('homunculus'); | ||
} | ||
else if(c == '\n') { | ||
res += '\\'; | ||
} | ||
else if(c == '$') { | ||
@@ -31,5 +34,22 @@ if(s.charAt(i + 1) == '{') { | ||
res += '" + '; | ||
var multi = false; | ||
for(var j = i + 2; j < s.length - 1; j++) { | ||
c = s.charAt(j); | ||
if(c == '}') { | ||
break; | ||
} | ||
else if(!/[a-z_\s]/i.test(c)) { | ||
multi = true; | ||
break; | ||
} | ||
} | ||
if(multi) { | ||
res += '('; | ||
} | ||
for(i = i + 2; i < s.length - 1; i++) { | ||
c = s.charAt(i); | ||
if(c == '}') { | ||
if(multi) { | ||
res += ')'; | ||
} | ||
res += ' + "'; | ||
@@ -36,0 +56,0 @@ continue outer; |
@@ -1,85 +0,83 @@ | ||
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 ArrayCmph = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
var self = this; | ||
//忽略[] | ||
if(start) { | ||
self.hash[node.nid()] = { | ||
id: '', | ||
f: 0 | ||
}; | ||
self.jsdc.ignore(node.first(), 'arrcmph1'); | ||
self.jsdc.ignore(node.last(), 'arrcmph2'); | ||
self.jsdc.append('function(){'); | ||
//forbinding的变量要提出来声明 | ||
var leaves = node.leaf(1).leaves(); | ||
for(var i = 0; i < leaves.length - 1; i++) { | ||
var leaf = leaves[i]; | ||
if(leaf.name() == JsNode.CMPHFOR) { | ||
var id = leaf.leaf(2).first().token().content(); | ||
self.jsdc.append('var ' + id + ';'); | ||
self.hash[node.nid()].f++; | ||
} | ||
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 ArrayCmph = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
var self = this; | ||
//忽略[] | ||
if(start) { | ||
self.hash[node.nid()] = { | ||
id: '', | ||
f: 0 | ||
}; | ||
self.jsdc.ignore(node.first(), 'arrcmph1'); | ||
self.jsdc.ignore(node.last(), 'arrcmph2'); | ||
self.jsdc.append('function(){'); | ||
//forbinding的变量要提出来声明 | ||
var leaves = node.leaf(1).leaves(); | ||
for(var i = 0; i < leaves.length - 1; i++) { | ||
var leaf = leaves[i]; | ||
if(leaf.name() == JsNode.CMPHFOR) { | ||
var id = leaf.leaf(2).first().token().content(); | ||
self.jsdc.append('var ' + id + ';'); | ||
self.hash[node.nid()].f++; | ||
} | ||
var id = this.jsdc.uid(); | ||
self.hash[node.nid()].id = id; | ||
self.jsdc.append('var ' + id + '=[];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(')'); | ||
//有多少个cmphfor则包裹多少个{} | ||
while(self.hash[node.nid()].f--) { | ||
self.jsdc.appendBefore('}'); | ||
} | ||
self.jsdc.appendBefore('return ' + self.hash[node.nid()].id + '}()'); | ||
var id = this.jsdc.uid(); | ||
self.hash[node.nid()].id = id; | ||
self.jsdc.append('var ' + id + '=[];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(')'); | ||
//有多少个cmphfor则包裹多少个{} | ||
while(self.hash[node.nid()].f--) { | ||
self.jsdc.appendBefore('}'); | ||
} | ||
}, | ||
for: function(node, start) { | ||
var top = node.parent().parent(); | ||
if(top.name() == JsNode.ARRCMPH) { | ||
if(!start) { | ||
var id = node.leaf(2).first().token().content(); | ||
this.jsdc.appendBefore('{'); | ||
this.jsdc.appendBefore(id); | ||
this.jsdc.appendBefore('='); | ||
this.jsdc.appendBefore(join(node.leaf(4))); | ||
this.jsdc.appendBefore('['); | ||
this.jsdc.appendBefore(id); | ||
this.jsdc.appendBefore('];'); | ||
var s = node.next().name(); | ||
if(s != JsNode.CMPHIF | ||
&& s != JsNode.CMPHFOR) { | ||
this.jsdc.appendBefore(this.hash[top.nid()].id + '.push('); | ||
} | ||
} | ||
} | ||
}, | ||
if: function(node, start) { | ||
var top = node.parent().parent(); | ||
if(top.name() == JsNode.ARRCMPH) { | ||
if(!start) { | ||
self.jsdc.appendBefore('return ' + self.hash[node.nid()].id + '}()'); | ||
} | ||
}, | ||
for: function(node, start) { | ||
var top = node.parent().parent(); | ||
if(top.name() == JsNode.ARRCMPH) { | ||
if(!start) { | ||
var id = node.leaf(2).first().token().content(); | ||
this.jsdc.appendBefore('{'); | ||
this.jsdc.appendBefore(id); | ||
this.jsdc.appendBefore('='); | ||
this.jsdc.appendBefore(join(node.leaf(4))); | ||
this.jsdc.appendBefore('['); | ||
this.jsdc.appendBefore(id); | ||
this.jsdc.appendBefore('];'); | ||
var s = node.next().name(); | ||
if(s != JsNode.CMPHIF | ||
&& s != JsNode.CMPHFOR) { | ||
this.jsdc.appendBefore(this.hash[top.nid()].id + '.push('); | ||
} | ||
} | ||
}, | ||
of: function(node) { | ||
if(node.parent().name() == JsNode.CMPHFOR) { | ||
this.jsdc.ignore(node, 'arrcmph3'); | ||
this.jsdc.append('in '); | ||
} | ||
}, | ||
if: function(node, start) { | ||
var top = node.parent().parent(); | ||
if(top.name() == JsNode.ARRCMPH) { | ||
if(!start) { | ||
this.jsdc.appendBefore(this.hash[top.nid()].id + '.push('); | ||
} | ||
} | ||
}); | ||
module.exports = ArrayCmph; | ||
}, | ||
of: function(node) { | ||
if(node.parent().name() == JsNode.CMPHFOR) { | ||
this.jsdc.ignore(node, 'arrcmph3'); | ||
this.jsdc.append('in '); | ||
} | ||
} | ||
}); | ||
module.exports = ArrayCmph; | ||
}); |
@@ -1,42 +0,40 @@ | ||
define(function(require, exports, module) { | ||
var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Class = require('./util/Class'); | ||
var ArrowFn = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
}).methods({ | ||
parse: function(node) { | ||
this.jsdc.append('function'); | ||
}, | ||
params: function(node, start) { | ||
//默认一个参数需要加() | ||
if(node.first().name() == JsNode.TOKEN) { | ||
if(start) { | ||
this.jsdc.append('('); | ||
} | ||
else { | ||
this.jsdc.appendBefore(')'); | ||
} | ||
define(function(require, exports, module){var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Class = require('./util/Class'); | ||
var ArrowFn = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
}).methods({ | ||
parse: function(node) { | ||
this.jsdc.append('function'); | ||
}, | ||
params: function(node, start) { | ||
//默认一个参数需要加() | ||
if(node.first().name() == JsNode.TOKEN) { | ||
if(start) { | ||
this.jsdc.append('('); | ||
} | ||
}, | ||
arrow: function(t) { | ||
this.jsdc.ignore(t, 'arrow1'); | ||
}, | ||
body: function(node, start) { | ||
//表达式只有1个需要加{return } | ||
if(node.size() == 1) { | ||
if(start) { | ||
this.jsdc.append('{return '); | ||
} | ||
else { | ||
this.jsdc.appendBefore('}'); | ||
} | ||
else { | ||
this.jsdc.appendBefore(')'); | ||
} | ||
} | ||
}); | ||
module.exports = ArrowFn; | ||
}, | ||
arrow: function(t) { | ||
this.jsdc.ignore(t, 'arrow1'); | ||
}, | ||
body: function(node, start) { | ||
//表达式只有1个需要加{return } | ||
if(node.size() == 1) { | ||
if(start) { | ||
this.jsdc.append('{return '); | ||
} | ||
else { | ||
this.jsdc.appendBefore('}'); | ||
} | ||
} | ||
} | ||
}); | ||
module.exports = ArrowFn; | ||
}); |
@@ -1,44 +0,42 @@ | ||
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 DefaultValue = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
}).methods({ | ||
param: function(fmparams) { | ||
var self = this; | ||
if(fmparams.name() == JsNode.FMPARAMS && fmparams.size()) { | ||
var fnbody = fmparams.next().next().next().nid(); | ||
fmparams.leaves().forEach(function(sgname) { | ||
if(sgname.name() == JsNode.SINGLENAME && sgname.size() == 2) { | ||
var init = sgname.last(); | ||
if(init.name() == JsNode.INITLZ) { | ||
self.jsdc.ignore(init, 'default1', true); | ||
(self.hash[fnbody] = self.hash[fnbody] || []).push(sgname); | ||
} | ||
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 DefaultValue = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
}).methods({ | ||
param: function(fmparams) { | ||
var self = this; | ||
if(fmparams.name() == JsNode.FMPARAMS && fmparams.size()) { | ||
var fnbody = fmparams.next().next().next().nid(); | ||
fmparams.leaves().forEach(function(sgname) { | ||
if(sgname.name() == JsNode.SINGLENAME && sgname.size() == 2) { | ||
var init = sgname.last(); | ||
if(init.name() == JsNode.INITLZ) { | ||
self.jsdc.ignore(init, 'default1', true); | ||
(self.hash[fnbody] = self.hash[fnbody] || []).push(sgname); | ||
} | ||
}); | ||
} | ||
}, | ||
enter: function(fnbody) { | ||
var self = this; | ||
if(self.hash.hasOwnProperty(fnbody.nid())) { | ||
var sgnames = self.hash[fnbody.nid()]; | ||
sgnames.forEach(function(sgname) { | ||
var id = sgname.first().first().token().content(); | ||
self.jsdc.append('if(' + id + '===void 0)' + id); | ||
self.jsdc.append(join(sgname.last())); | ||
self.jsdc.append(';'); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
module.exports = DefaultValue; | ||
}, | ||
enter: function(fnbody) { | ||
var self = this; | ||
if(self.hash.hasOwnProperty(fnbody.nid())) { | ||
var sgnames = self.hash[fnbody.nid()]; | ||
sgnames.forEach(function(sgname) { | ||
var id = sgname.first().first().token().content(); | ||
self.jsdc.append('if(' + id + '===void 0)' + id); | ||
self.jsdc.append(join(sgname.last())); | ||
self.jsdc.append(';'); | ||
}); | ||
} | ||
} | ||
}); | ||
module.exports = DefaultValue; | ||
}); |
1148
web/Destruct.js
@@ -1,284 +0,120 @@ | ||
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 Destruct = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.inAssign = {}; | ||
this.idCache = {}; | ||
}).methods({ | ||
getIds: function(node, res) { | ||
if(this.idCache.hasOwnProperty(node.nid())) { | ||
return this.idCache[node.nid()]; | ||
} | ||
this.jsdc.ignore(node, 'destruct1'); | ||
res = res || { arr: [] }; | ||
this.recursion(node, res); | ||
this.idCache[node.nid()] = res.arr; | ||
return res.arr; | ||
}, | ||
recursion: function(node, res) { | ||
var self = this; | ||
var rec = false; | ||
switch(node.name()) { | ||
case JsNode.ARRBINDPAT: | ||
res.arr = res.arr.concat(this.arrbindpat(node)); | ||
rec = true; | ||
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 Destruct = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.inAssign = {}; | ||
this.idCache = {}; | ||
}).methods({ | ||
getIds: function(node, res) { | ||
if(this.idCache.hasOwnProperty(node.nid())) { | ||
return this.idCache[node.nid()]; | ||
} | ||
this.jsdc.ignore(node, 'destruct1'); | ||
res = res || { arr: [] }; | ||
this.recursion(node, res); | ||
this.idCache[node.nid()] = res.arr; | ||
return res.arr; | ||
}, | ||
recursion: function(node, res) { | ||
var self = this; | ||
var rec = false; | ||
switch(node.name()) { | ||
case JsNode.ARRBINDPAT: | ||
res.arr = res.arr.concat(this.arrbindpat(node)); | ||
rec = true; | ||
break; | ||
case JsNode.OBJBINDPAT: | ||
res.arr = res.arr.concat(this.objbindpat(node)); | ||
rec = true; | ||
break; | ||
case JsNode.BINDELEM: | ||
case JsNode.BINDPROPT: | ||
rec = true; | ||
break; | ||
} | ||
rec && node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf, res); | ||
}); | ||
}, | ||
arrbindpat: function(node) { | ||
if(this.idCache.hasOwnProperty(node.nid())) { | ||
return this.idCache[node.nid()]; | ||
} | ||
var res = []; | ||
node.leaves().forEach(function(leaf) { | ||
switch(leaf.name()) { | ||
case JsNode.SINGLENAME: | ||
res.push(leaf.first().first().token().content()); | ||
break; | ||
case JsNode.OBJBINDPAT: | ||
res.arr = res.arr.concat(this.objbindpat(node)); | ||
rec = true; | ||
break; | ||
case JsNode.BINDELEM: | ||
case JsNode.BINDPROPT: | ||
rec = true; | ||
break; | ||
case JsNode.BINDREST: | ||
res.push(leaf.last().first().token().content()); | ||
} | ||
rec && node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf, res); | ||
}); | ||
}, | ||
arrbindpat: function(node) { | ||
if(this.idCache.hasOwnProperty(node.nid())) { | ||
return this.idCache[node.nid()]; | ||
} | ||
var res = []; | ||
node.leaves().forEach(function(leaf) { | ||
switch(leaf.name()) { | ||
case JsNode.SINGLENAME: | ||
res.push(leaf.first().first().token().content()); | ||
break; | ||
case JsNode.BINDREST: | ||
res.push(leaf.last().first().token().content()); | ||
}); | ||
this.idCache[node.nid()] = res; | ||
return res; | ||
}, | ||
objbindpat: function(node) { | ||
if(this.idCache.hasOwnProperty(node.nid())) { | ||
return this.idCache[node.nid()]; | ||
} | ||
var res = []; | ||
node.leaves().forEach(function(leaf) { | ||
if(leaf.name() == JsNode.BINDPROPT) { | ||
var first = leaf.first(); | ||
if(first.name() == JsNode.SINGLENAME) { | ||
res.push(first.first().first().token().content()); | ||
} | ||
}); | ||
this.idCache[node.nid()] = res; | ||
return res; | ||
}, | ||
objbindpat: function(node) { | ||
if(this.idCache.hasOwnProperty(node.nid())) { | ||
return this.idCache[node.nid()]; | ||
} | ||
var res = []; | ||
node.leaves().forEach(function(leaf) { | ||
if(leaf.name() == JsNode.BINDPROPT) { | ||
var first = leaf.first(); | ||
else if(first.name() == JsNode.PROPTNAME) { | ||
first = leaf.leaf(2); | ||
if(first.name() == JsNode.SINGLENAME) { | ||
res.push(first.first().first().token().content()); | ||
} | ||
else if(first.name() == JsNode.PROPTNAME) { | ||
first = leaf.leaf(2); | ||
if(first.name() == JsNode.SINGLENAME) { | ||
res.push(first.first().first().token().content()); | ||
} | ||
} | ||
} | ||
}); | ||
this.idCache[node.nid()] = res; | ||
return res; | ||
}, | ||
parse: function(node, start, ret) { | ||
var self = this; | ||
var first; | ||
if(ret) { | ||
first = node; | ||
} | ||
else { | ||
first = node.first(); | ||
} | ||
switch(first.name()) { | ||
case JsNode.ARRBINDPAT: | ||
if(start) { | ||
if(!ret) { | ||
//忽略前面的,改为; | ||
var prev = node.prev(); | ||
if(prev.token().content() == ',') { | ||
self.jsdc.append(';'); | ||
} | ||
self.jsdc.append('!function(){var '); | ||
}); | ||
this.idCache[node.nid()] = res; | ||
return res; | ||
}, | ||
parse: function(node, start, ret) { | ||
var self = this; | ||
var first; | ||
if(ret) { | ||
first = node; | ||
} | ||
else { | ||
first = node.first(); | ||
} | ||
switch(first.name()) { | ||
case JsNode.ARRBINDPAT: | ||
if(start) { | ||
if(!ret) { | ||
//忽略前面的,改为; | ||
var prev = node.prev(); | ||
if(prev.token().content() == ',') { | ||
self.jsdc.append(';'); | ||
} | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
!ret && self.jsdc.append(temp); | ||
self.jsdc.append('!function(){var '); | ||
} | ||
else { | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
var target = self.getArray(first.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1; | ||
switch(leaf.name()) { | ||
case JsNode.SINGLENAME: | ||
var id = leaf.first().first().token().content(); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '[' + i + '];'); | ||
var init = leaf.last(); | ||
ret.append('if(' + temp2 + '.indexOf(' + id + ')!=' + i + ')'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
case JsNode.BINDELEM: | ||
self.destruct(leaf.first(), { | ||
temp: temp, | ||
end: end, | ||
index: i | ||
}, ret); | ||
break; | ||
case JsNode.BINDREST: | ||
var id = leaf.last().first().token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
}); | ||
if(!ret) { | ||
(ret || self.jsdc).appendBefore('}()'); | ||
//忽略后面的,改为; | ||
var next = node.next(); | ||
if(next.token().content() == ',') { | ||
next = next.next(); | ||
if(next.first().name() == JsNode.BINDID) { | ||
(ret || self.jsdc).appendBefore(';var '); | ||
} | ||
} | ||
} | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
break; | ||
case JsNode.OBJBINDPAT: | ||
if(start) { | ||
if(!ret) { | ||
//忽略前面的,改为; | ||
var prev = node.prev(); | ||
if(prev.token().content() == ',') { | ||
self.jsdc.append(';'); | ||
} | ||
self.jsdc.append('!function(){var '); | ||
} | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
!ret && self.jsdc.append(temp); | ||
} | ||
else { | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
var target = self.getName(first.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1; | ||
leaf = leaf.first(); | ||
switch(leaf.name()) { | ||
case JsNode.SINGLENAME: | ||
var id = leaf.first().first().token().content(); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + id + '"];'); | ||
var init = leaf.last(); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + id + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
case JsNode.PROPTNAME: | ||
var last = leaf.next().next(); | ||
var name = leaf.first().first().token().content(); | ||
switch(last.name()) { | ||
case JsNode.SINGLENAME: | ||
var id = last.first().first().token().content(); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.size() == 2) { | ||
var temp2 = this.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + ';'); | ||
ret.appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
var init = last.last(); | ||
ret.appendBefore('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.appendBefore(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
case JsNode.BINDELEM: | ||
self.destruct(last.first(), { | ||
temp: temp, | ||
end: end, | ||
name: name, | ||
index: 0 | ||
}, ret); | ||
break; | ||
} | ||
break; | ||
} | ||
}); | ||
if(!ret) { | ||
self.jsdc.appendBefore('}()'); | ||
//忽略后面的,改为; | ||
var next = node.next(); | ||
if(next.token().content() == ',') { | ||
next = next.next(); | ||
if(next.first().name() == JsNode.BINDID) { | ||
self.jsdc.appendBefore(';var '); | ||
} | ||
} | ||
} | ||
temp = self.jsdc.uid(); | ||
} | ||
break; | ||
} | ||
}, | ||
destruct: function(node, data, ret) { | ||
var self = this; | ||
switch(node.name()) { | ||
case JsNode.ARRBINDPAT: | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
self.hash[first.nid()] = temp; | ||
!ret && self.jsdc.append(temp); | ||
} | ||
else { | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
var target = self.getArray(first.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1 && data.end; | ||
var end = i == target.length - 1; | ||
switch(leaf.name()) { | ||
@@ -318,15 +154,41 @@ case JsNode.SINGLENAME: | ||
}); | ||
break; | ||
case JsNode.OBJBINDPAT: | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
if(!ret) { | ||
(ret || self.jsdc).appendBefore('}()'); | ||
//忽略后面的,改为; | ||
var next = node.next(); | ||
if(next.token().content() == ',') { | ||
next = next.next(); | ||
if(next.first().name() == JsNode.BINDID) { | ||
(ret || self.jsdc).appendBefore(';var '); | ||
} | ||
} | ||
} | ||
} | ||
break; | ||
case JsNode.OBJBINDPAT: | ||
if(start) { | ||
if(!ret) { | ||
//忽略前面的,改为; | ||
var prev = node.prev(); | ||
if(prev.token().content() == ',') { | ||
self.jsdc.append(';'); | ||
} | ||
self.jsdc.append('!function(){var '); | ||
} | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
temp = self.jsdc.uid(); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
self.hash[first.nid()] = temp; | ||
!ret && self.jsdc.append(temp); | ||
} | ||
else { | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
var target = self.getName(first.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1 && data.end; | ||
var end = i == target.length - 1; | ||
leaf = leaf.first(); | ||
@@ -392,151 +254,168 @@ switch(leaf.name()) { | ||
}); | ||
break; | ||
} | ||
}, | ||
expr: function(assignexpr, start, ret) { | ||
var self = this; | ||
var first; | ||
if(ret) { | ||
first = assignexpr; | ||
} | ||
else { | ||
first = assignexpr.first(); | ||
first = first.first(); | ||
} | ||
switch(first.name()) { | ||
case JsNode.ARRLTR: | ||
if(start) { | ||
first.destruct = true; | ||
!ret && this.jsdc.ignore(first, 'destruct2'); | ||
if(assignexpr.parent().name() == JsNode.ASSIGNEXPR) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().first().first().nid()]; | ||
!ret && this.jsdc.ignore(assignexpr.leaf(1), 'destruct3'); | ||
if(!ret) { | ||
self.jsdc.appendBefore('}()'); | ||
//忽略后面的,改为; | ||
var next = node.next(); | ||
if(next.token().content() == ',') { | ||
next = next.next(); | ||
if(next.first().name() == JsNode.BINDID) { | ||
self.jsdc.appendBefore(';var '); | ||
} | ||
} | ||
else if(assignexpr.parent().name() == JsNode.INITLZ) { | ||
var bindpat = assignexpr.parent().prev(); | ||
if(bindpat.name() == JsNode.ARRBINDPAT | ||
|| bindpat.name() == JsNode.OBJBINDPAT) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().prev().nid()]; | ||
!ret && this.jsdc.ignore(assignexpr.leaf(1), 'destruct4'); | ||
} | ||
} | ||
break; | ||
} | ||
}, | ||
destruct: function(node, data, ret) { | ||
var self = this; | ||
switch(node.name()) { | ||
case JsNode.ARRBINDPAT: | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1 && data.end; | ||
switch(leaf.name()) { | ||
case JsNode.SINGLENAME: | ||
var id = leaf.first().first().token().content(); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '[' + i + '];'); | ||
var init = leaf.last(); | ||
ret.append('if(' + temp2 + '.indexOf(' + id + ')!=' + i + ')'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
!ret && self.jsdc.append('function(){var '); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
self.inAssign[first.nid()] = true; | ||
} | ||
self.hash[first.nid()] = temp; | ||
!ret && self.jsdc.append(temp); | ||
} | ||
} | ||
else { | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
break; | ||
case JsNode.BINDELEM: | ||
self.destruct(leaf.first(), { | ||
temp: temp, | ||
end: end, | ||
index: i | ||
}, ret); | ||
break; | ||
case JsNode.BINDREST: | ||
var id = leaf.last().first().token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
}); | ||
break; | ||
case JsNode.OBJBINDPAT: | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1 && data.end; | ||
leaf = leaf.first(); | ||
switch(leaf.name()) { | ||
case JsNode.SINGLENAME: | ||
var id = leaf.first().first().token().content(); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + id + '"];'); | ||
var init = leaf.last(); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + id + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
self.hash[first.nid()] = temp; | ||
!ret && self.jsdc.append('function(){var '); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
self.getArray(first.leaves()).forEach(function(leaf) { | ||
if(leaf.name() == JsNode.TOKEN) { | ||
return; | ||
} | ||
leaf = leaf.first(); | ||
if(leaf.name() == JsNode.ARRLTR) { | ||
self.destructExpr(leaf, true); | ||
} | ||
}); | ||
} | ||
else { | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
var target = self.getArray(first.leaves()); | ||
target.forEach(function(leaf, i) { | ||
if(leaf.name() == JsNode.TOKEN) { | ||
return; | ||
} | ||
var end = i == target.length - 1; | ||
leaf = leaf.first(); | ||
switch(leaf.name()) { | ||
case JsNode.TOKEN: | ||
if(leaf.token().content() == '...') { | ||
var id = leaf.next().first().token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
else { | ||
var id = leaf.token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.PRMREXPR: | ||
var id = leaf.first().token().content(); | ||
break; | ||
case JsNode.PROPTNAME: | ||
var last = leaf.next().next(); | ||
var name = leaf.first().first().token().content(); | ||
switch(last.name()) { | ||
case JsNode.SINGLENAME: | ||
var id = last.first().first().token().content(); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '[' + i + '];'); | ||
ret.append('if(' + temp2 + '.indexOf(' + id + ')!=' + i + ')'); | ||
ret.append(id + '=' + join(leaf.next().next()) + '}();'); | ||
if(ret && end && last.size() == 2) { | ||
var temp2 = this.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + ';'); | ||
ret.appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
var init = last.last(); | ||
ret.appendBefore('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.appendBefore(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + '];'); | ||
(ret || self.jsdc).appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + join(leaf.next().next()) + (!ret && end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
case JsNode.ARRLTR: | ||
case JsNode.OBJLTR: | ||
self.destructExpr(leaf, { | ||
case JsNode.BINDELEM: | ||
self.destruct(last.first(), { | ||
temp: temp, | ||
end: end, | ||
index: i | ||
name: name, | ||
index: 0 | ||
}, ret); | ||
break; | ||
} | ||
}); | ||
if(!self.inAssign[first.nid()] | ||
&& (assignexpr.parent().name() == JsNode.ASSIGNEXPR | ||
|| assignexpr.parent().name() == JsNode.INITLZ)) { | ||
return; | ||
} | ||
ret || (self.jsdc.appendBefore(';return ' + temp)); | ||
ret || (self.jsdc.appendBefore('}()')); | ||
break; | ||
} | ||
break; | ||
case JsNode.OBJLTR: | ||
if(start) { | ||
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), 'destruct6'); | ||
return; | ||
}); | ||
break; | ||
} | ||
}, | ||
expr: function(assignexpr, start, ret) { | ||
var self = this; | ||
var first; | ||
if(ret) { | ||
first = assignexpr; | ||
} | ||
else { | ||
first = assignexpr.first(); | ||
first = first.first(); | ||
} | ||
switch(first.name()) { | ||
case JsNode.ARRLTR: | ||
if(start) { | ||
first.destruct = true; | ||
!ret && this.jsdc.ignore(first, 'destruct2'); | ||
if(assignexpr.parent().name() == JsNode.ASSIGNEXPR) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().first().first().nid()]; | ||
!ret && this.jsdc.ignore(assignexpr.leaf(1), 'destruct3'); | ||
} | ||
else if(assignexpr.parent().name() == JsNode.INITLZ) { | ||
var bindpat = assignexpr.parent().prev(); | ||
if(bindpat.name() == JsNode.ARRBINDPAT | ||
|| bindpat.name() == JsNode.OBJBINDPAT) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().prev().nid()]; | ||
!ret && this.jsdc.ignore(assignexpr.leaf(1), 'destruct4'); | ||
} | ||
else if(assignexpr.parent().name() == JsNode.INITLZ) { | ||
var bindpat = assignexpr.parent().prev(); | ||
if(bindpat.name() == JsNode.ARRBINDPAT | ||
|| bindpat.name() == JsNode.OBJBINDPAT) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().prev().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct7'); | ||
} | ||
else { | ||
!ret && self.jsdc.append('function(){var '); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
self.inAssign[first.nid()] = true; | ||
} | ||
self.hash[first.nid()] = temp; | ||
!ret && self.jsdc.append(temp); | ||
} | ||
} | ||
else { | ||
@@ -550,2 +429,3 @@ !ret && self.jsdc.append('function(){var '); | ||
temp = self.jsdc.uid(); | ||
self.inAssign[first.nid()] = true; | ||
} | ||
@@ -557,123 +437,27 @@ self.hash[first.nid()] = temp; | ||
else { | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
var target = self.getName(first.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1; | ||
leaf = leaf.first(); | ||
switch(leaf.name()) { | ||
case JsNode.TOKEN: | ||
var id = leaf.token().content(); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.next()) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + id + '"];'); | ||
var init = leaf.next(); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + id + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
if(leaf.next()) { | ||
var init = leaf.next(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
case JsNode.PROPTNAME: | ||
var last = leaf.next().next().first(); | ||
var name = leaf.first().first().token().content(); | ||
switch(last.name()) { | ||
case JsNode.TOKEN: | ||
var id = last.token().content(); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + name + '"];'); | ||
var init = last.last(); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
case JsNode.PRMREXPR: | ||
var id = last.first().token().content(); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.next()) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + name + '"];'); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.append(join(last.parent()) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(join(last.parent()) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
case JsNode.ARRLTR: | ||
case JsNode.OBJLTR: | ||
self.destructExpr(last, { | ||
temp: temp, | ||
end: end, | ||
name: name, | ||
index: 0 | ||
}, ret); | ||
break; | ||
} | ||
break; | ||
} | ||
}); | ||
if(!self.inAssign[first.nid()] | ||
&& (assignexpr.parent().name() == JsNode.ASSIGNEXPR | ||
|| assignexpr.parent().name() == JsNode.INITLZ)) { | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
!ret && self.jsdc.append('function(){var '); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
self.getArray(first.leaves()).forEach(function(leaf) { | ||
if(leaf.name() == JsNode.TOKEN) { | ||
return; | ||
} | ||
!ret && self.jsdc.appendBefore(';return ' + temp); | ||
!ret && self.jsdc.appendBefore('}()'); | ||
} | ||
break; | ||
} | ||
}, | ||
destructExpr: function(node, data, ret) { | ||
var self = this; | ||
if(data === true) { | ||
node.destruct = true; | ||
self.getArray(node.leaves()).forEach(function(leaf) { | ||
if(leaf.name() == JsNode.TOKEN) { | ||
return; | ||
} | ||
leaf = leaf.first(); | ||
if(leaf.name() == JsNode.ARRLTR) { | ||
self.destructExpr(leaf, true); | ||
} | ||
}); | ||
return; | ||
} | ||
switch(node.name()) { | ||
case JsNode.ARRLTR: | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
leaf = leaf.first(); | ||
if(leaf.name() == JsNode.ARRLTR) { | ||
self.destructExpr(leaf, true); | ||
} | ||
}); | ||
} | ||
else { | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
var target = self.getArray(first.leaves()); | ||
target.forEach(function(leaf, i) { | ||
@@ -683,3 +467,3 @@ if(leaf.name() == JsNode.TOKEN) { | ||
} | ||
var end = i == target.length - 1 && data.end; | ||
var end = i == target.length - 1; | ||
leaf = leaf.first(); | ||
@@ -699,5 +483,15 @@ switch(leaf.name()) { | ||
var id = leaf.first().token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + '];'); | ||
(ret || self.jsdc).appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + join(leaf.next().next()) + (!ret && end ? '' : ';')); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '[' + i + '];'); | ||
ret.append('if(' + temp2 + '.indexOf(' + id + ')!=' + i + ')'); | ||
ret.append(id + '=' + join(leaf.next().next()) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + '];'); | ||
(ret || self.jsdc).appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + join(leaf.next().next()) + (!ret && end ? '' : ';')); | ||
} | ||
break; | ||
@@ -713,15 +507,59 @@ case JsNode.ARRLTR: | ||
}); | ||
break; | ||
case JsNode.OBJLTR: | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
if(!self.inAssign[first.nid()] | ||
&& (assignexpr.parent().name() == JsNode.ASSIGNEXPR | ||
|| assignexpr.parent().name() == JsNode.INITLZ)) { | ||
return; | ||
} | ||
ret || (self.jsdc.appendBefore(';return ' + temp)); | ||
ret || (self.jsdc.appendBefore('}()')); | ||
} | ||
break; | ||
case JsNode.OBJLTR: | ||
if(start) { | ||
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), 'destruct6'); | ||
return; | ||
} | ||
else if(assignexpr.parent().name() == JsNode.INITLZ) { | ||
var bindpat = assignexpr.parent().prev(); | ||
if(bindpat.name() == JsNode.ARRBINDPAT | ||
|| bindpat.name() == JsNode.OBJBINDPAT) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().prev().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct7'); | ||
} | ||
else { | ||
!ret && self.jsdc.append('function(){var '); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
self.inAssign[first.nid()] = true; | ||
} | ||
self.hash[first.nid()] = temp; | ||
!ret && self.jsdc.append(temp); | ||
} | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
!ret && self.jsdc.append('function(){var '); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
!ret && self.jsdc.append(temp); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
} | ||
else { | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
var target = self.getName(first.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1 && data.end; | ||
var end = i == target.length - 1; | ||
leaf = leaf.first(); | ||
@@ -731,8 +569,19 @@ switch(leaf.name()) { | ||
var id = leaf.token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
if(leaf.next()) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.next()) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + id + '"];'); | ||
var init = leaf.next(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + id + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
if(leaf.next()) { | ||
var init = leaf.next(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -745,18 +594,39 @@ case JsNode.PROPTNAME: | ||
var id = last.token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + name + '"];'); | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
case JsNode.PRMREXPR: | ||
var id = last.first().token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(join(last.parent()) + (!ret && end ? '' : ';')); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.next()) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + name + '"];'); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.append(join(last.parent()) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(join(last.parent()) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -776,31 +646,159 @@ case JsNode.ARRLTR: | ||
}); | ||
break; | ||
} | ||
}, | ||
getArray: function(leaves) { | ||
leaves = leaves.slice(1, leaves.length - 1); | ||
var target = []; | ||
for(var i = 0; i < leaves.length; i++) { | ||
var leaf = leaves[i]; | ||
if(!self.inAssign[first.nid()] | ||
&& (assignexpr.parent().name() == JsNode.ASSIGNEXPR | ||
|| assignexpr.parent().name() == JsNode.INITLZ)) { | ||
return; | ||
} | ||
!ret && self.jsdc.appendBefore(';return ' + temp); | ||
!ret && self.jsdc.appendBefore('}()'); | ||
} | ||
break; | ||
} | ||
}, | ||
destructExpr: function(node, data, ret) { | ||
var self = this; | ||
if(data === true) { | ||
node.destruct = true; | ||
self.getArray(node.leaves()).forEach(function(leaf) { | ||
if(leaf.name() == JsNode.TOKEN) { | ||
if(!leaf.prev() | ||
|| leaf.prev().name() == JsNode.TOKEN) { | ||
target.push(leaf); | ||
return; | ||
} | ||
leaf = leaf.first(); | ||
if(leaf.name() == JsNode.ARRLTR) { | ||
self.destructExpr(leaf, true); | ||
} | ||
}); | ||
return; | ||
} | ||
switch(node.name()) { | ||
case JsNode.ARRLTR: | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
target.forEach(function(leaf, i) { | ||
if(leaf.name() == JsNode.TOKEN) { | ||
return; | ||
} | ||
var end = i == target.length - 1 && data.end; | ||
leaf = leaf.first(); | ||
switch(leaf.name()) { | ||
case JsNode.TOKEN: | ||
if(leaf.token().content() == '...') { | ||
var id = leaf.next().first().token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
else { | ||
var id = leaf.token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.PRMREXPR: | ||
var id = leaf.first().token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + '];'); | ||
(ret || self.jsdc).appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + join(leaf.next().next()) + (!ret && end ? '' : ';')); | ||
break; | ||
case JsNode.ARRLTR: | ||
case JsNode.OBJLTR: | ||
self.destructExpr(leaf, { | ||
temp: temp, | ||
end: end, | ||
index: i | ||
}, ret); | ||
} | ||
}); | ||
break; | ||
case JsNode.OBJLTR: | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1 && data.end; | ||
leaf = leaf.first(); | ||
switch(leaf.name()) { | ||
case JsNode.TOKEN: | ||
var id = leaf.token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
if(leaf.next()) { | ||
var init = leaf.next(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.PROPTNAME: | ||
var last = leaf.next().next().first(); | ||
var name = leaf.first().first().token().content(); | ||
switch(last.name()) { | ||
case JsNode.TOKEN: | ||
var id = last.token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.PRMREXPR: | ||
var id = last.first().token().content(); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(join(last.parent()) + (!ret && end ? '' : ';')); | ||
} | ||
break; | ||
case JsNode.ARRLTR: | ||
case JsNode.OBJLTR: | ||
self.destructExpr(last, { | ||
temp: temp, | ||
end: end, | ||
name: name, | ||
index: 0 | ||
}, ret); | ||
break; | ||
} | ||
break; | ||
} | ||
}); | ||
break; | ||
} | ||
}, | ||
getArray: function(leaves) { | ||
leaves = leaves.slice(1, leaves.length - 1); | ||
var target = []; | ||
for(var i = 0; i < leaves.length; i++) { | ||
var leaf = leaves[i]; | ||
if(leaf.name() == JsNode.TOKEN) { | ||
if(!leaf.prev() | ||
|| leaf.prev().name() == JsNode.TOKEN) { | ||
target.push(leaf); | ||
} | ||
} | ||
return target; | ||
}, | ||
getName: function(leaves) { | ||
return leaves.filter(function(leaf, i) { | ||
return i % 2 == 1 && i != leaves.length - 1; | ||
}); | ||
else { | ||
target.push(leaf); | ||
} | ||
} | ||
}); | ||
module.exports = Destruct; | ||
return target; | ||
}, | ||
getName: function(leaves) { | ||
return leaves.filter(function(leaf, i) { | ||
return i % 2 == 1 && i != leaves.length - 1; | ||
}); | ||
} | ||
}); | ||
module.exports = Destruct; | ||
}); |
@@ -1,55 +0,53 @@ | ||
define(function(require, exports, module) { | ||
var events = {}; | ||
// Bind event | ||
exports.on = function(name, callback) { | ||
var list = events[name] || (events[name] = []); | ||
list.push(callback); | ||
define(function(require, exports, module){var events = {}; | ||
// Bind event | ||
exports.on = function(name, callback) { | ||
var list = events[name] || (events[name] = []); | ||
list.push(callback); | ||
return exports; | ||
} | ||
// Remove event. If `callback` is undefined, remove all callbacks for the | ||
// event. If `event` and `callback` are both undefined, remove all callbacks | ||
// for all events | ||
exports.off = function(name, callback) { | ||
// Remove *all* events | ||
if(!(name || callback)) { | ||
events = {}; | ||
return exports; | ||
} | ||
// Remove event. If `callback` is undefined, remove all callbacks for the | ||
// event. If `event` and `callback` are both undefined, remove all callbacks | ||
// for all events | ||
exports.off = function(name, callback) { | ||
// Remove *all* events | ||
if(!(name || callback)) { | ||
events = {}; | ||
return exports; | ||
} | ||
var list = events[name]; | ||
if(list) { | ||
if(callback) { | ||
for(var i = list.length - 1; i >= 0; i--) { | ||
if(list[i] === callback) { | ||
list.splice(i, 1); | ||
} | ||
var list = events[name]; | ||
if(list) { | ||
if(callback) { | ||
for(var i = list.length - 1; i >= 0; i--) { | ||
if(list[i] === callback) { | ||
list.splice(i, 1); | ||
} | ||
} | ||
else { | ||
delete events[name]; | ||
} | ||
} | ||
return exports; | ||
else { | ||
delete events[name]; | ||
} | ||
} | ||
// Emit event, firing all bound callbacks. Callbacks receive the same | ||
// arguments as `emit` does, apart from the event name | ||
exports.emit = function(name, data) { | ||
var list = events[name]; | ||
if(list) { | ||
// Copy callback lists to prevent modification | ||
list = list.slice(); | ||
// Execute event callbacks, use index because it's the faster. | ||
for(var i = 0, len = list.length; i < len; i++) { | ||
list[i].apply(exports, data); | ||
} | ||
return exports; | ||
} | ||
// Emit event, firing all bound callbacks. Callbacks receive the same | ||
// arguments as `emit` does, apart from the event name | ||
exports.emit = function(name, data) { | ||
var list = events[name]; | ||
if(list) { | ||
// Copy callback lists to prevent modification | ||
list = list.slice(); | ||
// Execute event callbacks, use index because it's the faster. | ||
for(var i = 0, len = list.length; i < len; i++) { | ||
list[i].apply(exports, data); | ||
} | ||
return exports; | ||
} | ||
}); | ||
return exports; | ||
}}); |
412
web/Forof.js
@@ -1,193 +0,233 @@ | ||
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 Forof = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.pos = {}; | ||
this.destruct = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
//有可能被Generator中改写过 | ||
if(node.gen) { | ||
return; | ||
} | ||
if(start) { | ||
if(node.first().token().content() == 'for') { | ||
var of = node.leaf(3); | ||
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 Forof = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.pos = {}; | ||
this.destruct = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
//有可能被Generator中改写过 | ||
if(node.gen) { | ||
return; | ||
} | ||
if(start) { | ||
if(node.first().token().content() == 'for') { | ||
var of = node.leaf(3); | ||
if(of.name() == JsNode.TOKEN | ||
&& of.token().content() == 'of') { | ||
this.pos[node.nid()] = 3; | ||
//存放临时id供block首改写引用 | ||
var leaf = node.leaf(2); | ||
if(leaf.name() == JsNode.PRMREXPR | ||
&& leaf.size() == 1 | ||
&& [JsNode.OBJLTR, JsNode.ARRLTR].indexOf(leaf.first().name()) > -1) { | ||
this.destruct[node.nid()] = leaf.first(); | ||
this.hash[node.nid()] = this.getLast(leaf.first()); | ||
this.jsdc.ignore(leaf, 'forof1'); | ||
} | ||
else { | ||
this.hash[node.nid()] = true; | ||
} | ||
this.jsdc.ignore(of, 'forof2'); | ||
} | ||
//for(var forbind of中为4 | ||
else { | ||
of = node.leaf(4); | ||
if(of.name() == JsNode.TOKEN | ||
&& of.token().content() == 'of') { | ||
this.pos[node.nid()] = 3; | ||
this.pos[node.nid()] = 4; | ||
this.destruct[node.nid()] = node.leaf(3); | ||
//存放临时id供block首改写引用 | ||
var leaf = node.leaf(2); | ||
if(leaf.name() == JsNode.PRMREXPR | ||
&& leaf.size() == 1 | ||
&& [JsNode.OBJLTR, JsNode.ARRLTR].indexOf(leaf.first().name()) > -1) { | ||
this.destruct[node.nid()] = leaf.first(); | ||
this.hash[node.nid()] = this.getLast(leaf.first()); | ||
this.jsdc.ignore(leaf, 'forof1'); | ||
} | ||
else { | ||
this.hash[node.nid()] = true; | ||
} | ||
this.jsdc.ignore(of, 'forof2'); | ||
this.hash[node.nid()] = this.getLast(node.leaf(3)); | ||
this.jsdc.ignore(of, 'forof3'); | ||
this.jsdc.ignore(node.leaf(3), 'forof4'); | ||
} | ||
//for(var forbind of中为4 | ||
else { | ||
of = node.leaf(4); | ||
if(of.name() == JsNode.TOKEN | ||
&& of.token().content() == 'of') { | ||
this.pos[node.nid()] = 4; | ||
this.destruct[node.nid()] = node.leaf(3); | ||
//存放临时id供block首改写引用 | ||
this.hash[node.nid()] = this.getLast(node.leaf(3)); | ||
this.jsdc.ignore(of, 'forof3'); | ||
this.jsdc.ignore(node.leaf(3), 'forof4'); | ||
} | ||
} | ||
} | ||
} | ||
else if(this.hash.hasOwnProperty(node.nid())) { | ||
var last = node.last(); | ||
if(last.name() != JsNode.BLOCKSTMT) { | ||
//}闭合 | ||
this.jsdc.appendBefore('}'); | ||
} | ||
} | ||
else if(this.hash.hasOwnProperty(node.nid())) { | ||
var last = node.last(); | ||
if(last.name() != JsNode.BLOCKSTMT) { | ||
//}闭合 | ||
this.jsdc.appendBefore('}'); | ||
} | ||
}, | ||
of: function(node) { | ||
var parent = node.parent(); | ||
if(parent.name() == JsNode.ITERSTMT | ||
&& this.hash.hasOwnProperty(parent.nid())) { | ||
} | ||
}, | ||
of: function(node) { | ||
var parent = node.parent(); | ||
if(parent.name() == JsNode.ITERSTMT | ||
&& this.hash.hasOwnProperty(parent.nid())) { | ||
if(typeof this.hash[parent.nid()] == 'string') { | ||
this.jsdc.append(this.hash[parent.nid()]); | ||
} | ||
this.jsdc.append('='); | ||
} | ||
}, | ||
prts: function(node, start) { | ||
var parent = node.parent(); | ||
if(parent.name() == JsNode.ITERSTMT | ||
&& this.hash.hasOwnProperty(parent.nid())) { | ||
if(start) { | ||
this.jsdc.append('.next();!'); | ||
var k; | ||
if(typeof this.hash[parent.nid()] == 'string') { | ||
this.jsdc.append(this.hash[parent.nid()]); | ||
k = this.hash[parent.nid()]; | ||
} | ||
this.jsdc.append('='); | ||
} | ||
}, | ||
prts: function(node, start) { | ||
var parent = node.parent(); | ||
if(parent.name() == JsNode.ITERSTMT | ||
&& this.hash.hasOwnProperty(parent.nid())) { | ||
if(start) { | ||
this.jsdc.append('.next();!'); | ||
var k; | ||
if(typeof this.hash[parent.nid()] == 'string') { | ||
k = this.hash[parent.nid()]; | ||
else { | ||
k = parent.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr或destruct | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
} | ||
else { | ||
k = parent.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr或destruct | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
} | ||
else { | ||
k = join(k); | ||
} | ||
k = join(k); | ||
} | ||
var v = join(parent.leaf(this.pos[parent.nid()]+1)); | ||
this.jsdc.append(k + '.done;'); | ||
this.jsdc.append(k + '=' + v + '.next()'); | ||
} | ||
else { | ||
//for of的语句如果省略{}则加上 | ||
var last = parent.last(); | ||
if(last.name() != JsNode.BLOCKSTMT) { | ||
this.jsdc.appendBefore('{'); | ||
this.assign(parent); | ||
} | ||
var v = join(parent.leaf(this.pos[parent.nid()]+1)); | ||
this.jsdc.append(k + '.done;'); | ||
this.jsdc.append(k + '=' + v + '.next()'); | ||
} | ||
else { | ||
//for of的语句如果省略{}则加上 | ||
var last = parent.last(); | ||
if(last.name() != JsNode.BLOCKSTMT) { | ||
this.jsdc.appendBefore('{'); | ||
this.assign(parent); | ||
} | ||
} | ||
}, | ||
block: function(node) { | ||
var parent = node.parent(); | ||
if(parent.name() == JsNode.BLOCK) { | ||
} | ||
}, | ||
block: function(node) { | ||
var parent = node.parent(); | ||
if(parent.name() == JsNode.BLOCK) { | ||
parent = parent.parent(); | ||
if(parent.name() == JsNode.BLOCKSTMT) { | ||
parent = parent.parent(); | ||
if(parent.name() == JsNode.BLOCKSTMT) { | ||
parent = parent.parent(); | ||
if(parent.name() == JsNode.ITERSTMT | ||
&& this.hash.hasOwnProperty(parent.nid())) { | ||
this.assign(parent); | ||
} | ||
if(parent.name() == JsNode.ITERSTMT | ||
&& this.hash.hasOwnProperty(parent.nid())) { | ||
this.assign(parent); | ||
} | ||
} | ||
}, | ||
assign: function(node, ret) { | ||
var k; | ||
if(ret) { | ||
k = ret.o; | ||
} | ||
}, | ||
assign: function(node, ret) { | ||
var k; | ||
if(ret) { | ||
k = ret.o; | ||
} | ||
else if(typeof this.hash[node.nid()] == 'string') { | ||
k = this.hash[node.nid()]; | ||
} | ||
else { | ||
k = node.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr或destruct | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
} | ||
else if(typeof this.hash[node.nid()] == 'string') { | ||
k = this.hash[node.nid()]; | ||
else { | ||
k = join(k); | ||
} | ||
} | ||
if(ret) { | ||
if(ret.pos == 4) { | ||
this.jsdc.destruct.parse(node, true, ret); | ||
this.jsdc.destruct.parse(node, false, ret); | ||
} | ||
else { | ||
k = node.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr或destruct | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
this.jsdc.destruct.expr(node, true, ret); | ||
this.jsdc.destruct.expr(node, false, ret); | ||
} | ||
return ret.s; | ||
} | ||
else { | ||
this.jsdc.append(k + '=' + k + '.value;'); | ||
if(this.destruct.hasOwnProperty(node.nid())) { | ||
var ret2 = { | ||
o: k, | ||
s: '', | ||
append: function(s) { | ||
this.s += s; | ||
}, | ||
appendBefore: function(s) { | ||
this.s += s; | ||
} | ||
}; | ||
if(this.pos[node.nid()] == 4) { | ||
this.jsdc.destruct.parse(this.destruct[node.nid()], true, ret2); | ||
this.jsdc.destruct.parse(this.destruct[node.nid()], false, ret2); | ||
} | ||
else { | ||
k = join(k); | ||
this.jsdc.destruct.expr(this.destruct[node.nid()], true, ret2); | ||
this.jsdc.destruct.expr(this.destruct[node.nid()], false, ret2); | ||
} | ||
this.jsdc.append(ret2.s); | ||
} | ||
if(ret) { | ||
if(ret.pos == 4) { | ||
this.jsdc.destruct.parse(node, true, ret); | ||
this.jsdc.destruct.parse(node, false, ret); | ||
} | ||
}, | ||
getLast: function(node) { | ||
if(node.name() == JsNode.ARRLTR | ||
|| node.name() == JsNode.ARRBINDPAT) { | ||
return this.getArrLast(node); | ||
} | ||
else { | ||
return this.getObjLast(node); | ||
} | ||
}, | ||
getArrLast: function(node) { | ||
for(var leaves = node.leaves(), i = leaves.length - 2; i > 0; i--) { | ||
var temp = leaves[i]; | ||
var s = temp.name(); | ||
if(s == JsNode.SINGLENAME | ||
|| s == JsNode.ASSIGNEXPR) { | ||
return temp.first().first().token().content(); | ||
} | ||
else if(s == JsNode.PRMREXPR) { | ||
temp = temp.first(); | ||
s = temp.name(); | ||
if(s == JsNode.TOKEN) { | ||
return temp.token().content(); | ||
} | ||
else { | ||
this.jsdc.destruct.expr(node, true, ret); | ||
this.jsdc.destruct.expr(node, false, ret); | ||
return this.getLast(temp); | ||
} | ||
return ret.s; | ||
} | ||
else { | ||
this.jsdc.append(k + '=' + k + '.value;'); | ||
if(this.destruct.hasOwnProperty(node.nid())) { | ||
var ret2 = { | ||
o: k, | ||
s: '', | ||
append: function(s) { | ||
this.s += s; | ||
}, | ||
appendBefore: function(s) { | ||
this.s += s; | ||
} | ||
}; | ||
if(this.pos[node.nid()] == 4) { | ||
this.jsdc.destruct.parse(this.destruct[node.nid()], true, ret2); | ||
this.jsdc.destruct.parse(this.destruct[node.nid()], false, ret2); | ||
else if(s == JsNode.BINDELEM) { | ||
return this.getLast(temp.first()); | ||
} | ||
} | ||
}, | ||
getObjLast: function(node) { | ||
for(var leaves = node.leaves(), i = leaves.length - 2; i > 0; i--) { | ||
var temp = leaves[i]; | ||
var s = temp.name(); | ||
if(s == JsNode.BINDPROPT) { | ||
leaves = temp.leaves(); | ||
if(leaves.length < 3) { | ||
s = leaves[0].name(); | ||
return leaves[0].first().first().token().content(); | ||
} | ||
else { | ||
temp = leaves[2]; | ||
s = temp.name(); | ||
if(s == JsNode.SINGLENAME) { | ||
return temp.first().first().token().content(); | ||
} | ||
else { | ||
this.jsdc.destruct.expr(this.destruct[node.nid()], true, ret2); | ||
this.jsdc.destruct.expr(this.destruct[node.nid()], false, ret2); | ||
else if(s == JsNode.BINDELEM) { | ||
return this.getLast(temp.first()); | ||
} | ||
this.jsdc.append(ret2.s); | ||
} | ||
} | ||
}, | ||
getLast: function(node) { | ||
if(node.name() == JsNode.ARRLTR | ||
|| node.name() == JsNode.ARRBINDPAT) { | ||
return this.getArrLast(node); | ||
} | ||
else { | ||
return this.getObjLast(node); | ||
} | ||
}, | ||
getArrLast: function(node) { | ||
for(var leaves = node.leaves(), i = leaves.length - 2; i > 0; i--) { | ||
var temp = leaves[i]; | ||
var s = temp.name(); | ||
if(s == JsNode.SINGLENAME | ||
|| s == JsNode.ASSIGNEXPR) { | ||
return temp.first().first().token().content(); | ||
else if(s == JsNode.PROPTDEF) { | ||
leaves = temp.leaves(); | ||
if(leaves.length < 3) { | ||
return leaves[0].token().content(); | ||
} | ||
else if(s == JsNode.PRMREXPR) { | ||
temp = temp.first(); | ||
else { | ||
temp = leaves[2].first(); | ||
s = temp.name(); | ||
@@ -197,2 +237,5 @@ if(s == JsNode.TOKEN) { | ||
} | ||
else if(s == JsNode.PRMREXPR) { | ||
return temp.first().token().content(); | ||
} | ||
else { | ||
@@ -202,52 +245,7 @@ return this.getLast(temp); | ||
} | ||
else if(s == JsNode.BINDELEM) { | ||
return this.getLast(temp.first()); | ||
} | ||
} | ||
}, | ||
getObjLast: function(node) { | ||
for(var leaves = node.leaves(), i = leaves.length - 2; i > 0; i--) { | ||
var temp = leaves[i]; | ||
var s = temp.name(); | ||
if(s == JsNode.BINDPROPT) { | ||
leaves = temp.leaves(); | ||
if(leaves.length < 3) { | ||
s = leaves[0].name(); | ||
return leaves[0].first().first().token().content(); | ||
} | ||
else { | ||
temp = leaves[2]; | ||
s = temp.name(); | ||
if(s == JsNode.SINGLENAME) { | ||
return temp.first().first().token().content(); | ||
} | ||
else if(s == JsNode.BINDELEM) { | ||
return this.getLast(temp.first()); | ||
} | ||
} | ||
} | ||
else if(s == JsNode.PROPTDEF) { | ||
leaves = temp.leaves(); | ||
if(leaves.length < 3) { | ||
return leaves[0].token().content(); | ||
} | ||
else { | ||
temp = leaves[2].first(); | ||
s = temp.name(); | ||
if(s == JsNode.TOKEN) { | ||
return temp.token().content(); | ||
} | ||
else if(s == JsNode.PRMREXPR) { | ||
return temp.first().token().content(); | ||
} | ||
else { | ||
return this.getLast(temp); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
module.exports = Forof; | ||
}); | ||
} | ||
}); | ||
module.exports = Forof;}); |
1496
web/Generator.js
@@ -1,390 +0,414 @@ | ||
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'); | ||
var Generator = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.star = {}; | ||
this.stmt = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
var self = this; | ||
if(start) { | ||
self.jsdc.ignore(node.leaf(1), 'gen1'); | ||
var token = node.leaf(2).first().token(); | ||
//有可能被scope前置过 | ||
var hasPre = token.ignore; | ||
self.jsdc.ignore(token, 'gen2'); | ||
if(!hasPre) { | ||
self.jsdc.append('var '); | ||
self.jsdc.append(node.leaf(2).first().token().content()); | ||
self.jsdc.append('='); | ||
} | ||
self.gen(node); | ||
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'); | ||
var Generator = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.star = {}; | ||
this.stmt = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
var self = this; | ||
if(start) { | ||
self.jsdc.ignore(node.leaf(1), 'gen1'); | ||
var token = node.leaf(2).first().token(); | ||
//有可能被scope前置过 | ||
var hasPre = token.ignore; | ||
self.jsdc.ignore(token, 'gen2'); | ||
if(!hasPre) { | ||
self.jsdc.append('var '); | ||
self.jsdc.append(node.leaf(2).first().token().content()); | ||
self.jsdc.append('='); | ||
} | ||
self.gen(node); | ||
} | ||
else { | ||
self.jsdc.appendBefore('}();'); | ||
} | ||
}, | ||
expr: function(node, start) { | ||
var self = this; | ||
if(start) { | ||
self.jsdc.ignore(node.first(), 'gen3'); | ||
self.jsdc.ignore(node.leaf(1), 'gen4'); | ||
if(node.leaf(2).name() == JsNode.BINDID) { | ||
self.jsdc.ignore(node.leaf(2), 'gen5'); | ||
} | ||
self.gen(node); | ||
} | ||
else { | ||
self.jsdc.appendBefore('}()'); | ||
} | ||
}, | ||
gen: function(node) { | ||
var self = this; | ||
self.jsdc.ignore(node.first(), 'gen6'); | ||
var state; | ||
var res = self.count(node.last().prev(), node); | ||
var count = res.count; | ||
if(count) { | ||
state = self.jsdc.uid(); | ||
} | ||
var temp = self.jsdc.uid(); | ||
var param = node.leaf(4).first(); | ||
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 { | ||
self.jsdc.appendBefore('}();'); | ||
param = ''; | ||
} | ||
}, | ||
expr: function(node, start) { | ||
var self = this; | ||
if(start) { | ||
self.jsdc.ignore(node.first(), 'gen3'); | ||
self.jsdc.ignore(node.leaf(1), 'gen4'); | ||
if(node.leaf(2).name() == JsNode.BINDID) { | ||
self.jsdc.ignore(node.leaf(2), 'gen5'); | ||
} | ||
self.gen(node); | ||
} | ||
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, | ||
param: param, | ||
last: null, | ||
yield: [], | ||
pre: res.pre | ||
}; | ||
self.jsdc.append('function(){'); | ||
if(o.count) { | ||
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); | ||
}, | ||
yield: function(node, start) { | ||
var self = this; | ||
var top = self.closest(node); | ||
var o = self.hash[top.nid()]; | ||
if(start) { | ||
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; | ||
} | ||
//加上状态变更 | ||
o.index++; | ||
self.jsdc.append(o.state + '=' + ++o.index2 + ';'); | ||
//yield * | ||
if(node.size() > 2 | ||
&& node.leaf(1).name() == JsNode.TOKEN | ||
&& node.leaf(1).token().content() == '*') { | ||
self.jsdc.ignore(node.leaf(1), 'gen8'); | ||
var temp = this.star[node.nid()] = self.jsdc.uid(); | ||
self.jsdc.append('var ' + temp + '='); | ||
} | ||
else { | ||
self.jsdc.appendBefore('}()'); | ||
self.jsdc.append('return{value:'); | ||
//无yield返回,则附上undefined | ||
if(node.size() == 1) { | ||
self.jsdc.append('void 0'); | ||
} | ||
} | ||
}, | ||
gen: function(node) { | ||
var self = this; | ||
self.jsdc.ignore(node.first(), 'gen6'); | ||
var state; | ||
var res = self.count(node.last().prev(), node); | ||
var count = res.count; | ||
if(count) { | ||
state = self.jsdc.uid(); | ||
} | ||
else { | ||
//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: self.jsdc.i, | ||
star: temp | ||
}); | ||
} | ||
var temp = self.jsdc.uid(); | ||
var param = node.leaf(4).first(); | ||
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 { | ||
var belong = self.belong(node); | ||
if(belong.length) { | ||
var done = belong.map(function(o) { | ||
return o.done || 0; | ||
}); | ||
done.push((o.index == o.count) ? 1 : 0); | ||
done.push((o.return) ? 0 : 1); | ||
if(done.indexOf(0) > -1) { | ||
done = [false]; | ||
} | ||
var hash = {}; | ||
done = done.filter(function(o) { | ||
var res = !hash.hasOwnProperty(o); | ||
hash[o] = true; | ||
return res; | ||
}); | ||
self.jsdc.appendBefore(',done:' + done.join('&&') + '}'); | ||
} | ||
else { | ||
param = ''; | ||
self.jsdc.appendBefore(',done:' + (o.index == o.count && !o.return) + '}'); | ||
} | ||
o.yield.push({ | ||
i: self.jsdc.i | ||
}); | ||
} | ||
else if(param.name() == JsNode.SINGLENAME) { | ||
param = param.first().first().token().content(); | ||
self.ignoreNext(node, ';'); | ||
if(o.index < o.count) { | ||
self.jsdc.appendBefore(';case ' + o.index2 + ':'); | ||
} | ||
else if(param.name() == JsNode.BINDREST) { | ||
param = param.last().first().token().content() + '[0]'; | ||
else { | ||
self.jsdc.appendBefore(';case ' + o.index2 + ':'); | ||
} | ||
var o = self.hash[node.nid()] = { | ||
state: state, | ||
index: 0, | ||
index2: 0, | ||
count: count, | ||
return: ret, | ||
if: 0, | ||
temp: temp, | ||
param: param, | ||
last: null, | ||
yield: [], | ||
pre: res.pre | ||
}; | ||
self.jsdc.append('function(){'); | ||
if(o.count) { | ||
self.jsdc.append('var ' + state + '=0;'); | ||
//有赋值需要先赋值 | ||
if(o.last) { | ||
self.jsdc.appendBefore(o.last + '=' + o.param + ';'); | ||
} | ||
self.jsdc.append('return function(){return{next:' + temp + '}};'); | ||
o.pos = self.jsdc.res.length; | ||
self.jsdc.append('function ' + temp); | ||
}, | ||
yield: function(node, start) { | ||
var self = this; | ||
var top = self.closest(node); | ||
var o = self.hash[top.nid()]; | ||
o.last = null; | ||
} | ||
}, | ||
body: function(node, start) { | ||
var top = node.parent(); | ||
if(top.name() == JsNode.GENDECL) { | ||
var o = this.hash[top.nid()]; | ||
if(start) { | ||
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; | ||
if(o.count) { | ||
this.jsdc.append('while(1){switch(' + o.state + '){case 0:'); | ||
} | ||
//加上状态变更 | ||
o.index++; | ||
self.jsdc.append(o.state + '=' + ++o.index2 + ';'); | ||
//yield * | ||
if(node.size() > 2 | ||
&& node.leaf(1).name() == JsNode.TOKEN | ||
&& node.leaf(1).token().content() == '*') { | ||
self.jsdc.ignore(node.leaf(1), 'gen8'); | ||
var temp = this.star[node.nid()] = self.jsdc.uid(); | ||
self.jsdc.append('var ' + temp + '='); | ||
} | ||
else { | ||
self.jsdc.append('return{value:'); | ||
//无yield返回,则附上undefined | ||
if(node.size() == 1) { | ||
self.jsdc.append('void 0'); | ||
} | ||
} | ||
} | ||
else { | ||
//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: self.jsdc.i, | ||
star: temp | ||
}); | ||
} | ||
else { | ||
var belong = self.belong(node); | ||
if(belong.length) { | ||
var done = belong.map(function(o) { | ||
return o.done || 0; | ||
}); | ||
done.push((o.index == o.count) ? 1 : 0); | ||
done.push((o.return) ? 0 : 1); | ||
if(done.indexOf(0) > -1) { | ||
done = [false]; | ||
} | ||
var hash = {}; | ||
done = done.filter(function(o) { | ||
var res = !hash.hasOwnProperty(o); | ||
hash[o] = true; | ||
return res; | ||
}); | ||
self.jsdc.appendBefore(',done:' + done.join('&&') + '}'); | ||
else if(!o.return) { | ||
if(o.count) { | ||
if(!this.jsdc.endsWith(';') | ||
&& !this.jsdc.endsWith(':') | ||
&& !this.jsdc.endsWith('{') | ||
&& !this.jsdc.endsWith('}') | ||
&& !this.jsdc.endsWith('\n')) { | ||
this.jsdc.appendBefore(';'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(',done:' + (o.index == o.count && !o.return) + '}'); | ||
} | ||
o.yield.push({ | ||
i: self.jsdc.i | ||
}); | ||
this.jsdc.appendBefore(o.state + '=-1'); | ||
this.jsdc.appendBefore(';default:return{done:true}}}'); | ||
} | ||
self.ignoreNext(node, ';'); | ||
if(o.index < o.count) { | ||
self.jsdc.appendBefore(';case ' + o.index2 + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';case ' + o.index2 + ':'); | ||
this.jsdc.appendBefore(';return{done:true}'); | ||
} | ||
//有赋值需要先赋值 | ||
if(o.last) { | ||
self.jsdc.appendBefore(o.last + '=' + o.param + ';'); | ||
} | ||
o.last = null; | ||
} | ||
}, | ||
body: function(node, start) { | ||
var top = node.parent(); | ||
if(top.name() == JsNode.GENDECL) { | ||
var o = this.hash[top.nid()]; | ||
if(start) { | ||
if(o.count) { | ||
this.jsdc.append('while(1){switch(' + o.state + '){case 0:'); | ||
} | ||
}, | ||
closest: function(node) { | ||
var parent = node; | ||
while(parent = parent.parent()) { | ||
if(parent.name() == JsNode.GENDECL | ||
|| parent.name() == JsNode.GENEXPR) { | ||
return parent; | ||
} | ||
} | ||
}, | ||
prevar: function(varstmt) { | ||
var self = this; | ||
var top = varstmt.gen; | ||
if(top) { | ||
self.jsdc.ignore(varstmt.first(), 'gen9'); | ||
varstmt.leaves().forEach(function(leaf, i) { | ||
if(i % 2 == 1) { | ||
var first = leaf.first(); | ||
switch(first.name()) { | ||
case JsNode.BINDID: | ||
self.jsdc.insert('var ' + first.first().token().content() + ';', self.hash[top.nid()].pos); | ||
break; | ||
case JsNode.ARRBINDPAT: | ||
case JsNode.OBJBINDPAT: | ||
//destruct需忽略前后可能的,再改为; var也需忽略 | ||
self.jsdc.ignore(leaf.prev(), 'gen40'); | ||
var next = leaf.next(); | ||
if(next.token().content() == ',') { | ||
self.jsdc.ignore(next, 'gen41'); | ||
} | ||
var ids = self.jsdc.destruct.getIds(first); | ||
ids.forEach(function(id) { | ||
self.jsdc.insert('var ' + id + ';', self.hash[top.nid()].pos); | ||
}); | ||
break; | ||
} | ||
} | ||
else if(!o.return) { | ||
if(o.count) { | ||
if(!this.jsdc.endsWith(';') | ||
&& !this.jsdc.endsWith(':') | ||
&& !this.jsdc.endsWith('{') | ||
&& !this.jsdc.endsWith('}') | ||
&& !this.jsdc.endsWith('\n')) { | ||
this.jsdc.appendBefore(';'); | ||
}); | ||
} | ||
}, | ||
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()]; | ||
if(o.count) { | ||
self.jsdc.appendBefore(';' + o.state + '=-1;default:'); | ||
} | ||
} | ||
this.jsdc.appendBefore(o.state + '=-1'); | ||
this.jsdc.appendBefore(';default:return{done:true}}}'); | ||
}); | ||
//无return内容分开侦听 | ||
if(node.leaf(1).name() == JsNode.TOKEN) { | ||
eventbus.on(node.leaf(0).nid(), function(node, start) { | ||
if(!start) { | ||
self.jsdc.append('{value:void 0'); | ||
} | ||
}); | ||
eventbus.on(node.nid(), function(node, start) { | ||
if(!start) { | ||
self.jsdc.appendBefore(',done:true}'); | ||
} | ||
}); | ||
} | ||
//有则侦听内容 | ||
else { | ||
this.jsdc.appendBefore(';return{done:true}'); | ||
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; | ||
} | ||
}, | ||
closest: function(node) { | ||
var parent = node; | ||
while(parent = parent.parent()) { | ||
if(parent.name() == JsNode.GENDECL | ||
|| parent.name() == JsNode.GENEXPR) { | ||
return parent; | ||
} | ||
} | ||
}, | ||
prevar: function(varstmt) { | ||
var self = this; | ||
var top = varstmt.gen; | ||
if(top) { | ||
self.jsdc.ignore(varstmt.first(), 'gen9'); | ||
varstmt.leaves().forEach(function(leaf, i) { | ||
if(i % 2 == 1) { | ||
var first = leaf.first(); | ||
switch(first.name()) { | ||
case JsNode.BINDID: | ||
self.jsdc.insert('var ' + first.first().token().content() + ';', self.hash[top.nid()].pos); | ||
break; | ||
case JsNode.ARRBINDPAT: | ||
case JsNode.OBJBINDPAT: | ||
//destruct需忽略前后可能的,再改为; var也需忽略 | ||
self.jsdc.ignore(leaf.prev(), 'gen40'); | ||
var next = leaf.next(); | ||
if(next.token().content() == ',') { | ||
self.jsdc.ignore(next, 'gen41'); | ||
} | ||
var ids = self.jsdc.destruct.getIds(first); | ||
ids.forEach(function(id) { | ||
self.jsdc.insert('var ' + id + ';', self.hash[top.nid()].pos); | ||
}); | ||
break; | ||
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())) { | ||
var ifstmt = node; | ||
res.index++; | ||
var block = ifstmt.leaf(4); | ||
//改写if语句 | ||
self.jsdc.ignore(ifstmt.first(), 'gen14'); | ||
self.jsdc.ignore(ifstmt.leaf(1), 'gen15'); | ||
self.jsdc.ignore(ifstmt.leaf(2), 'gen16'); | ||
self.jsdc.ignore(ifstmt.leaf(3), 'gen17'); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen18'); | ||
self.jsdc.ignore(block.first().last(), 'gen19'); | ||
} | ||
} | ||
}); | ||
} | ||
}, | ||
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 elseTemp; | ||
var ifEndTemp; | ||
var top; | ||
var elset = block.next(); | ||
var elseblock; | ||
if(elset && elset.name() == JsNode.TOKEN) { | ||
elseblock = elset.next(); | ||
} | ||
var belong = self.belong(node); | ||
belong.forEach(function(f) { | ||
self.stmt[f.nid()] = true; | ||
res.pre = true; | ||
//if结束后的状态 | ||
eventbus.on(ifstmt.nid(), function(node, start) { | ||
if(!start) { | ||
if(!self.jsdc.endsWith(';') | ||
&& !self.jsdc.endsWith(':') | ||
&& !self.jsdc.endsWith('{') | ||
&& !self.jsdc.endsWith('}') | ||
&& !self.jsdc.endsWith('\n')) { | ||
self.jsdc.appendBefore(';'); | ||
} | ||
self.jsdc.append('case ' + ifEndTemp + ':'); | ||
} | ||
}); | ||
break; | ||
case JsNode.RETSTMT: | ||
res.return = node; | ||
eventbus.on(node.nid(), function(node, start) { | ||
//根据表达式true/false分2个state | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
var o = self.hash[top.nid()]; | ||
if(o.count) { | ||
self.jsdc.appendBefore(';' + o.state + '=-1;default:'); | ||
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 + ':'); | ||
self.jsdc.append((elseblock ? ++top.index2 : top.index2 + 1) + ';break;'); | ||
self.jsdc.append('case ' + (elseblock ? top.index2 - 1 : top.index2) + ':'); | ||
elseTemp = top.index2; | ||
ifEndTemp = ++top.index2; | ||
} | ||
else { | ||
if(!self.jsdc.endsWith(';') | ||
&& !self.jsdc.endsWith(':') | ||
&& !self.jsdc.endsWith('{') | ||
&& !self.jsdc.endsWith('}') | ||
&& !self.jsdc.endsWith('\n')) { | ||
self.jsdc.appendBefore(';'); | ||
} | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(ifEndTemp); | ||
self.jsdc.appendBefore(';break;'); | ||
} | ||
}); | ||
//无return内容分开侦听 | ||
if(node.leaf(1).name() == JsNode.TOKEN) { | ||
eventbus.on(node.leaf(0).nid(), function(node, start) { | ||
if(!start) { | ||
self.jsdc.append('{value:void 0'); | ||
} | ||
}); | ||
eventbus.on(node.nid(), function(node, start) { | ||
if(!start) { | ||
self.jsdc.appendBefore(',done:true}'); | ||
} | ||
}); | ||
} | ||
//有则侦听内容 | ||
else { | ||
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())) { | ||
var ifstmt = node; | ||
res.index++; | ||
var block = ifstmt.leaf(4); | ||
//改写if语句 | ||
self.jsdc.ignore(ifstmt.first(), 'gen14'); | ||
self.jsdc.ignore(ifstmt.leaf(1), 'gen15'); | ||
self.jsdc.ignore(ifstmt.leaf(2), 'gen16'); | ||
self.jsdc.ignore(ifstmt.leaf(3), 'gen17'); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen18'); | ||
self.jsdc.ignore(block.first().last(), 'gen19'); | ||
//else语句忽略{} | ||
if(elseblock) { | ||
self.jsdc.ignore(elset, 'gen20'); | ||
if(elseblock.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(elseblock.first().first(), 'gen21'); | ||
self.jsdc.ignore(elseblock.first().last(), 'gen22'); | ||
} | ||
var elseTemp; | ||
var ifEndTemp; | ||
var top; | ||
var elset = block.next(); | ||
var elseblock; | ||
if(elset && elset.name() == JsNode.TOKEN) { | ||
elseblock = elset.next(); | ||
} | ||
//if结束后的状态 | ||
eventbus.on(ifstmt.nid(), function(node, start) { | ||
if(!start) { | ||
if(!self.jsdc.endsWith(';') | ||
&& !self.jsdc.endsWith(':') | ||
&& !self.jsdc.endsWith('{') | ||
&& !self.jsdc.endsWith('}') | ||
&& !self.jsdc.endsWith('\n')) { | ||
self.jsdc.appendBefore(';'); | ||
} | ||
self.jsdc.append('case ' + ifEndTemp + ':'); | ||
} | ||
}); | ||
//根据表达式true/false分2个state | ||
eventbus.on(block.nid(), function(node, start) { | ||
eventbus.on(elseblock.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 + ':'); | ||
self.jsdc.append((elseblock ? ++top.index2 : top.index2 + 1) + ';break;'); | ||
self.jsdc.append('case ' + (elseblock ? top.index2 - 1 : top.index2) + ':'); | ||
elseTemp = top.index2; | ||
ifEndTemp = ++top.index2; | ||
self.jsdc.append('case ' + elseTemp + ':'); | ||
} | ||
else { | ||
else if(elseblock.name() != JsNode.IFSTMT | ||
&& elseblock.parent().name() != JsNode.IFSTMT) { | ||
if(!self.jsdc.endsWith(';') | ||
@@ -402,128 +426,160 @@ && !self.jsdc.endsWith(':') | ||
}); | ||
//else语句忽略{} | ||
if(elseblock) { | ||
self.jsdc.ignore(elset, 'gen20'); | ||
if(elseblock.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(elseblock.first().first(), 'gen21'); | ||
self.jsdc.ignore(elseblock.first().last(), 'gen22'); | ||
} | ||
eventbus.on(elseblock.nid(), function(node, start) { | ||
} | ||
} | ||
break; | ||
case JsNode.ITERSTMT: | ||
var itstmt = node; | ||
var first = itstmt.first(); | ||
var top; | ||
var loopTemp; | ||
var itTemp; | ||
var itEndTemp; | ||
var endTemp; | ||
switch(first.token().content()) { | ||
case 'for': | ||
var block = itstmt.last(); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen23'); | ||
self.jsdc.ignore(block.first().last(), 'gen24'); | ||
} | ||
self.jsdc.ignore(first, 'gen25'); | ||
self.jsdc.ignore(node.leaf(1), 'gen26'); | ||
self.jsdc.ignore(block.prev(), 'gen27'); | ||
//特殊的for(var forbind of语句 | ||
if(itstmt.leaf(4).name() == JsNode.TOKEN | ||
&& itstmt.leaf(4).token().content() == 'of') { | ||
self.jsdc.ignore(itstmt.leaf(2), 'gen36'); | ||
var ids = self.jsdc.destruct.getIds(itstmt.leaf(3), { arr: [] }); | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + elseTemp + ':'); | ||
ids.forEach(function(id) { | ||
self.jsdc.insert('var ' + id + ';', self.hash[nid].pos); | ||
}); | ||
} | ||
else if(elseblock.name() != JsNode.IFSTMT | ||
&& elseblock.parent().name() != JsNode.IFSTMT) { | ||
if(!self.jsdc.endsWith(';') | ||
&& !self.jsdc.endsWith(':') | ||
&& !self.jsdc.endsWith('{') | ||
&& !self.jsdc.endsWith('}') | ||
&& !self.jsdc.endsWith('\n')) { | ||
self.jsdc.appendBefore(';'); | ||
} | ||
}); | ||
var iterator; | ||
//标记使Forof类处理失效 | ||
itstmt.gen = true; | ||
var next = self.jsdc.uid(); | ||
var isDestruct = [JsNode.ARRBINDPAT, JsNode.OBJBINDPAT].indexOf(node.leaf(3).name()) > -1 ? node.leaf(3) : false; | ||
var id = isDestruct ? self.jsdc.forof.getLast(isDestruct) : join(node.leaf(3)); | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen37'); | ||
self.jsdc.ignore(node.leaf(3), 'gen38'); | ||
self.jsdc.ignore(node.leaf(4), 'gen39'); | ||
eventbus.on(itstmt.leaf(5).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
else { | ||
self.jsdc.appendBefore(',' + next); | ||
self.jsdc.appendBefore('=' + obj + '.next();'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(ifEndTemp); | ||
self.jsdc.appendBefore(';break;'); | ||
self.jsdc.appendBefore(next + '.done' + '?'); | ||
iterator = ++top.index2; | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = next + '.done'; | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + iterator + ':'); | ||
self.jsdc.append(next + '=' + obj + '.next();'); | ||
self.jsdc.append(top.state + '=' + endTemp); | ||
self.jsdc.append(';break;case ' + itTemp + ':'); | ||
self.jsdc.append(id + '=' + next + '.value;'); | ||
if(isDestruct) { | ||
var ret = { | ||
o: id, | ||
s: '', | ||
pos: 4, | ||
append: function(s) { | ||
this.s += s; | ||
}, | ||
appendBefore: function(s) { | ||
this.s += s; | ||
} | ||
}; | ||
self.jsdc.forof.assign(isDestruct, ret); | ||
self.jsdc.appendBefore(ret.s); | ||
} | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
} | ||
} | ||
break; | ||
case JsNode.ITERSTMT: | ||
var itstmt = node; | ||
var first = itstmt.first(); | ||
var top; | ||
var loopTemp; | ||
var itTemp; | ||
var itEndTemp; | ||
var endTemp; | ||
switch(first.token().content()) { | ||
case 'for': | ||
var block = itstmt.last(); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen23'); | ||
self.jsdc.ignore(block.first().last(), 'gen24'); | ||
} | ||
self.jsdc.ignore(first, 'gen25'); | ||
self.jsdc.ignore(node.leaf(1), 'gen26'); | ||
self.jsdc.ignore(block.prev(), 'gen27'); | ||
//特殊的for(var forbind of语句 | ||
if(itstmt.leaf(4).name() == JsNode.TOKEN | ||
&& itstmt.leaf(4).token().content() == 'of') { | ||
self.jsdc.ignore(itstmt.leaf(2), 'gen36'); | ||
var ids = self.jsdc.destruct.getIds(itstmt.leaf(3), { arr: [] }); | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
ids.forEach(function(id) { | ||
self.jsdc.insert('var ' + id + ';', self.hash[nid].pos); | ||
}); | ||
else { | ||
switch(itstmt.leaf(3).token().content()) { | ||
case 'in': | ||
var keys = self.jsdc.uid(); | ||
var len = self.jsdc.uid(); | ||
var index = self.jsdc.uid(); | ||
var id; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
} | ||
}); | ||
var iterator; | ||
//标记使Forof类处理失效 | ||
itstmt.gen = true; | ||
var next = self.jsdc.uid(); | ||
var isDestruct = [JsNode.ARRBINDPAT, JsNode.OBJBINDPAT].indexOf(node.leaf(3).name()) > -1 ? node.leaf(3) : false; | ||
var id = isDestruct ? self.jsdc.forof.getLast(isDestruct) : join(node.leaf(3)); | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen37'); | ||
self.jsdc.ignore(node.leaf(3), 'gen38'); | ||
self.jsdc.ignore(node.leaf(4), 'gen39'); | ||
eventbus.on(itstmt.leaf(5).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
else { | ||
self.jsdc.appendBefore(',' + next); | ||
self.jsdc.appendBefore('=' + obj + '.next();'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(next + '.done' + '?'); | ||
iterator = ++top.index2; | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = next + '.done'; | ||
id = join(node.leaf(2)); | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + iterator + ':'); | ||
self.jsdc.append(next + '=' + obj + '.next();'); | ||
self.jsdc.append(top.state + '=' + endTemp); | ||
self.jsdc.append(';break;case ' + itTemp + ':'); | ||
self.jsdc.append(id + '=' + next + '.value;'); | ||
if(isDestruct) { | ||
var ret = { | ||
o: id, | ||
s: '', | ||
pos: 4, | ||
append: function(s) { | ||
this.s += s; | ||
}, | ||
appendBefore: function(s) { | ||
this.s += s; | ||
} | ||
}; | ||
self.jsdc.forof.assign(isDestruct, ret); | ||
self.jsdc.appendBefore(ret.s); | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen28'); | ||
self.jsdc.ignore(node.leaf(3), 'gen29'); | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
else { | ||
self.jsdc.appendBefore(',' + keys + '=Object.keys('); | ||
self.jsdc.appendBefore(obj); | ||
self.jsdc.appendBefore('),' + len); | ||
self.jsdc.appendBefore('=' + keys + '.length,'); | ||
self.jsdc.appendBefore(index + '=0;'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(index + '++<' + len + '?'); | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = index + '<' + len; | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + itTemp + ':'); | ||
self.jsdc.append(id + '='); | ||
self.jsdc.append(keys + '['); | ||
self.jsdc.append(index + '];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
break; | ||
case 'of': | ||
var iterator; | ||
//标记使Forof类处理失效 | ||
itstmt.gen = true; | ||
var next = self.jsdc.uid(); | ||
var id; | ||
var isDestruct; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
} | ||
else { | ||
switch(itstmt.leaf(3).token().content()) { | ||
case 'in': | ||
var keys = self.jsdc.uid(); | ||
var len = self.jsdc.uid(); | ||
var index = self.jsdc.uid(); | ||
var id; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
if(node.leaf(2).name() == JsNode.PRMREXPR | ||
&& [JsNode.ARRLTR, JsNode.OBJLTR].indexOf(node.leaf(2).first().name()) > -1) { | ||
isDestruct = node.leaf(2).first(); | ||
id = self.jsdc.forof.getLast(isDestruct); | ||
} | ||
@@ -533,303 +589,245 @@ else { | ||
} | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen28'); | ||
self.jsdc.ignore(node.leaf(3), 'gen29'); | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen30'); | ||
self.jsdc.ignore(node.leaf(3), 'gen31'); | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
else { | ||
self.jsdc.appendBefore(',' + next); | ||
self.jsdc.appendBefore('=' + obj + '.next();'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(next + '.done' + '?'); | ||
iterator = ++top.index2; | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = next + '.done'; | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + iterator + ':'); | ||
self.jsdc.append(next + '=' + obj + '.next();'); | ||
self.jsdc.append(top.state + '=' + endTemp); | ||
self.jsdc.append(';break;case ' + itTemp + ':'); | ||
self.jsdc.append(id + '=' + next + '.value;'); | ||
if(isDestruct) { | ||
var ret = { | ||
o: id, | ||
s: '', | ||
pos: 3, | ||
append: function(s) { | ||
this.s += s; | ||
}, | ||
appendBefore: function(s) { | ||
this.s += s; | ||
} | ||
}; | ||
self.jsdc.forof.assign(isDestruct, ret); | ||
self.jsdc.appendBefore(ret.s); | ||
} | ||
else { | ||
self.jsdc.appendBefore(',' + keys + '=Object.keys('); | ||
self.jsdc.appendBefore(obj); | ||
self.jsdc.appendBefore('),' + len); | ||
self.jsdc.appendBefore('=' + keys + '.length,'); | ||
self.jsdc.appendBefore(index + '=0;'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(index + '++<' + len + '?'); | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = index + '<' + len; | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
break; | ||
default: | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
if(start) { | ||
top = self.hash[nid]; | ||
loopTemp = ++top.index2; | ||
self.jsdc.append('case ' + loopTemp + ':'); | ||
self.jsdc.append(top.state + '='); | ||
//防止优先级错误 | ||
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) { | ||
self.jsdc.append('('); | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + itTemp + ':'); | ||
self.jsdc.append(id + '='); | ||
self.jsdc.append(keys + '['); | ||
self.jsdc.append(index + '];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
break; | ||
case 'of': | ||
var iterator; | ||
//标记使Forof类处理失效 | ||
itstmt.gen = true; | ||
var next = self.jsdc.uid(); | ||
var id; | ||
var isDestruct; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
itTemp = ++top.index2; | ||
} | ||
else { | ||
if(node.leaf(2).name() == JsNode.PRMREXPR | ||
&& [JsNode.ARRLTR, JsNode.OBJLTR].indexOf(node.leaf(2).first().name()) > -1) { | ||
isDestruct = node.leaf(2).first(); | ||
id = self.jsdc.forof.getLast(isDestruct); | ||
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) { | ||
self.jsdc.appendBefore(')'); | ||
} | ||
else { | ||
id = join(node.leaf(2)); | ||
} | ||
itEndTemp = ++top.index2; | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('?' + itTemp + ':' + itEndTemp); | ||
self.jsdc.appendBefore(';break'); | ||
//供yield判断 | ||
itstmt.done = join(itstmt.leaf(4)); | ||
} | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen30'); | ||
self.jsdc.ignore(node.leaf(3), 'gen31'); | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
else { | ||
self.jsdc.appendBefore(',' + next); | ||
self.jsdc.appendBefore('=' + obj + '.next();'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(next + '.done' + '?'); | ||
iterator = ++top.index2; | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = next + '.done'; | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + iterator + ':'); | ||
self.jsdc.append(next + '=' + obj + '.next();'); | ||
self.jsdc.append(top.state + '=' + endTemp); | ||
self.jsdc.append(';break;case ' + itTemp + ':'); | ||
self.jsdc.append(id + '=' + next + '.value;'); | ||
if(isDestruct) { | ||
var ret = { | ||
o: id, | ||
s: '', | ||
pos: 3, | ||
append: function(s) { | ||
this.s += s; | ||
}, | ||
appendBefore: function(s) { | ||
this.s += s; | ||
} | ||
}; | ||
self.jsdc.forof.assign(isDestruct, ret); | ||
self.jsdc.appendBefore(ret.s); | ||
} | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
break; | ||
default: | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
if(start) { | ||
top = self.hash[nid]; | ||
loopTemp = ++top.index2; | ||
self.jsdc.append('case ' + loopTemp + ':'); | ||
self.jsdc.append(top.state + '='); | ||
//防止优先级错误 | ||
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) { | ||
self.jsdc.append('('); | ||
} | ||
itTemp = ++top.index2; | ||
} | ||
else { | ||
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) { | ||
self.jsdc.appendBefore(')'); | ||
} | ||
itEndTemp = ++top.index2; | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('?' + itTemp + ':' + itEndTemp); | ||
self.jsdc.appendBefore(';break'); | ||
//供yield判断 | ||
itstmt.done = join(itstmt.leaf(4)); | ||
} | ||
}); | ||
eventbus.on(itstmt.leaf(6).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';' + top.state + '=' + loopTemp); | ||
self.jsdc.appendBefore(';break'); | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append(';case ' + itTemp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
} | ||
}); | ||
eventbus.on(itstmt.leaf(6).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';' + top.state + '=' + loopTemp); | ||
self.jsdc.appendBefore(';break'); | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append(';case ' + itTemp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
} | ||
break; | ||
case 'while': | ||
self.jsdc.ignore(itstmt.first()); | ||
loopTemp = self.jsdc.uid(); | ||
var block = itstmt.last(); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen32'); | ||
self.jsdc.ignore(block.first().last(), 'gen33'); | ||
} | ||
break; | ||
case 'while': | ||
self.jsdc.ignore(itstmt.first()); | ||
loopTemp = self.jsdc.uid(); | ||
var block = itstmt.last(); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen32'); | ||
self.jsdc.ignore(block.first().last(), 'gen33'); | ||
} | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + loopTemp + ';'); | ||
top = self.hash[nid]; | ||
endTemp = ++top.index2; | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
self.jsdc.append(top.state + '='); | ||
} | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + loopTemp + ';'); | ||
top = self.hash[nid]; | ||
endTemp = ++top.index2; | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
self.jsdc.append(top.state + '='); | ||
}); | ||
eventbus.on(itstmt.leaf(2).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append(loopTemp + '='); | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.append('?' + itTemp + ':' + itEndTemp); | ||
self.jsdc.append(';break;case ' + itTemp + ':'); | ||
//供yield判断 | ||
itstmt.done = loopTemp; | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
break; | ||
case 'do': | ||
loopTemp = self.jsdc.uid(); | ||
//供yield判断 | ||
itstmt.done = loopTemp; | ||
self.jsdc.ignore(itstmt.first()); | ||
self.jsdc.ignore(itstmt.leaf(2)); | ||
var block = itstmt.leaf(1); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen34'); | ||
self.jsdc.ignore(block.first().last(), 'gen35'); | ||
} | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + loopTemp + ';'); | ||
} | ||
else { | ||
self.jsdc.appendBefore('?' + itTemp + ':' + itEndTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append(loopTemp + '='); | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
top = self.hash[nid]; | ||
itTemp = ++top.index2; | ||
endTemp = ++top.index2; | ||
self.jsdc.append('case ' + itTemp + ':'); | ||
} | ||
else { | ||
if(!self.jsdc.endsWith(';') | ||
&& !self.jsdc.endsWith(':') | ||
&& !self.jsdc.endsWith('{') | ||
&& !self.jsdc.endsWith('}') | ||
&& !self.jsdc.endsWith('\n')) { | ||
self.jsdc.appendBefore(';'); | ||
} | ||
}); | ||
eventbus.on(itstmt.leaf(2).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append(loopTemp + '='); | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.append('?' + itTemp + ':' + itEndTemp); | ||
self.jsdc.append(';break;case ' + itTemp + ':'); | ||
//供yield判断 | ||
itstmt.done = loopTemp; | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
break; | ||
case 'do': | ||
loopTemp = self.jsdc.uid(); | ||
//供yield判断 | ||
itstmt.done = loopTemp; | ||
self.jsdc.ignore(itstmt.first()); | ||
self.jsdc.ignore(itstmt.leaf(2)); | ||
var block = itstmt.leaf(1); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen34'); | ||
self.jsdc.ignore(block.first().last(), 'gen35'); | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(';break;case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
} | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + loopTemp + ';'); | ||
} | ||
else { | ||
self.jsdc.appendBefore('?' + itTemp + ':' + itEndTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append(loopTemp + '='); | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
top = self.hash[nid]; | ||
itTemp = ++top.index2; | ||
endTemp = ++top.index2; | ||
self.jsdc.append('case ' + itTemp + ':'); | ||
} | ||
else { | ||
if(!self.jsdc.endsWith(';') | ||
&& !self.jsdc.endsWith(':') | ||
&& !self.jsdc.endsWith('{') | ||
&& !self.jsdc.endsWith('}') | ||
&& !self.jsdc.endsWith('\n')) { | ||
self.jsdc.appendBefore(';'); | ||
} | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(';break;case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
} | ||
}); | ||
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); | ||
}); | ||
}); | ||
break; | ||
} | ||
break; | ||
//忽略这些节点中的所有逻辑 | ||
case JsNode.CLASSDECL: | ||
case JsNode.CLASSEXPR: | ||
case JsNode.FNDECL: | ||
case JsNode.FNEXPR: | ||
case JsNode.GENDECL: | ||
case JsNode.GENEXPR: | ||
case JsNode.METHOD: | ||
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; | ||
} | ||
node.leaves().forEach(function(leaf) { | ||
self.pre(leaf, nid, bid, res); | ||
}); | ||
} | ||
}, | ||
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; | ||
}, | ||
getLast: function(node) { | ||
while(node = node.last()) { | ||
if(node.name() == JsNode.TOKEN) { | ||
return node; | ||
} | ||
} | ||
return 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, 'gen36'); | ||
return; | ||
} | ||
else { | ||
return; | ||
} | ||
} | ||
}, | ||
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, 'gen36'); | ||
return; | ||
} | ||
else { | ||
return; | ||
} | ||
} | ||
} | ||
}); | ||
module.exports = Generator; | ||
}); | ||
} | ||
}); | ||
module.exports = Generator;}); |
@@ -1,38 +0,36 @@ | ||
define(function(require, exports, module) { | ||
var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Token = homunculus.getClass('Token'); | ||
module.exports = function(node, word) { | ||
var res = recursion(node, { 's': '', 'word': word }); | ||
return res.s; | ||
}; | ||
function recursion(node, res) { | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
var token = node.token(); | ||
if(!isVirtual) { | ||
if(res.word && [Token.ID, Token.NUMBER, Token.KEYWORD].indexOf(token.type()) > -1) { | ||
res.s += ' '; | ||
} | ||
if(token.content() == '}' && res.s.charAt(res.s.length - 1) == ';') { | ||
res.s = res.s.replace(/;$/, ''); | ||
} | ||
res.s += token.content(); | ||
res.word = [Token.ID, Token.NUMBER, Token.KEYWORD].indexOf(token.type()) > -1; | ||
define(function(require, exports, module){var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Token = homunculus.getClass('Token'); | ||
module.exports = function(node, word) { | ||
var res = recursion(node, { 's': '', 'word': word }); | ||
return res.s; | ||
}; | ||
function recursion(node, res) { | ||
var isToken = node.name() == JsNode.TOKEN; | ||
var isVirtual = isToken && node.token().type() == Token.VIRTUAL; | ||
if(isToken) { | ||
var token = node.token(); | ||
if(!isVirtual) { | ||
if(res.word && [Token.ID, Token.NUMBER, Token.KEYWORD].indexOf(token.type()) > -1) { | ||
res.s += ' '; | ||
} | ||
else if(token.content() == ';') { | ||
res.s += ';'; | ||
res.word = false; | ||
if(token.content() == '}' && res.s.charAt(res.s.length - 1) == ';') { | ||
res.s = res.s.replace(/;$/, ''); | ||
} | ||
res.s += token.content(); | ||
res.word = [Token.ID, Token.NUMBER, Token.KEYWORD].indexOf(token.type()) > -1; | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
recursion(leaf, res); | ||
}); | ||
else if(token.content() == ';') { | ||
res.s += ';'; | ||
res.word = false; | ||
} | ||
return res; | ||
} | ||
}); | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
recursion(leaf, res); | ||
}); | ||
} | ||
return res; | ||
}}); |
848
web/Jsdc.js
@@ -1,448 +0,446 @@ | ||
define(function(require, exports, module) { | ||
var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Token = homunculus.getClass('Token'); | ||
var character = require('./util/character'); | ||
var Class = require('./util/Class'); | ||
var eventbus = require('./eventbus'); | ||
var Scope = require('./Scope'); | ||
var DefaultValue = require('./DefaultValue'); | ||
var Rest = require('./Rest'); | ||
var Template = require('./Template'); | ||
var Forof = require('./Forof'); | ||
var Klass = require('./Klass'); | ||
var Num = require('./Num'); | ||
var Module = require('./Module'); | ||
var ArrayCmph = require('./ArrayCmph'); | ||
var ArrowFn = require('./ArrowFn'); | ||
var Generator = require('./Generator'); | ||
var Destruct = require('./Destruct'); | ||
var Str = require('./Str'); | ||
var Obj = require('./Obj'); | ||
var Jsdc = Class(function(code) { | ||
this.code = (code + '') || ''; | ||
this.index = 0; | ||
this.res = ''; | ||
this.node = {}; | ||
this.ignores = {}; | ||
this.ts = []; | ||
this.scope = new Scope(this); | ||
this.defaultValue = new DefaultValue(this); | ||
this.rest = new Rest(this); | ||
this.template = new Template(this); | ||
this.forof = new Forof(this); | ||
this.klass = new Klass(this); | ||
this.num = new Num(this); | ||
this.module = new Module(this); | ||
this.arrCmph = new ArrayCmph(this); | ||
this.arrowFn = new ArrowFn(this); | ||
this.gen = new Generator(this); | ||
this.destruct = new Destruct(this); | ||
this.str = new Str(this); | ||
this.obj = new Obj(this); | ||
this.i = 0; | ||
this.ids = {}; | ||
return this; | ||
}).methods({ | ||
parse: function(code) { | ||
var self = this; | ||
if(!character.isUndefined(code)) { | ||
self.code = code + ''; | ||
define(function(require, exports, module){var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Token = homunculus.getClass('Token'); | ||
var character = require('./util/character'); | ||
var Class = require('./util/Class'); | ||
var eventbus = require('./eventbus'); | ||
var Scope = require('./Scope'); | ||
var DefaultValue = require('./DefaultValue'); | ||
var Rest = require('./Rest'); | ||
var Template = require('./Template'); | ||
var Forof = require('./Forof'); | ||
var Klass = require('./Klass'); | ||
var Num = require('./Num'); | ||
var Module = require('./Module'); | ||
var ArrayCmph = require('./ArrayCmph'); | ||
var ArrowFn = require('./ArrowFn'); | ||
var Generator = require('./Generator'); | ||
var Destruct = require('./Destruct'); | ||
var Str = require('./Str'); | ||
var Obj = require('./Obj'); | ||
var Jsdc = Class(function(code) { | ||
this.code = (code + '') || ''; | ||
this.index = 0; | ||
this.res = ''; | ||
this.node = {}; | ||
this.ignores = {}; | ||
this.ts = []; | ||
this.scope = new Scope(this); | ||
this.defaultValue = new DefaultValue(this); | ||
this.rest = new Rest(this); | ||
this.template = new Template(this); | ||
this.forof = new Forof(this); | ||
this.klass = new Klass(this); | ||
this.num = new Num(this); | ||
this.module = new Module(this); | ||
this.arrCmph = new ArrayCmph(this); | ||
this.arrowFn = new ArrowFn(this); | ||
this.gen = new Generator(this); | ||
this.destruct = new Destruct(this); | ||
this.str = new Str(this); | ||
this.obj = new Obj(this); | ||
this.i = 0; | ||
this.ids = {}; | ||
return this; | ||
}).methods({ | ||
parse: function(code) { | ||
var self = this; | ||
if(!character.isUndefined(code)) { | ||
self.code = code + ''; | ||
} | ||
var parser = homunculus.getParser('es6'); | ||
try { | ||
self.node = parser.parse(code); | ||
} catch(e) { | ||
return e.toString(); | ||
} | ||
self.ignores = parser.ignore(); | ||
//记录所有id | ||
var lexer = parser.lexer; | ||
lexer.tokens().forEach(function(token) { | ||
if(token.type() == Token.ID) { | ||
//防止关键字或内置原生方法如hasOwnProperty被设置为true,将所有id前面加_记录 | ||
self.ids['_' + token.content()] = true; | ||
} | ||
var parser = homunculus.getParser('es6'); | ||
try { | ||
self.node = parser.parse(code); | ||
} catch(e) { | ||
return e.toString(); | ||
}); | ||
self.ts = lexer.tokens(); | ||
//开头部分的ignore | ||
while(self.ignores[self.index]) { | ||
self.append(self.ignores[self.index++].content()); | ||
} | ||
//预分析局部变量,将影响的let和const声明查找出来 | ||
self.scope.parse(self.node); | ||
//递归处理 | ||
self.recursion(self.node); | ||
return self.res; | ||
}, | ||
ast: function() { | ||
return this.node; | ||
}, | ||
tokens: function() { | ||
return this.ts; | ||
}, | ||
append: function(s) { | ||
s = String(s); | ||
this.res += s; | ||
this.i = this.res.length; | ||
}, | ||
appendBefore: function(s) { | ||
s = String(s); | ||
if(this.i < this.res.length) { | ||
this.insert(s, this.i); | ||
this.i += s.length; | ||
} | ||
else { | ||
this.append(s); | ||
} | ||
}, | ||
insert: function(s, i) { | ||
s = String(s); | ||
this.res = this.res.slice(0, i) + s + this.res.slice(i); | ||
}, | ||
replace: function(s, i, len) { | ||
this.res = this.res.slice(0, i) + s + this.res.slice(i + len); | ||
this.i += s.length - len; | ||
}, | ||
endsWith: function(s) { | ||
s = String(s); | ||
if(this.i < this.res.length) { | ||
return this.res.slice(this.i - s.length, this.i) == s; | ||
} | ||
else { | ||
return this.res.slice(this.res.length - s.length) == s; | ||
} | ||
}, | ||
next: function() { | ||
var i = ++this.index; | ||
return this.ignores.hasOwnProperty(i) ? this.ignores[i] : null; | ||
}, | ||
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.token(node); | ||
} | ||
self.ignores = parser.ignore(); | ||
//记录所有id | ||
var lexer = parser.lexer; | ||
lexer.tokens().forEach(function(token) { | ||
if(token.type() == Token.ID) { | ||
//防止关键字或内置原生方法如hasOwnProperty被设置为true,将所有id前面加_记录 | ||
self.ids['_' + token.content()] = true; | ||
} | ||
} | ||
else { | ||
self.before(node); | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf); | ||
}); | ||
self.ts = lexer.tokens(); | ||
//开头部分的ignore | ||
while(self.ignores[self.index]) { | ||
self.append(self.ignores[self.index++].content()); | ||
self.after(node); | ||
} | ||
}, | ||
token: function(node) { | ||
var token = node.token(); | ||
var content = token.content(); | ||
//替换掉let和const为var | ||
if(content == 'let' | ||
|| content == 'const') { | ||
!token.ignore && this.append('var'); | ||
} | ||
else { | ||
if(content == '}') { | ||
this.scope.block(node); | ||
} | ||
//预分析局部变量,将影响的let和const声明查找出来 | ||
self.scope.parse(self.node); | ||
//递归处理 | ||
self.recursion(self.node); | ||
return self.res; | ||
}, | ||
ast: function() { | ||
return this.node; | ||
}, | ||
tokens: function() { | ||
return this.ts; | ||
}, | ||
append: function(s) { | ||
s = String(s); | ||
this.res += s; | ||
this.i = this.res.length; | ||
}, | ||
appendBefore: function(s) { | ||
s = String(s); | ||
if(this.i < this.res.length) { | ||
this.insert(s, this.i); | ||
this.i += s.length; | ||
else if(content == 'of') { | ||
this.forof.of(node); | ||
this.arrCmph.of(node); | ||
} | ||
else { | ||
this.append(s); | ||
else if(content == '(') { | ||
this.klass.prts(node, true); | ||
} | ||
}, | ||
insert: function(s, i) { | ||
s = String(s); | ||
this.res = this.res.slice(0, i) + s + this.res.slice(i); | ||
}, | ||
replace: function(s, i, len) { | ||
this.res = this.res.slice(0, i) + s + this.res.slice(i + len); | ||
this.i += s.length - len; | ||
}, | ||
endsWith: function(s) { | ||
s = String(s); | ||
if(this.i < this.res.length) { | ||
return this.res.slice(this.i - s.length, this.i) == s; | ||
else if(content == '=>') { | ||
this.arrowFn.arrow(token); | ||
} | ||
else { | ||
return this.res.slice(this.res.length - s.length) == s; | ||
else if(token.type() == Token.KEYWORD | ||
&& content == 'return') { | ||
eventbus.emit(node.nid(), [node, true]); | ||
} | ||
}, | ||
next: function() { | ||
var i = ++this.index; | ||
return this.ignores.hasOwnProperty(i) ? this.ignores[i] : null; | ||
}, | ||
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.token(node); | ||
} | ||
else if(token.type() == Token.KEYWORD | ||
&& content == 'super'){ | ||
this.klass.super(node); | ||
} | ||
else { | ||
self.before(node); | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf); | ||
}); | ||
self.after(node); | ||
else if(content == ')') { | ||
this.forof.prts(node, true); | ||
} | ||
}, | ||
token: function(node) { | ||
var token = node.token(); | ||
var content = token.content(); | ||
//替换掉let和const为var | ||
if(content == 'let' | ||
|| content == 'const') { | ||
!token.ignore && this.append('var'); | ||
else if(content == ',') { | ||
this.rest.comma(node); | ||
} | ||
else { | ||
if(content == '}') { | ||
this.scope.block(node); | ||
} | ||
else if(content == 'of') { | ||
this.forof.of(node); | ||
this.arrCmph.of(node); | ||
} | ||
else if(content == '(') { | ||
this.klass.prts(node, true); | ||
} | ||
else if(content == '=>') { | ||
this.arrowFn.arrow(token); | ||
} | ||
else if(token.type() == Token.KEYWORD | ||
&& content == 'return') { | ||
eventbus.emit(node.nid(), [node, true]); | ||
} | ||
else if(token.type() == Token.KEYWORD | ||
&& content == 'super'){ | ||
this.klass.super(node); | ||
} | ||
else if(content == ')') { | ||
this.forof.prts(node, true); | ||
} | ||
else if(content == ',') { | ||
this.rest.comma(node); | ||
} | ||
else if(token.type() == Token.TEMPLATE) { | ||
this.template.parse(token); | ||
} | ||
else if(token.type() == Token.STRING) { | ||
this.str.parse(token); | ||
} | ||
else if(!token.ignore && token.type() == Token.NUMBER) { | ||
this.num.parse(token); | ||
} | ||
//替换操作会设置ignore属性将其忽略 | ||
if(!token.ignore) { | ||
this.append(content); | ||
} | ||
if(content == '{') { | ||
this.scope.block(node, true); | ||
this.forof.block(node); | ||
} | ||
else if(content == ')') { | ||
this.forof.prts(node); | ||
} | ||
else if(content == '(') { | ||
this.klass.prts(node); | ||
} | ||
else if(content == 'return') { | ||
eventbus.emit(node.nid(), [node]); | ||
} | ||
else if(token.type() == Token.TEMPLATE) { | ||
this.template.parse(token); | ||
} | ||
var ignore = token.ignore; | ||
this.i = this.res.length; | ||
//加上ignore | ||
var ig; | ||
while(ig = this.next()) { | ||
if(!ignore || ig.type() != Token.BLANK) { | ||
if(!ig.ignore) { | ||
this.res += ig.content(); | ||
} | ||
ignore && (ig.ignore = true); | ||
ignore = false; | ||
} | ||
else if(token.type() == Token.STRING) { | ||
this.str.parse(token); | ||
} | ||
}, | ||
before: function(node) { | ||
switch(node.name()) { | ||
//var变量前置,赋值部分删除var,如此可以将block用匿名函数包裹达到局部作用与效果 | ||
case JsNode.VARSTMT: | ||
this.scope.prevar(node); | ||
this.gen.prevar(node); | ||
break; | ||
case JsNode.VARDECL: | ||
this.destruct.parse(node, true); | ||
break; | ||
case JsNode.ASSIGNEXPR: | ||
this.destruct.expr(node, true); | ||
break; | ||
case JsNode.GENDECL: | ||
this.gen.parse(node, true); | ||
break; | ||
case JsNode.GENEXPR: | ||
this.gen.expr(node, true); | ||
break; | ||
case JsNode.FNBODY: | ||
this.scope.enter(node); | ||
this.defaultValue.enter(node); | ||
this.rest.enter(node); | ||
this.gen.body(node, true); | ||
break; | ||
case JsNode.BLOCK: | ||
this.scope.block(node, true); | ||
break; | ||
case JsNode.FMPARAMS: | ||
this.defaultValue.param(node); | ||
this.rest.param(node); | ||
break; | ||
case JsNode.CALLEXPR: | ||
this.rest.expr(node); | ||
break; | ||
case JsNode.ARGS: | ||
this.rest.args(node); | ||
break; | ||
case JsNode.ARGLIST: | ||
this.klass.arglist(node); | ||
this.rest.arglist(node); | ||
break; | ||
case JsNode.ITERSTMT: | ||
this.forof.parse(node, true); | ||
break; | ||
case JsNode.CLASSDECL: | ||
case JsNode.CLASSEXPR: | ||
this.klass.parse(node, true); | ||
break; | ||
case JsNode.CLASSELEM: | ||
this.klass.elem(node, true); | ||
break; | ||
case JsNode.PROPTNAME: | ||
this.klass.prptn(node); | ||
break; | ||
case JsNode.MODULEBODY: | ||
this.module.enter(node); | ||
break; | ||
case JsNode.MODULEIMPORT: | ||
this.module.module(node); | ||
break; | ||
case JsNode.IMPORTDECL: | ||
this.module.import(node); | ||
break; | ||
case JsNode.EXPORTDECL: | ||
this.module.export(node); | ||
break; | ||
case JsNode.ARRCMPH: | ||
this.arrCmph.parse(node, true); | ||
break; | ||
case JsNode.CMPHFOR: | ||
this.arrCmph.for(node, true); | ||
break; | ||
case JsNode.CMPHIF: | ||
this.arrCmph.if(node, true); | ||
break; | ||
case JsNode.ARROWFN: | ||
this.arrowFn.parse(node); | ||
break; | ||
case JsNode.ARROWPARAMS: | ||
this.arrowFn.params(node, true); | ||
break; | ||
case JsNode.CNCSBODY: | ||
this.arrowFn.body(node, true); | ||
break; | ||
case JsNode.YIELDEXPR: | ||
this.gen.yield(node, true); | ||
break; | ||
case JsNode.PROPTDEF: | ||
this.obj.propt(node, true); | ||
break; | ||
case JsNode.OBJLTR: | ||
this.obj.parse(node, true); | ||
break; | ||
case JsNode.SPREAD: | ||
this.rest.spread(node, true); | ||
break; | ||
else if(!token.ignore && token.type() == Token.NUMBER) { | ||
this.num.parse(token); | ||
} | ||
eventbus.emit(node.nid(), [node, true]); | ||
}, | ||
after: function(node) { | ||
switch(node.name()) { | ||
case JsNode.VARDECL: | ||
this.destruct.parse(node); | ||
break; | ||
case JsNode.ASSIGNEXPR: | ||
this.destruct.expr(node); | ||
break; | ||
case JsNode.FNBODY: | ||
this.scope.leave(node); | ||
this.gen.body(node); | ||
break; | ||
case JsNode.BLOCK: | ||
this.scope.block(node); | ||
break; | ||
case JsNode.ITERSTMT: | ||
this.forof.parse(node); | ||
break; | ||
case JsNode.CLASSDECL: | ||
case JsNode.CLASSEXPR: | ||
this.klass.parse(node); | ||
break; | ||
case JsNode.CLASSELEM: | ||
this.klass.elem(node); | ||
break; | ||
case JsNode.MODULEBODY: | ||
this.module.leave(node); | ||
break; | ||
case JsNode.ARRCMPH: | ||
this.arrCmph.parse(node); | ||
break; | ||
case JsNode.CMPHFOR: | ||
this.arrCmph.for(node); | ||
break; | ||
case JsNode.CMPHIF: | ||
this.arrCmph.if(node); | ||
break; | ||
case JsNode.ARROWPARAMS: | ||
this.arrowFn.params(node); | ||
break; | ||
case JsNode.CNCSBODY: | ||
this.arrowFn.body(node); | ||
break; | ||
case JsNode.GENDECL: | ||
this.gen.parse(node); | ||
break; | ||
case JsNode.GENEXPR: | ||
this.gen.expr(node); | ||
break; | ||
case JsNode.YIELDEXPR: | ||
this.gen.yield(node); | ||
break; | ||
case JsNode.PROPTDEF: | ||
this.obj.propt(node); | ||
break; | ||
case JsNode.PROPTNAME: | ||
this.obj.name(node); | ||
break; | ||
case JsNode.OBJLTR: | ||
this.obj.parse(node); | ||
break; | ||
case JsNode.SPREAD: | ||
this.rest.spread(node); | ||
break; | ||
//替换操作会设置ignore属性将其忽略 | ||
if(!token.ignore) { | ||
this.append(content); | ||
} | ||
eventbus.emit(node.nid(), [node]); | ||
}, | ||
ignore: function(node, msg, prev) { | ||
var self = this; | ||
if(node instanceof Token) { | ||
node.ignore = msg || true; | ||
//忽略前置空格 | ||
if(prev) { | ||
prev = node; | ||
while(prev = prev.prev()) { | ||
if(prev.type() == Token.BLANK) { | ||
prev.ignore = msg || true; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
} | ||
if(content == '{') { | ||
this.scope.block(node, true); | ||
this.forof.block(node); | ||
} | ||
else if(node.name() == JsNode.TOKEN) { | ||
this.ignore(node.token(), msg, prev); | ||
else if(content == ')') { | ||
this.forof.prts(node); | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.ignore(leaf, msg, prev); | ||
}); | ||
else if(content == '(') { | ||
this.klass.prts(node); | ||
} | ||
}, | ||
uid: function() { | ||
var temp; | ||
while(temp = '_' + uid++) { | ||
if(!this.ids.hasOwnProperty('_' + temp)) { | ||
return temp; | ||
else if(content == 'return') { | ||
eventbus.emit(node.nid(), [node]); | ||
} | ||
} | ||
var ignore = token.ignore; | ||
this.i = this.res.length; | ||
//加上ignore | ||
var ig; | ||
while(ig = this.next()) { | ||
if(!ignore || ig.type() != Token.BLANK) { | ||
if(!ig.ignore) { | ||
this.res += ig.content(); | ||
} | ||
ignore && (ig.ignore = true); | ||
ignore = false; | ||
} | ||
}, | ||
define: function(d) { | ||
return Jsdc.define(d); | ||
} | ||
}).statics({ | ||
parse: function(code) { | ||
jsdc = new Jsdc(); | ||
return jsdc.parse(code); | ||
}, | ||
ast: function() { | ||
return jsdc.ast(); | ||
}, | ||
tokens: function() { | ||
return jsdc.tokens(); | ||
}, | ||
reset: function() { | ||
uid = 0; | ||
}, | ||
define: function(d) { | ||
if(!character.isUndefined(d)) { | ||
def = d; | ||
}, | ||
before: function(node) { | ||
switch(node.name()) { | ||
//var变量前置,赋值部分删除var,如此可以将block用匿名函数包裹达到局部作用与效果 | ||
case JsNode.VARSTMT: | ||
this.scope.prevar(node); | ||
this.gen.prevar(node); | ||
break; | ||
case JsNode.VARDECL: | ||
this.destruct.parse(node, true); | ||
break; | ||
case JsNode.ASSIGNEXPR: | ||
this.destruct.expr(node, true); | ||
break; | ||
case JsNode.GENDECL: | ||
this.gen.parse(node, true); | ||
break; | ||
case JsNode.GENEXPR: | ||
this.gen.expr(node, true); | ||
break; | ||
case JsNode.FNBODY: | ||
this.scope.enter(node); | ||
this.defaultValue.enter(node); | ||
this.rest.enter(node); | ||
this.gen.body(node, true); | ||
break; | ||
case JsNode.BLOCK: | ||
this.scope.block(node, true); | ||
break; | ||
case JsNode.FMPARAMS: | ||
this.defaultValue.param(node); | ||
this.rest.param(node); | ||
break; | ||
case JsNode.CALLEXPR: | ||
this.rest.expr(node); | ||
break; | ||
case JsNode.ARGS: | ||
this.rest.args(node); | ||
break; | ||
case JsNode.ARGLIST: | ||
this.klass.arglist(node); | ||
this.rest.arglist(node); | ||
break; | ||
case JsNode.ITERSTMT: | ||
this.forof.parse(node, true); | ||
break; | ||
case JsNode.CLASSDECL: | ||
case JsNode.CLASSEXPR: | ||
this.klass.parse(node, true); | ||
break; | ||
case JsNode.CLASSELEM: | ||
this.klass.elem(node, true); | ||
break; | ||
case JsNode.PROPTNAME: | ||
this.klass.prptn(node); | ||
break; | ||
case JsNode.MODULEBODY: | ||
this.module.enter(node); | ||
break; | ||
case JsNode.MODULEIMPORT: | ||
this.module.module(node); | ||
break; | ||
case JsNode.IMPORTDECL: | ||
this.module.import(node); | ||
break; | ||
case JsNode.EXPORTDECL: | ||
this.module.export(node); | ||
break; | ||
case JsNode.ARRCMPH: | ||
this.arrCmph.parse(node, true); | ||
break; | ||
case JsNode.CMPHFOR: | ||
this.arrCmph.for(node, true); | ||
break; | ||
case JsNode.CMPHIF: | ||
this.arrCmph.if(node, true); | ||
break; | ||
case JsNode.ARROWFN: | ||
this.arrowFn.parse(node); | ||
break; | ||
case JsNode.ARROWPARAMS: | ||
this.arrowFn.params(node, true); | ||
break; | ||
case JsNode.CNCSBODY: | ||
this.arrowFn.body(node, true); | ||
break; | ||
case JsNode.YIELDEXPR: | ||
this.gen.yield(node, true); | ||
break; | ||
case JsNode.PROPTDEF: | ||
this.obj.propt(node, true); | ||
break; | ||
case JsNode.OBJLTR: | ||
this.obj.parse(node, true); | ||
break; | ||
case JsNode.SPREAD: | ||
this.rest.spread(node, true); | ||
break; | ||
} | ||
eventbus.emit(node.nid(), [node, true]); | ||
}, | ||
after: function(node) { | ||
switch(node.name()) { | ||
case JsNode.VARDECL: | ||
this.destruct.parse(node); | ||
break; | ||
case JsNode.ASSIGNEXPR: | ||
this.destruct.expr(node); | ||
break; | ||
case JsNode.FNBODY: | ||
this.scope.leave(node); | ||
this.gen.body(node); | ||
break; | ||
case JsNode.BLOCK: | ||
this.scope.block(node); | ||
break; | ||
case JsNode.ITERSTMT: | ||
this.forof.parse(node); | ||
break; | ||
case JsNode.CLASSDECL: | ||
case JsNode.CLASSEXPR: | ||
this.klass.parse(node); | ||
break; | ||
case JsNode.CLASSELEM: | ||
this.klass.elem(node); | ||
break; | ||
case JsNode.MODULEBODY: | ||
this.module.leave(node); | ||
break; | ||
case JsNode.ARRCMPH: | ||
this.arrCmph.parse(node); | ||
break; | ||
case JsNode.CMPHFOR: | ||
this.arrCmph.for(node); | ||
break; | ||
case JsNode.CMPHIF: | ||
this.arrCmph.if(node); | ||
break; | ||
case JsNode.ARROWPARAMS: | ||
this.arrowFn.params(node); | ||
break; | ||
case JsNode.CNCSBODY: | ||
this.arrowFn.body(node); | ||
break; | ||
case JsNode.GENDECL: | ||
this.gen.parse(node); | ||
break; | ||
case JsNode.GENEXPR: | ||
this.gen.expr(node); | ||
break; | ||
case JsNode.YIELDEXPR: | ||
this.gen.yield(node); | ||
break; | ||
case JsNode.PROPTDEF: | ||
this.obj.propt(node); | ||
break; | ||
case JsNode.PROPTNAME: | ||
this.obj.name(node); | ||
break; | ||
case JsNode.OBJLTR: | ||
this.obj.parse(node); | ||
break; | ||
case JsNode.SPREAD: | ||
this.rest.spread(node); | ||
break; | ||
} | ||
eventbus.emit(node.nid(), [node]); | ||
}, | ||
ignore: function(node, msg, prev) { | ||
var self = this; | ||
if(node instanceof Token) { | ||
node.ignore = msg || true; | ||
//忽略前置空格 | ||
if(prev) { | ||
prev = node; | ||
while(prev = prev.prev()) { | ||
if(prev.type() == Token.BLANK) { | ||
prev.ignore = msg || true; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
} | ||
return def; | ||
} | ||
}); | ||
var jsdc = null; | ||
var uid = 0; | ||
var def = false; | ||
module.exports = Jsdc; | ||
}); | ||
else if(node.name() == JsNode.TOKEN) { | ||
this.ignore(node.token(), msg, prev); | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.ignore(leaf, msg, prev); | ||
}); | ||
} | ||
}, | ||
uid: function() { | ||
var temp; | ||
while(temp = '_' + uid++) { | ||
if(!this.ids.hasOwnProperty('_' + temp)) { | ||
return temp; | ||
} | ||
} | ||
}, | ||
define: function(d) { | ||
return Jsdc.define(d); | ||
} | ||
}).statics({ | ||
parse: function(code) { | ||
jsdc = new Jsdc(); | ||
return jsdc.parse(code); | ||
}, | ||
ast: function() { | ||
return jsdc.ast(); | ||
}, | ||
tokens: function() { | ||
return jsdc.tokens(); | ||
}, | ||
reset: function() { | ||
uid = 0; | ||
}, | ||
define: function(d) { | ||
if(!character.isUndefined(d)) { | ||
def = d; | ||
} | ||
return def; | ||
} | ||
}); | ||
var jsdc = null; | ||
var uid = 0; | ||
var def = false; | ||
module.exports = Jsdc;}); |
416
web/Klass.js
@@ -1,248 +0,246 @@ | ||
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 Klass = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.sup = {}; | ||
this.gs = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
if(node.name() == JsNode.CLASSDECL) { | ||
if(start) { | ||
var o = {}; | ||
o.name = node.leaf(1).first().token().content(); | ||
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), 'klass4'); | ||
this.body(node.last().prev(), o.name); | ||
} | ||
else { | ||
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); | ||
var temp = this.jsdc.uid(); | ||
this.jsdc.append('!function(){'); | ||
this.jsdc.append('var ' + temp + '=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append(temp + '.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=' + temp); | ||
this.jsdc.append('}();'); | ||
} | ||
this.hash[node.nid()] = o; | ||
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 Klass = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.sup = {}; | ||
this.gs = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
if(node.name() == JsNode.CLASSDECL) { | ||
if(start) { | ||
var o = {}; | ||
o.name = node.leaf(1).first().token().content(); | ||
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), 'klass4'); | ||
this.body(node.last().prev(), o.name); | ||
} | ||
else { | ||
var o = this.hash[node.nid()]; | ||
if(o.extend) { | ||
this.jsdc.appendBefore('Object.keys(' + o.extend + ').forEach(function(k){' + o.name + '[k]=' + o.extend + '[k]});'); | ||
} | ||
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); | ||
var temp = this.jsdc.uid(); | ||
this.jsdc.append('!function(){'); | ||
this.jsdc.append('var ' + temp + '=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append(temp + '.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=' + temp); | ||
this.jsdc.append('}();'); | ||
} | ||
this.hash[node.nid()] = o; | ||
} | ||
//classexpr由于没有类名,需要封装成一个返回class的fnexpr | ||
else { | ||
if(start) { | ||
this.jsdc.append('function(){'); | ||
var o = {}; | ||
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), 'klass9'); | ||
if(node.leaf(1).name() == JsNode.HERITAGE) { | ||
o.extend = join(node.leaf(1).last()); | ||
o.name = this.jsdc.uid(); | ||
} | ||
else { | ||
o.name = node.leaf(1).first().token().content(); | ||
} | ||
var o = this.hash[node.nid()]; | ||
if(o.extend) { | ||
this.jsdc.appendBefore('Object.keys(' + o.extend + ').forEach(function(k){' + o.name + '[k]=' + o.extend + '[k]});'); | ||
} | ||
} | ||
} | ||
//classexpr由于没有类名,需要封装成一个返回class的fnexpr | ||
else { | ||
if(start) { | ||
this.jsdc.append('function(){'); | ||
var o = {}; | ||
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), 'klass9'); | ||
if(node.leaf(1).name() == JsNode.HERITAGE) { | ||
o.extend = join(node.leaf(1).last()); | ||
o.name = this.jsdc.uid(); | ||
} | ||
else if(node.leaf(3).name() == JsNode.TOKEN | ||
&& node.leaf(3).token().content() == '{') { | ||
this.jsdc.ignore(node.leaf(3), 'klass10'); | ||
else { | ||
o.name = node.leaf(1).first().token().content(); | ||
o.extend = join(node.leaf(2).last()); | ||
} | ||
else { | ||
o.name = this.jsdc.uid(); | ||
this.jsdc.ignore(node.leaf(1), 'klass11'); | ||
} | ||
this.jsdc.ignore(node.last(), 'klass12'); | ||
var classbody = node.last().prev(); | ||
this.body(classbody, o.name, o.extend); | ||
if(o.extend) { | ||
var temp = this.jsdc.uid(); | ||
this.jsdc.append('!function(){'); | ||
this.jsdc.append('var ' + temp + '=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append(temp + '.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=' + temp); | ||
this.jsdc.append('}();'); | ||
} | ||
this.hash[node.nid()] = o; | ||
} | ||
else if(node.leaf(3).name() == JsNode.TOKEN | ||
&& node.leaf(3).token().content() == '{') { | ||
this.jsdc.ignore(node.leaf(3), 'klass10'); | ||
o.name = node.leaf(1).first().token().content(); | ||
o.extend = join(node.leaf(2).last()); | ||
} | ||
else { | ||
var o = this.hash[node.nid()]; | ||
if(o.extend) { | ||
this.jsdc.appendBefore('Object.keys(' + o.extend + ').forEach(function(k){' + o.name + '[k]=' + o.extend + '[k]});'); | ||
} | ||
this.jsdc.appendBefore('return ' + o.name); | ||
//特别注意这里的()没有结尾分号,因为是表达式 | ||
this.jsdc.appendBefore('}()'); | ||
o.name = this.jsdc.uid(); | ||
this.jsdc.ignore(node.leaf(1), 'klass11'); | ||
} | ||
this.jsdc.ignore(node.last(), 'klass12'); | ||
var classbody = node.last().prev(); | ||
this.body(classbody, o.name, o.extend); | ||
if(o.extend) { | ||
var temp = this.jsdc.uid(); | ||
this.jsdc.append('!function(){'); | ||
this.jsdc.append('var ' + temp + '=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append(temp + '.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=' + temp); | ||
this.jsdc.append('}();'); | ||
} | ||
this.hash[node.nid()] = o; | ||
} | ||
}, | ||
elem: function(node, start) { | ||
var first = node.first(); | ||
var top = node.parent().parent(); | ||
var o = this.hash[top.nid()]; | ||
if(first.name() == JsNode.METHOD) { | ||
first = first.first(); | ||
if(first.name() == JsNode.PROPTNAME) { | ||
if(start) { | ||
var token = first.first().first().token(); | ||
this.jsdc.ignore(token, 'klass13'); | ||
if(token.content() == 'constructor') { | ||
this.jsdc.append('function '); | ||
this.jsdc.append(o.name); | ||
} | ||
else { | ||
this.jsdc.append(o.name); | ||
this.jsdc.append('.prototype.' + token.content() + ' = function'); | ||
} | ||
} | ||
else { | ||
var o = this.hash[node.nid()]; | ||
if(o.extend) { | ||
this.jsdc.appendBefore('Object.keys(' + o.extend + ').forEach(function(k){' + o.name + '[k]=' + o.extend + '[k]});'); | ||
} | ||
else { | ||
var token = first.token(); | ||
if(start) { | ||
var prptn = first.next(); | ||
this.gs[prptn.nid()] = true; | ||
this.jsdc.ignore(prptn, 'klass14'); | ||
this.jsdc.append('Object.defineProperty('); | ||
this.jsdc.appendBefore('return ' + o.name); | ||
//特别注意这里的()没有结尾分号,因为是表达式 | ||
this.jsdc.appendBefore('}()'); | ||
} | ||
} | ||
}, | ||
elem: function(node, start) { | ||
var first = node.first(); | ||
var top = node.parent().parent(); | ||
var o = this.hash[top.nid()]; | ||
if(first.name() == JsNode.METHOD) { | ||
first = first.first(); | ||
if(first.name() == JsNode.PROPTNAME) { | ||
if(start) { | ||
var token = first.first().first().token(); | ||
this.jsdc.ignore(token, 'klass13'); | ||
if(token.content() == 'constructor') { | ||
this.jsdc.append('function '); | ||
this.jsdc.append(o.name); | ||
this.jsdc.append('.prototype, "'); | ||
if(token.content() == 'get') { | ||
var n = first.next().first().first().token(); | ||
o.g = n.content(); | ||
this.jsdc.append(o.g); | ||
} | ||
else { | ||
var n = first.next().first().first().token(); | ||
o.s = n.content(); | ||
this.jsdc.append(o.s); | ||
} | ||
this.jsdc.append('", {'); | ||
} | ||
else { | ||
this.jsdc.appendBefore('});'); | ||
this.jsdc.append(o.name); | ||
this.jsdc.append('.prototype.' + token.content() + ' = function'); | ||
} | ||
} | ||
} | ||
else if(first.name() == JsNode.TOKEN | ||
&& first.token().content() == 'static') { | ||
else { | ||
var token = first.token(); | ||
if(start) { | ||
this.jsdc.ignore(first.token(), 'klass15'); | ||
this.jsdc.append(o.name + '.'); | ||
} | ||
} | ||
}, | ||
prptn: function(node) { | ||
if(this.gs.hasOwnProperty(node.nid())) { | ||
this.jsdc.append(':function'); | ||
} | ||
}, | ||
super: function(node) { | ||
this.jsdc.ignore(node, 'klass16'); | ||
var top = this.closest(node); | ||
if(this.hash.hasOwnProperty(top.nid())) { | ||
this.jsdc.append(this.hash[top.nid()].extend); | ||
if(node.next()) { | ||
if(node.next().name() == JsNode.ARGS) { | ||
this.jsdc.append('.call'); | ||
var prptn = first.next(); | ||
this.gs[prptn.nid()] = true; | ||
this.jsdc.ignore(prptn, 'klass14'); | ||
this.jsdc.append('Object.defineProperty('); | ||
this.jsdc.append(o.name); | ||
this.jsdc.append('.prototype, "'); | ||
if(token.content() == 'get') { | ||
var n = first.next().first().first().token(); | ||
o.g = n.content(); | ||
this.jsdc.append(o.g); | ||
} | ||
else { | ||
this.jsdc.append('.prototype'); | ||
var parent = node.parent(); | ||
while(parent = parent.parent()) { | ||
if(parent.name() == JsNode.CALLEXPR) { | ||
this.sup[parent.leaf(1).leaf(1).nid()] = this.hash[top.nid()].extend; | ||
break; | ||
} | ||
else if(parent.name() == JsNode.FNBODY) { | ||
break; | ||
} | ||
} | ||
var n = first.next().first().first().token(); | ||
o.s = n.content(); | ||
this.jsdc.append(o.s); | ||
} | ||
this.jsdc.append('", {'); | ||
} | ||
} | ||
}, | ||
arglist: function(node) { | ||
if(this.sup.hasOwnProperty(node.nid())) { | ||
var ex = this.sup[node.nid()]; | ||
var i = this.jsdc.res.lastIndexOf('('); | ||
this.jsdc.insert('.call', i); | ||
if(node.size()) { | ||
this.jsdc.append('this,'); | ||
} | ||
else { | ||
this.jsdc.append('this'); | ||
this.jsdc.appendBefore('});'); | ||
} | ||
} | ||
}, | ||
prts: function(node, start) { | ||
var parent = node.parent(); | ||
} | ||
else if(first.name() == JsNode.TOKEN | ||
&& first.token().content() == 'static') { | ||
if(start) { | ||
if(parent.name() == JsNode.METHOD | ||
&& parent.prev() | ||
&& parent.prev().name() == JsNode.TOKEN | ||
&& parent.prev().token().content() == 'static') { | ||
parent = parent.parent(); | ||
if(parent.name() == JsNode.CLASSELEM) { | ||
this.jsdc.appendBefore('=function'); | ||
this.jsdc.ignore(first.token(), 'klass15'); | ||
this.jsdc.append(o.name + '.'); | ||
} | ||
} | ||
}, | ||
prptn: function(node) { | ||
if(this.gs.hasOwnProperty(node.nid())) { | ||
this.jsdc.append(':function'); | ||
} | ||
}, | ||
super: function(node) { | ||
this.jsdc.ignore(node, 'klass16'); | ||
var top = this.closest(node); | ||
if(this.hash.hasOwnProperty(top.nid())) { | ||
this.jsdc.append(this.hash[top.nid()].extend); | ||
if(node.next()) { | ||
if(node.next().name() == JsNode.ARGS) { | ||
this.jsdc.append('.call'); | ||
} | ||
else { | ||
this.jsdc.append('.prototype'); | ||
var parent = node.parent(); | ||
while(parent = parent.parent()) { | ||
if(parent.name() == JsNode.CALLEXPR) { | ||
this.sup[parent.leaf(1).leaf(1).nid()] = this.hash[top.nid()].extend; | ||
break; | ||
} | ||
else if(parent.name() == JsNode.FNBODY) { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
arglist: function(node) { | ||
if(this.sup.hasOwnProperty(node.nid())) { | ||
var ex = this.sup[node.nid()]; | ||
var i = this.jsdc.res.lastIndexOf('('); | ||
this.jsdc.insert('.call', i); | ||
if(node.size()) { | ||
this.jsdc.append('this,'); | ||
} | ||
else { | ||
if(parent.name() == JsNode.ARGS | ||
&& parent.prev().name() == JsNode.TOKEN | ||
&& parent.prev().token().content() == 'super') { | ||
this.jsdc.appendBefore('this'); | ||
} | ||
this.jsdc.append('this'); | ||
} | ||
}, | ||
body: function(node, id, extend) { | ||
var noCons = true; | ||
var leaves = node.leaves(); | ||
for(var i = 0; i < leaves.length; i++) { | ||
var leaf = leaves[i]; | ||
if(leaf.name() == JsNode.CLASSELEM | ||
&& (leaf = leaf.first()).name() == JsNode.METHOD | ||
&& (leaf = leaf.first()).name() == JsNode.PROPTNAME | ||
&& (leaf = leaf.first()).first().token().content() == 'constructor') { | ||
noCons = false; | ||
break; | ||
} | ||
}, | ||
prts: function(node, start) { | ||
var parent = node.parent(); | ||
if(start) { | ||
if(parent.name() == JsNode.METHOD | ||
&& parent.prev() | ||
&& parent.prev().name() == JsNode.TOKEN | ||
&& parent.prev().token().content() == 'static') { | ||
parent = parent.parent(); | ||
if(parent.name() == JsNode.CLASSELEM) { | ||
this.jsdc.appendBefore('=function'); | ||
} | ||
} | ||
if(noCons) { | ||
this.jsdc.append('function ' + id + '(){' + (extend ? (extend + '.call(this)') : '') + '}'); | ||
} | ||
else { | ||
if(parent.name() == JsNode.ARGS | ||
&& parent.prev().name() == JsNode.TOKEN | ||
&& parent.prev().token().content() == 'super') { | ||
this.jsdc.appendBefore('this'); | ||
} | ||
}, | ||
closest: function(node) { | ||
var parent = node; | ||
while(parent = parent.parent()) { | ||
if(parent.name() == JsNode.CLASSDECL | ||
|| parent.name() == JsNode.CLASSEXPR) { | ||
return parent; | ||
} | ||
} | ||
}, | ||
body: function(node, id, extend) { | ||
var noCons = true; | ||
var leaves = node.leaves(); | ||
for(var i = 0; i < leaves.length; i++) { | ||
var leaf = leaves[i]; | ||
if(leaf.name() == JsNode.CLASSELEM | ||
&& (leaf = leaf.first()).name() == JsNode.METHOD | ||
&& (leaf = leaf.first()).name() == JsNode.PROPTNAME | ||
&& (leaf = leaf.first()).first().token().content() == 'constructor') { | ||
noCons = false; | ||
break; | ||
} | ||
} | ||
}); | ||
module.exports = Klass; | ||
if(noCons) { | ||
this.jsdc.append('function ' + id + '(){' + (extend ? (extend + '.call(this)') : '') + '}'); | ||
} | ||
}, | ||
closest: function(node) { | ||
var parent = node; | ||
while(parent = parent.parent()) { | ||
if(parent.name() == JsNode.CLASSDECL | ||
|| parent.name() == JsNode.CLASSEXPR) { | ||
return parent; | ||
} | ||
} | ||
} | ||
}); | ||
module.exports = Klass; | ||
}); |
@@ -1,163 +0,161 @@ | ||
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 Module = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
}).methods({ | ||
parse: function(t) { | ||
}, | ||
module: function(node) { | ||
this.jsdc.ignore(node, 'module1'); | ||
this.jsdc.append('var '); | ||
this.jsdc.append(node.leaf(1).first().token().content()); | ||
this.jsdc.append('=require('); | ||
this.jsdc.append(node.leaf(2).last().token().content()); | ||
this.jsdc.append(');'); | ||
}, | ||
import: function(node) { | ||
var self = this; | ||
self.jsdc.ignore(node, 'module2'); | ||
var one = node.leaf(1); | ||
//import "string" | ||
if(one.name() == JsNode.TOKEN | ||
&& one.token().type() == Token.STRING) { | ||
self.jsdc.append('require('); | ||
self.jsdc.append(one.token().content()); | ||
self.jsdc.append(');'); | ||
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 Module = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
}).methods({ | ||
parse: function(t) { | ||
}, | ||
module: function(node) { | ||
this.jsdc.ignore(node, 'module1'); | ||
this.jsdc.append('var '); | ||
this.jsdc.append(node.leaf(1).first().token().content()); | ||
this.jsdc.append('=require('); | ||
this.jsdc.append(node.leaf(2).last().token().content()); | ||
this.jsdc.append(');'); | ||
}, | ||
import: function(node) { | ||
var self = this; | ||
self.jsdc.ignore(node, 'module2'); | ||
var one = node.leaf(1); | ||
//import "string" | ||
if(one.name() == JsNode.TOKEN | ||
&& one.token().type() == Token.STRING) { | ||
self.jsdc.append('require('); | ||
self.jsdc.append(one.token().content()); | ||
self.jsdc.append(');'); | ||
} | ||
//import id from "string" | ||
else if(one.name() == JsNode.IMPORTCAULSE | ||
&& one.size() == 1 | ||
&& one.first().name() == JsNode.TOKEN) { | ||
var id = one.first().token().content(); | ||
var last = node.last(); | ||
if(last.name() != JsNode.FROMCAULSE) { | ||
last = last.prev(); | ||
} | ||
//import id from "string" | ||
else if(one.name() == JsNode.IMPORTCAULSE | ||
&& one.size() == 1 | ||
&& one.first().name() == JsNode.TOKEN) { | ||
var id = one.first().token().content(); | ||
var last = node.last(); | ||
if(last.name() != JsNode.FROMCAULSE) { | ||
last = last.prev(); | ||
var temp = self.jsdc.uid(); | ||
self.jsdc.append('var ' + id + '='); | ||
self.jsdc.append('function(){var ' + temp); | ||
self.jsdc.append('=require('); | ||
self.jsdc.append(last.last().token().content()); | ||
self.jsdc.append(');return '); | ||
self.jsdc.append(temp + '.hasOwnProperty("' + id + '")?'); | ||
self.jsdc.append(temp + '.' + id + ':' + temp + '.hasOwnProperty("default")?'); | ||
self.jsdc.append(temp + '.default:' + temp); | ||
self.jsdc.append('}();'); | ||
} | ||
//import ids from "string",import {ids} from "string" | ||
else { | ||
var ids = getIds(one); | ||
var temp = self.jsdc.uid(); | ||
Object.keys(ids).forEach(function(k) { | ||
self.jsdc.append('var ' + k + ';'); | ||
}); | ||
self.jsdc.append('!function(){var ' + temp); | ||
self.jsdc.append('=require('); | ||
var last = node.last(); | ||
if(last.name() != JsNode.FROMCAULSE) { | ||
last = last.prev(); | ||
} | ||
self.jsdc.append(last.last().token().content()); | ||
self.jsdc.append(');'); | ||
Object.keys(ids).forEach(function(k) { | ||
self.jsdc.append(k); | ||
self.jsdc.append('=' + temp + '.'); | ||
self.jsdc.append(ids[k]); | ||
self.jsdc.append(';'); | ||
}); | ||
self.jsdc.append('}();'); | ||
} | ||
}, | ||
export: function(node) { | ||
var s = node.leaf(1).name(); | ||
switch(s) { | ||
case JsNode.TOKEN: | ||
s = node.leaf(1).token().content(); | ||
if(s == '*') { | ||
var temp = this.jsdc.uid(); | ||
this.jsdc.append('!function(){'); | ||
this.jsdc.append('var ' + temp + '=require('); | ||
this.jsdc.append(node.leaf(2).last().token().content()); | ||
this.jsdc.append(');'); | ||
this.jsdc.append('Object.keys(' + temp + ').forEach(function(k){'); | ||
this.jsdc.append('module.exports[k]=' + temp + '[k];'); | ||
this.jsdc.append('});}();'); | ||
this.jsdc.ignore(node, 'module3'); | ||
} | ||
var temp = self.jsdc.uid(); | ||
self.jsdc.append('var ' + id + '='); | ||
self.jsdc.append('function(){var ' + temp); | ||
self.jsdc.append('=require('); | ||
self.jsdc.append(last.last().token().content()); | ||
self.jsdc.append(');return '); | ||
self.jsdc.append(temp + '.hasOwnProperty("' + id + '")?'); | ||
self.jsdc.append(temp + '.' + id + ':' + temp + '.hasOwnProperty("default")?'); | ||
self.jsdc.append(temp + '.default:' + temp); | ||
self.jsdc.append('}();'); | ||
else if(s == 'default') { | ||
this.jsdc.append('exports.default='); | ||
this.jsdc.ignore(node.leaf(0), 'module4'); | ||
this.jsdc.ignore(node.leaf(1), 'module5'); | ||
} | ||
break; | ||
case JsNode.VARSTMT: | ||
case JsNode.LEXDECL: | ||
var varstmt = node.leaf(1); | ||
var vardecl = varstmt.leaf(1); | ||
this.jsdc.append('var '); | ||
var id = vardecl.first().first().token().content(); | ||
this.jsdc.append(id); | ||
this.jsdc.append(';exports.' + id + '='); | ||
this.jsdc.ignore(varstmt.first(), 'module6'); | ||
this.jsdc.ignore(node.first(), 'module7'); | ||
break; | ||
case JsNode.FNDECL: | ||
case JsNode.CLASSDECL: | ||
var id = node.last().leaf(1).first().token().content(); | ||
this.jsdc.append('exports.' + id + '='); | ||
this.jsdc.append(id); | ||
this.jsdc.append(';'); | ||
this.jsdc.ignore(node.first(), 'module8'); | ||
break; | ||
} | ||
}, | ||
enter: function() { | ||
if(this.jsdc.define()) { | ||
var s = /(?:\/\/.*\r?\n)?([\s]+)$/.exec(this.jsdc.res); | ||
if(s) { | ||
this.jsdc.insert('define(function(require,exports,module){', this.jsdc.res.length - s[1].length); | ||
} | ||
//import ids from "string",import {ids} from "string" | ||
else { | ||
var ids = getIds(one); | ||
var temp = self.jsdc.uid(); | ||
Object.keys(ids).forEach(function(k) { | ||
self.jsdc.append('var ' + k + ';'); | ||
}); | ||
self.jsdc.append('!function(){var ' + temp); | ||
self.jsdc.append('=require('); | ||
var last = node.last(); | ||
if(last.name() != JsNode.FROMCAULSE) { | ||
last = last.prev(); | ||
} | ||
self.jsdc.append(last.last().token().content()); | ||
self.jsdc.append(');'); | ||
Object.keys(ids).forEach(function(k) { | ||
self.jsdc.append(k); | ||
self.jsdc.append('=' + temp + '.'); | ||
self.jsdc.append(ids[k]); | ||
self.jsdc.append(';'); | ||
}); | ||
self.jsdc.append('}();'); | ||
this.jsdc.append('define(function(require,exports,module){'); | ||
} | ||
}, | ||
export: function(node) { | ||
var s = node.leaf(1).name(); | ||
switch(s) { | ||
case JsNode.TOKEN: | ||
s = node.leaf(1).token().content(); | ||
if(s == '*') { | ||
var temp = this.jsdc.uid(); | ||
this.jsdc.append('!function(){'); | ||
this.jsdc.append('var ' + temp + '=require('); | ||
this.jsdc.append(node.leaf(2).last().token().content()); | ||
this.jsdc.append(');'); | ||
this.jsdc.append('Object.keys(' + temp + ').forEach(function(k){'); | ||
this.jsdc.append('module.exports[k]=' + temp + '[k];'); | ||
this.jsdc.append('});}();'); | ||
this.jsdc.ignore(node, 'module3'); | ||
} | ||
}, | ||
leave: function() { | ||
this.jsdc.define() && this.jsdc.append('});'); | ||
} | ||
}); | ||
function getIds(node) { | ||
var res = {}; | ||
node.leaves().forEach(function(leaf, i) { | ||
if(i % 2 == 0) { | ||
if(leaf.name() == JsNode.NAMEIMPORT) { | ||
leaf.leaves().forEach(function(leaf, i) { | ||
if(i % 2 == 1) { | ||
var s = leaf.first().token().content(); | ||
if(leaf.size() == 1) { | ||
res[s] = s; | ||
} | ||
else { | ||
res[leaf.last().first().token().content()] = s; | ||
} | ||
} | ||
else if(s == 'default') { | ||
this.jsdc.append('exports.default='); | ||
this.jsdc.ignore(node.leaf(0), 'module4'); | ||
this.jsdc.ignore(node.leaf(1), 'module5'); | ||
} | ||
break; | ||
case JsNode.VARSTMT: | ||
case JsNode.LEXDECL: | ||
var varstmt = node.leaf(1); | ||
var vardecl = varstmt.leaf(1); | ||
this.jsdc.append('var '); | ||
var id = vardecl.first().first().token().content(); | ||
this.jsdc.append(id); | ||
this.jsdc.append(';exports.' + id + '='); | ||
this.jsdc.ignore(varstmt.first(), 'module6'); | ||
this.jsdc.ignore(node.first(), 'module7'); | ||
break; | ||
case JsNode.FNDECL: | ||
case JsNode.CLASSDECL: | ||
var id = node.last().leaf(1).first().token().content(); | ||
this.jsdc.append('exports.' + id + '='); | ||
this.jsdc.append(id); | ||
this.jsdc.append(';'); | ||
this.jsdc.ignore(node.first(), 'module8'); | ||
break; | ||
}); | ||
} | ||
}, | ||
enter: function() { | ||
if(this.jsdc.define()) { | ||
var s = /(?:\/\/.*\r?\n)?([\s]+)$/.exec(this.jsdc.res); | ||
if(s) { | ||
this.jsdc.insert('define(function(require,exports,module){', this.jsdc.res.length - s[1].length); | ||
} | ||
else { | ||
this.jsdc.append('define(function(require,exports,module){'); | ||
} | ||
else { | ||
var s = leaf.token().content(); | ||
res[s] = s; | ||
} | ||
}, | ||
leave: function() { | ||
this.jsdc.define() && this.jsdc.append('});'); | ||
} | ||
}); | ||
function getIds(node) { | ||
var res = {}; | ||
node.leaves().forEach(function(leaf, i) { | ||
if(i % 2 == 0) { | ||
if(leaf.name() == JsNode.NAMEIMPORT) { | ||
leaf.leaves().forEach(function(leaf, i) { | ||
if(i % 2 == 1) { | ||
var s = leaf.first().token().content(); | ||
if(leaf.size() == 1) { | ||
res[s] = s; | ||
} | ||
else { | ||
res[leaf.last().first().token().content()] = s; | ||
} | ||
} | ||
}); | ||
} | ||
else { | ||
var s = leaf.token().content(); | ||
res[s] = s; | ||
} | ||
} | ||
}); | ||
return res; | ||
} | ||
module.exports = Module; | ||
return res; | ||
} | ||
module.exports = Module; | ||
}); |
@@ -1,26 +0,24 @@ | ||
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 Number = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
}).methods({ | ||
parse: function(t) { | ||
var s = t.content(); | ||
if(s.toLowerCase().indexOf('0b') == 0) { | ||
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, 'num2'); | ||
this.jsdc.append('parseInt("' + t.content().slice(2) + '", 8)'); | ||
} | ||
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 Number = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
}).methods({ | ||
parse: function(t) { | ||
var s = t.content(); | ||
if(s.toLowerCase().indexOf('0b') == 0) { | ||
this.jsdc.ignore(t, 'num1'); | ||
this.jsdc.append('parseInt("' + t.content().slice(2) + '", 2)'); | ||
} | ||
}); | ||
module.exports = Number; | ||
else if(s.toLowerCase().indexOf('0o') == 0) { | ||
this.jsdc.ignore(t, 'num2'); | ||
this.jsdc.append('parseInt("' + t.content().slice(2) + '", 8)'); | ||
} | ||
} | ||
}); | ||
module.exports = Number; | ||
}); |
168
web/Obj.js
@@ -1,61 +0,18 @@ | ||
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 Obj = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.method = {}; | ||
this.temp = {}; | ||
}).methods({ | ||
propt: function(node, start) { | ||
var objltr = node.parent(); | ||
if(objltr.name() == JsNode.OBJLTR) { | ||
var prmr = objltr.parent(); | ||
var parent = prmr.parent(); | ||
if(prmr.name() == JsNode.PRMREXPR | ||
&& !prmr.next() | ||
&& (parent.name() == JsNode.ASSIGNEXPR | ||
|| parent.name() == JsNode.INITLZ)) { | ||
if(node.size() == 1) { | ||
var first = node.first(); | ||
if(first.name() == JsNode.TOKEN) { | ||
if(!start) { | ||
this.jsdc.appendBefore(':' + first.token().content()); | ||
} | ||
} | ||
else if(first.name() == JsNode.METHOD) { | ||
first = first.first(); | ||
this.method[first.nid()] = true; | ||
} | ||
} | ||
else if(node.size() == 3) { | ||
var first = node.first(); | ||
if(start | ||
&& first.name() == JsNode.PROPTNAME | ||
&& (first = first.first()).name() == JsNode.CMPTPROPT) { | ||
var temp = this.jsdc.uid(); | ||
this.hash[objltr.nid()].temp.push({ | ||
temp: temp, | ||
id: join(first) | ||
}); | ||
this.jsdc.append(temp); | ||
this.jsdc.ignore(first, 'obj1'); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
name: function(node, start) { | ||
if(!start && this.method.hasOwnProperty(node.nid())) { | ||
this.jsdc.append(':function'); | ||
} | ||
}, | ||
parse: function(node, start) { | ||
var self = this; | ||
var prmr = node.parent(); | ||
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 Obj = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.method = {}; | ||
this.temp = {}; | ||
}).methods({ | ||
propt: function(node, start) { | ||
var objltr = node.parent(); | ||
if(objltr.name() == JsNode.OBJLTR) { | ||
var prmr = objltr.parent(); | ||
var parent = prmr.parent(); | ||
@@ -66,32 +23,73 @@ if(prmr.name() == JsNode.PRMREXPR | ||
|| parent.name() == JsNode.INITLZ)) { | ||
if(start) { | ||
node.leaves().forEach(function(leaf) { | ||
if(leaf.name() == JsNode.PROPTDEF | ||
&& (leaf = leaf.first()).name() == JsNode.PROPTNAME | ||
&& (leaf = leaf.first()).name() == JsNode.CMPTPROPT) { | ||
var id = self.jsdc.uid(); | ||
self.hash[node.nid()] = { | ||
id: id, | ||
temp: [] | ||
}; | ||
self.jsdc.append('function(){var ' + id + '='); | ||
if(node.size() == 1) { | ||
var first = node.first(); | ||
if(first.name() == JsNode.TOKEN) { | ||
if(!start) { | ||
this.jsdc.appendBefore(':' + first.token().content()); | ||
} | ||
}); | ||
} | ||
else if(first.name() == JsNode.METHOD) { | ||
first = first.first(); | ||
this.method[first.nid()] = true; | ||
} | ||
} | ||
else if(self.hash.hasOwnProperty(node.nid())){ | ||
var h = self.hash[node.nid()]; | ||
var id = h.id; | ||
h.temp.forEach(function(t) { | ||
self.jsdc.appendBefore(';' + id); | ||
self.jsdc.appendBefore(t.id + '=' + id + '.' + t.temp); | ||
self.jsdc.appendBefore(';delete ' + id + '.' + t.temp); | ||
}); | ||
self.jsdc.appendBefore(';return ' + id + '}()'); | ||
else if(node.size() == 3) { | ||
var first = node.first(); | ||
if(start | ||
&& first.name() == JsNode.PROPTNAME | ||
&& (first = first.first()).name() == JsNode.CMPTPROPT) { | ||
var temp = this.jsdc.uid(); | ||
this.hash[objltr.nid()].temp.push({ | ||
temp: temp, | ||
id: join(first) | ||
}); | ||
this.jsdc.append(temp); | ||
this.jsdc.ignore(first, 'obj1'); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
module.exports = Obj; | ||
}, | ||
name: function(node, start) { | ||
if(!start && this.method.hasOwnProperty(node.nid())) { | ||
this.jsdc.append(':function'); | ||
} | ||
}, | ||
parse: function(node, start) { | ||
var self = this; | ||
var prmr = node.parent(); | ||
var parent = prmr.parent(); | ||
if(prmr.name() == JsNode.PRMREXPR | ||
&& !prmr.next() | ||
&& (parent.name() == JsNode.ASSIGNEXPR | ||
|| parent.name() == JsNode.INITLZ)) { | ||
if(start) { | ||
node.leaves().forEach(function(leaf) { | ||
if(leaf.name() == JsNode.PROPTDEF | ||
&& (leaf = leaf.first()).name() == JsNode.PROPTNAME | ||
&& (leaf = leaf.first()).name() == JsNode.CMPTPROPT) { | ||
var id = self.jsdc.uid(); | ||
self.hash[node.nid()] = { | ||
id: id, | ||
temp: [] | ||
}; | ||
self.jsdc.append('function(){var ' + id + '='); | ||
} | ||
}); | ||
} | ||
else if(self.hash.hasOwnProperty(node.nid())){ | ||
var h = self.hash[node.nid()]; | ||
var id = h.id; | ||
h.temp.forEach(function(t) { | ||
self.jsdc.appendBefore(';' + id); | ||
self.jsdc.appendBefore(t.id + '=' + id + '.' + t.temp); | ||
self.jsdc.appendBefore(';delete ' + id + '.' + t.temp); | ||
}); | ||
self.jsdc.appendBefore(';return ' + id + '}()'); | ||
} | ||
} | ||
} | ||
}); | ||
module.exports = Obj; | ||
}); |
384
web/Rest.js
@@ -1,215 +0,213 @@ | ||
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'); | ||
var Rest = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.hash2 = {}; | ||
this.hash3 = {}; | ||
}).methods({ | ||
param: function(fmparams) { | ||
if(fmparams.name() == JsNode.FMPARAMS && fmparams.size()) { | ||
var last = fmparams.last(); | ||
var fnbody = fmparams.next().next().next(); | ||
if(last.name() == JsNode.BINDREST) { | ||
var rest = last.first(); | ||
this.jsdc.ignore(rest, 'rest1'); | ||
this.hash[fnbody.nid()] = { | ||
index: fmparams.size() - 1, | ||
token: last.last().first().token() | ||
}; | ||
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'); | ||
var Rest = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.hash2 = {}; | ||
this.hash3 = {}; | ||
}).methods({ | ||
param: function(fmparams) { | ||
if(fmparams.name() == JsNode.FMPARAMS && fmparams.size()) { | ||
var last = fmparams.last(); | ||
var fnbody = fmparams.next().next().next(); | ||
if(last.name() == JsNode.BINDREST) { | ||
var rest = last.first(); | ||
this.jsdc.ignore(rest, 'rest1'); | ||
this.hash[fnbody.nid()] = { | ||
index: fmparams.size() - 1, | ||
token: last.last().first().token() | ||
}; | ||
} | ||
} | ||
}, | ||
enter: function(fnbody) { | ||
if(this.hash.hasOwnProperty(fnbody.nid())) { | ||
var o = this.hash[fnbody.nid()]; | ||
var index = o.index; | ||
var id = o.token.content(); | ||
this.jsdc.append(id + '=[].slice.call(arguments, ' + index + ');'); | ||
} | ||
}, | ||
expr: function(node) { | ||
var args = node.last(); | ||
var arglist = args.leaf(1); | ||
var self = this; | ||
if(arglist.size() > 1) { | ||
var last = arglist.last(); | ||
var spread = last.prev(); | ||
if(spread.name() == JsNode.TOKEN && spread.token().content() == '...') { | ||
var first = node.first(); | ||
var needTemp = self.needTemp(first).ret; | ||
var temp = needTemp ? self.jsdc.uid() : ''; | ||
self.hash2[node.nid()] = { | ||
node: first, | ||
needTemp: needTemp, | ||
temp: needTemp ? temp : '' | ||
}; | ||
//主表达式中含有生成的对象,不是直接引用,需创建一个临时变量保存引用 | ||
if(needTemp) { | ||
self.jsdc.append('function(){var ' + temp + '='); | ||
var first = first.first(); | ||
eventbus.on(first.nid(), function(node2, start) { | ||
if(!start) { | ||
self.jsdc.append(';return ' + temp); | ||
} | ||
}); | ||
} | ||
self.jsdc.ignore(arglist, 'rest2'); | ||
} | ||
}, | ||
enter: function(fnbody) { | ||
if(this.hash.hasOwnProperty(fnbody.nid())) { | ||
var o = this.hash[fnbody.nid()]; | ||
var index = o.index; | ||
var id = o.token.content(); | ||
this.jsdc.append(id + '=[].slice.call(arguments, ' + index + ');'); | ||
} | ||
}, | ||
needTemp: function(node, res) { | ||
res = res || { ret: false }; | ||
var self = this; | ||
if(res.ret) { | ||
return res; | ||
} | ||
var isToken = node.name() == JsNode.TOKEN; | ||
if(!isToken) { | ||
if([JsNode.CALLEXPR, JsNode.NEWEXPR].indexOf(node.first().name()) > -1) { | ||
res.ret = true; | ||
} | ||
}, | ||
expr: function(node) { | ||
var args = node.last(); | ||
var arglist = args.leaf(1); | ||
var self = this; | ||
if(arglist.size() > 1) { | ||
var last = arglist.last(); | ||
var spread = last.prev(); | ||
if(spread.name() == JsNode.TOKEN && spread.token().content() == '...') { | ||
var first = node.first(); | ||
var needTemp = self.needTemp(first).ret; | ||
var temp = needTemp ? self.jsdc.uid() : ''; | ||
self.hash2[node.nid()] = { | ||
node: first, | ||
needTemp: needTemp, | ||
temp: needTemp ? temp : '' | ||
}; | ||
//主表达式中含有生成的对象,不是直接引用,需创建一个临时变量保存引用 | ||
if(needTemp) { | ||
self.jsdc.append('function(){var ' + temp + '='); | ||
var first = first.first(); | ||
eventbus.on(first.nid(), function(node2, start) { | ||
if(!start) { | ||
self.jsdc.append(';return ' + temp); | ||
} | ||
}); | ||
} | ||
self.jsdc.ignore(arglist, 'rest2'); | ||
} | ||
} | ||
return res; | ||
}, | ||
args: function(node) { | ||
var parent = node.parent(); | ||
if(parent.name() == JsNode.CALLEXPR && this.hash2.hasOwnProperty(parent.nid())) { | ||
this.jsdc.append('.apply'); | ||
} | ||
}, | ||
arglist: function(node) { | ||
var parent = node.parent().parent(); | ||
if(parent.name() == JsNode.CALLEXPR && this.hash2.hasOwnProperty(parent.nid())) { | ||
var o = this.hash2[parent.nid()]; | ||
if(o.needTemp) { | ||
//主表达式中含有生成的对象,不是直接引用,使用临时变量引用 | ||
this.jsdc.append(o.temp); | ||
} | ||
}, | ||
needTemp: function(node, res) { | ||
res = res || { ret: false }; | ||
var self = this; | ||
if(res.ret) { | ||
return res; | ||
else { | ||
//主表达式无需设置apply的context,成员需设 | ||
var mmb = this.hash2[parent.nid()].node; | ||
this.jsdc.append(mmb.name() == JsNode.MMBEXPR ? join(mmb.first()) : 'this'); | ||
} | ||
var isToken = node.name() == JsNode.TOKEN; | ||
if(!isToken) { | ||
if([JsNode.CALLEXPR, JsNode.NEWEXPR].indexOf(node.first().name()) > -1) { | ||
res.ret = true; | ||
} | ||
this.jsdc.append(', ['); | ||
var leaves = node.leaves(); | ||
for(var i = 0; i < leaves.length - 3; i++) { | ||
this.jsdc.append(join(leaves[i])); | ||
} | ||
return res; | ||
}, | ||
args: function(node) { | ||
var parent = node.parent(); | ||
if(parent.name() == JsNode.CALLEXPR && this.hash2.hasOwnProperty(parent.nid())) { | ||
this.jsdc.append('.apply'); | ||
this.jsdc.append(']'); | ||
this.jsdc.append('.concat(function(){'); | ||
var last = node.last(); | ||
var isPrm = last.name() == JsNode.PRMREXPR; | ||
var v; | ||
if(isPrm) { | ||
v = last.first().token().content(); | ||
} | ||
}, | ||
arglist: function(node) { | ||
var parent = node.parent().parent(); | ||
if(parent.name() == JsNode.CALLEXPR && this.hash2.hasOwnProperty(parent.nid())) { | ||
var o = this.hash2[parent.nid()]; | ||
if(o.needTemp) { | ||
//主表达式中含有生成的对象,不是直接引用,使用临时变量引用 | ||
this.jsdc.append(o.temp); | ||
} | ||
else { | ||
//主表达式无需设置apply的context,成员需设 | ||
var mmb = this.hash2[parent.nid()].node; | ||
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(join(leaves[i])); | ||
} | ||
this.jsdc.append(']'); | ||
this.jsdc.append('.concat(function(){'); | ||
var last = node.last(); | ||
var isPrm = last.name() == JsNode.PRMREXPR; | ||
var v; | ||
if(isPrm) { | ||
v = last.first().token().content(); | ||
} | ||
else { | ||
v = join(last); | ||
} | ||
var temp = this.jsdc.uid(); | ||
var temp2 = this.jsdc.uid(); | ||
this.jsdc.append('var ' + temp + '=[],' + temp2); | ||
if(!isPrm) { | ||
var temp3 = this.jsdc.uid(); | ||
this.jsdc.append(',' + temp3 + '=' + v); | ||
} | ||
this.jsdc.append(';while(!(' + temp2 + '=' + (isPrm ? v : temp3) + '.next()).done)'); | ||
this.jsdc.append(temp + '.push(' + temp2 + '.value' + ')'); | ||
this.jsdc.append(';return ' + temp + '}())'); | ||
if(o.needTemp) { | ||
//主表达式中含有生成的对象,不是直接引用 | ||
this.jsdc.append(')}('); | ||
} | ||
else { | ||
v = join(last); | ||
} | ||
}, | ||
comma: function(node) { | ||
var next = node.next(); | ||
if(next.name() == JsNode.SPREAD && !node.destruct) { | ||
this.jsdc.ignore(node); | ||
var temp = this.jsdc.uid(); | ||
var temp2 = this.jsdc.uid(); | ||
this.jsdc.append('var ' + temp + '=[],' + temp2); | ||
if(!isPrm) { | ||
var temp3 = this.jsdc.uid(); | ||
this.jsdc.append(',' + temp3 + '=' + v); | ||
} | ||
}, | ||
spread: function(node, start) { | ||
if(node.destruct || node.parent().destruct) { | ||
return; | ||
this.jsdc.append(';while(!(' + temp2 + '=' + (isPrm ? v : temp3) + '.next()).done)'); | ||
this.jsdc.append(temp + '.push(' + temp2 + '.value' + ')'); | ||
this.jsdc.append(';return ' + temp + '}())'); | ||
if(o.needTemp) { | ||
//主表达式中含有生成的对象,不是直接引用 | ||
this.jsdc.append(')}('); | ||
} | ||
if(start) { | ||
var last = node.last(); | ||
var s = join(last); | ||
this.hash3[node.nid()] = { | ||
isStr: last.name() == JsNode.PRMREXPR | ||
&& last.first().name() == JsNode.TOKEN | ||
&& last.first().token().type() == Token.STRING, | ||
isPrm: last.name() == JsNode.PRMREXPR, | ||
value: join(last) | ||
}; | ||
this.jsdc.ignore(node); | ||
} | ||
}, | ||
comma: function(node) { | ||
var next = node.next(); | ||
if(next.name() == JsNode.SPREAD && !node.destruct) { | ||
this.jsdc.ignore(node); | ||
} | ||
}, | ||
spread: function(node, start) { | ||
if(node.destruct || node.parent().destruct) { | ||
return; | ||
} | ||
if(start) { | ||
var last = node.last(); | ||
var s = join(last); | ||
this.hash3[node.nid()] = { | ||
isStr: last.name() == JsNode.PRMREXPR | ||
&& last.first().name() == JsNode.TOKEN | ||
&& last.first().token().type() == Token.STRING, | ||
isPrm: last.name() == JsNode.PRMREXPR, | ||
value: join(last) | ||
}; | ||
this.jsdc.ignore(node); | ||
} | ||
else if(this.hash3.hasOwnProperty(node.nid())) { | ||
var o = this.hash3[node.nid()]; | ||
var prev = node.prev(); | ||
//开始的[ | ||
if(prev.token().content() == '[') { | ||
this.jsdc.appendBefore(']'); | ||
} | ||
else if(this.hash3.hasOwnProperty(node.nid())) { | ||
var o = this.hash3[node.nid()]; | ||
var prev = node.prev(); | ||
//开始的[ | ||
if(prev.token().content() == '[') { | ||
//中间情况是,号 | ||
else { | ||
prev = prev.prev(); | ||
if(prev.name() != JsNode.SPREAD) { | ||
this.jsdc.appendBefore(']'); | ||
} | ||
//中间情况是,号 | ||
else { | ||
//隔了一个普通数组值之前又是spread,即2个spread不相邻 | ||
prev = prev.prev(); | ||
if(prev.name() != JsNode.SPREAD) { | ||
this.jsdc.appendBefore(']'); | ||
//隔了一个普通数组值之前又是spread,即2个spread不相邻 | ||
if(prev) { | ||
prev = prev.prev(); | ||
if(prev) { | ||
prev = prev.prev(); | ||
if(prev && prev.name() == JsNode.SPREAD) { | ||
this.jsdc.appendBefore(')'); | ||
} | ||
if(prev && prev.name() == JsNode.SPREAD) { | ||
this.jsdc.appendBefore(')'); | ||
} | ||
} | ||
} | ||
this.jsdc.appendBefore('.concat('); | ||
if(o.isStr) { | ||
this.jsdc.appendBefore(o.value); | ||
this.jsdc.appendBefore('.split("")'); | ||
} | ||
this.jsdc.appendBefore('.concat('); | ||
if(o.isStr) { | ||
this.jsdc.appendBefore(o.value); | ||
this.jsdc.appendBefore('.split("")'); | ||
} | ||
else { | ||
this.jsdc.appendBefore('function(){var '); | ||
var temp = this.jsdc.uid(); | ||
var temp2 = this.jsdc.uid(); | ||
this.jsdc.appendBefore(temp); | ||
this.jsdc.appendBefore('=[],' + temp2); | ||
var temp3; | ||
if(!o.isPrm) { | ||
temp3 = this.jsdc.uid(); | ||
this.jsdc.appendBefore(',' + temp3 + '=' + o.value); | ||
} | ||
else { | ||
this.jsdc.appendBefore('function(){var '); | ||
var temp = this.jsdc.uid(); | ||
var temp2 = this.jsdc.uid(); | ||
this.jsdc.appendBefore(temp); | ||
this.jsdc.appendBefore('=[],' + temp2); | ||
var temp3; | ||
if(!o.isPrm) { | ||
temp3 = this.jsdc.uid(); | ||
this.jsdc.appendBefore(',' + temp3 + '=' + o.value); | ||
} | ||
this.jsdc.appendBefore(';while(!(' + temp2 + '=' + (o.isPrm ? o.value : temp3) + '.next()).done)'); | ||
this.jsdc.appendBefore(temp + '.push(' + temp2 + '.value);return ' + temp + '}()'); | ||
this.jsdc.appendBefore(';while(!(' + temp2 + '=' + (o.isPrm ? o.value : temp3) + '.next()).done)'); | ||
this.jsdc.appendBefore(temp + '.push(' + temp2 + '.value);return ' + temp + '}()'); | ||
} | ||
this.jsdc.appendBefore(')'); | ||
var next = node.next(); | ||
if(next.token().content() == ']') { | ||
this.jsdc.ignore(next); | ||
} | ||
else { | ||
this.jsdc.ignore(next); | ||
next = next.next(); | ||
if(next.name() != JsNode.SPREAD) { | ||
this.jsdc.appendBefore('.concat(['); | ||
} | ||
this.jsdc.appendBefore(')'); | ||
var next = node.next(); | ||
if(next.token().content() == ']') { | ||
this.jsdc.ignore(next); | ||
} | ||
else { | ||
this.jsdc.ignore(next); | ||
next = next.next(); | ||
if(next.name() != JsNode.SPREAD) { | ||
this.jsdc.appendBefore('.concat(['); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
module.exports = Rest; | ||
} | ||
}); | ||
module.exports = Rest; | ||
}); |
310
web/Scope.js
@@ -1,174 +0,172 @@ | ||
define(function(require, exports, module) { | ||
var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Class = require('./util/Class'); | ||
var SCOPE = {}; | ||
SCOPE[JsNode.BLOCK] = | ||
SCOPE[JsNode.FNBODY] = true; | ||
var NOT_ABS_BLOCK = {}; | ||
NOT_ABS_BLOCK[JsNode.ITERSTMT] = | ||
NOT_ABS_BLOCK[JsNode.IFSTMT] = true; | ||
var Scope = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.history = {}; | ||
this.index = [jsdc.res.length]; | ||
}).methods({ | ||
parse: function(node) { | ||
this.recursion(node); | ||
}, | ||
recursion: function(node) { | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
if(!isToken) { | ||
var name = node.name(); | ||
//每个{}作用域记录是否有lexdecl或fn或generator或classdecl | ||
if(name == JsNode.LEXDECL | ||
|| name == JsNode.FNDECL | ||
|| name == JsNode.GENDECL | ||
|| name == JsNode.CLASSDECL) { | ||
var parent = self.closest(node); | ||
//全局lexdecl没有作用域无需记录,fnbody的也无需记录 | ||
if(parent && parent.name() != JsNode.FNBODY) { | ||
self.hash[parent.nid()] = true; | ||
} | ||
define(function(require, exports, module){var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Class = require('./util/Class'); | ||
var SCOPE = {}; | ||
SCOPE[JsNode.BLOCK] = | ||
SCOPE[JsNode.FNBODY] = true; | ||
var NOT_ABS_BLOCK = {}; | ||
NOT_ABS_BLOCK[JsNode.ITERSTMT] = | ||
NOT_ABS_BLOCK[JsNode.IFSTMT] = true; | ||
var Scope = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
this.hash = {}; | ||
this.history = {}; | ||
this.index = [jsdc.res.length]; | ||
}).methods({ | ||
parse: function(node) { | ||
this.recursion(node); | ||
}, | ||
recursion: function(node) { | ||
var self = this; | ||
var isToken = node.name() == JsNode.TOKEN; | ||
if(!isToken) { | ||
var name = node.name(); | ||
//每个{}作用域记录是否有lexdecl或fn或generator或classdecl | ||
if(name == JsNode.LEXDECL | ||
|| name == JsNode.FNDECL | ||
|| name == JsNode.GENDECL | ||
|| name == JsNode.CLASSDECL) { | ||
var parent = self.closest(node); | ||
//全局lexdecl没有作用域无需记录,fnbody的也无需记录 | ||
if(parent && parent.name() != JsNode.FNBODY) { | ||
self.hash[parent.nid()] = true; | ||
} | ||
else if(name == JsNode.BLOCKSTMT | ||
&& !NOT_ABS_BLOCK.hasOwnProperty(node.parent().name())) { | ||
self.hash[node.first().nid()] = true; | ||
} | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf); | ||
}); | ||
} | ||
}, | ||
prevar: function(varstmt) { | ||
var self = this; | ||
var gen = self.inGen(varstmt); | ||
//genarator的忽略 | ||
if(gen) { | ||
varstmt.gen = gen; | ||
return; | ||
else if(name == JsNode.BLOCKSTMT | ||
&& !NOT_ABS_BLOCK.hasOwnProperty(node.parent().name())) { | ||
self.hash[node.first().nid()] = true; | ||
} | ||
var parent = self.closest(varstmt); | ||
//插入声明的变量到作用域开始,并删除这个var | ||
var i = self.index[self.index.length - 1]; | ||
self.history[i] = self.history[i] || {}; | ||
var his = self.history[i]; | ||
var vardecls = varstmt.leaves().filter(function(o, i) { | ||
return i % 2 == 1; | ||
node.leaves().forEach(function(leaf) { | ||
self.recursion(leaf); | ||
}); | ||
vardecls.forEach(function(vardecl) { | ||
if(vardecl.first().name() != JsNode.BINDID | ||
|| parent | ||
&& self.hash[parent.nid()]) { | ||
self.join(vardecl).forEach(function(id) { | ||
if(!his.hasOwnProperty(id)) { | ||
his[id] = true; | ||
self.jsdc.insert('var ' + id + ';', i); | ||
} | ||
}); | ||
if(vardecl.first().name() == JsNode.BINDID) { | ||
self.jsdc.ignore(varstmt.first().token(), 'scope1'); | ||
} | ||
}, | ||
prevar: function(varstmt) { | ||
var self = this; | ||
var gen = self.inGen(varstmt); | ||
//genarator的忽略 | ||
if(gen) { | ||
varstmt.gen = gen; | ||
return; | ||
} | ||
var parent = self.closest(varstmt); | ||
//插入声明的变量到作用域开始,并删除这个var | ||
var i = self.index[self.index.length - 1]; | ||
self.history[i] = self.history[i] || {}; | ||
var his = self.history[i]; | ||
var vardecls = varstmt.leaves().filter(function(o, i) { | ||
return i % 2 == 1; | ||
}); | ||
vardecls.forEach(function(vardecl) { | ||
if(vardecl.first().name() != JsNode.BINDID | ||
|| parent | ||
&& self.hash[parent.nid()]) { | ||
self.join(vardecl).forEach(function(id) { | ||
if(!his.hasOwnProperty(id)) { | ||
his[id] = true; | ||
self.jsdc.insert('var ' + id + ';', i); | ||
} | ||
else { | ||
//destruct需忽略前后可能的,再改为; var也需忽略 | ||
self.jsdc.ignore(vardecl.prev(), 'scope2'); | ||
var next = vardecl.next(); | ||
if(next.token().content() == ',') { | ||
self.jsdc.ignore(next, 'scope3'); | ||
} | ||
}); | ||
if(vardecl.first().name() == JsNode.BINDID) { | ||
self.jsdc.ignore(varstmt.first().token(), 'scope1'); | ||
} | ||
else { | ||
//destruct需忽略前后可能的,再改为; var也需忽略 | ||
self.jsdc.ignore(vardecl.prev(), 'scope2'); | ||
var next = vardecl.next(); | ||
if(next.token().content() == ',') { | ||
self.jsdc.ignore(next, 'scope3'); | ||
} | ||
} | ||
}); | ||
}, | ||
join: function(node) { | ||
var first = node.first(); | ||
var res = []; | ||
switch(first.name()) { | ||
case JsNode.BINDID: | ||
res.push(first.first().token().content()); | ||
break; | ||
case JsNode.ARRBINDPAT: | ||
case JsNode.OBJBINDPAT: | ||
res = res.concat(this.jsdc.destruct.getIds(first)); | ||
break; | ||
} | ||
return res; | ||
}, | ||
enter: function(node) { | ||
this.index.push(this.jsdc.res.length); | ||
}, | ||
leave: function(node) { | ||
this.index.pop(); | ||
}, | ||
block: function(node, start) { | ||
switch(node.name()) { | ||
case JsNode.BLOCK: | ||
node = node.parent(); | ||
var pname = node.name(); | ||
if(pname == JsNode.BLOCKSTMT) { | ||
pname = node.parent().name(); | ||
//纯block父节点为blockstmt且祖父节点不是iteratorstmt,ifstmt | ||
//try,catch,final已在父节点不是blockstmt排除 | ||
if(!NOT_ABS_BLOCK.hasOwnProperty(pname) | ||
|| this.hash.hasOwnProperty(node.nid())) { | ||
if(start) { | ||
this.jsdc.append('!function()'); | ||
} | ||
else { | ||
this.jsdc.appendBefore('();'); | ||
} | ||
}); | ||
}, | ||
join: function(node) { | ||
var first = node.first(); | ||
var res = []; | ||
switch(first.name()) { | ||
case JsNode.BINDID: | ||
res.push(first.first().token().content()); | ||
break; | ||
case JsNode.ARRBINDPAT: | ||
case JsNode.OBJBINDPAT: | ||
res = res.concat(this.jsdc.destruct.getIds(first)); | ||
break; | ||
} | ||
return res; | ||
}, | ||
enter: function(node) { | ||
this.index.push(this.jsdc.res.length); | ||
}, | ||
leave: function(node) { | ||
this.index.pop(); | ||
}, | ||
block: function(node, start) { | ||
switch(node.name()) { | ||
case JsNode.BLOCK: | ||
node = node.parent(); | ||
var pname = node.name(); | ||
if(pname == JsNode.BLOCKSTMT) { | ||
pname = node.parent().name(); | ||
//纯block父节点为blockstmt且祖父节点不是iteratorstmt,ifstmt | ||
//try,catch,final已在父节点不是blockstmt排除 | ||
if(!NOT_ABS_BLOCK.hasOwnProperty(pname) | ||
|| this.hash.hasOwnProperty(node.nid())) { | ||
if(start) { | ||
this.jsdc.append('!function()'); | ||
} | ||
else { | ||
this.jsdc.appendBefore('();'); | ||
} | ||
} | ||
break; | ||
//{和}需要添加匿名函数,排除纯block,即父节点不为blockstmt或祖父节点不为iteratorstmt,ifstmt | ||
case JsNode.TOKEN: | ||
} | ||
break; | ||
//{和}需要添加匿名函数,排除纯block,即父节点不为blockstmt或祖父节点不为iteratorstmt,ifstmt | ||
case JsNode.TOKEN: | ||
node = node.parent(); | ||
if(node.name() == JsNode.BLOCK | ||
&& this.hash.hasOwnProperty(node.nid())) { | ||
node = node.parent(); | ||
if(node.name() == JsNode.BLOCK | ||
&& this.hash.hasOwnProperty(node.nid())) { | ||
node = node.parent(); | ||
if(node.name() != JsNode.BLOCKSTMT | ||
|| NOT_ABS_BLOCK.hasOwnProperty(node.parent().name())) { | ||
if(start) { | ||
this.jsdc.append('!function(){'); | ||
} | ||
else { | ||
this.jsdc.appendBefore('}();'); | ||
} | ||
if(node.name() != JsNode.BLOCKSTMT | ||
|| NOT_ABS_BLOCK.hasOwnProperty(node.parent().name())) { | ||
if(start) { | ||
this.jsdc.append('!function(){'); | ||
} | ||
else { | ||
this.jsdc.appendBefore('}();'); | ||
} | ||
} | ||
break; | ||
} | ||
break; | ||
} | ||
}, | ||
closest: function(node) { | ||
var parent = node; | ||
while(parent = parent.parent()) { | ||
if(SCOPE.hasOwnProperty(parent.name())) { | ||
return parent; | ||
} | ||
}, | ||
closest: function(node) { | ||
var parent = node; | ||
while(parent = parent.parent()) { | ||
if(SCOPE.hasOwnProperty(parent.name())) { | ||
} | ||
}, | ||
inGen: function(node) { | ||
var parent = node; | ||
while(parent = parent.parent()) { | ||
switch(parent.name()) { | ||
case JsNode.GENDECL: | ||
case JsNode.GENEXPR: | ||
return parent; | ||
} | ||
case JsNode.FNDECL: | ||
case JsNode.FNEXPR: | ||
case JsNode.CLASSDECL: | ||
case JsNode.CLASSEXPR: | ||
case JsNode.METHOD: | ||
return false; | ||
} | ||
}, | ||
inGen: function(node) { | ||
var parent = node; | ||
while(parent = parent.parent()) { | ||
switch(parent.name()) { | ||
case JsNode.GENDECL: | ||
case JsNode.GENEXPR: | ||
return parent; | ||
case JsNode.FNDECL: | ||
case JsNode.FNEXPR: | ||
case JsNode.CLASSDECL: | ||
case JsNode.CLASSEXPR: | ||
case JsNode.METHOD: | ||
return false; | ||
} | ||
} | ||
} | ||
}); | ||
module.exports = Scope; | ||
} | ||
}); | ||
module.exports = Scope; | ||
}); |
102
web/Str.js
@@ -1,58 +0,56 @@ | ||
define(function(require, exports, module) { | ||
var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Class = require('./util/Class'); | ||
var String = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
}).methods({ | ||
parse: function(t) { | ||
var s = t.content(); | ||
var res = s.replace(/\\u\{([\da-f]+)\}/ig, function(m, n, i) { | ||
var count = 0; | ||
for(i -= 1; i > 0; i--) { | ||
if(s.charAt(i) == '\\') { | ||
count++; | ||
} | ||
else { | ||
break; | ||
} | ||
define(function(require, exports, module){var homunculus = require('homunculus'); | ||
var JsNode = homunculus.getClass('Node', 'es6'); | ||
var Class = require('./util/Class'); | ||
var String = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
}).methods({ | ||
parse: function(t) { | ||
var s = t.content(); | ||
var res = s.replace(/\\u\{([\da-f]+)\}/ig, function(m, n, i) { | ||
var count = 0; | ||
for(i -= 1; i > 0; i--) { | ||
if(s.charAt(i) == '\\') { | ||
count++; | ||
} | ||
if(count % 2 == 1) { | ||
return m; | ||
else { | ||
break; | ||
} | ||
var arr = []; | ||
n = parseInt(n, 16); | ||
while(n > 0xFFFF) { | ||
arr.push('FFFF'); | ||
n -= 0xFFFF; | ||
} | ||
arr.push(fix(n.toString(16))); | ||
return '\\u' + arr.join('\\u'); | ||
}); | ||
if(res != s) { | ||
this.jsdc.ignore(t, 'str1'); | ||
this.jsdc.append(res); | ||
} | ||
if(count % 2 == 1) { | ||
return m; | ||
} | ||
var arr = []; | ||
n = parseInt(n, 16); | ||
while(n > 0xFFFF) { | ||
arr.push('FFFF'); | ||
n -= 0xFFFF; | ||
} | ||
arr.push(fix(n.toString(16))); | ||
return '\\u' + arr.join('\\u'); | ||
}); | ||
if(res != s) { | ||
this.jsdc.ignore(t, 'str1'); | ||
this.jsdc.append(res); | ||
} | ||
}); | ||
function fix(n) { | ||
var i = 4 - n.length; | ||
switch(i) { | ||
case -1: | ||
case 0: | ||
return n; | ||
case 1: | ||
return '0' + n; | ||
case 2: | ||
return '00' + n; | ||
case 3: | ||
return '000' + n; | ||
} | ||
} | ||
module.exports = String; | ||
}); | ||
function fix(n) { | ||
var i = 4 - n.length; | ||
switch(i) { | ||
case -1: | ||
case 0: | ||
return n; | ||
case 1: | ||
return '0' + n; | ||
case 2: | ||
return '00' + n; | ||
case 3: | ||
return '000' + n; | ||
} | ||
} | ||
module.exports = String; | ||
}); |
@@ -1,54 +0,72 @@ | ||
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 Template = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
}).methods({ | ||
parse: function(t) { | ||
this.jsdc.ignore(t, 'template1'); | ||
var s = t.content(); | ||
var res = '"'; | ||
var has = false; | ||
outer: | ||
for(var i = 1; i < s.length - 1; i++) { | ||
var c = s.charAt(i); | ||
if(c == '\\') { | ||
res += '\\'; | ||
res += s.charAt(++i); | ||
continue; | ||
} | ||
else if(c == '"') { | ||
res += '\\'; | ||
} | ||
else if(c == '$') { | ||
if(s.charAt(i + 1) == '{') { | ||
has = true; | ||
res += '" + '; | ||
for(i = i + 2; i < s.length - 1; i++) { | ||
c = s.charAt(i); | ||
if(c == '}') { | ||
res += ' + "'; | ||
continue outer; | ||
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 Template = Class(function(jsdc) { | ||
this.jsdc = jsdc; | ||
}).methods({ | ||
parse: function(t) { | ||
this.jsdc.ignore(t, 'template1'); | ||
var s = t.content(); | ||
var res = '"'; | ||
var has = false; | ||
outer: | ||
for(var i = 1; i < s.length - 1; i++) { | ||
var c = s.charAt(i); | ||
if(c == '\\') { | ||
res += '\\'; | ||
res += s.charAt(++i); | ||
continue; | ||
} | ||
else if(c == '"') { | ||
res += '\\'; | ||
} | ||
else if(c == '\n') { | ||
res += '\\'; | ||
} | ||
else if(c == '$') { | ||
if(s.charAt(i + 1) == '{') { | ||
has = true; | ||
res += '" + '; | ||
var multi = false; | ||
for(var j = i + 2; j < s.length - 1; j++) { | ||
c = s.charAt(j); | ||
if(c == '}') { | ||
break; | ||
} | ||
else if(!/[a-z_\s]/i.test(c)) { | ||
multi = true; | ||
break; | ||
} | ||
} | ||
if(multi) { | ||
res += '('; | ||
} | ||
for(i = i + 2; i < s.length - 1; i++) { | ||
c = s.charAt(i); | ||
if(c == '}') { | ||
if(multi) { | ||
res += ')'; | ||
} | ||
else { | ||
res += c; | ||
} | ||
res += ' + "'; | ||
continue outer; | ||
} | ||
else { | ||
res += c; | ||
} | ||
} | ||
} | ||
res += c; | ||
} | ||
res += '"'; | ||
this.jsdc.append((has ? '(' : '') | ||
+ res.replace(/^""\s\+\s/, '').replace(/\s\+\s""$/, '') | ||
+ (has ? ')' : '')); | ||
res += c; | ||
} | ||
}); | ||
module.exports = Template; | ||
res += '"'; | ||
this.jsdc.append((has ? '(' : '') | ||
+ res.replace(/^""\s\+\s/, '').replace(/\s\+\s""$/, '') | ||
+ (has ? ')' : '')); | ||
} | ||
}); | ||
module.exports = Template; | ||
}); |
@@ -1,54 +0,52 @@ | ||
define(function(require, exports, module) { | ||
exports.LINE = '\n'; | ||
exports.ENTER = '\r'; | ||
exports.BLANK = ' '; | ||
exports.TAB = '\t'; | ||
exports.UNDERLINE = '_'; | ||
exports.DOLLAR = '$'; | ||
exports.SHARP = '#'; | ||
exports.MINUS = '-'; | ||
exports.AT = '@'; | ||
exports.SLASH = '/'; | ||
exports.BACK_SLASH = '\\'; | ||
exports.DECIMAL = '.'; | ||
exports.LEFT_BRACKET = '['; | ||
exports.RIGHT_BRACKET = ']'; | ||
exports.STAR = '*'; | ||
exports.LEFT_PARENTHESE = '('; | ||
exports.RIGHT_PARENTHESE = ')'; | ||
exports.COMMA = ','; | ||
exports.SEMICOLON = ';'; | ||
exports.EQUAL = '='; | ||
exports.isDigit = function(c) { | ||
return c >= '0' && c <= '9'; | ||
}; | ||
exports.isDigit16 = function(c) { | ||
return exports.isDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); | ||
}; | ||
exports.isDigit2 = function(c) { | ||
return c == '0' || c == '1'; | ||
}; | ||
exports.isDigit8 = function(c) { | ||
return c >= '0' && c <= '7'; | ||
}; | ||
exports.isLetter = function(c) { | ||
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); | ||
}; | ||
exports.count = function(s, c) { | ||
var count = 0, | ||
i = -1; | ||
while((i = s.indexOf(c, i + 1)) != -1) { | ||
count++; | ||
} | ||
return count; | ||
}; | ||
exports.isUndefined = function(s) { | ||
return typeof s == 'undefined'; | ||
}; | ||
exports.isString = function(s) { | ||
return Object.prototype.toString.call(s) == "[object String]"; | ||
}; | ||
exports.isNumber = function(s) { | ||
return Object.prototype.toString.call(s) == "[object Number]"; | ||
}; | ||
}); | ||
define(function(require, exports, module){exports.LINE = '\n'; | ||
exports.ENTER = '\r'; | ||
exports.BLANK = ' '; | ||
exports.TAB = '\t'; | ||
exports.UNDERLINE = '_'; | ||
exports.DOLLAR = '$'; | ||
exports.SHARP = '#'; | ||
exports.MINUS = '-'; | ||
exports.AT = '@'; | ||
exports.SLASH = '/'; | ||
exports.BACK_SLASH = '\\'; | ||
exports.DECIMAL = '.'; | ||
exports.LEFT_BRACKET = '['; | ||
exports.RIGHT_BRACKET = ']'; | ||
exports.STAR = '*'; | ||
exports.LEFT_PARENTHESE = '('; | ||
exports.RIGHT_PARENTHESE = ')'; | ||
exports.COMMA = ','; | ||
exports.SEMICOLON = ';'; | ||
exports.EQUAL = '='; | ||
exports.isDigit = function(c) { | ||
return c >= '0' && c <= '9'; | ||
}; | ||
exports.isDigit16 = function(c) { | ||
return exports.isDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); | ||
}; | ||
exports.isDigit2 = function(c) { | ||
return c == '0' || c == '1'; | ||
}; | ||
exports.isDigit8 = function(c) { | ||
return c >= '0' && c <= '7'; | ||
}; | ||
exports.isLetter = function(c) { | ||
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); | ||
}; | ||
exports.count = function(s, c) { | ||
var count = 0, | ||
i = -1; | ||
while((i = s.indexOf(c, i + 1)) != -1) { | ||
count++; | ||
} | ||
return count; | ||
}; | ||
exports.isUndefined = function(s) { | ||
return typeof s == 'undefined'; | ||
}; | ||
exports.isString = function(s) { | ||
return Object.prototype.toString.call(s) == "[object String]"; | ||
}; | ||
exports.isNumber = function(s) { | ||
return Object.prototype.toString.call(s) == "[object Number]"; | ||
};}); |
@@ -1,36 +0,34 @@ | ||
define(function(require, exports, module) { | ||
function inheritPrototype(subType, superType) { | ||
var prototype = Object.create(superType.prototype); | ||
prototype.constructor = subType; | ||
subType.prototype = prototype; | ||
//继承static变量 | ||
Object.keys(superType).forEach(function(k) { | ||
subType[k] = superType[k]; | ||
define(function(require, exports, module){function inheritPrototype(subType, superType) { | ||
var prototype = Object.create(superType.prototype); | ||
prototype.constructor = subType; | ||
subType.prototype = prototype; | ||
//继承static变量 | ||
Object.keys(superType).forEach(function(k) { | ||
subType[k] = superType[k]; | ||
}); | ||
return subType; | ||
} | ||
function wrap(fn) { | ||
fn.extend = function(sub) { | ||
inheritPrototype(sub, fn); | ||
return wrap(sub); | ||
} | ||
fn.methods = function(o) { | ||
Object.keys(o).forEach(function(k) { | ||
fn.prototype[k] = o[k]; | ||
}); | ||
return subType; | ||
} | ||
function wrap(fn) { | ||
fn.extend = function(sub) { | ||
inheritPrototype(sub, fn); | ||
return wrap(sub); | ||
} | ||
fn.methods = function(o) { | ||
Object.keys(o).forEach(function(k) { | ||
fn.prototype[k] = o[k]; | ||
}); | ||
return fn; | ||
}; | ||
fn.statics = function(o) { | ||
Object.keys(o).forEach(function(k) { | ||
fn[k] = o[k]; | ||
}); | ||
return fn; | ||
}; | ||
return fn; | ||
} | ||
function klass(cons) { | ||
return wrap(cons || function() {}); | ||
} | ||
klass.extend = inheritPrototype; | ||
module.exports = klass; | ||
}); | ||
}; | ||
fn.statics = function(o) { | ||
Object.keys(o).forEach(function(k) { | ||
fn[k] = o[k]; | ||
}); | ||
return fn; | ||
}; | ||
return fn; | ||
} | ||
function klass(cons) { | ||
return wrap(cons || function() {}); | ||
} | ||
klass.extend = inheritPrototype; | ||
module.exports = klass;}); |
@@ -1,78 +0,71 @@ | ||
define(function(require, exports, module) { | ||
function quickSort(arr, begin, end, compare) { | ||
if(begin >= end) { | ||
return; | ||
} | ||
var i = begin, j = end, p = i, n = arr[p], seq = true; | ||
outer: | ||
while(i < j) { | ||
if(seq) { | ||
for(; i < j; j--) { | ||
if((compare && compare.call(arr, n, arr[j])) || (!compare && n > arr[j])) { | ||
swap(arr, p, j); | ||
p = j; | ||
seq = !seq; | ||
continue outer; | ||
} | ||
define(function(require, exports, module){function quickSort(arr, begin, end, compare) { | ||
if(begin >= end) { | ||
return; | ||
} | ||
var i = begin, j = end, p = i, n = arr[p], seq = true; | ||
outer: | ||
while(i < j) { | ||
if(seq) { | ||
for(; i < j; j--) { | ||
if((compare && compare.call(arr, n, arr[j])) || (!compare && n > arr[j])) { | ||
swap(arr, p, j); | ||
p = j; | ||
seq = !seq; | ||
continue outer; | ||
} | ||
} | ||
else { | ||
for(; i < j; i++) { | ||
if((compare && compare.call(arr, arr[i], n)) || (!compare && n < arr[i])) { | ||
swap(arr, p, i); | ||
p = i; | ||
seq = !seq; | ||
continue outer; | ||
} | ||
} | ||
else { | ||
for(; i < j; i++) { | ||
if((compare && compare.call(arr, arr[i], n)) || (!compare && n < arr[i])) { | ||
swap(arr, p, i); | ||
p = i; | ||
seq = !seq; | ||
continue outer; | ||
} | ||
} | ||
} | ||
quickSort(arr, begin, p, compare); | ||
quickSort(arr, p + 1, end, compare); | ||
} | ||
function swap(arr, a, b) { | ||
var temp = arr[a]; | ||
arr[a] = arr[b]; | ||
arr[b] = temp; | ||
} | ||
function getM(arr, a, b) { | ||
var max = min = arr[a]; | ||
var i = j = a; | ||
for(; a < b; a++) { | ||
if(arr[a] > max) { | ||
max = arr[a]; | ||
i = a; | ||
} | ||
else if(arr[a] < min) { | ||
min = arr[a]; | ||
j = a; | ||
} | ||
quickSort(arr, begin, p, compare); | ||
quickSort(arr, p + 1, end, compare); | ||
} | ||
function swap(arr, a, b) { | ||
var temp = arr[a]; | ||
arr[a] = arr[b]; | ||
arr[b] = temp; | ||
} | ||
function getM(arr, a, b) { | ||
var max = min = arr[a]; | ||
var i = j = a; | ||
for(; a < b; a++) { | ||
if(arr[a] > max) { | ||
max = arr[a]; | ||
i = a; | ||
} | ||
return { | ||
max: max, | ||
min: min, | ||
i: i, | ||
j: j | ||
}; | ||
else if(arr[a] < min) { | ||
min = arr[a]; | ||
j = a; | ||
} | ||
} | ||
module.exports = function(arr, compare) { | ||
if(!Array.isArray(arr)) { | ||
throw new Error('quick sort need an array'); | ||
} | ||
if(arr.length < 2) { | ||
return arr; | ||
} | ||
//优化尽可能选取中间值,5等分取每组最大最小值,然后10个数取中值,保证最坏情况首次分割值也在2/5~3/5 | ||
if(!compare && arr.length > 9) { | ||
var n = Math.floor(arr.length / 5); | ||
var split = []; | ||
var index = []; | ||
for(var i = 0; i < 4; i++) { | ||
var m = getM(arr, i * n, (i + 1) * n); | ||
split.push(m.max); | ||
split.push(m.min); | ||
index.push(m.i); | ||
index.push(m.j); | ||
} | ||
m = getM(arr, 4 * n, arr.length); | ||
return { | ||
max: max, | ||
min: min, | ||
i: i, | ||
j: j | ||
}; | ||
} | ||
module.exports = function(arr, compare) { | ||
if(!Array.isArray(arr)) { | ||
throw new Error('quick sort need an array'); | ||
} | ||
if(arr.length < 2) { | ||
return arr; | ||
} | ||
//优化尽可能选取中间值,5等分取每组最大最小值,然后10个数取中值,保证最坏情况首次分割值也在2/5~3/5 | ||
if(!compare && arr.length > 9) { | ||
var n = Math.floor(arr.length / 5); | ||
var split = []; | ||
var index = []; | ||
for(var i = 0; i < 4; i++) { | ||
var m = getM(arr, i * n, (i + 1) * n); | ||
split.push(m.max); | ||
@@ -82,13 +75,18 @@ split.push(m.min); | ||
index.push(m.j); | ||
quickSort(split, 0, split.length); | ||
for(var i = 0; i < index.length; i++) { | ||
if(arr[index[i]] == split[4]) { | ||
swap(arr, 0, index[i]); | ||
break; | ||
} | ||
} | ||
m = getM(arr, 4 * n, arr.length); | ||
split.push(m.max); | ||
split.push(m.min); | ||
index.push(m.i); | ||
index.push(m.j); | ||
quickSort(split, 0, split.length); | ||
for(var i = 0; i < index.length; i++) { | ||
if(arr[index[i]] == split[4]) { | ||
swap(arr, 0, index[i]); | ||
break; | ||
} | ||
} | ||
quickSort(arr, 0, arr.length - 1, compare); | ||
return arr; | ||
}; | ||
}); | ||
} | ||
quickSort(arr, 0, arr.length - 1, compare); | ||
return arr; | ||
};}); |
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
7423
319014
Updatedhomunculus@^0.5.6