Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Scoping and inferring inner classes
Scoping and inferring inner classes [message #1184685] Wed, 13 November 2013 16:19 Go to next message
Matthias Spiller is currently offline Matthias SpillerFriend
Messages: 13
Registered: September 2013
Junior Member
I have a grammar that is based on XBase.
An if-statment like
if(condition) {
  println(1)
}
else {
  println(2)
}

(with println just being an example for arbitrary expressions) should be inferred to something like:

MyIf.run(condition,
  new Runnable() {
    public void run() {
      println(1);
    }
  }, 
  new Runnable() {
    public void run() {
      println(2);
    }
  }
);

I have achieved this by customizing XBaseCompiler._toJavaStatement(XIfExpression, ...)

This of-course only works as long as no fields are used inside one of the then- or else-branch.
E.g.
if(...) {
  println(this.a)
}
else {
  this.a = 1
}


How could I make this working?
What would be the best way to generate the inner classes correctly?
I suspect, I would have to customize the scopeprovider for the branches. Any other ideas?

Many thanks,
Matthias
Re: Scoping and inferring inner classes [message #1184914 is a reply to message #1184685] Wed, 13 November 2013 19:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6494
Registered: July 2009
Senior Member
Hi,

just a 5 mins try

grammar org.xtext.example.mydsl3.MyDsl with org.eclipse.xtext.xbase.Xbase

generate myDsl "http://www.xtext.org/example/mydsl3/MyDsl"

import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase

Model:
	body=MyXBlockExpression;
	
MyXBlockExpression returns xbase::XExpression: 
	{xbase::XBlockExpression}
	'{'
		(expressions+=MyXIfExpression ';'?)
	'}';	
	
MyXIfExpression returns xbase::XExpression:
	{xbase::XIfExpression}
	'if' '(' if=XExpression ')'
	then=XClosureX
	(=>'else' else=XClosureX)?;

	
XClosureX returns xbase::XExpression:
	{xbase::XClosure}
	explicitSyntax?="{"expression=XExpressionInClosure"}";


class MyDslJvmModelInferrer extends AbstractModelInferrer {
	@Inject extension JvmTypesBuilder

   	def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
   		acceptor.accept(element.toClass("my.company.greeting.MyGreetings"))
   			.initializeLater([
   				
   					members += element.toMethod("doSomething", element.newTypeRef(Runnable)) [
   						body = element.body
   					]
   				
   			])
   	}
}

Re: Scoping and inferring inner classes [message #1186292 is a reply to message #1184685] Thu, 14 November 2013 16:04 Go to previous message
Matthias Spiller is currently offline Matthias SpillerFriend
Messages: 13
Registered: September 2013
Junior Member
Thanks a lot.
That was pointing me to the right direction.
I have basically adopted your proposed grammar.

I have modified the TypeComputer so that the expected type for the closures that are children of the if-expression is Runnable.

In the XbaseCompiler, I first call _toJavaStatement for the two closures and generate the appropriate code that passes them to the call to MyIf.run(...).

Thanks again!
Matthias
Previous Topic:Thanks for the help!!!!
Next Topic:Compiling Xassignment to FeatureCall
Goto Forum:
  


Current Time: Wed Nov 26 05:55:25 GMT 2014

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

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