Skip to content
This repository
Browse code

made #& and #@ interpolated

  • Loading branch information...
commit ca361c6d300be9660ecf6866eea349a128e53d64 1 parent ee47572
Satoshi Murakami authored
16 lib/lexer.js
@@ -774,7 +774,7 @@ exports.parameters = function(arrow){
774 774 this.token(')PARAM', '');
775 775 };
776 776 exports.interpolate = function(str, idx, end){
777   - var parts, end0, pos, i, ch, id, stringified, length, e, delta, nested, clone, ref$;
  777 + var parts, end0, pos, i, ch, c1, id, stringified, length, tag, e, delta, nested, clone, ref$;
778 778 parts = [];
779 779 end0 = end.charAt(0);
780 780 pos = 0;
@@ -789,7 +789,9 @@ exports.interpolate = function(str, idx, end){
789 789 parts.push(['S', this.countLines(str.slice(0, i)), this.line]);
790 790 return parts.size = pos + i + end.length, parts;
791 791 case '#':
792   - if (!((id = (ID.lastIndex = i + 1, ID).exec(str)[1]) || '{' === str.charAt(i + 1))) {
  792 + c1 = str.charAt(i + 1);
  793 + id = (c1 === '@' || c1 === '&') && c1 || (ID.lastIndex = i + 1, ID).exec(str)[1];
  794 + if (!(id || c1 === '{')) {
793 795 continue;
794 796 }
795 797 break;
@@ -804,7 +806,12 @@ exports.interpolate = function(str, idx, end){
804 806 }
805 807 if (id) {
806 808 length = id.length;
807   - if (id !== 'this') {
  809 + if (id === '@') {
  810 + id = 'this';
  811 + }
  812 + if (id === 'this' || id === '&') {
  813 + tag = 'LITERAL';
  814 + } else {
808 815 id = camelize(id);
809 816 try {
810 817 Function("'use strict'; var " + id);
@@ -812,9 +819,10 @@ exports.interpolate = function(str, idx, end){
812 819 e = e$;
813 820 this.carp("invalid variable interpolation \"" + id + "\"");
814 821 }
  822 + tag = 'ID';
815 823 }
816 824 str = str.slice(delta = i + 1 + length);
817   - parts.push(['TOKENS', nested = [['ID', id, this.line]]]);
  825 + parts.push(['TOKENS', nested = [[tag, id, this.line]]]);
818 826 } else {
819 827 clone = (ref$ = clone$(exports), ref$.inter = true, ref$.emender = this.emender, ref$);
820 828 nested = clone.tokenize(str.slice(i + 2), {
2  src/lang-co.co
@@ -69,7 +69,7 @@ tint \co [
69 69 ]
70 70
71 71 ### Subrules
72   -interps = [\lang-co // ^# ({[\S\s]*?} | #ident) // \#]
  72 +interps = [\lang-co // ^# ({[\S\s]*?} | #ident | [@&]) // \#]
73 73 regexes = [\lit /^[\S\s]+?/]
74 74
75 75 tint \co-qq [interps] [[\str /^[\S\s]+?/]]
13 src/lexer.co
@@ -520,8 +520,9 @@ exports import
520 520 parts.push [\S; @countLines str.slice 0 i; @line]
521 521 return parts <<< size: pos + i + end.length
522 522 case \#
523   - continue unless (id = (ID <<< lastIndex: i+1)exec str .1)
524   - or \{ is str.charAt i+1
  523 + c1 = str.charAt i+1
  524 + id = c1 of <[ @ & ]> and c1 or (ID <<< lastIndex: i+1)exec str .1
  525 + continue unless id or c1 is \{
525 526 case \\ then ++i; fallthrough
526 527 default continue
527 528 # `"#a#{b || c}"` => `a + "" + (b || c)`
@@ -529,12 +530,16 @@ exports import
529 530 stringified = parts.push [\S; @countLines str.slice 0 i; @line]
530 531 if id
531 532 {length} = id
532   - unless id is \this
  533 + id = \this if id is \@
  534 + if id of <[ this & ]>
  535 + tag = \LITERAL
  536 + else
533 537 id = camelize id
534 538 try Function "'use strict'; var #id" catch
535 539 @carp "invalid variable interpolation \"#id\""
  540 + tag = \ID
536 541 str.=slice delta = i + 1 + length
537   - parts.push [\TOKENS nested = [[\ID id, @line]]]
  542 + parts.push [\TOKENS nested = [[tag, id, @line]]]
538 543 else
539 544 clone = ^exports <<< {+inter, @emender}
540 545 nested = clone.tokenize str.slice(i+2), {@line, +raw}
2  test/operator.co
@@ -420,7 +420,7 @@ eq o.c * o.d, 14
420 420 ### Pipe / Cascade
421 421 Array 0 |> &concat 1, 2
422 422 |> & <<< {3: 4}
423   - |> eq '1,2,,4' "#{&}"
  423 + |> eq '1,2,,4' "#&"
424 424
425 425 String 0
426 426 |> if & then &+& else &*&
2  test/regex.co
@@ -69,7 +69,7 @@ eq \01234 // 0 #{
69 69 } 4 //source
70 70
71 71 let this = \THIS
72   - ok //^ \\##this $//test //\#THIS//source
  72 + ok //^ \\##@#this $//test //\#THISTHIS//source
73 73
74 74
75 75 # [coffee#584](https://github.com/jashkenas/coffee-script/issues/584)

3 comments on commit ca361c6

Nami-Doc

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.

Nami-Doc

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.