Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Mar 7, 2012
1 parent 30a27d7 commit 131fabb
Showing 1 changed file with 43 additions and 14 deletions.
57 changes: 43 additions & 14 deletions lib/lexer.js
Expand Up @@ -4,8 +4,11 @@
*/ */


var lexer = function(str, options) { var lexer = function(str, options) {
str = str.replace(/\r\n|\r/g, '\n') options.pretty = true;
.replace(/"/g, '\\"');
str = str
.replace(/\r\n|\r/g, '\n')
.replace(/"/g, '\\"');


var i = 0 var i = 0
, l = str.length , l = str.length
Expand All @@ -18,26 +21,43 @@ var lexer = function(str, options) {
, stack = [] , stack = []
, indent = 0 , indent = 0
, indents = [] , indents = []
, newline = true; , newline = true
, indentSize = 0;


var state = function() { var state = function() {
return stack[stack.length-1]; return stack[stack.length-1];
}; };


var out = function() {
if (!options.pretty) return buff;
var i = indents.length * indentSize;
if (i) buff = outdent(buff, i);
if (options.indent) {
i = options.indent * indentSize;
buff = indent(buff, i);
}
return buff;
};

for (; i < l; i++) { for (; i < l; i++) {
ch = str[i]; ch = str[i];
offset++; offset++;


if (ch > ' ' && newline) { if (ch > ' ' && newline) {
indent = buff.length / 2; if (!indentSize && indent < buff.length) {
indentSize = buff.length - indent;
}
// We could use offset - 1 here
// instead of buff.length.
indent = buff.length;
while (indents[indents.length-1] >= indent) { while (indents[indents.length-1] >= indent) {
tokens.push({ tokens.push({
type: 'end', type: 'end',
line: line line: line
}); });
indents.pop(); indents.pop();
} }
buff = ''; if (!options.pretty) buff = '';
newline = false; newline = false;
} }


Expand All @@ -54,14 +74,14 @@ var lexer = function(str, options) {
if (stack.length) { if (stack.length) {
tokens.push({ tokens.push({
type: stack.pop(), type: stack.pop(),
name: buff, name: out(),
line: line line: line
}); });
} else { } else {
if (options.pretty) buff += '\\n'; if (options.pretty) buff += '\\n';
tokens.push({ tokens.push({
type: 'text', type: 'text',
text: buff, text: out(),
line: line line: line
}); });
} }
Expand All @@ -73,8 +93,7 @@ var lexer = function(str, options) {
case '@': case '@':
case '?': case '?':
case '!': case '!':
// the buffer should be empty if (str[i+1] === ':' && !buff.trim()) {
if (str[i+1] === ':' && !buff) {
switch (ch) { switch (ch) {
case '@': case '@':
stack.push('iterate'); stack.push('iterate');
Expand All @@ -98,7 +117,7 @@ var lexer = function(str, options) {
case 'evaluate': case 'evaluate':
tokens.push({ tokens.push({
type: 'evaluate', type: 'evaluate',
code: buff, code: out(),
line: line line: line
}); });
buff = ''; buff = '';
Expand All @@ -110,7 +129,7 @@ var lexer = function(str, options) {
default: default:
tokens.push({ tokens.push({
type: 'text', type: 'text',
text: buff, text: out(),
line: line line: line
}); });
buff = ''; buff = '';
Expand All @@ -128,7 +147,7 @@ var lexer = function(str, options) {
default: default:
tokens.push({ tokens.push({
type: 'text', type: 'text',
text: buff, text: out(),
line: line line: line
}); });
buff = ''; buff = '';
Expand All @@ -145,7 +164,7 @@ var lexer = function(str, options) {
case 'interpolate': case 'interpolate':
tokens.push({ tokens.push({
type: 'interpolate', type: 'interpolate',
code: buff, code: out(),
line: line line: line
}); });
buff = ''; buff = '';
Expand All @@ -165,14 +184,24 @@ var lexer = function(str, options) {
if (buff) { if (buff) {
tokens.push({ tokens.push({
type: 'text', type: 'text',
text: buff, text: out(),
line: line line: line
}); });
} }


return tokens; return tokens;
}; };


var outdent = function(str, n) {
if (!n) return str;
return str.replace(new RegExp('^ {' + n + '}', 'gm'), '');
};

var indent = function(str, n) {
if (!n) return str;
return str.replace(/^/gm, Array(n).join(' '));
};

/** /**
* Expose * Expose
*/ */
Expand Down

0 comments on commit 131fabb

Please sign in to comment.