groovy - Binding 'and' to && -
i'm trying build groovy dsl binds and &&:
def binding = new binding([ and: && ]) def shell = new groovyshell(binding) println shell.evaluate ''' true , false ''' } however i'm getting compile-time error:
>groovyc andor.groovy org.codehaus.groovy.control.multiplecompilationerrorsexception: startup failed: andor.groovy: 2: unexpected token: && @ line 2, column 7. and: && ^ 1 error how can bind "and" && operator?
groovy parses left operand right left(operand).getright(). turning and && operator parser related, , doubt able without tweaking groovy source code, antlr, etc.
to snippet working, code intercepts boolean::call metaprogramming, creating new eboolean object. uses getproperty method intercept getright() part:
import org.codehaus.groovy.control.compilerconfiguration class eboolean { boolean left op op def getproperty(string property) { def right = boolean.parseboolean(property) op == op.and ? left && right : null } } class meta { { boolean.metaclass.call { op op -> new eboolean(left: delegate, op: op) } } } enum op { , } binding = new binding([ and: op.and ]) def compilerconfig = new compilerconfiguration( scriptbaseclass: delegatingscript.class.name) def shell = new groovyshell(this.class.classloader, binding, compilerconfig) script = shell.parse( ''' true , false ''' ) script.setdelegate new meta() assert script.run() == false it works, it's pretty specific, , hard use generically. won't work if want variable name context, a=false; true , a. binding won't much, doesn't work variables declared def inside scripts:
Comments
Post a Comment