Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to get an inverse boolean assignment operator?
How to get an inverse boolean assignment operator? [message #1736535] Thu, 30 June 2016 09:10 Go to next message
Dennis Hendriks is currently offline Dennis HendriksFriend
Messages: 66
Registered: January 2010
Location: Eindhoven, The Netherland...
Member
Consider a class 'A' with an attribute 'b' of type 'EBoolean'. In the textual syntax, there is a 'notb' keyword, that is optional. If it is specified, attribute 'b' should be 'false'. If it is not specified, it should be 'true'. How can this be specified in Xtext?

I tried using the 'boolean assignment operator', but in this case it needs to be inverted from the default behavior that if the keyword is present, the attribute becomes 'true'.

That is:

A: ... some other attributes and stuff... b?='notb'


Doesn't work, as this way attrbute 'b' gets the wrong boolean value. I couldn't find a way to invert the boolean value.

I also tried:

A: ... some other attributes and stuff... (b?=|'notb')


Here I want to recognize no input to assign 'true' using the boolean assignment operator. Alternatively, 'notb' is parsed, and the 'b' feature keeps its value of 'false'. Xtext doesn't allow this.

I tried the same with a terminal that can be either empty or the 'notb' keyword, but that was not allowed either.

Note that I can't change the syntax, as it is in production already (C code, yacc generated parser). I use an imported metamodel. I can change it to rename the 'b' attribute to 'notb'. That works for now. But I don't like the negation being part of the name.

Is there any solution to this problem?
Re: How to get an inverse boolean assignment operator? [message #1736549 is a reply to message #1736535] Thu, 30 June 2016 09:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 10423
Registered: July 2009
Senior Member
you can try something like

package org.xtext.example.mydsl8

import org.eclipse.xtext.parser.DefaultEcoreElementFactory
import org.eclipse.emf.ecore.EClassifier
import org.xtext.example.mydsl8.myDsl.Greeting
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.nodemodel.INode
import org.eclipse.xtext.conversion.ValueConverterException
import org.xtext.example.mydsl8.myDsl.MyDslPackage

class MyDslEcoreElementFactory extends DefaultEcoreElementFactory {
	
	override create(EClassifier classifier) {
		val result = super.create(classifier)
		if (result instanceof Greeting) {
			result.flag = true
		}
		result
	}
	
	override set(EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException {
		if (object instanceof Greeting && feature == MyDslPackage.Literals.GREETING__FLAG.name) {
			super.set(object, feature, false, ruleName, node)
		} else {
			super.set(object, feature, value, ruleName, node)
		}
		
	}
	
}


class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
	
	override bindIAstFactory() {
		MyDslEcoreElementFactory
	}
	
}


or switch to a manually maintained metamodel with default value = true and a grammar like (flag="flag")? with a valueConverter turning "flag" into false
Re: How to get an inverse boolean assignment operator? [message #1749013 is a reply to message #1736549] Thu, 01 December 2016 07:17 Go to previous messageGo to next message
Niels Brouwers is currently offline Niels BrouwersFriend
Messages: 78
Registered: July 2009
Member
Christian Dietrich wrote on Thu, 30 June 2016 11:59
you can try something like

package org.xtext.example.mydsl8

import org.eclipse.xtext.parser.DefaultEcoreElementFactory
import org.eclipse.emf.ecore.EClassifier
import org.xtext.example.mydsl8.myDsl.Greeting
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.nodemodel.INode
import org.eclipse.xtext.conversion.ValueConverterException
import org.xtext.example.mydsl8.myDsl.MyDslPackage

class MyDslEcoreElementFactory extends DefaultEcoreElementFactory {
	
	override create(EClassifier classifier) {
		val result = super.create(classifier)
		if (result instanceof Greeting) {
			result.flag = true
		}
		result
	}
	
	override set(EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException {
		if (object instanceof Greeting && feature == MyDslPackage.Literals.GREETING__FLAG.name) {
			super.set(object, feature, false, ruleName, node)
		} else {
			super.set(object, feature, value, ruleName, node)
		}
		
	}
	
}


class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
	
	override bindIAstFactory() {
		MyDslEcoreElementFactory
	}
	
}


or switch to a manually maintained metamodel with default value = true and a grammar like (flag="flag")? with a valueConverter turning "flag" into false


Hi Dennis, we have pursued the second approach proposed by Christian. We could share our experiences if you want.

Kind regards,
Niels Brouwers (Altran)
Re: How to get an inverse boolean assignment operator? [message #1749023 is a reply to message #1749013] Thu, 01 December 2016 08:57 Go to previous message
Dennis Hendriks is currently offline Dennis HendriksFriend
Messages: 66
Registered: January 2010
Location: Eindhoven, The Netherland...
Member
Niels Brouwers wrote on Thu, 01 December 2016 08:17

...

Hi Dennis, we have pursued the second approach proposed by Christian. We could share our experiences if you want.

Kind regards,
Niels Brouwers (Altran)


We ended up simply changing the metamodel, inverting the meaning of the feature. Not ideal, but it works and is simple.

But if you have some code example of how to do it properly, by all means, share it. Also for others who might stumble upon this forum post in the future.
Previous Topic:DSL tools from Eclipse Site
Next Topic:xtext A conflict occurred for ALT+CTRL+H error
Goto Forum:
  


Current Time: Mon Apr 24 09:53:08 GMT 2017

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

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