Skip to content
Browse files

add defaults. refactor. eat newlines.

  • Loading branch information...
1 parent eaaf654 commit 9adf49e8292dcde9798c0eadf8abb438ea13c89f @chjj committed
Showing with 30 additions and 35 deletions.
  1. +15 −11 lib/lexer.js
  2. +3 −1 lib/liquor.js
  3. +4 −4 lib/liquor_minimal.js
  4. +8 −19 lib/parser.js
View
26 lib/lexer.js
@@ -4,7 +4,8 @@
*/
var lexer = function(src, options) {
- var i = 0
+ var src = src.replace(/\r\n|\r/g, '\n')
+ , i = 0
, l = src.length
, ch
, buff = ''
@@ -16,10 +17,10 @@ var lexer = function(src, options) {
, indent
, indents = []
, newline = true
- , indentSize;
+ , size;
if (options.pretty && options.indent) {
- src = applyIndent(src, options.indent);
+ src = indentation(src, options.indent);
l = src.length;
}
@@ -29,9 +30,9 @@ var lexer = function(src, options) {
var out = function() {
if (!options.pretty) return buff;
- var i = indents.length * indentSize;
+ var i = indents.length * size;
if (!i) return buff;
- return applyOutdent(buff, i);
+ return outdent(buff, i);
};
for (; i < l; i++) {
@@ -39,11 +40,11 @@ var lexer = function(src, options) {
offset++;
if (ch > ' ' && newline) {
- if (!indentSize && indent != null && indent < buff.length) {
- indentSize = buff.length - indent;
+ if (!size && indent != null && indent < buff.length) {
+ size = buff.length - indent;
}
indent = buff.length;
- // assert(indent % indentSize === 0);
+ // assert(indent % size === 0);
while (indents[indents.length-1] >= indent) {
tokens.push({
type: 'end',
@@ -55,7 +56,7 @@ var lexer = function(src, options) {
newline = false;
}
- switch(ch) {
+ switch (ch) {
case '\r':
if (src[i+1] === '\n') break;
;
@@ -86,6 +87,9 @@ var lexer = function(src, options) {
newline = true;
break;
}
+ while (src[i+1] === '\n') {
+ i++, line++;
+ }
break;
case '@':
case '?':
@@ -203,12 +207,12 @@ var lexer = function(src, options) {
return tokens;
};
-var applyOutdent = function(src, n) {
+var outdent = function(src, n) {
if (!n) return src;
return src.replace(new RegExp('^[ \t]{' + n + '}', 'gm'), '');
};
-var applyIndent = function(src, n) {
+var indentation = function(src, n) {
if (!n) return src;
var s = '';
while (n--) s += ' ';
View
4 lib/liquor.js
@@ -12,7 +12,7 @@ var lexer = require('./lexer')
*/
var liquor = function(src, options) {
- options = options || {};
+ options = options || liquor.defaults;
src = parser(lexer(src, options), options);
@@ -24,6 +24,8 @@ var liquor = function(src, options) {
};
};
+liquor.defaults = {};
+
/**
* Helper
*/
View
8 lib/liquor_minimal.js
@@ -50,7 +50,7 @@ var liquor = (function() {
return src.replace(new RegExp('^ {' + n + '}', 'gm'), '');
};
- var indentSize = function(src) {
+ var indent = function(src) {
var start = /^( +)(?:[^\n]+\n\1)+/.exec(src);
if (start) src = outdent(src, start[1].length);
var size = /\n( +)/.exec(src);
@@ -60,7 +60,7 @@ var liquor = (function() {
var depth
, size;
- function lexer(src) {
+ var lexer = function(src) {
var out = ''
, cap;
@@ -116,11 +116,11 @@ var liquor = (function() {
depth--;
return out;
- }
+ };
return function(src, opt) {
depth = -1;
- size = indentSize(src);
+ size = indent(src);
// normalize whitespace
src = src
View
27 lib/parser.js
@@ -13,7 +13,7 @@ var parser = (function() {
var tok = function() {
var token_ = token
- , body;
+ , body = '';
switch (token_.type) {
case 'evaluate':
@@ -25,20 +25,18 @@ var parser = (function() {
+ token_.code
+ '), "';
case 'iterate':
- body = [];
while (next().type !== 'end') {
- body.push(tok());
+ body += tok();
}
return '"); each('
+ token_.name
+ ', function() { __out.push("'
- + body.join('')
+ + body
+ '"); }); __out.push("';
case 'if':
case 'not':
- body = [];
while (next().type !== 'end') {
- body.push(tok());
+ body += tok();
}
return '"); if ('
+ (token_.type === 'not' ? '!' : '')
@@ -47,7 +45,7 @@ var parser = (function() {
+ ' !== "undefined" && '
+ token_.name
+ ')) { __out.push("'
- + body.join('')
+ + body
+ '"); } __out.push("';
case 'text':
return token_.text;
@@ -55,23 +53,14 @@ var parser = (function() {
};
return function(src, options) {
- var out = [];
+ var out = '';
tokens = src.reverse();
- try {
- while (next()) {
- out.push(tok());
- }
- } catch(e) {
- if (token) {
- token = JSON.stringify(token);
- e.message = token + '\n' + e;
- }
- throw e;
+ while (next()) {
+ out += tok();
}
- out = out.join('');
out = 'with ($) { var __out = []; __out.push("'
+ out
+ '"); return __out.join(""); }';

0 comments on commit 9adf49e

Please sign in to comment.
Something went wrong with that request. Please try again.