How to generate code using values of declared variables with JvmModelInferrer in xtext? [message #910939] |
Mon, 10 September 2012 16:59 |
Marcus Mathioudakis Messages: 18 Registered: June 2012 |
Junior Member |
|
|
So I have the following XText grammar:
Start:
rules+=Rule+;
Rule:
constantDecs+= XVariableDeclaration*
elementDecs+=elementDec+
'PAYLOAD' payload=PAYLOAD 'CONSTRAINT' expression= XExpression
;
elementDec:
variable=FullJvmFormalParameter '=' xpath=XPATH
;
PAYLOAD:
"Stacons" | "PFResults" | "any" | "sse";
//we override the definition in Xtype.xtext, so that we can have // in Xpaths
terminal SL_COMMENT:
'#' !('\n' | '\r')* ('\r'? '\n')?;
terminal XPATH:
(('//'|'/')('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|'_'|':'|'0'..'9')*)+
;
And I am using a JvmModelInferrer to infer a method for each Rule. The relevant part of the inferrer's code is:
def dispatch void infer(Start start, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
for (rule: start.rules) {
acceptor.accept(rule.toClass("Rule" + counter)).initializeLater[
...
// method used to check the rule this class represents
members += rule.toMethod("checkRule",rule.newTypeRef('boolean'))[
for (e : rule.elementDecs) {
parameters += e.variable.toParameter(e.variable.name, e.variable.parameterType)
}
setStatic(true)
body = expression
]
What I can't figure out how to do is make the values of the variable decalarations in constantDecs visible to the expression, and more specifically how to generate code for that expression which contains those values. At the moment the variable declarations are in scope in the generated code, but their value is declared to be their name.
For example input:
val j = Integer::parseInt('199')
int x = //val/v
PAYLOAD Stacons CONSTRAINT x>j
results in the generated method:
public static boolean checkRule(final int x) {
int _j = j;
boolean _greaterThan = (x > _j);
return _greaterThan;
}
whereas I would like it to generate the method:
public static boolean checkRule(final int x) {
int _j = 199;
boolean _greaterThan = (x > _j);
return _greaterThan;
}
My scope provider looks like this:
@Inject
IJvmModelAssociations associations;
@Override
protected IScope createLocalVarScopeForJvmOperation(JvmOperation context, IScope parentScope) {
parentScope = super.createLocalVarScopeForJvmOperation(context, parentScope);
// retrieve the AST element associated to the method
// created by our model inferrer
EObject sourceElement = associations.getPrimarySourceElement(context);
if (sourceElement instanceof Rule) {
Rule rule = (Rule) sourceElement;
return Scopes.scopeFor(rule.getConstantDecs(), parentScope);
}
return parentScope;
I have tried fiddling with the scopes and the inferrer but to no avail. Is what I'm trying to do possible?
|
|
|
|
Re: How to generate code using values of declared variables with JvmModelInferrer in xtext? [message #910966 is a reply to message #910951] |
Mon, 10 September 2012 18:05 |
Marcus Mathioudakis Messages: 18 Registered: June 2012 |
Junior Member |
|
|
You mean infer fields for each XVariableDeclaration? That doesn't seem possible, as the to Field method requires a name, a type and an initializer, but the XVariableDeclaration rule returns nothing but an XExpression, which I believe is just the name of the declared variable. So how could I get the actual value of the declaration and the type from this XExpression? if you could post the actual code that achieves this then that would be much appreciated, as I can't see how its possible.
Just to be clear, I am not inferring anything for the variable declarations at the moment, as can be seen in the modelInferrer. I am only inferring parameters from the elementDecs, and that works fine. The elementDecs are distinct from the variable declarations (rule constantDecs).
[Updated on: Mon, 10 September 2012 18:09] Report message to a moderator
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03927 seconds