Skip to main content



      Home
Home » Modeling » TMF (Xtext) » How to generate code using values of declared variables with JvmModelInferrer in xtext?
How to generate code using values of declared variables with JvmModelInferrer in xtext? [message #910939] Mon, 10 September 2012 12:59 Go to next message
Eclipse UserFriend
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 #910951 is a reply to message #910939] Mon, 10 September 2012 13:25 Go to previous messageGo to next message
Eclipse UserFriend
Hi did you try to inferr fields instead of parameters? Then it should
work out of the box

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
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 14:05 Go to previous messageGo to next message
Eclipse UserFriend
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 14:09] by Moderator

Re: How to generate code using values of declared variables with JvmModelInferrer in xtext? [message #910973 is a reply to message #910966] Mon, 10 September 2012 14:22 Go to previous messageGo to next message
Eclipse UserFriend
Sure we are talking about the same XvariableDeclaration

XVariableDeclaration returns XExpression:
	{XVariableDeclaration}
	(writeable?='var'|'val') (=>(type=JvmTypeReference name=ValidID) | name=ValidID) ('=' right=XExpression)?;



note the {XVariableDeclaration}
Re: How to generate code using values of declared variables with JvmModelInferrer in xtext? [message #911196 is a reply to message #910973] Tue, 11 September 2012 03:08 Go to previous messageGo to next message
Eclipse UserFriend
I still don't understand how you would go about inferring a field from the XExpression retuned by the XVariableDeclaration. I assume you mean code like this:

for(varDecl : constantDecs) {
members += varDecl.toField(varDecl.name, varDecl.type) [
initializer = varDecl.right
setStatic(true)
setFinal(true)
]
}

But once again, as varDecl is an XExpression, it has no methods to get its name or type. Is it possible to cast it to an XVariableDeclaration?

Re: How to generate code using values of declared variables with JvmModelInferrer in xtext? [message #911217 is a reply to message #911196] Tue, 11 September 2012 03:39 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

still the same: XVariableDeclaration is a subclass of xexpression

=> you can filter the list (downcast)

e.g.

it. constantDecs.filter(typeof(XVariableDecl))

Re: How to generate code using values of declared variables with JvmModelInferrer in xtext? [message #911256 is a reply to message #911217] Tue, 11 September 2012 04:48 Go to previous message
Eclipse UserFriend
Christian thanks for your help, I was unclear how to do the downcasting, but this code courtesy of Sebastian Zarnekow worked for me:

for(varDecl : rule.constantDecs) {
switch(varDecl) {
XVariableDeclaration: members += varDecl.toField(varDecl.name, varDecl.type) [
initializer = varDecl.right
setStatic(true)
setFinal(true)
]
}

}




Previous Topic:change type of problem markers
Next Topic:cannot add XMemberFeatureCall to sealed type XExpression
Goto Forum:
  


Current Time: Sat Jul 05 12:33:03 EDT 2025

Powered by FUDForum. Page generated in 0.12951 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top