2

I'm playing around with sweetjs and for the life of me can't figure out why this rule for parameterless multiline skinny arrow syntax isn't matched

Code:

macro -> {
  rule infix { () | { $body ... $last:expr } } => {
    function( ) { 
      $body ...;
      return $last
    }
  }
}

var fn = () -> {
  var a = 1;
  a + 2;
};
expect(fn()).to.equal(3);

results in

SyntaxError: [macro] Macro `-` could not be matched with `> {} ; expect ()...`
10: var fn = () -> {
                ^
George Mauer
  • 117,483
  • 131
  • 382
  • 612

1 Answers1

2

Try removing the semi-colon on the last line of the closure, for some reason the sweetjs compiler has trouble with $last and semi-colons.

macro -> {
 rule infix { () | { $body ... $last:expr } } => {
    function() {
      $($body) ...
      return $last
    }
  }
}

var fn = () -> {
  var a = 1
  a + 2
};
George Mauer
  • 117,483
  • 131
  • 382
  • 612
Justen Martin
  • 626
  • 1
  • 6
  • 13
  • 1
    The reason the compiler has trouble with $last and `;` is because `;` isn't part of an expression. So the pattern will match up to `a + 2` but there will still be a token left. You can handle this by having two rules, one that matches without the `;` at the end and one with a `;`. – timdisney Feb 18 '14 at 22:09