我想创建一个像这样的宏
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解析相邻的“|”(管道)分隔符。 任何人都可以建议我该怎么做?