Browse files

made #& and #@ interpolated

  • Loading branch information...
1 parent ee47572 commit ca361c6d300be9660ecf6866eea349a128e53d64 @satyr committed Oct 20, 2012
Showing with 24 additions and 11 deletions.
  1. +12 −4 lib/lexer.js
  2. +1 −1 src/lang-co.co
  3. +9 −4 src/lexer.co
  4. +1 −1 test/operator.co
  5. +1 −1 test/regex.co
View
16 lib/lexer.js
@@ -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,17 +806,23 @@ 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);
} catch (e$) {
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), {
View
2 src/lang-co.co
@@ -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]+?/]]
View
13 src/lexer.co
@@ -520,21 +520,26 @@ 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)`
if i or nested and not stringified
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}
View
2 test/operator.co
@@ -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 &*&
View
2 test/regex.co
@@ -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

@vendethiel

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

@satyr
Owner

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

@vendethiel

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

Please sign in to comment.