Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Parameters in error() using the Xtext/Xtend validation
Parameters in error() using the Xtext/Xtend validation [message #1712065] Wed, 21 October 2015 07:57 Go to next message
John Cole is currently offline John ColeFriend
Messages: 66
Registered: June 2013
Member
Hello everyone,

this is an extract of my DSL:

Element:
	'Element' name=ID
	('firstValue' firstValue=DOUBLE firstValueUnit=UNIT)? &
	'end' 'Element' 
;

enum UNIT:
	mm='mm' |
	cm='cm' |
	m='m' |
;



And this is how I use it in my editor:

Element MyElement
	firstValue -3.536 cm
end Element



I now would like to use the validator, which is generated automatically by Xtext. This is how it looks like:

class MyLanguageValidator extends AbstractMyLanguageValidator {
	@Check
	def unitCheck(Element element) {
		val firstValueUnit = element.firstValueUnit;

		if (firstValueUnit != null) {
			if (!firstValueUnit.equals("m")) {
				// TODO: show warning at right position!
			}
		}
	}
}


The problem is, that the parameters are dubious to me. I tried to google EStructuralFeature but I haven't found useful information, what is expected at this point.

I would like to show an error message "firstValue must be defined in m" at the corresponding line.

I tried to use this:

error("firstValue must be defined in m", null, MyLanguagePackage.ELEMENT__FIRSTVALUE_UNIT);


Unfortunately it marks the complete Element block and also shows this warning, even if firstElement is defined in m.

How do I define a correct error message at the right position, based on the given example?


Greets,
John

[Updated on: Wed, 21 October 2015 07:57]

Report message to a moderator

Re: Parameters in error() using the Xtext/Xtend validation [message #1712066 is a reply to message #1712065] Wed, 21 October 2015 08:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i did not totally get what you actually want to underline but maybe something like

error("firstValue must be defined in m", element, MyLanguagePackage.ELEMENT__FIRSTVALUE_UNIT);

is what you are lookung for


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parameters in error() using the Xtext/Xtend validation [message #1712067 is a reply to message #1712066] Wed, 21 October 2015 08:13 Go to previous messageGo to next message
John Cole is currently offline John ColeFriend
Messages: 66
Registered: June 2013
Member
Hi Christian,

many thanks for your answer!

I would like to underline the second line in the second code snipped of my first post:

Element MyElement
	firstValue -3.536 cm
end Element


The firstValue must be defined in m instead if cm, so the editor should show me an error and point/underline the line firstValue -3.536 cm.

If i try to use your code, I am getting the followinf error, which points to the third parameter in error():

Type mismatch: cannot convert from int to EStructuralFeature


What am I doing wrong? What is an EStructuralFeature, which is expected here? I haven't found any information.
Re: Parameters in error() using the Xtext/Xtend validation [message #1712077 is a reply to message #1712067] Wed, 21 October 2015 08:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

estructual feature is an attribute or a reference of an object.
but you want to highlight a keyword and a estructuralfeature

there is no convenient api for that. you have to calculate the position in the file yourself (Using the Node Model)and then call

getMessageAcceptor().acceptError("Message", object, offset, length, "ErrorCode")


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parameters in error() using the Xtext/Xtend validation [message #1712083 is a reply to message #1712077] Wed, 21 October 2015 08:57 Go to previous messageGo to next message
John Cole is currently offline John ColeFriend
Messages: 66
Registered: June 2013
Member
Oh, I see. I will give it a try! Thank you very much!
Re: Parameters in error() using the Xtext/Xtend validation [message #1712095 is a reply to message #1712083] Wed, 21 October 2015 10:03 Go to previous messageGo to next message
Stefan Oehme is currently offline Stefan OehmeFriend
Messages: 159
Registered: April 2010
Location: Kiel
Senior Member

Christian's snippet was just slightly wrong, you need to use

error("firstValue must be defined in m", element, MyLanguagePackage.Literals.ELEMENT__FIRSTVALUE_UNIT);

That will only underline the unit however. So if you want to underline more, you need to use lower level APIs as Christian suggested.
Re: Parameters in error() using the Xtext/Xtend validation [message #1712097 is a reply to message #1712083] Wed, 21 October 2015 10:12 Go to previous messageGo to next message
John Cole is currently offline John ColeFriend
Messages: 66
Registered: June 2013
Member
I've got another question. Smile
How would I reference the id inside warning()? In the second code snippet of my first post the first line is:

Element MyElement



I now would like to check if the first letter is upper-case. This is my validation method:

@Check
def checkNameIsUppercase(Element element) {
	if(!Character::isUpperCase(element.name.charAt(0))) {
		var message = "Element names should be capitalized."
		// show warning in line 'Element MyElement'
	}
}


I tried using MyLanguagePackage$Literals::ELEMENT__... but unfortunately there is no ELEMENT__NAME. Am I missing something?



EDIT: Got it:

warning("Element names should be capitalized.", null, MyLanguagePackage.ELEMENT__NAME)


But this underlines the complete element implementation, not just a single line, where the warning occurs. Is it possible to underline just one single line, where the warning sign appears?

[Updated on: Wed, 21 October 2015 10:35]

Report message to a moderator

Re: Parameters in error() using the Xtext/Xtend validation [message #1712104 is a reply to message #1712097] Wed, 21 October 2015 10:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

the existence of the constants depends on how you grammar and object hierarchy is.
if you have a look at the Element class. does it or its parent have a name property?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parameters in error() using the Xtext/Xtend validation [message #1712106 is a reply to message #1712104] Wed, 21 October 2015 10:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
P.S:

and it should be again MyDslPackage.Literals.ELEMENT__NAME


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parameters in error() using the Xtext/Xtend validation [message #1712117 is a reply to message #1712106] Wed, 21 October 2015 11:45 Go to previous messageGo to next message
John Cole is currently offline John ColeFriend
Messages: 66
Registered: June 2013
Member
Christian,

I was able to access the element name but I am just using

warning("Error message...", null, MyLanguagePackage.ELEMENT__NAME)


so without the Literals. Please correct me, if I am doing it wrong. Wink
Re: Parameters in error() using the Xtext/Xtend validation [message #1712119 is a reply to message #1712117] Wed, 21 October 2015 11:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
the method accepts an EStructureFeature. There are Constants for all FEATURES in the Literals interface. (if the grammar is to big you may have to use the methods in MyDslPackage.eInstance.getXXX instead. and you shoud not pass null as EObject


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parameters in error() using the Xtext/Xtend validation [message #1712226 is a reply to message #1712119] Thu, 22 October 2015 08:56 Go to previous message
John Cole is currently offline John ColeFriend
Messages: 66
Registered: June 2013
Member
Thank you Christian, I will try to comply with it.
Previous Topic:Handling terminal conflicts
Next Topic:How to run generated java code
Goto Forum:
  


Current Time: Fri Apr 26 16:01:06 GMT 2024

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

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

Back to the top