Skip to content

Commit

Permalink
<mark>lexer时深度 #53
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed Apr 17, 2015
1 parent fafb99a commit bb5cf8d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/lexer/JSXLexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ var JSXLexer = Class(function(rule) {
this.last = null;
this.html = false; //目前是否为解析html状态
this.state = false; //是否在<>中
this.hStack = []; //当mark开始时++,减少时--,以此得知jsx部分结束回归js
this.jStack = []; //当{开始时++,减少时--,以此得知js部分结束回归jsx
},
parse: function(code) {
this.code = code || '';
Expand Down Expand Up @@ -77,6 +79,11 @@ var JSXLexer = Class(function(rule) {
if(this.peek == '/') {
if(this.code.charAt(this.index) == '>') {
this.state = false;
this.hStack[this.hStack.length - 1]--;
if(this.hStack[this.hStack.length - 1] == 0) {
this.hStack.pop();
this.html = false;
}
var token = new JSXToken(JSXToken.MARK, this.peek + '>', this.peek + '>');
token.prev(this.last);
this.last.next(token);
Expand All @@ -96,6 +103,9 @@ var JSXLexer = Class(function(rule) {
//>
else if(this.peek == '>') {
this.state = false;
if(!this.hStack.length) {
this.html = false;
}
var token = new JSXToken(JSXToken.MARK, this.peek, this.peek);
token.prev(this.last);
this.last.next(token);
Expand Down Expand Up @@ -145,6 +155,7 @@ var JSXLexer = Class(function(rule) {
this.error('unknow jsx token');
}
}
//<>外面
else {
//<之前的text部分
var idx = this.code.indexOf('<', this.index);
Expand Down Expand Up @@ -195,6 +206,10 @@ var JSXLexer = Class(function(rule) {
this.addText(this.code.slice(this.index - 1, idx), temp);
}
this.state = true;
this.hStack[this.hStack.length - 1]--;
if(this.hStack[this.hStack.length - 1] == 0) {
this.hStack.pop();
}
//</
var token = new JSXToken(JSXToken.MARK, '</', '</');
if(this.last) {
Expand Down Expand Up @@ -231,6 +246,7 @@ var JSXLexer = Class(function(rule) {
this.addText(this.code.slice(this.index - 1, idx), temp);
}
this.state = true;
this.hStack[this.hStack.length - 1]++;
//<
var token = new JSXToken(JSXToken.MARK, '<', '<');
if(this.last) {
Expand Down Expand Up @@ -272,6 +288,8 @@ var JSXLexer = Class(function(rule) {
else if(this.isReg == JSXLexer.IS_REG
&& this.peek == '<'
&& character.isLetter(this.code.charAt(this.index))) {
//新的jsx开始,深度++
this.hStack.push(1);
this.html = true;
this.state = true;
//<
Expand Down Expand Up @@ -301,7 +319,6 @@ var JSXLexer = Class(function(rule) {
token.col(this.colNum);
this.colNum += matchLen;
this.colMax = Math.max(this.colMax, this.colNum);
this.isReg = JSXLexer.NOT_REG;
}
//perl风格正则
else if(perlReg
Expand Down
2 changes: 2 additions & 0 deletions src/parser/jsx/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var Node = Es6Node.extend(function(type, children) {
Es6Node.call(this, type, children);
return this;
}).statics({
JSXELEM: 'jsxelem',
PROPERTY: 'property',
getKey: function(s) {
if(!s) {
throw new Error('empty value');
Expand Down
18 changes: 17 additions & 1 deletion src/parser/jsx/Parser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
var Es6Parser = require('../es6/Parser');
var Node = require('./Node');
var Token = require('../../lexer/JSXToken');

var Parser = Es6Parser.extend(function(lexer) {
Es6Parser.call(this, lexer);
this.init(lexer);
return this;
}).methods({
prmrexpr: function() {
if(this.look.type() == Token.MARK) {
var node = new Node(Node.PRMREXPR);
node.add(this.jsxelem());
}
else {
return Es6Parser.prototype.prmrexpr.call(this);
}
},
jsxelem: function() {
var node = new Node(Node.JSXELEM);
return node;
}
});

module.exports = Es6Parser;
module.exports = Parser;
18 changes: 18 additions & 0 deletions web/lexer/JSXLexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ var JSXLexer = Class(function(rule) {
this.last = null;
this.html = false; //目前是否为解析html状态
this.state = false; //是否在<>中
this.hStack = []; //当mark开始时++,减少时--,以此得知jsx部分结束回归js
this.jStack = []; //当{开始时++,减少时--,以此得知js部分结束回归jsx
},
parse: function(code) {
this.code = code || '';
Expand Down Expand Up @@ -77,6 +79,11 @@ var JSXLexer = Class(function(rule) {
if(this.peek == '/') {
if(this.code.charAt(this.index) == '>') {
this.state = false;
this.hStack[this.hStack.length - 1]--;
if(this.hStack[this.hStack.length - 1] == 0) {
this.hStack.pop();
this.html = false;
}
var token = new JSXToken(JSXToken.MARK, this.peek + '>', this.peek + '>');
token.prev(this.last);
this.last.next(token);
Expand All @@ -96,6 +103,9 @@ var JSXLexer = Class(function(rule) {
//>
else if(this.peek == '>') {
this.state = false;
if(!this.hStack.length) {
this.html = false;
}
var token = new JSXToken(JSXToken.MARK, this.peek, this.peek);
token.prev(this.last);
this.last.next(token);
Expand Down Expand Up @@ -145,6 +155,7 @@ var JSXLexer = Class(function(rule) {
this.error('unknow jsx token');
}
}
//<>外面
else {
//<之前的text部分
var idx = this.code.indexOf('<', this.index);
Expand Down Expand Up @@ -195,6 +206,10 @@ var JSXLexer = Class(function(rule) {
this.addText(this.code.slice(this.index - 1, idx), temp);
}
this.state = true;
this.hStack[this.hStack.length - 1]--;
if(this.hStack[this.hStack.length - 1] == 0) {
this.hStack.pop();
}
//</
var token = new JSXToken(JSXToken.MARK, '</', '</');
if(this.last) {
Expand Down Expand Up @@ -231,6 +246,7 @@ var JSXLexer = Class(function(rule) {
this.addText(this.code.slice(this.index - 1, idx), temp);
}
this.state = true;
this.hStack[this.hStack.length - 1]++;
//<
var token = new JSXToken(JSXToken.MARK, '<', '<');
if(this.last) {
Expand Down Expand Up @@ -272,6 +288,8 @@ var JSXLexer = Class(function(rule) {
else if(this.isReg == JSXLexer.IS_REG
&& this.peek == '<'
&& character.isLetter(this.code.charAt(this.index))) {
//新的jsx开始,深度++
this.hStack.push(1);
this.html = true;
this.state = true;
//<
Expand Down
2 changes: 2 additions & 0 deletions web/parser/jsx/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var Node = Es6Node.extend(function(type, children) {
Es6Node.call(this, type, children);
return this;
}).statics({
JSXELEM: 'jsxelem',
PROPERTY: 'property',
getKey: function(s) {
if(!s) {
throw new Error('empty value');
Expand Down
18 changes: 17 additions & 1 deletion web/parser/jsx/Parser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
define(function(require, exports, module) {var Es6Parser = require('../es6/Parser');
var Node = require('./Node');
var Token = require('../../lexer/JSXToken');

var Parser = Es6Parser.extend(function(lexer) {
Es6Parser.call(this, lexer);
this.init(lexer);
return this;
}).methods({
prmrexpr: function() {
if(this.look.type() == Token.MARK) {
var node = new Node(Node.PRMREXPR);
node.add(this.jsxelem());
}
else {
return Es6Parser.prototype.prmrexpr.call(this);
}
},
jsxelem: function() {
var node = new Node(Node.JSXELEM);
return node;
}
});

module.exports = Es6Parser;});
module.exports = Parser;});

0 comments on commit bb5cf8d

Please sign in to comment.