【发布时间】:2016-04-13 15:05:28
【问题描述】:
我想用语法写一个 groovy DSL:
returnValue when booleanCondition
我想使用编译定制器将此表达式转换为使用 AST 转换的典型 if return 语句。
对于这个脚本:
2 when 1 == 1
我收到异常消息:
MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: expecting EOF, found '1' @ line 1, column 8.
我不明白为什么我的编译定制器根本没有被调用? 我需要在编译之前调用它,这样我才能把它变成一个有效的 groovy 代码。
如果脚本包含有效的 groovy 代码,则调用我的编译定制器。
我的代码:
class MyDslTest {
public static void main(String[] args) {
String script = '''2 when 1 == 1
'''
def compilerConfig = new CompilerConfiguration()
compilerConfig.addCompilationCustomizers(new MyCompilationCustomizer())
GroovyShell groovyShell = new GroovyShell(compilerConfig)
groovyShell.evaluate(script)
}
}
class MyCompilationCustomizer extends CompilationCustomizer {
MyCompilationCustomizer() {
super(CompilePhase.CONVERSION)
}
@Override
void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
println 'in compilation customizer'
}
}
【问题讨论】:
标签: groovy abstract-syntax-tree