Permalink
Browse files

clean up misc work

  • Loading branch information...
1 parent 7e50d68 commit 4e53a0080ce1df778d0d47a954b6dc6f348f309a @chjj committed Mar 7, 2012
Showing with 10 additions and 46 deletions.
  1. +10 −46 lib/lexer.js
View
@@ -4,8 +4,12 @@
*/
var lexer = function(str, options) {
+ str = str
+ .replace(/\r\n|\r/g, '\n')
+ .replace(/"/g, '\\"');
+
var i = 0
- , l
+ , l = str.length
, ch
, buff = ''
, key
@@ -16,25 +20,13 @@ var lexer = function(str, options) {
, indent = 0
, indents = []
, newline = true
- , indentSize;
-
- options.pretty = true;
- options.indent = 1;
-
- str = str
- .replace(/\r\n|\r/g, '\n')
- .replace(/"/g, '\\"');
-
- if (options.indent || options.pretty) {
- indentSize = getIndentSize(str);
- }
+ , indentSize = getIndentSize(str);
- if (options.indent) {
+ if (options.pretty && options.indent) {
str = applyIndent(str, options.indent * indentSize);
+ l = str.length;
}
- l = str.length;
-
var state = function() {
return stack[stack.length-1];
};
@@ -51,9 +43,8 @@ var lexer = function(str, options) {
offset++;
if (ch > ' ' && newline) {
- // We could use offset - 1 here
- // instead of buff.length.
indent = buff.length;
+ // assert(indent % indentSize === 0);
while (indents[indents.length-1] >= indent) {
tokens.push({
type: 'end',
@@ -209,39 +200,12 @@ var applyIndent = function(str, n) {
};
var getIndentSize = function(str) {
- var start = /^([ \t]+)/.exec(str);
+ var start = /^([ \t]+)(?:[^\n]+\n\1)+/.exec(str);
if (start) str = applyOutdent(str, start[1].length);
var size = /\n([ \t]+)/.exec(str);
return size ? size[1].length : 0;
};
-var getIndentSize_ = function(str) {
- var i = 0
- , l = str.length
- , indent = 0
- , offset = 0
- , newline = true
- , ch;
-
- for (; i < l; i++) {
- ch = buff[i];
- if (ch > ' ' && newline) {
- if (indent < offset) {
- return buff.length - indent;
- }
- newline = false;
- }
- if (ch === '\n') {
- offset = 0;
- newline = true;
- }
- offset++;
- }
-
- return 0;
-};
-
-
/**
* Expose
*/

0 comments on commit 4e53a00

Please sign in to comment.