made simple strings shortcut

This commit is contained in:
satyr 2010-09-24 22:38:28 +09:00
parent f051d0880e
commit d457423c24
2 changed files with 32 additions and 19 deletions

View File

@ -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])/;

View File

@ -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 = /^\/([^\/])/