1

我有一个宏,我可以在括号之间输入任何代码。然后我将它传递给另一个对该代码有规则的宏。我在其中做其他事情,test它的名称不同,但我认为一个最小的测试用例在这里会很有用。

macro testimpl {
    rule {
         { $lhs:expr is $rhs:expr } 
    } => {
    }
}

macro test {
    rule { $code ... } => { testimpl { $code ... } }
}

testimpl { true is true }   // Works
test(true is true)          // Throws the next error

这是我收到的错误消息:

{ name: 'macro',
  message: 'Macro `testimpl` could not be matched with `{} ...`',
  stx: 
   { token: 
      { type: 3,
        value: 'testimpl',
        lineNumber: 13,
        lineStart: 98,
        range: [Object],
        sm_lineNumber: 9,
        sm_lineStart: 98,
        sm_range: [Object],
        leadingComments: [Object] },
     context: { mark: 649, context: [Object], instNum: 131515 },
     deferredContext: null } }

/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/sweet.js:100
                    throw new SyntaxError(syn.printSyntaxError(source$2, err))
                          ^
SyntaxError: [macro] Macro `testimpl` could not be matched with `{} ...`
9:     rule { $code ... } => { testimpl { $code ... } }
                               ^
    at expand$2 (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/sweet.js:100:27)
    at parse (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/sweet.js:136:29)
    at Object.compile (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/sweet.js:144:19)
    at Object.exports.run (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/sjs.js:85:27)
    at Object.<anonymous> (/home/havvy/sweetjs/playground/node_modules/sweet.js/bin/sjs:7:23)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
4

1 回答 1

2

我认为您缺少规则中的外部括号test

test(true is true)

扩展到

testimpl { (true is true) }

这不符合你的规则testimpl

test将规则更改为rule { ($code ...) }应该有效。

于 2014-10-25T21:26:29.953 回答