ANTLR - Embedding Java code, evaluation before or after?
I am writing a simple scripting language on top of Java / JVM where you can also embed Java code using parentheses {}
. The problem is, how do I parse it in grammar? I have two options:
- Allow everything in it like :
[a-z|a-Z|0-9|_|$]
, and continue - Get some additional java grammar and use this grammar to parse this little code (is it really possible and efficient?)
Since option 2] is mostly double-checked, it is also checked when evaluating Java code. Now my last question is a way that can dynamically execute Java code also with objects that were created at runtime?
Thanks,
William van Doorne
a source to share
1] Allow everything in it, for example: [az | aZ | 0-9 | _ | $], and continue
You can't just do this: you have to consider opening and closing parentheses.
2] Get some extra java grammar and use this grammar to parse this little code (is it really possible and efficient?)
Yes it is possible. But I suggest you get something working first and then worry about efficiency (is this really the problem here?).
... a way that can dynamically execute java code also with objects created at runtime?
Yes, since Java 6, there is a way to compile source files dynamically. See JavaCompiler API.
a source to share
I suggest wrap your Java code inside characters like '' that are not used in Java code and are barely present in literals.
JavaCode: ' ' ( EscapeSequence | ~('\\'|'
')) * '' ';
Use the java.g provided by the antlr examples to get the definition of EscapeSequence, ...
The only thing that is needed is to ask programmers to use the code of this character (`` '), if you want it to be as a literal.
a source to share