2

I'm brand new to Sweet.js. My first simple macro is the following

macro test {
  rule {
    $className($entityName)
  } => {
    function test$className()
    {
      console.print("$className");
      console.print("$entityName");
    }
  }
}

test me(More)

which produces

function test$className() {
    console.print(me);
    console.print(More);
}

but I'd like it to produce this:

function testMe() {
    console.print("me");
    console.print("More");
}

but any variants I've tried for it haven't worked. Any suggestions?

4

1 回答 1

4

您需要使用 case 宏来构造您想要的确切标记:

macro test {
    case {_
        $className($entityName)
    } => {
        var classStr = unwrapSyntax(#{$className}[0]);
        var entityStr = unwrapSyntax(#{$entityName}[0]);
        letstx $fnName = [makeIdent("test" + classStr, #{here})];
        letstx $classStr = [makeValue(classStr, #{here})];
        letstx $entityStr = [makeValue(entityStr, #{here})];
        return #{
            function $fnName()
            {
                console.print($classStr);
                console.print($entityStr);
            }
        }
    }
}

test me(More)
于 2014-08-09T01:01:00.327 回答