Reputation: 3976
I am trying to expose a expression engine built on top of groovy for end users. I have some excel style keywords and user can use variables within those keywords (which need to be substituted at runtime) for evaluation. example, static keyword FIRSTCHAR(varName) will return the first character of a string. I have got this to work in a way.
Current Implementation I have overridden the groovy class loader to create a compilation unit with an overridden PrimaryClassNodeOperation something like
class MyGroovyClassLoader extends GroovyClassLoader {
protected CompilationUnit createCompilationUnit(
CompilerConfiguration config, CodeSource source) {
CompilationUnit cu = super.createCompilationUnit(config, source);
cu.addPhaseOperation(new PrimaryClassNodeOperation(){
@Override
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode)
throws CompilationFailedException {
source.getAST().getStatementBlock().visit(visitor);
}
}, Phases.SEMANTIC_ANALYSIS);
return cu;
}
}
The visitor then recognizes the static expression and variable expression. However, using this approach I am having difficulties in
1) Supporting variable names with dot (.) since dot is considered a property expression. Can I during this phase keep the property expression as variable expression.
2) Also, for an expression like (left(name,1)=='S' && right(name,1)=='n') [first character in name is s and last is n] - I want the equal to check to be single equals rather than double equals, and/AND instead of && or .and() so something like (left(name,1)='S' and right(name,1)='n')
What's the best way to achieve such customizations?
Upvotes: 1
Views: 391
Reputation: 511
The best approach would be to use a custom parser (since you dont want your users to write any groovy) and then use groovy to actually implement the back-end
eg: Very indicative, your implementation can vary
(left(name,1)='S')
For the snipet above, tokens could be method:left, args:((var:name),(val:1)), exp:EQUALS, val:'S' Have a map at the back which equates your tokens to groovy equivalents
You could then be fire your parsed tokens against a groovy class, of course the methods and arguments dont exist, use some groovy tricks to translate it to your actual implementation 1
Upvotes: 1