Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Initializer from XExpression
Initializer from XExpression [message #1722396] Thu, 04 February 2016 15:05 Go to next message
Daniel Cardin is currently offline Daniel CardinFriend
Messages: 109
Registered: July 2009
Senior Member
I have the following rule I want to infer

Attribute :
	(transient='transient')? type=JvmTypeReference name=ValidID ('=' defaultValue=XLiteral)?
;


to the following static definition

public static final PropertyAttribute<«attribute.type»> «attribute.name» = new PersistentPropertyAttribute<«attribute.type»>("«attribute.name»", «attribute.type».class, «DEFAULT VALUE CODE»);


The normal inferrer works, of course, but I have to deal with the default value, which is not as straightforward as I thought.

My idea, greatly inspired by the past conversations, needs to be validated, please.

If I changed my grammar to :

Attribute returns xbase::XExpression :
	{Attribute} (transient='transient')? type=JvmTypeReference name=ValidID ('=' defaultValue=XLiteral)?
;


... and adapted my compiler to return the "new ...." part with the properly interpreted default value.

Could I do this in the inferrer ?

attributes.toField(attribute.name, genericAttributeType) => [
	visibility = JvmVisibility.PUBLIC
	static = true
	final = true
 	initializer = attribute   ///////////// Since it's an XExpression... I assume it would go through the normal compiler mechanism?
]


Obviously (now ahah), I could use the type computer to make sure the expected type is compatible with the whole expression type.

Do you think that could work?
Re: Initializer from XExpression [message #1722397 is a reply to message #1722396] Thu, 04 February 2016 15:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
What about inferring a default value Method and call this method from the
text that describes the constructor call


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Initializer from XExpression [message #1722403 is a reply to message #1722397] Thu, 04 February 2016 15:43 Go to previous messageGo to next message
Daniel Cardin is currently offline Daniel CardinFriend
Messages: 109
Registered: July 2009
Senior Member
Since it's a static init, I would prefer a constant expression vs a static init method, but I'm having a go at it.
Thanks for your opinion, Christian Smile
Re: Initializer from XExpression [message #1722414 is a reply to message #1722403] Thu, 04 February 2016 16:56 Go to previous messageGo to next message
Daniel Cardin is currently offline Daniel CardinFriend
Messages: 109
Registered: July 2009
Senior Member
Just for the fun of it, and educational value Wink

The parser works, the type inference works (the default value is correctly type checked for the type coming from the attribute definition).
I have added my switch to the doInternalToJavaStatement to support the new Attribute expression.

but it's not being called anymore. I get
Coudn't find a compilation strategy for expressions of type com.netappsid.configurator.modelDsl.impl.AttributeImpl

I have a feeling it's caused by something missing in the type computer, because before I added the type inference, it called the xbase compiler

	def dispatch computeTypes(Attribute attribute, ITypeComputationState state) {
		val typeRef = state.referenceOwner.toPlainTypeReference(attribute.type.type)
		state.withExpectation(typeRef).computeTypes(attribute.defaultValue)
//		state.acceptActualType( .... )   /// missing the global accept or something ?
	}

Re: Initializer from XExpression [message #1722426 is a reply to message #1722414] Thu, 04 February 2016 18:16 Go to previous messageGo to next message
Daniel Cardin is currently offline Daniel CardinFriend
Messages: 109
Registered: July 2009
Senior Member
For the record, the proof of concept works. The problem at this stage was that I was trying to deal with the JavaStatement override, when it was the _toJavaExpression I needed to override, like

	override void _toJavaExpression(XExpression obj, ITreeAppendable appendable) { 
		switch (obj) { 
			case Attribute: _toJavaExpression(obj, appendable) 
			default: super.toJavaExpression(obj, appendable)
		}
	}


Anyway, both the static init function as well and the initializer approach work.
You have taught me much these past days.

Thanks again.
Re: Initializer from XExpression [message #1722427 is a reply to message #1722426] Thu, 04 February 2016 18:30 Go to previous message
Daniel Cardin is currently offline Daniel CardinFriend
Messages: 109
Registered: July 2009
Senior Member
In case anyone finds this post and wonders why the approach didn't work, I pasted the wrong version. Here is the one which works correctly
	override void _toJavaExpression(XExpression obj, ITreeAppendable appendable) {
		switch (obj) {
			Attribute: _toJavaExpression(obj as Attribute, appendable)
			default: super._toJavaExpression(obj, appendable)
		}
	}
Previous Topic:Programmatically modify a DSL file.
Next Topic:Regarding rule precedence and Optional Category
Goto Forum:
  


Current Time: Thu Apr 18 19:20:10 GMT 2024

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

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

Back to the top