Permalink
Browse files

Interpolated strings are expressions.

  • Loading branch information...
1 parent 2e744a1 commit 19ed63129e9215399f3b5e6ec67750ddadc87b95 @chrislloyd chrislloyd committed Apr 4, 2010
Showing with 87 additions and 77 deletions.
  1. +4 −4 lib/cake.js
  2. +1 −1 lib/coffee-script.js
  3. +2 −2 lib/command.js
  4. +3 −3 lib/grammar.js
  5. +1 −1 lib/helpers.js
  6. +21 −14 lib/lexer.js
  7. +46 −46 lib/nodes.js
  8. +3 −3 lib/optparse.js
  9. +2 −2 lib/rewriter.js
  10. +1 −1 lib/scope.js
  11. +3 −0 src/lexer.coffee
View
@@ -57,7 +57,7 @@
return path.exists('Cakefile', function(exists) {
var _a, _b, _c, _d, arg, args;
if (!(exists)) {
- throw new Error("Cakefile not found in " + (process.cwd()));
+ throw new Error(("Cakefile not found in " + (process.cwd())));
}
args = process.argv.slice(2, process.argv.length);
CoffeeScript.run(fs.readFileSync('Cakefile'), {
@@ -91,16 +91,16 @@
}
return _b;
}).call(this).join('') : '';
- desc = task.description ? "# " + (task.description) : '';
- puts("cake " + name + spaces + " " + desc);
+ desc = task.description ? ("# " + (task.description)) : '';
+ puts(("cake " + name + spaces + " " + desc));
}}
if (switches.length) {
return puts(oparse.help());
}
};
// Print an error and exit when attempting to all an undefined task.
no_such_task = function no_such_task(task) {
- process.stdio.writeError("No such task: \"" + task + "\"\n");
+ process.stdio.writeError(("No such task: \"" + task + "\"\n"));
return process.exit(1);
};
})();
View
@@ -34,7 +34,7 @@
return (parser.parse(lexer.tokenize(code))).compile(options);
} catch (err) {
if (options.source) {
- err.message = "In " + (options.source) + ", " + (err.message);
+ err.message = ("In " + (options.source) + ", " + (err.message));
}
throw err;
}
View
@@ -61,7 +61,7 @@
compile = function compile(source) {
return path.exists(source, function(exists) {
if (!(exists)) {
- throw new Error("File not found: " + source);
+ throw new Error(("File not found: " + source));
}
return fs.readFile(source, function(err, code) {
return compile_script(source, code);
@@ -221,7 +221,7 @@
};
// Print the `--version` message and exit.
version = function version() {
- puts("CoffeeScript version " + (CoffeeScript.VERSION));
+ puts(("CoffeeScript version " + (CoffeeScript.VERSION)));
return process.exit(0);
};
})();
View
@@ -33,8 +33,8 @@
if (!(action)) {
return [pattern_string, '$$ = $1;', options];
}
- action = (match = (action + '').match(unwrap)) ? match[1] : "(" + action + "())";
- return [pattern_string, "$$ = " + action + ";", options];
+ action = (match = (action + '').match(unwrap)) ? match[1] : ("(" + action + "())");
+ return [pattern_string, ("$$ = " + action + ";"), options];
};
// Grammatical Rules
// -----------------
@@ -693,7 +693,7 @@
}
}
if (name === 'Root') {
- alt[1] = "return " + (alt[1]);
+ alt[1] = ("return " + (alt[1]));
}
return alt;
}).call(this));
View
@@ -132,7 +132,7 @@
if (slash) {
return false;
}
- throw new Error("SyntaxError: Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1));
+ throw new Error(("SyntaxError: Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1)));
}
if (!i) {
return false;
View
@@ -185,7 +185,7 @@
}
quote = match[1].substr(0, 1);
doc = this.sanitize_heredoc(match[2] || match[4], quote);
- this.interpolate_string("" + quote + doc + quote);
+ this.interpolate_string(("" + quote + doc + quote));
this.line += count(match[1], "\n");
this.i += match[1].length;
return true;
@@ -225,8 +225,8 @@
return '\\' + escaped;
});
this.tokens = this.tokens.concat([['(', '('], ['NEW', 'new'], ['IDENTIFIER', 'RegExp'], ['CALL_START', '(']]);
- this.interpolate_string("\"" + str + "\"", true);
- this.tokens = this.tokens.concat([[',', ','], ['STRING', "\"" + flags + "\""], [')', ')'], [')', ')']]);
+ this.interpolate_string(("\"" + str + "\""), true);
+ this.tokens = this.tokens.concat([[',', ','], ['STRING', ("\"" + flags + "\"")], [')', ')'], [')', ')']]);
} else {
this.token('REGEX', regex);
}
@@ -412,7 +412,7 @@
Lexer.prototype.tag_half_assignment = function tag_half_assignment(tag) {
var last;
last = this.tokens.pop();
- this.tokens.push(["" + tag + "=", "" + tag + "=", last[2]]);
+ this.tokens.push([("" + tag + "="), ("" + tag + "="), last[2]]);
return true;
};
// A source of ambiguity in our grammar used to be parameter lists in function
@@ -448,12 +448,12 @@
// The error for when you try to use a forbidden word in JavaScript as
// an identifier.
Lexer.prototype.identifier_error = function identifier_error(word) {
- throw new Error("SyntaxError: Reserved word \"" + word + "\" on line " + (this.line + 1));
+ throw new Error(("SyntaxError: Reserved word \"" + word + "\" on line " + (this.line + 1)));
};
// The error for when you try to assign to a reserved word in JavaScript,
// like "function" or "default".
Lexer.prototype.assignment_error = function assignment_error() {
- throw new Error("SyntaxError: Reserved word \"" + (this.value()) + "\" on line " + (this.line + 1) + " can't be assigned");
+ throw new Error(("SyntaxError: Reserved word \"" + (this.value()) + "\" on line " + (this.line + 1) + " can't be assigned"));
};
// Expand variables and expressions inside double-quoted strings using
// [ECMA Harmony's interpolation syntax](http://wiki.ecmascript.org/doku.php?id=strawman:string_interpolation)
@@ -464,7 +464,7 @@
// new Lexer, tokenize the interpolated contents, and merge them into the
// token stream.
Lexer.prototype.interpolate_string = function interpolate_string(str, escape_quotes) {
- var _a, _b, _c, _d, _e, escaped, expr, group, i, inner, interp, lexer, match, nested, pi, quote, tag, token, tokens, value;
+ var _a, _b, _c, _d, _e, escaped, expr, group, i, inner, interp, interpolated, lexer, match, nested, pi, quote, tag, token, tokens, value;
if (str.length < 3 || !starts(str, '"')) {
return this.token('STRING', str);
} else {
@@ -482,40 +482,44 @@
group = _b[0];
interp = _b[1];
if (starts(interp, '@')) {
- interp = "this." + (interp.substring(1));
+ interp = ("this." + (interp.substring(1)));
}
if (pi < i) {
- tokens.push(['STRING', "" + quote + (str.substring(pi, i)) + quote]);
+ tokens.push(['STRING', ("" + quote + (str.substring(pi, i)) + quote)]);
}
tokens.push(['IDENTIFIER', interp]);
i += group.length - 1;
pi = i + 1;
} else if ((expr = balanced_string(str.substring(i), [['${', '}']]))) {
if (pi < i) {
- tokens.push(['STRING', "" + quote + (str.substring(pi, i)) + quote]);
+ tokens.push(['STRING', ("" + quote + (str.substring(pi, i)) + quote)]);
}
inner = expr.substring(2, expr.length - 1);
if (inner.length) {
- nested = lexer.tokenize("(" + inner + ")", {
+ nested = lexer.tokenize(("(" + inner + ")"), {
rewrite: false,
line: this.line
});
nested.pop();
tokens.push(['TOKENS', nested]);
} else {
- tokens.push(['STRING', "" + quote + quote]);
+ tokens.push(['STRING', ("" + quote + quote)]);
}
i += expr.length - 1;
pi = i + 1;
}
i += 1;
}
if (pi < i && pi < str.length - 1) {
- tokens.push(['STRING', "" + quote + (str.substring(pi, i)) + quote]);
+ tokens.push(['STRING', ("" + quote + (str.substring(pi, i)) + quote)]);
}
if (!(tokens[0][0] === 'STRING')) {
tokens.unshift(['STRING', '""']);
}
+ interpolated = tokens.length > 1;
+ if (interpolated) {
+ this.token('(', '(');
+ }
_c = tokens;
for (i = 0, _d = _c.length; i < _d; i++) {
token = _c[i];
@@ -526,14 +530,17 @@
this.tokens = this.tokens.concat(value);
} else if (tag === 'STRING' && escape_quotes) {
escaped = value.substring(1, value.length - 1).replace(/"/g, '\\"');
- this.token(tag, "\"" + escaped + "\"");
+ this.token(tag, ("\"" + escaped + "\""));
} else {
this.token(tag, value);
}
if (i < tokens.length - 1) {
this.token('+', '+');
}
}
+ if (interpolated) {
+ this.token(')', ')');
+ }
return tokens;
}
};
Oops, something went wrong.

0 comments on commit 19ed631

Please sign in to comment.