당신이 폐쇄에 넣을 수 있다면, 내 예에서와 같이, any
방법에 응답하거나 객체, invokeMethod
에 위임 :
당신이 파일에서 스크립트를 읽는 경우
class Dsl {
def params = []
def invokeMethod(String method, args) {
params << [method, args]
this
}
def propertyMissing(String prop) { prop }
}
a = {
any live cell with fewer than two live neighbours dies
}
dsl = new Dsl()
a.delegate = dsl
a()
assert dsl.params == [
['any', ['live']],
['cell', ['with']],
['fewer', ['than']],
['two', ['live']],
['neighbours', ['dies']],
]
, 명시 적으로 any
라는 방법을 가지고하는 것은 필요한 것 같다 CompilerConfiguration에 mrhaki에
import org.codehaus.groovy.control.CompilerConfiguration
class Dsl {
def params = []
def invokeMethod(String method, args) {
params << [method, args]
this
}
def any(param) { invokeMethod('any', [param]) }
def propertyMissing(String prop) { prop }
}
code = 'any live cell with fewer than two live neighbours dies'
parsed = new GroovyShell(
getClass().classLoader,
new Binding(),
new CompilerConfiguration(scriptBaseClass : DelegatingScript.class.name)
).parse(code)
dsl = new Dsl()
parsed.setDelegate(dsl)
parsed.run()
assert dsl.params == [
['any', ['live']],
['cell', ['with']],
['fewer', ['than']],
['two', ['live']],
['neighbours', ['dies']],
]
했네.