switching interpolateString's method signature to take on options hash.

This commit is contained in:
Jeremy Ashkenas 2010-07-22 09:38:26 -07:00
parent dfa50c90b3
commit 1dab47176b
2 changed files with 16 additions and 10 deletions

View File

@ -149,7 +149,9 @@
doc = this.sanitizeHeredoc(match[2] || match[4], { doc = this.sanitizeHeredoc(match[2] || match[4], {
quote: quote quote: quote
}); });
this.interpolateString(("" + quote + doc + quote), false, true); this.interpolateString(("" + quote + doc + quote), {
heredoc: true
});
this.line += count(match[1], "\n"); this.line += count(match[1], "\n");
this.i += match[1].length; this.i += match[1].length;
return true; return true;
@ -205,7 +207,9 @@
return '\\' + escaped; return '\\' + escaped;
}); });
this.tokens = this.tokens.concat([['(', '('], ['NEW', 'new'], ['IDENTIFIER', 'RegExp'], ['CALL_START', '(']]); this.tokens = this.tokens.concat([['(', '('], ['NEW', 'new'], ['IDENTIFIER', 'RegExp'], ['CALL_START', '(']]);
this.interpolateString(("\"" + str + "\""), true); this.interpolateString(("\"" + str + "\""), {
escapeQuotes: true
});
if (flags) { if (flags) {
this.tokens.splice(this.tokens.length, 0, [',', ','], ['STRING', ("\"" + flags + "\"")]); this.tokens.splice(this.tokens.length, 0, [',', ','], ['STRING', ("\"" + flags + "\"")]);
} }
@ -453,8 +457,9 @@
} }
return !i ? false : str.substring(0, i); return !i ? false : str.substring(0, i);
}; };
Lexer.prototype.interpolateString = function(str, escapeQuotes, heredoc) { Lexer.prototype.interpolateString = function(str, options) {
var _d, _e, _f, _g, _h, _i, _j, escaped, expr, group, i, idx, inner, interp, interpolated, lexer, match, nested, pi, quote, tag, tok, token, tokens, value; var _d, _e, _f, _g, _h, _i, _j, escaped, expr, group, i, idx, inner, interp, interpolated, lexer, match, nested, pi, quote, tag, tok, token, tokens, value;
options = options || {};
if (str.length < 3 || !starts(str, '"')) { if (str.length < 3 || !starts(str, '"')) {
return this.token('STRING', str); return this.token('STRING', str);
} else { } else {
@ -486,7 +491,7 @@
} }
inner = expr.substring(2, expr.length - 1); inner = expr.substring(2, expr.length - 1);
if (inner.length) { if (inner.length) {
if (heredoc) { if (options.heredoc) {
inner = inner.replace(new RegExp('\\\\' + quote, 'g'), quote); inner = inner.replace(new RegExp('\\\\' + quote, 'g'), quote);
} }
nested = lexer.tokenize(("(" + inner + ")"), { nested = lexer.tokenize(("(" + inner + ")"), {
@ -525,7 +530,7 @@
value = _j[1]; value = _j[1];
if (tag === 'TOKENS') { if (tag === 'TOKENS') {
this.tokens = this.tokens.concat(value); this.tokens = this.tokens.concat(value);
} else if (tag === 'STRING' && escapeQuotes) { } else if (tag === 'STRING' && options.escapeQuotes) {
escaped = value.substring(1, value.length - 1).replace(/"/g, '\\"'); escaped = value.substring(1, value.length - 1).replace(/"/g, '\\"');
this.token(tag, ("\"" + escaped + "\"")); this.token(tag, ("\"" + escaped + "\""));
} else { } else {

View File

@ -132,7 +132,7 @@ exports.Lexer: class Lexer
return false unless match: @chunk.match(HEREDOC) return false unless match: @chunk.match(HEREDOC)
quote: match[1].substr 0, 1 quote: match[1].substr 0, 1
doc: @sanitizeHeredoc match[2] or match[4], {quote} doc: @sanitizeHeredoc match[2] or match[4], {quote}
@interpolateString "$quote$doc$quote", no, yes @interpolateString "$quote$doc$quote", {heredoc: yes}
@line: + count match[1], "\n" @line: + count match[1], "\n"
@i: + match[1].length @i: + match[1].length
true true
@ -170,7 +170,7 @@ exports.Lexer: class Lexer
str: regex.substring(1).split('/')[0] str: regex.substring(1).split('/')[0]
str: str.replace REGEX_ESCAPE, (escaped) -> '\\' + escaped str: str.replace REGEX_ESCAPE, (escaped) -> '\\' + escaped
@tokens: @tokens.concat [['(', '('], ['NEW', 'new'], ['IDENTIFIER', 'RegExp'], ['CALL_START', '(']] @tokens: @tokens.concat [['(', '('], ['NEW', 'new'], ['IDENTIFIER', 'RegExp'], ['CALL_START', '(']]
@interpolateString "\"$str\"", yes @interpolateString "\"$str\"", {escapeQuotes: yes}
@tokens.splice @tokens.length, 0, [',', ','], ['STRING', "\"$flags\""] if flags @tokens.splice @tokens.length, 0, [',', ','], ['STRING', "\"$flags\""] if flags
@tokens.splice @tokens.length, 0, [')', ')'], [')', ')'] @tokens.splice @tokens.length, 0, [')', ')'], [')', ')']
else else
@ -389,7 +389,8 @@ exports.Lexer: class Lexer
# If it encounters an interpolation, this method will recursively create a # If it encounters an interpolation, this method will recursively create a
# new Lexer, tokenize the interpolated contents, and merge them into the # new Lexer, tokenize the interpolated contents, and merge them into the
# token stream. # token stream.
interpolateString: (str, escapeQuotes, heredoc) -> interpolateString: (str, options) ->
options: or {}
if str.length < 3 or not starts str, '"' if str.length < 3 or not starts str, '"'
@token 'STRING', str @token 'STRING', str
else else
@ -411,7 +412,7 @@ exports.Lexer: class Lexer
tokens.push ['STRING', "$quote${ str.substring(pi, i) }$quote"] if pi < i tokens.push ['STRING', "$quote${ str.substring(pi, i) }$quote"] if pi < i
inner: expr.substring(2, expr.length - 1) inner: expr.substring(2, expr.length - 1)
if inner.length if inner.length
inner: inner.replace new RegExp('\\\\' + quote, 'g'), quote if heredoc inner: inner.replace new RegExp('\\\\' + quote, 'g'), quote if options.heredoc
nested: lexer.tokenize "($inner)", {line: @line} nested: lexer.tokenize "($inner)", {line: @line}
(tok[0]: ')') for tok, idx in nested when tok[0] is 'CALL_END' (tok[0]: ')') for tok, idx in nested when tok[0] is 'CALL_END'
nested.pop() nested.pop()
@ -429,7 +430,7 @@ exports.Lexer: class Lexer
[tag, value]: token [tag, value]: token
if tag is 'TOKENS' if tag is 'TOKENS'
@tokens: @tokens.concat value @tokens: @tokens.concat value
else if tag is 'STRING' and escapeQuotes else if tag is 'STRING' and options.escapeQuotes
escaped: value.substring(1, value.length - 1).replace(/"/g, '\\"') escaped: value.substring(1, value.length - 1).replace(/"/g, '\\"')
@token tag, "\"$escaped\"" @token tag, "\"$escaped\""
else else