From 121f01c06fb0268e53c42e79f230cdef574b4b0c Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 8 Mar 2010 19:43:12 -0500 Subject: [PATCH] merged stan's interpolation_3, a couple of tweaks --- lib/lexer.js | 5 ++++- lib/rewriter.js | 2 +- src/lexer.coffee | 3 ++- src/rewriter.coffee | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/lexer.js b/lib/lexer.js index 1986ba76..988fb32e 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -33,7 +33,7 @@ Lexer.prototype.tokenize = function tokenize(code, options) { var o; o = options || {}; - this.code = code; + this.code = code || ''; // The remainder of the source code. this.i = 0; // Current character position we're parsing. @@ -176,6 +176,9 @@ // JavaScript and Ruby. Lexer.prototype.regex_token = function regex_token() { var _a, _b, _c, _d, _e, each, flags, i, index, interp_tokens, merge, regex, str, supress; + if (!(starts(this.chunk, '/'))) { + return false; + } if (!((regex = this.balanced_token((supress = true), ['/', '/'])))) { return false; } diff --git a/lib/rewriter.js b/lib/rewriter.js index cbf63c26..e09299c4 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -81,7 +81,7 @@ Rewriter.prototype.remove_leading_newlines = function remove_leading_newlines() { var _a; _a = []; - while (this.tokens[0][0] === 'TERMINATOR') { + while (this.tokens[0] && this.tokens[0][0] === 'TERMINATOR') { _a.push(this.tokens.shift()); } return _a; diff --git a/src/lexer.coffee b/src/lexer.coffee index b8822a51..6f5e4024 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -36,7 +36,7 @@ exports.Lexer: class Lexer # unless explicitly asked not to. tokenize: (code, options) -> o : options or {} - @code : code # The remainder of the source code. + @code : code or '' # The remainder of the source code. @i : 0 # Current character position we're parsing. @line : o.line or 0 # The current line. @indent : 0 # The current indentation level. @@ -126,6 +126,7 @@ exports.Lexer: class Lexer # to distinguish from division, so we borrow some basic heuristics from # JavaScript and Ruby. regex_token: -> + return false unless starts @chunk, '/' return false unless regex: @balanced_token supress: true, ['/', '/'] return false if regex.length < 3 or regex.match /^\/\s+|\n/ return false if include NOT_REGEX, @tag() diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 1fe3a8d8..8848f6db 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -62,7 +62,7 @@ exports.Rewriter: class Rewriter # Leading newlines would introduce an ambiguity in the grammar, so we # dispatch them here. remove_leading_newlines: -> - @tokens.shift() while @tokens[0][0] is 'TERMINATOR' + @tokens.shift() while @tokens[0] and @tokens[0][0] is 'TERMINATOR' # Some blocks occur in the middle of expressions -- when we're expecting # this, remove their trailing newlines.