Search code examples
macrossweet.js

Sweet.js Macro taking anything passed to macro taking structure failing


I have a macro where I take in any code in between parenthesis. I then pass it to another macro that has rules on that code. I do other things in test and it is named differently, but I figured a minimal testcase would be useful here.

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

Here's the error message I get:

{ 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)

Solution

  • I think you are missing the outer parens in the rule for test.

    test(true is true)
    

    expands to

    testimpl { (true is true) }
    

    which doesn't match your rule in testimpl.

    Changing the test rule to rule { ($code ...) } should work.