Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Using XBlockExpressions and XExpressios without JvmModelInferrer
Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1721812] Sun, 31 January 2016 09:34 Go to next message
Larry LeBron is currently offline Larry LeBronFriend
Messages: 124
Registered: October 2015
Senior Member
My DSL extends the XBase grammar, but I have written a custom generator because of some quirks in the way my files rely on the structure of the project.

I would like to be able to use XBlockExpressions as well as XExpressions, and have added them to the grammar.

However, unsurprisingly, the scoping and validation in the XBlockExpression is not working. I understand that the JvmModelInferrer typically handles the scoping, so I can take that on, but am having trouble setting up the proper hooks.

I'm experimenting with binding a custom ITypeComputer and adding a custom ScopeProvider delegate via configureIScopeProviderDelegate(), but I just don't see my XExpressions or XBlockExpressions being checked for scopes.

My custom scope provider extends ImportedNamespaceAwareLocalScopeProvider, and they are bound like so:

	def Class<? extends ITypeComputer> bindITypeComputer() {
		return MyTypeComputer;
	}
	
	/* Bind the delegate in the scope provider */
	override configureIScopeProviderDelegate(Binder binder) {
		binder.bind(IScopeProvider).
		annotatedWith(Names.
		named(AbstractDeclarativeScopeProvider.NAMED_DELEGATE)).
		to(MyImportedNamespaceAwareLocalScopeProvider);
	}


I have overridden the following methods, just to see if they're working:
// In my scope provider
override IScope getScope(EObject context, EReference reference) {...}

// In my TypeComputer
override computeTypes(XExpression expression, ITypeComputationState state) {...}
override void _computeTypes(XBlockExpression b, ITypeComputationState typeState) {...}


I have only seen some output from the custom getScope, but it only seems to be triggered when I explicitly reference a type. A non-explicit variable declaration such as:
val x = y

does not seem to trigger any scope checking, nor does it trigger a validation error.

Likewise,
val int x = "hi"

triggers some scope checking, but does not trigger a validation error based on the mismatched types.

Any guidance to help me get the proper framework set up would be greatly appreciated! In the meantime, I'll continue to fumble around with it Smile
Re: Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1721813 is a reply to message #1721812] Sun, 31 January 2016 09:56 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1784
Registered: July 2009
Location: Firenze, Italy
Senior Member
Hi Larry

you should really use the JvmModelInferrer if you're using Xbase,
otherwise, as you noted, scoping and typing will not work. You can
still bind your own XbaseCompiler to have a custom generation for
expressions... if you need to tweak the way Java classes are generated
then you can bind your own JvmModelGenerator.

Hope this helps
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Re: Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1721815 is a reply to message #1721813] Sun, 31 January 2016 11:11 Go to previous messageGo to next message
Larry LeBron is currently offline Larry LeBronFriend
Messages: 124
Registered: October 2015
Senior Member
Thanks for the advice, Lorenzo. I hand't really considered that as an option.

How does the interplay between the JvmModelInferrer and a custom generator pan out?

With a custom generator, can I just hook into the default xtext generation and modify it?

Can the generator output something completely different from the inferred model if desired?
Re: Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1721816 is a reply to message #1721815] Sun, 31 January 2016 11:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14075
Registered: July 2009
Senior Member
the magic place is jvmmodelgenenerator. this is a normal IGenerator that transforms the inferred jvmmodel to java. it uses the XbaseCompiler for compiling the expressions.

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1722169 is a reply to message #1721815] Wed, 03 February 2016 08:22 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1784
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 31/01/2016 12:11, Larry LeBron wrote:
> Thanks for the advice, Lorenzo. I hand't really considered that as an
> option.
>
> How does the interplay between the JvmModelInferrer and a custom
> generator pan out?
>
> With a custom generator, can I just hook into the default xtext
> generation and modify it?
>
> Can the generator output something completely different from the
> inferred model if desired?

Hi

What do you mean by "completely different"? If your model inferrer
mapped your DSL elements into Java elements, then all the typing and
scoping has been performed according to that mapping. Generating
something completely different will break the overall semantics, i.e.,
you might generate Java code that does not behave correctly (or it might
not even compile).

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Re: Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1722281 is a reply to message #1722169] Wed, 03 February 2016 18:37 Go to previous messageGo to next message
Larry LeBron is currently offline Larry LeBronFriend
Messages: 124
Registered: October 2015
Senior Member
Quote:
Generating
something completely different will break the overall semantics, i.e.,
you might generate Java code that does not behave correctly (or it might
not even compile).


Right, of course. I was mostly wondering if a generator overriding JvmModelGenerator is technically "free" to generate whatever output is desired, vs having some kind of underlying link to the model from JvmModelInferrer. I'm not saying this is a good idea, was more just curious to understand the underlying infrastructure.

Another question would be, does a customized JvmModelInferrer have easy access to the Java code which would be generated by default, allowing straightforward additions or alterations. I understand that alterations made at the generator level would not be reflected in the model, and therefore not accessible to a programmer writing DSL code. This would be more for functionality that need only be present at runtime.
Re: Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1722284 is a reply to message #1722281] Wed, 03 February 2016 18:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14075
Registered: July 2009
Senior Member
there is no general answer to that. with no hints on what you actually want to so it is hard to give advice.

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1722301 is a reply to message #1722284] Wed, 03 February 2016 23:12 Go to previous messageGo to next message
Larry LeBron is currently offline Larry LeBronFriend
Messages: 124
Registered: October 2015
Senior Member
Sure, I understand.

My first case of wanting to do this is that I'd like to append a main method to the file generated by any instance of a certain EClass. The contents of this main should be the mostly the same for any instance of this class, customizing only the invocation of the constructor.

For example, imagine this is a valid entry in my language:
MainObject myMain;


I would then like to generate this java file:
class myMain extends SuperMain {
   public static void main (String[] args) {
      myMain m = new myMain();
      m.start(); // call to superclass method
   }
}


I am currently doing this in the inferrer:

def dispatch void infer(MainObject mainObj, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {		
		val objName = mainObj.name

		acceptor.accept(mainObj.toClass(objName)) [
			superTypes += typeRef(SuperMain)
			
			/* add main method */
			members += mainObj.toMethod('main', typeRef(Void.TYPE)) [
   	    		   parameters += mainObj.toParameter("args",  typeRef(String).addArrayTypeDimension)
   	    		   static = true
        		   varArgs = true
		           body = '''
		        	«objName» m = new «objName»();
		        	m.start();
		        	'''		        
			]	
		]
}


Considering I don't want this main to be callable from the DSL itself, is the inferrer the best place to handle this customization, or would it be better to:


  • Override the generator, appending the main method there
  • Override the XBaseCompiler, customizing the compilation output to somehow add the method
  • Do something else I haven't yet considered?


Thanks!

[Updated on: Thu, 04 February 2016 00:27]

Report message to a moderator

Re: Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1722307 is a reply to message #1722301] Thu, 04 February 2016 03:54 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14075
Registered: July 2009
Senior Member
What about inferring a subclass and add the method there

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Hover provider for terminal rules
Next Topic:Regarding empty state
Goto Forum:
  


Current Time: Sun Oct 17 04:45:50 GMT 2021

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

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

Back to the top