From 4932d2554030983eb8dda1b1b07bac060e49c5f3 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 9 Mar 2010 20:53:56 -0500 Subject: [PATCH] making balanced_string accept an array of delimiters, in hope of using it in the Rewriter --- lib/lexer.js | 11 +++++------ src/lexer.coffee | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/lexer.js b/lib/lexer.js index 80d761ae..cb7ef043 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -32,8 +32,8 @@ // unless explicitly asked not to. Lexer.prototype.tokenize = function tokenize(code, options) { var o; - o = options || {}; code = code.replace(/\r/g, ''); + o = options || {}; this.code = code; // The remainder of the source code. this.i = 0; @@ -203,7 +203,7 @@ Lexer.prototype.balanced_token = function balanced_token() { var delimited; delimited = Array.prototype.slice.call(arguments, 0); - return this.balanced_string.apply(this, [this.chunk].concat(delimited)); + return this.balanced_string(this.chunk, delimited); }; // Matches and conumes comments. We pass through comments into JavaScript, // so they're treated as real tokens, like any other part of the language. @@ -407,9 +407,8 @@ // a series of delimiters, all of which must be nested correctly within the // contents of the string. This method allows us to have strings within // interpolations within strings etc... - Lexer.prototype.balanced_string = function balanced_string(str) { - var _a, _b, _c, _d, close, delimited, i, levels, open, pair, slash; - delimited = Array.prototype.slice.call(arguments, 1); + Lexer.prototype.balanced_string = function balanced_string(str, delimited) { + var _a, _b, _c, _d, close, i, levels, open, pair, slash; slash = delimited[0][0] === '/'; levels = []; i = 0; @@ -485,7 +484,7 @@ tokens.push(['IDENTIFIER', interp]); i += group.length - 1; pi = i + 1; - } else if (((expr = this.balanced_string(str.substring(i), ['${', '}'])))) { + } else if (((expr = this.balanced_string(str.substring(i), [['${', '}']])))) { if (pi < i) { tokens.push(['STRING', '' + quote + (str.substring(pi, i)) + quote]); } diff --git a/src/lexer.coffee b/src/lexer.coffee index 75350b7d..277030f3 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -146,7 +146,7 @@ exports.Lexer: class Lexer # Matches a token in which which the passed delimiter pairs must be correctly # balanced (ie. strings, JS literals). balanced_token: (delimited...) -> - @balanced_string @chunk, delimited... + @balanced_string @chunk, delimited # Matches and conumes comments. We pass through comments into JavaScript, # so they're treated as real tokens, like any other part of the language. @@ -308,7 +308,7 @@ exports.Lexer: class Lexer # a series of delimiters, all of which must be nested correctly within the # contents of the string. This method allows us to have strings within # interpolations within strings etc... - balanced_string: (str, delimited...) -> + balanced_string: (str, delimited) -> slash: delimited[0][0] is '/' levels: [] i: 0 @@ -362,7 +362,7 @@ exports.Lexer: class Lexer tokens.push ['IDENTIFIER', interp] i += group.length - 1 pi: i + 1 - else if (expr: @balanced_string str.substring(i), ['${', '}']) + else if (expr: @balanced_string str.substring(i), [['${', '}']]) tokens.push ['STRING', "$quote${ str.substring(pi, i) }$quote"] if pi < i inner: expr.substring(2, expr.length - 1) if inner.length