Skip to content

Commit

Permalink
made #& and #@ interpolated
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Oct 20, 2012
1 parent ee47572 commit ca361c6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
16 changes: 12 additions & 4 deletions lib/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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), {
Expand Down
2 changes: 1 addition & 1 deletion src/lang-co.co
Original file line number Diff line number Diff line change
Expand Up @@ -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]+?/]]
Expand Down
13 changes: 9 additions & 4 deletions src/lexer.co
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion test/operator.co
Original file line number Diff line number Diff line change
Expand Up @@ -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 &*&
Expand Down
2 changes: 1 addition & 1 deletion test/regex.co
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

3 comments on commit ca361c6

@vendethiel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@satyr
Copy link
Owner Author

@satyr satyr commented on ca361c6 Nov 24, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@vendethiel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.