1

我想创建一个这样的宏

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

应该像这样扩展:

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

SweetJS 代码:

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

仅当我更改 $guards:(h) 和 $lastGuard:(h) 而不是 $guards:(|) 和 $lastGuard:(|) 以及类似的实际代码时才有效

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

原因是我无法阻止 sweetjs 解析相邻的“|”(管道)分隔符。谁能建议我该怎么做?

4

0 回答 0