homunculus
Advanced tools
Comparing version 1.6.4 to 1.6.5
{ | ||
"name": "homunculus", | ||
"version": "1.6.4", | ||
"version": "1.6.5", | ||
"description": "A lexer&parser by Javascript", | ||
@@ -15,2 +15,3 @@ "maintainers": [ | ||
"watch": "gulp watch", | ||
"dev": "rollup -c rollup.config.js --watch", | ||
"pack": "rollup -c rollup.config.js" | ||
@@ -17,0 +18,0 @@ }, |
@@ -45,3 +45,3 @@ var character = require('../util/character'); | ||
var JSXLexer = EcmascriptLexer.extend(function(rule) { | ||
var CSXLexer = EcmascriptLexer.extend(function(rule) { | ||
EcmascriptLexer.call(this, rule); | ||
@@ -53,7 +53,7 @@ }).methods({ | ||
this.state = false; //是否在<>中 | ||
this.hStack = []; //当mark开始时++,减少时--,以此得知jsx部分结束回归js | ||
this.jStack = []; //当{开始时++,}减少时--,以此得知js部分结束回归jsx | ||
this.hStack = []; //当mark开始时++,减少时--,以此得知csx部分结束回归js | ||
this.jStack = []; //当{开始时++,}减少时--,以此得知js部分结束回归csx | ||
this.aStack = []; //html和js互相递归时,记录当前层是否在attr状态中 | ||
this.cStack = []; //html和js互相递归时,记录当前jsx标签是否是自闭合 | ||
this.selfClose = false; //当前jsx标签是否是自闭合 | ||
this.cStack = []; //html和js互相递归时,记录当前csx标签是否是自闭合 | ||
this.selfClose = false; //当前csx标签是否是自闭合 | ||
}, | ||
@@ -89,3 +89,3 @@ scan: function(temp) { | ||
else { | ||
this.error('unknow jsx token: / '); | ||
this.error('unknow csx token: ' + this.code.charAt(this.index)); | ||
} | ||
@@ -110,6 +110,26 @@ } | ||
} | ||
//<>和</> | ||
else if(this.peek == '<') { | ||
if(this.code.charAt(this.index) == '>') { | ||
var token = new CSXToken(CSXToken.MARK, this.peek, this.peek, this.index - 1); | ||
this.dealToken(token, 1, 0, temp); | ||
this.readch(); | ||
token = new CSXToken(CSXToken.MARK, this.peek, this.peek, this.index - 1); | ||
this.dealToken(token, 1, 0, temp); | ||
} | ||
else if(this.code.charAt(this.index) == '/' && this.code.charAt(this.index + 1) == '>') { | ||
var token = new CSXToken(CSXToken.MARK, this.peek, this.peek, this.index - 1); | ||
this.dealToken(token, 1, 0, temp); | ||
this.readch(); | ||
token = new CSXToken(CSXToken.MARK, '</', '</', this.index - 1); | ||
this.dealToken(token, 2, 0, temp); | ||
this.index += 2; | ||
} | ||
else { | ||
this.error('unknow csx token: ' + this.code.charAt(this.index)); | ||
} | ||
} | ||
//{递归进入js状态 | ||
else if(this.peek == '{') { | ||
this.html = false; | ||
this.braceState = false; | ||
this.jStack.push(1); | ||
@@ -139,3 +159,3 @@ this.cStack.push(this.selfClose); | ||
//如果有未匹配的,说明规则不完整,抛出错误 | ||
this.error('unknow jsx token'); | ||
this.error('unknow csx token'); | ||
} | ||
@@ -177,2 +197,15 @@ } | ||
} | ||
//</> | ||
else if(c1 == '/' && c2 == '>') { | ||
if(idx > this.index - 1) { | ||
this.addText(this.code.slice(this.index - 1, idx), temp); | ||
this.index = idx; | ||
} | ||
this.state = true; | ||
this.hStack[this.hStack.length - 1]--; | ||
//</ | ||
var token = new CSXToken(CSXToken.MARK, '</', '</', this.index - 1); | ||
this.dealToken(token, 2, 0, temp); | ||
this.index = idx + 2; | ||
} | ||
//<\w | ||
@@ -206,3 +239,2 @@ else if(character.isLetter(c1) || character.DOLLAR == c1) { | ||
this.html = false; | ||
this.braceState = false; | ||
var token = new CSXToken(CSXToken.SIGN, this.peek, this.peek, this.index - 1); | ||
@@ -214,7 +246,7 @@ this.dealToken(token, 1, 0, temp); | ||
} | ||
//<\w开始则jsx,<作为mark开头和识别正则/开头上下文语意相同 | ||
else if(this.isReg == JSXLexer.IS_REG | ||
//<\w开始则csx,<作为mark开头和识别正则/开头上下文语意相同 | ||
else if(this.isReg == CSXLexer.IS_REG | ||
&& this.peek == '<' | ||
&& (character.isLetter(this.code.charAt(this.index)) || character.DOLLAR == this.code.charAt(this.index))) { | ||
//新的jsx开始,html深度++,html状态开始,同时为非text状态 | ||
//新的csx开始,html深度++,html状态开始,同时为非text状态 | ||
this.hStack.push(1); | ||
@@ -229,11 +261,24 @@ this.html = true; | ||
this.dealTag(temp); | ||
this.braceState = false; | ||
} | ||
//<>则csx,fragment出现 | ||
else if(this.isReg == CSXLexer.IS_REG | ||
&& this.peek == '<' | ||
&& this.code.charAt(this.index) == '>') { | ||
//新的csx开始,html深度++,html状态开始,同时为非text状态 | ||
this.hStack.push(1); | ||
this.html = true; | ||
//<> | ||
var token = new CSXToken(CSXToken.MARK, this.peek, this.peek, this.index - 1); | ||
this.dealToken(token, 1, 0, temp); | ||
this.readch(); | ||
token = new CSXToken(CSXToken.MARK, this.peek, this.peek, this.index - 1); | ||
this.dealToken(token, 1, 0, temp); | ||
} | ||
//perl风格正则 | ||
else if(perlReg | ||
&& this.isReg == JSXLexer.IS_REG | ||
&& this.isReg == CSXLexer.IS_REG | ||
&& this.peek == character.SLASH | ||
&& !{ '/': true, '*': true }[this.code.charAt(this.index)]) { | ||
this.dealReg(temp, length); | ||
this.isReg = JSXLexer.NOT_REG; | ||
this.isReg = CSXLexer.NOT_REG; | ||
} | ||
@@ -323,3 +368,3 @@ //template特殊语法 | ||
if(!character.isLetter(this.peek)) { | ||
this.error('missing jsx identifier'); | ||
this.error('missing csx identifier'); | ||
} | ||
@@ -343,3 +388,3 @@ ELEM.match(this.peek, this.code, this.index); | ||
if(!character.isLetter(this.peek)) { | ||
this.error('missing jsx identifier'); | ||
this.error('missing csx identifier'); | ||
} | ||
@@ -378,2 +423,2 @@ ELEM.match(this.peek, this.code, this.index); | ||
}); | ||
module.exports = JSXLexer; | ||
module.exports = CSXLexer; |
@@ -18,2 +18,3 @@ var Es6Node = require('../es6/Node'); | ||
CSXAttributeValue: 'CSXAttributeValue', | ||
CSXFragment: 'CSXFragment', | ||
getKey: function(s) { | ||
@@ -35,2 +36,2 @@ if(!s) { | ||
var keys; | ||
module.exports = Node; | ||
module.exports = Node; |
@@ -14,3 +14,7 @@ var Es6Parser = require('../es6/Parser'); | ||
if(this.look.type() == Token.MARK) { | ||
return this.CSXelem(); | ||
var next = this.tokens[this.index]; | ||
if(next && next.content() == '>') { | ||
return this.csxfragment(); | ||
} | ||
return this.csxelem(); | ||
} | ||
@@ -21,7 +25,22 @@ else { | ||
}, | ||
CSXelem: function() { | ||
csxfragment: function() { | ||
var node = new Node(Node.CSXFragment); | ||
node.add( | ||
this.match('<'), | ||
this.match('>') | ||
); | ||
while(this.look && this.look.content() != '</') { | ||
node.add(this.csxchild()); | ||
} | ||
node.add( | ||
this.match('</'), | ||
this.match('>') | ||
); | ||
return node; | ||
}, | ||
csxelem: function() { | ||
var node = new Node(Node.CSXOpeningElement); | ||
node.add( | ||
this.match(), | ||
this.CSXelemname() | ||
this.csxelemname() | ||
); | ||
@@ -66,3 +85,3 @@ //id只有1个,member和namespace有多个 | ||
while(this.look && this.look.content() != '</') { | ||
n.add(this.CSXchild()); | ||
n.add(this.csxchild()); | ||
} | ||
@@ -72,3 +91,3 @@ n.add(this.close(name, type)); | ||
}, | ||
CSXelemname: function(name, type) { | ||
csxelemname: function(name, type) { | ||
if(name) { | ||
@@ -115,3 +134,3 @@ if(type) { | ||
}, | ||
CSXmember: function(names) { | ||
csxmember: function(names) { | ||
var node = new Node(Node.CSXMemberExpression); | ||
@@ -191,5 +210,8 @@ names.forEach(function(name) { | ||
} | ||
return this.CSXelem(); | ||
else if(this.look.content() == '<') { | ||
return this.csxfragment(); | ||
} | ||
return this.csxelem(); | ||
}, | ||
CSXchild: function() { | ||
csxchild: function() { | ||
switch(this.look.type()) { | ||
@@ -199,3 +221,7 @@ case Token.TEXT: | ||
case Token.MARK: | ||
return this.CSXelem(); | ||
var next = this.tokens[this.index]; | ||
if(next && next.content() == '>') { | ||
return this.csxfragment(); | ||
} | ||
return this.csxelem(); | ||
default: | ||
@@ -215,3 +241,3 @@ var node = new Node(Node.CSXChild); | ||
this.match('</'), | ||
this.CSXelemname(name, type), | ||
this.csxelemname(name, type), | ||
this.match('>') | ||
@@ -266,4 +292,4 @@ ) | ||
&& (!this.look | ||
|| (this.look.content() != type && this.hasMoveLine) | ||
|| this.look.content() == '}') | ||
|| (this.look.content() != type && this.hasMoveLine) | ||
|| this.look.content() == '}') | ||
) { | ||
@@ -299,2 +325,2 @@ if(this.look && Es6Parser.S[this.look.type()]) { | ||
module.exports = Parser; | ||
module.exports = Parser; |
@@ -45,3 +45,3 @@ define(function(require, exports, module) {var character = require('../util/character'); | ||
var JSXLexer = EcmascriptLexer.extend(function(rule) { | ||
var CSXLexer = EcmascriptLexer.extend(function(rule) { | ||
EcmascriptLexer.call(this, rule); | ||
@@ -53,7 +53,7 @@ }).methods({ | ||
this.state = false; //是否在<>中 | ||
this.hStack = []; //当mark开始时++,减少时--,以此得知jsx部分结束回归js | ||
this.jStack = []; //当{开始时++,}减少时--,以此得知js部分结束回归jsx | ||
this.hStack = []; //当mark开始时++,减少时--,以此得知csx部分结束回归js | ||
this.jStack = []; //当{开始时++,}减少时--,以此得知js部分结束回归csx | ||
this.aStack = []; //html和js互相递归时,记录当前层是否在attr状态中 | ||
this.cStack = []; //html和js互相递归时,记录当前jsx标签是否是自闭合 | ||
this.selfClose = false; //当前jsx标签是否是自闭合 | ||
this.cStack = []; //html和js互相递归时,记录当前csx标签是否是自闭合 | ||
this.selfClose = false; //当前csx标签是否是自闭合 | ||
}, | ||
@@ -89,3 +89,3 @@ scan: function(temp) { | ||
else { | ||
this.error('unknow jsx token: / '); | ||
this.error('unknow csx token: ' + this.code.charAt(this.index)); | ||
} | ||
@@ -110,6 +110,26 @@ } | ||
} | ||
//<>和</> | ||
else if(this.peek == '<') { | ||
if(this.code.charAt(this.index) == '>') { | ||
var token = new CSXToken(CSXToken.MARK, this.peek, this.peek, this.index - 1); | ||
this.dealToken(token, 1, 0, temp); | ||
this.readch(); | ||
token = new CSXToken(CSXToken.MARK, this.peek, this.peek, this.index - 1); | ||
this.dealToken(token, 1, 0, temp); | ||
} | ||
else if(this.code.charAt(this.index) == '/' && this.code.charAt(this.index + 1) == '>') { | ||
var token = new CSXToken(CSXToken.MARK, this.peek, this.peek, this.index - 1); | ||
this.dealToken(token, 1, 0, temp); | ||
this.readch(); | ||
token = new CSXToken(CSXToken.MARK, '</', '</', this.index - 1); | ||
this.dealToken(token, 2, 0, temp); | ||
this.index += 2; | ||
} | ||
else { | ||
this.error('unknow csx token: ' + this.code.charAt(this.index)); | ||
} | ||
} | ||
//{递归进入js状态 | ||
else if(this.peek == '{') { | ||
this.html = false; | ||
this.braceState = false; | ||
this.jStack.push(1); | ||
@@ -139,3 +159,3 @@ this.cStack.push(this.selfClose); | ||
//如果有未匹配的,说明规则不完整,抛出错误 | ||
this.error('unknow jsx token'); | ||
this.error('unknow csx token'); | ||
} | ||
@@ -162,3 +182,3 @@ } | ||
//</\w | ||
if(c1 == '/' && (character.isLetter(c2) || character.DOLLAR == c2)) { | ||
if(c1 == '/' && character.isLetter(c2)) { | ||
if(idx > this.index - 1) { | ||
@@ -178,4 +198,17 @@ this.addText(this.code.slice(this.index - 1, idx), temp); | ||
} | ||
//</> | ||
else if(c1 == '/' && c2 == '>') { | ||
if(idx > this.index - 1) { | ||
this.addText(this.code.slice(this.index - 1, idx), temp); | ||
this.index = idx; | ||
} | ||
this.state = true; | ||
this.hStack[this.hStack.length - 1]--; | ||
//</ | ||
var token = new CSXToken(CSXToken.MARK, '</', '</', this.index - 1); | ||
this.dealToken(token, 2, 0, temp); | ||
this.index = idx + 2; | ||
} | ||
//<\w | ||
else if(character.isLetter(c1) || character.DOLLAR == c1) { | ||
else if(character.isLetter(c1)) { | ||
if(idx > this.index - 1) { | ||
@@ -207,3 +240,2 @@ this.addText(this.code.slice(this.index - 1, idx), temp); | ||
this.html = false; | ||
this.braceState = false; | ||
var token = new CSXToken(CSXToken.SIGN, this.peek, this.peek, this.index - 1); | ||
@@ -215,7 +247,7 @@ this.dealToken(token, 1, 0, temp); | ||
} | ||
//<\w开始则jsx,<作为mark开头和识别正则/开头上下文语意相同 | ||
else if(this.isReg == JSXLexer.IS_REG | ||
//<\w开始则csx,<作为mark开头和识别正则/开头上下文语意相同 | ||
else if(this.isReg == CSXLexer.IS_REG | ||
&& this.peek == '<' | ||
&& (character.isLetter(this.code.charAt(this.index)) || character.DOLLAR == this.code.charAt(this.index))) { | ||
//新的jsx开始,html深度++,html状态开始,同时为非text状态 | ||
&& character.isLetter(this.code.charAt(this.index))) { | ||
//新的csx开始,html深度++,html状态开始,同时为非text状态 | ||
this.hStack.push(1); | ||
@@ -230,11 +262,24 @@ this.html = true; | ||
this.dealTag(temp); | ||
this.braceState = false; | ||
} | ||
//<>则csx,fragment出现 | ||
else if(this.isReg == CSXLexer.IS_REG | ||
&& this.peek == '<' | ||
&& this.code.charAt(this.index) == '>') { | ||
//新的csx开始,html深度++,html状态开始,同时为非text状态 | ||
this.hStack.push(1); | ||
this.html = true; | ||
//<> | ||
var token = new CSXToken(CSXToken.MARK, this.peek, this.peek, this.index - 1); | ||
this.dealToken(token, 1, 0, temp); | ||
this.readch(); | ||
token = new CSXToken(CSXToken.MARK, this.peek, this.peek, this.index - 1); | ||
this.dealToken(token, 1, 0, temp); | ||
} | ||
//perl风格正则 | ||
else if(perlReg | ||
&& this.isReg == JSXLexer.IS_REG | ||
&& this.isReg == CSXLexer.IS_REG | ||
&& this.peek == character.SLASH | ||
&& !{ '/': true, '*': true }[this.code.charAt(this.index)]) { | ||
this.dealReg(temp, length); | ||
this.isReg = JSXLexer.NOT_REG; | ||
this.isReg = CSXLexer.NOT_REG; | ||
} | ||
@@ -324,3 +369,3 @@ //template特殊语法 | ||
if(!character.isLetter(this.peek)) { | ||
this.error('missing jsx identifier'); | ||
this.error('missing csx identifier'); | ||
} | ||
@@ -344,3 +389,3 @@ ELEM.match(this.peek, this.code, this.index); | ||
if(!character.isLetter(this.peek)) { | ||
this.error('missing jsx identifier'); | ||
this.error('missing csx identifier'); | ||
} | ||
@@ -379,2 +424,3 @@ ELEM.match(this.peek, this.code, this.index); | ||
}); | ||
module.exports = JSXLexer;}); | ||
module.exports = CSXLexer; | ||
}); |
@@ -90,2 +90,3 @@ define(function(require, exports, module) {var Rule = require('./Rule'); | ||
}); | ||
module.exports = EcmascriptRule;}); | ||
module.exports = EcmascriptRule; | ||
}); |
@@ -18,2 +18,3 @@ define(function(require, exports, module) {var Es6Node = require('../es6/Node'); | ||
CSXAttributeValue: 'CSXAttributeValue', | ||
CSXFragment: 'CSXFragment', | ||
getKey: function(s) { | ||
@@ -35,2 +36,3 @@ if(!s) { | ||
var keys; | ||
module.exports = Node;}); | ||
module.exports = Node; | ||
}); |
@@ -14,3 +14,7 @@ define(function(require, exports, module) {var Es6Parser = require('../es6/Parser'); | ||
if(this.look.type() == Token.MARK) { | ||
return this.CSXelem(); | ||
var next = this.tokens[this.index]; | ||
if(next && next.content() == '>') { | ||
return this.csxfragment(); | ||
} | ||
return this.csxelem(); | ||
} | ||
@@ -21,7 +25,22 @@ else { | ||
}, | ||
CSXelem: function() { | ||
csxfragment: function() { | ||
var node = new Node(Node.CSXFragment); | ||
node.add( | ||
this.match('<'), | ||
this.match('>') | ||
); | ||
while(this.look && this.look.content() != '</') { | ||
node.add(this.csxchild()); | ||
} | ||
node.add( | ||
this.match('</'), | ||
this.match('>') | ||
); | ||
return node; | ||
}, | ||
csxelem: function() { | ||
var node = new Node(Node.CSXOpeningElement); | ||
node.add( | ||
this.match(), | ||
this.CSXelemname() | ||
this.csxelemname() | ||
); | ||
@@ -66,3 +85,3 @@ //id只有1个,member和namespace有多个 | ||
while(this.look && this.look.content() != '</') { | ||
n.add(this.CSXchild()); | ||
n.add(this.csxchild()); | ||
} | ||
@@ -72,3 +91,3 @@ n.add(this.close(name, type)); | ||
}, | ||
CSXelemname: function(name, type) { | ||
csxelemname: function(name, type) { | ||
if(name) { | ||
@@ -115,3 +134,3 @@ if(type) { | ||
}, | ||
CSXmember: function(names) { | ||
csxmember: function(names) { | ||
var node = new Node(Node.CSXMemberExpression); | ||
@@ -191,5 +210,8 @@ names.forEach(function(name) { | ||
} | ||
return this.CSXelem(); | ||
else if(this.look.content() == '<') { | ||
return this.csxfragment(); | ||
} | ||
return this.csxelem(); | ||
}, | ||
CSXchild: function() { | ||
csxchild: function() { | ||
switch(this.look.type()) { | ||
@@ -199,3 +221,7 @@ case Token.TEXT: | ||
case Token.MARK: | ||
return this.CSXelem(); | ||
var next = this.tokens[this.index]; | ||
if(next && next.content() == '>') { | ||
return this.csxfragment(); | ||
} | ||
return this.csxelem(); | ||
default: | ||
@@ -215,3 +241,3 @@ var node = new Node(Node.CSXChild); | ||
this.match('</'), | ||
this.CSXelemname(name, type), | ||
this.csxelemname(name, type), | ||
this.match('>') | ||
@@ -266,4 +292,4 @@ ) | ||
&& (!this.look | ||
|| (this.look.content() != type && this.hasMoveLine) | ||
|| this.look.content() == '}') | ||
|| (this.look.content() != type && this.hasMoveLine) | ||
|| this.look.content() == '}') | ||
) { | ||
@@ -299,2 +325,3 @@ if(this.look && Es6Parser.S[this.look.type()]) { | ||
module.exports = Parser;}); | ||
module.exports = Parser; | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
1891881
34143