1

I want to create a macro like this

var diffLength =
    | index1 === 0 then xPos[index1]
    | index1 === 2 then (xPos[index1] - xPos[index1 - 1]) * focusFactor
    | otherwise xPos[index1] - xPos[index1 - 1]

which should expand like :

var diffLength = 
   index1 === 0 ? xPos[index1] :
   index1 === 2 ? (xPos[index1] - xPos[index1 - 1]) * focusFactor : 
   xPos[index1] - xPos[index1 - 1];

SweetJS code :

macro |{
    rule { 
      $cond:expr then $check:expr $($guard:(|) $restCond:expr then $restCheck:expr) ... $lastGuard:(|) otherwise $lastCheck:expr
    } => { 
      ( $cond ? $check : $($restCond ? $restCheck :) ... $lastCheck )
    }
}

which works only if I change $guards:(h) and $lastGuard:(h) insteed of $guards:(|) and $lastGuard:(|) and actual code like

var diffLength =
    | index1 === 0 then xPos[index1]
    h index1 === 2 then (xPos[index1] - xPos[index1 - 1]) * focusFactor
    h otherwise xPos[index1] - xPos[index1 - 1]

Reason is I am unable to stop sweetjs to parse the adjacent "|"(pipe) delimiters. Can anyone suggest how can I do it?

kalpa
  • 657
  • 2
  • 11
  • 22
  • I suspect the issue with your pattern is that `|` is a legal JavaScript expression and so specifying something like `$cond:expr then $check:expr` will try and match `index1 === 0 then xPos[index1] | index1 === 2 then ...` and so on. If you add a symbol after each check expression (such as a comma), it works. – Anthony Calandra Jan 19 '16 at 06:40

0 Answers0