DSL中字符串的Groovy求值
我正在尝试使用Groovy编写一个类似DSL的BASIC,我还处于非常早期的阶段。我有一个简短的脚本(忽略包位,我将在适当的时候重构它): 这门课:DSL中字符串的Groovy求值,groovy,dsl,evaluate,Groovy,Dsl,Evaluate,我正在尝试使用Groovy编写一个类似DSL的BASIC,我还处于非常早期的阶段。我有一个简短的脚本(忽略包位,我将在适当的时候重构它): 这门课: package Binsic abstract class BinsicInterpreter extends Script { static def textArea static def setTextArea(def window) { textArea = window } def PRINT(def param) {
package Binsic
abstract class BinsicInterpreter extends Script {
static def textArea
static def setTextArea(def window)
{
textArea = window
}
def PRINT(def param) {
textArea.append param
}
}
这样称呼:
def engine = new BinsicEngine()
BinsicInterpreter.setTextArea(engine.binsicWindow.screenZX)
def conf = new CompilerConfiguration()
conf.setScriptBaseClass("BinsicInterpreter")
def shell = new GroovyShell(conf)
shell.evaluate(new File("./src/Binsic/test.bas"))
(BinsiEngine目前只设置文本区域)
此代码失败
org.codehaus.groovy.control.multipleCompositionErrorsException:启动失败:
/Users/adrian/Documents/workspace-sts-2.9.1.RELEASE/BINSIC/src/BINSIC/test.bas:3:意外标记:Hello World@第3行,第7列。
打印“Hello World”
^
1错误
但是如果我将语句改为打印(“Hello World”),它会工作
类似地,如果我调整打印代码以处理非字符串,我就可以让打印工作正常进行(即,它打印此文件的内存引用)。但不需要括号
为什么没有包装的版本不能工作?我该如何解决这个问题呢?问题在于大写印刷体,或者大写首字母(例如印刷体) 在Groovy中,省略括号是提供更好的DSL支持的语法糖。编译器将有一组关于何时允许和何时不允许的规则 在我的测试中
def Print(String arg) {
println arg
}
def a = Print "Hello World"
工作,而
def Print(String arg) {
println arg
}
Print "Hello World"
正如你发现的那样失败了。我建议把它作为一个问题提出来。它似乎与大写的打印方法有关。
把它从“PRINT”改为“foo”,它就可以工作了。将其更改为“FOO”,它就不起作用了。谢谢,我会在Groovy用户列表中将其作为该语言的一个潜在错误/功能提出来。顺便说一句,现在(也许永远)我将编写一个预处理器类,将输入脚本转换为Groovy可以轻松处理的内容,从而解决这个问题。看见
def Print(String arg) {
println arg
}
Print "Hello World"