From d457423c24a9f9fef8ca3a9155449acea05b35af Mon Sep 17 00:00:00 2001 From: satyr Date: Fri, 24 Sep 2010 22:38:28 +0900 Subject: [PATCH] made simple strings shortcut --- lib/lexer.js | 32 ++++++++++++++++++++------------ src/lexer.coffee | 19 ++++++++++++------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lib/lexer.js b/lib/lexer.js index bdd3d368..cea6eb80 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -1,5 +1,5 @@ (function() { - var ASSIGNED, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, CONVERSIONS, HEREDOC, HEREDOC_INDENT, IDENTIFIER, JS_FORBIDDEN, JS_KEYWORDS, LINE_BREAK, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NEXT_CHARACTER, NOT_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX_END, REGEX_ESCAPE, REGEX_INTERPOLATION, REGEX_START, RESERVED, Rewriter, SHIFT, UNARY, WHITESPACE, _ref, compact, count, include, starts; + var ASSIGNED, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, CONVERSIONS, HEREDOC, HEREDOC_INDENT, IDENTIFIER, JSTOKEN, JS_FORBIDDEN, JS_KEYWORDS, LINE_BREAK, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NEXT_CHARACTER, NOT_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX_END, REGEX_ESCAPE, REGEX_INTERPOLATION, REGEX_START, RESERVED, Rewriter, SHIFT, SIMPLESTR, UNARY, WHITESPACE, _ref, compact, count, include, starts; var __slice = Array.prototype.slice; _ref = require('./rewriter'); Rewriter = _ref.Rewriter; @@ -96,14 +96,23 @@ return true; }; Lexer.prototype.stringToken = function() { - var _ref2, string; - if (!(("'" === (_ref2 = this.chunk.charAt(0)) || '"' === _ref2))) { - return false; + var string; + switch (this.chunk.charAt(0)) { + case "'": + if (!(string = this.match(SIMPLESTR))) { + return false; + } + this.token('STRING', string.replace(MULTILINER, '\\\n')); + break; + case '"': + if (!(string = this.balancedToken(['"', '"'], ['#{', '}']))) { + return false; + } + this.interpolateString(string.replace(MULTILINER, '\\\n')); + break; + default: + return false; } - if (!(string = this.balancedToken(['"', '"'], ['#{', '}']) || this.balancedToken(["'", "'"]))) { - return false; - } - this.interpolateString(string.replace(MULTILINER, '\\\n')); this.line += count(string, '\n'); this.i += string.length; return true; @@ -146,10 +155,7 @@ }; Lexer.prototype.jsToken = function() { var script; - if (this.chunk.charAt(0) !== '`') { - return false; - } - if (!(script = this.balancedToken(['`', '`']))) { + if (!(this.chunk.charAt(0) === '`' && (script = this.match(JSTOKEN)))) { return false; } this.token('JS', script.slice(1, -1)); @@ -590,6 +596,8 @@ COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#])[^\n]*)+/; CODE = /^[-=]>/; MULTI_DENT = /^(?:\n[ \t]*)+/; + SIMPLESTR = /^'[^\\']*(?:\\.[^\\']*)*'/; + JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/; REGEX_START = /^\/([^\/])/; REGEX_INTERPOLATION = /[^\\]#\{.*[^\\]\}/; REGEX_END = /^[imgy]{0,4}(?![a-zA-Z])/; diff --git a/src/lexer.coffee b/src/lexer.coffee index a265e15e..483d3bda 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -118,11 +118,15 @@ exports.Lexer = class Lexer # Matches strings, including multi-line strings. Ensures that quotation marks # are balanced within the string's contents, and within nested interpolations. stringToken: -> - return false unless @chunk.charAt(0) in ["'", '"'] - return false unless string = - @balancedToken(['"', '"'], ['#{', '}']) or - @balancedToken ["'", "'"] - @interpolateString string.replace MULTILINER, '\\\n' + switch @chunk.charAt 0 + when "'" + return false unless string = @match SIMPLESTR + @token 'STRING', string.replace MULTILINER, '\\\n' + when '"' + return false unless string = @balancedToken ['"', '"'], ['#{', '}'] + @interpolateString string.replace MULTILINER, '\\\n' + else + return false @line += count string, '\n' @i += string.length true @@ -153,8 +157,7 @@ exports.Lexer = class Lexer # Matches JavaScript interpolated directly into the source via backticks. jsToken: -> - return false unless @chunk.charAt(0) is '`' - return false unless script = @balancedToken ['`', '`'] + return false unless @chunk.charAt(0) is '`' and script = @match JSTOKEN @token 'JS', script.slice 1, -1 @i += script.length true @@ -537,6 +540,8 @@ WHITESPACE = /^[ \t]+/ COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#])[^\n]*)+/ CODE = /^[-=]>/ MULTI_DENT = /^(?:\n[ \t]*)+/ +SIMPLESTR = /^'[^\\']*(?:\\.[^\\']*)*'/ +JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/ # Regex-matching-regexes. REGEX_START = /^\/([^\/])/