Permalink
Browse files

refactor

  • Loading branch information...
1 parent 30a27d7 commit 131fabb76420ac00c5e49ab47e03f029c32a80e4 @chjj committed Mar 7, 2012
Showing with 43 additions and 14 deletions.
  1. +43 −14 lib/lexer.js
View
@@ -4,8 +4,11 @@
*/
var lexer = function(str, options) {
- str = str.replace(/\r\n|\r/g, '\n')
- .replace(/"/g, '\\"');
+ options.pretty = true;
+
+ str = str
+ .replace(/\r\n|\r/g, '\n')
+ .replace(/"/g, '\\"');
var i = 0
, l = str.length
@@ -18,26 +21,43 @@ var lexer = function(str, options) {
, stack = []
, indent = 0
, indents = []
- , newline = true;
+ , newline = true
+ , indentSize = 0;
var state = function() {
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++) {
ch = str[i];
offset++;
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) {
tokens.push({
type: 'end',
line: line
});
indents.pop();
}
- buff = '';
+ if (!options.pretty) buff = '';
newline = false;
}
@@ -54,14 +74,14 @@ var lexer = function(str, options) {
if (stack.length) {
tokens.push({
type: stack.pop(),
- name: buff,
+ name: out(),
line: line
});
} else {
if (options.pretty) buff += '\\n';
tokens.push({
type: 'text',
- text: buff,
+ text: out(),
line: line
});
}
@@ -73,8 +93,7 @@ var lexer = function(str, options) {
case '@':
case '?':
case '!':
- // the buffer should be empty
- if (str[i+1] === ':' && !buff) {
+ if (str[i+1] === ':' && !buff.trim()) {
switch (ch) {
case '@':
stack.push('iterate');
@@ -98,7 +117,7 @@ var lexer = function(str, options) {
case 'evaluate':
tokens.push({
type: 'evaluate',
- code: buff,
+ code: out(),
line: line
});
buff = '';
@@ -110,7 +129,7 @@ var lexer = function(str, options) {
default:
tokens.push({
type: 'text',
- text: buff,
+ text: out(),
line: line
});
buff = '';
@@ -128,7 +147,7 @@ var lexer = function(str, options) {
default:
tokens.push({
type: 'text',
- text: buff,
+ text: out(),
line: line
});
buff = '';
@@ -145,7 +164,7 @@ var lexer = function(str, options) {
case 'interpolate':
tokens.push({
type: 'interpolate',
- code: buff,
+ code: out(),
line: line
});
buff = '';
@@ -165,14 +184,24 @@ var lexer = function(str, options) {
if (buff) {
tokens.push({
type: 'text',
- text: buff,
+ text: out(),
line: line
});
}
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
*/

0 comments on commit 131fabb

Please sign in to comment.