Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

made #& and #@ interpolated

  • Loading branch information...
commit ca361c6d300be9660ecf6866eea349a128e53d64 1 parent ee47572
Satoshi Murakami authored
16 lib/lexer.js
View
@@ -774,7 +774,7 @@ exports.parameters = function(arrow){
this.token(')PARAM', '');
};
exports.interpolate = function(str, idx, end){
- var parts, end0, pos, i, ch, id, stringified, length, e, delta, nested, clone, ref$;
+ var parts, end0, pos, i, ch, c1, id, stringified, length, tag, e, delta, nested, clone, ref$;
parts = [];
end0 = end.charAt(0);
pos = 0;
@@ -789,7 +789,9 @@ exports.interpolate = function(str, idx, end){
parts.push(['S', this.countLines(str.slice(0, i)), this.line]);
return parts.size = pos + i + end.length, parts;
case '#':
- if (!((id = (ID.lastIndex = i + 1, ID).exec(str)[1]) || '{' === str.charAt(i + 1))) {
+ c1 = str.charAt(i + 1);
+ id = (c1 === '@' || c1 === '&') && c1 || (ID.lastIndex = i + 1, ID).exec(str)[1];
+ if (!(id || c1 === '{')) {
continue;
}
break;
@@ -804,7 +806,12 @@ exports.interpolate = function(str, idx, end){
}
if (id) {
length = id.length;
- if (id !== 'this') {
+ if (id === '@') {
+ id = 'this';
+ }
+ if (id === 'this' || id === '&') {
+ tag = 'LITERAL';
+ } else {
id = camelize(id);
try {
Function("'use strict'; var " + id);
@@ -812,9 +819,10 @@ exports.interpolate = function(str, idx, end){
e = e$;
this.carp("invalid variable interpolation \"" + id + "\"");
}
+ tag = 'ID';
}
str = str.slice(delta = i + 1 + length);
- parts.push(['TOKENS', nested = [['ID', id, this.line]]]);
+ parts.push(['TOKENS', nested = [[tag, id, this.line]]]);
} else {
clone = (ref$ = clone$(exports), ref$.inter = true, ref$.emender = this.emender, ref$);
nested = clone.tokenize(str.slice(i + 2), {
2  src/lang-co.co
View
@@ -69,7 +69,7 @@ tint \co [
]
### Subrules
-interps = [\lang-co // ^# ({[\S\s]*?} | #ident) // \#]
+interps = [\lang-co // ^# ({[\S\s]*?} | #ident | [@&]) // \#]
regexes = [\lit /^[\S\s]+?/]
tint \co-qq [interps] [[\str /^[\S\s]+?/]]
13 src/lexer.co
View
@@ -520,8 +520,9 @@ exports import
parts.push [\S; @countLines str.slice 0 i; @line]
return parts <<< size: pos + i + end.length
case \#
- continue unless (id = (ID <<< lastIndex: i+1)exec str .1)
- or \{ is str.charAt i+1
+ c1 = str.charAt i+1
+ id = c1 of <[ @ & ]> and c1 or (ID <<< lastIndex: i+1)exec str .1
+ continue unless id or c1 is \{
case \\ then ++i; fallthrough
default continue
# `"#a#{b || c}"` => `a + "" + (b || c)`
@@ -529,12 +530,16 @@ exports import
stringified = parts.push [\S; @countLines str.slice 0 i; @line]
if id
{length} = id
- unless id is \this
+ id = \this if id is \@
+ if id of <[ this & ]>
+ tag = \LITERAL
+ else
id = camelize id
try Function "'use strict'; var #id" catch
@carp "invalid variable interpolation \"#id\""
+ tag = \ID
str.=slice delta = i + 1 + length
- parts.push [\TOKENS nested = [[\ID id, @line]]]
+ parts.push [\TOKENS nested = [[tag, id, @line]]]
else
clone = ^exports <<< {+inter, @emender}
nested = clone.tokenize str.slice(i+2), {@line, +raw}
2  test/operator.co
View
@@ -420,7 +420,7 @@ eq o.c * o.d, 14
### Pipe / Cascade
Array 0 |> &concat 1, 2
|> & <<< {3: 4}
- |> eq '1,2,,4' "#{&}"
+ |> eq '1,2,,4' "#&"
String 0
|> if & then &+& else &*&
2  test/regex.co
View
@@ -69,7 +69,7 @@ eq \01234 // 0 #{
} 4 //source
let this = \THIS
- ok //^ \\##this $//test //\#THIS//source
+ ok //^ \\##@#this $//test //\#THISTHIS//source
# [coffee#584](https://github.com/jashkenas/coffee-script/issues/584)

3 comments on commit ca361c6

ven

shouldn't "#@i" compile to "" + this.i; ?

Satoshi Murakami
Owner

It would no longer be variable interpolation if we were to allow that. Allowing arbitrary expressions in this form isn't viable.

ven

It's debatable, but allows toString use in more cases I suppose

Please sign in to comment.
Something went wrong with that request. Please try again.