I am trying to do the simplest example I can think of to use Xbase and the JvmModelInferrer, rather than writing a code generator. I've cut down the JVM language tutorial but I can't get correct Java code from an XExpression (or XBlockExpression). I've looked at answers like :-
How to JvmModelInferrer method body from XExpression and append boilerplate code
The specific error I am getting currently is that for an expression like 2+2, the code I generate is :-
return 2./* name is null */;
My grammar is :-
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
functions+=Function*
;
Function:
'function' name=ID 'body' exp=XBlockExpression
;
and my JvmModelInferrer is :-
def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(element.toClass("my.company.Functions")) [
for (function : element.functions) {
members += function.toMethod(function.name, typeRef(Object)) [
body = function.exp
]
}
]
}
For input :-
function TwoPlusTwo body {2+2}
The generated code is :-
package my.company;
public class Functions {
public java.lang.Object TwoPlusTwo() {
return 2./* name is null */;
}
}
Am I making some completely basic error or have some fundamental misunderstanding ?
I'm using Windows 10, Eclipse 2019-12, Xtext 2.20.0, Coretto JVM
Any help would be appreciated.