Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Xtext] Custom Validation of (E)List Elements
[Xtext] Custom Validation of (E)List Elements [message #631194] Wed, 06 October 2010 14:24 Go to next message
Jens von Pilgrim is currently offline Jens von PilgrimFriend
Messages: 313
Registered: July 2009
Senior Member
Hi all,

I'm trying to add some custom validations to an Xtext generated editor
using a (generated) sub-subclass of AbstractDeclarativeValidator, e.g.,
MyJavaValidator. It is working, however I have a problem of assigning
warnings or errors to elements of an EList.

Example: Assume the following grammar snippet:

Call: "call" ... argtypes+=[TypeDecl] ("," argtypes+=[TypeDecl])* ...

That is, the language contains a call with references to type
declaration, such as "call ... type1, type2, type3"

Now I want to validate the "argtypes". So I have a method

@Check public void checkArgTypes(Call call) {
...
for (TypeDecl typeDecl: call.getArgtypes()) {
// .. do some validation ...
if (error) {
error("Error in ref to type " + typeDecl.getName() ,
MyPackage.CALL__ARG_TYPES);
}
}

The problem is: How can I identify the element (of the feature
CALL__ARG_TYPES) causing the error? I cannot pass an index or something
to the o.e.xtext.validation.AbstractDeclarativeValidator.error(..)
methods. Thus, as in the call above, in case of errors always the first
item is marked (underlined), even if another item causes the error. At
least I would expect to see all items marked.

I probably could add a workaround by not referencing the TypeDecl
directly, but using a reference element, such as

Call: "call" ... argtypes+=TypeRef ("," argtypes+=TypeRef)* ...
TypeRef: type=[TypeDecl];

However, I don't like the idea of bloating the grammar if it is not
really necessary.

Do you know of a solution w/o changing the grammar?

Cheers,

Jens
Re: [Xtext] Custom Validation of (E)List Elements [message #631225 is a reply to message #631194] Wed, 06 October 2010 15:52 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
There's a version of the error and warning methods which you call to flag from the Java validator, that takes a 3rd parameter (actually a second one) which is the particular EObject which should be marked with the error/warning. This means you should be able to stick mostly with your current source. I do see a problem with the references being directly contained in the list, instead of in a separate object which happens to be contained in a list.

Re: [Xtext] Custom Validation of (E)List Elements [message #631244 is a reply to message #631225] Wed, 06 October 2010 16:18 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Meinte,

unfortunately you can't use the overloaded versions of error and warning
in this case as the actual object is contained in another resource and
the problem is not related to the object itself but to the feature setting.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 06.10.10 17:52, schrieb Meinte Boersma:
> There's a version of the error and warning methods which you call to
> flag from the Java validator, that takes a 3rd parameter (actually a
> second one) which is the particular EObject which should be marked with
> the error/warning. This means you should be able to stick mostly with
> your current source. I do see a problem with the references being
> directly contained in the list, instead of in a separate object which
> happens to be contained in a list.
Re: [Xtext] Custom Validation of (E)List Elements [message #631245 is a reply to message #631194] Wed, 06 October 2010 16:20 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Jens,

you are facing this one which will be fixed in the 2.x stream:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=307721

Please note that it is usually not that bad to have a type reference
that wraps the actual cross reference in an own instance.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 06.10.10 16:24, schrieb Jens v.P.:
> Hi all,
>
> I'm trying to add some custom validations to an Xtext generated editor
> using a (generated) sub-subclass of AbstractDeclarativeValidator, e.g.,
> MyJavaValidator. It is working, however I have a problem of assigning
> warnings or errors to elements of an EList.
>
> Example: Assume the following grammar snippet:
>
> Call: "call" ... argtypes+=[TypeDecl] ("," argtypes+=[TypeDecl])* ...
>
> That is, the language contains a call with references to type
> declaration, such as "call ... type1, type2, type3"
>
> Now I want to validate the "argtypes". So I have a method
>
> @Check public void checkArgTypes(Call call) {
> ...
> for (TypeDecl typeDecl: call.getArgtypes()) {
> // .. do some validation ...
> if (error) {
> error("Error in ref to type " + typeDecl.getName() ,
> MyPackage.CALL__ARG_TYPES);
> }
> }
>
> The problem is: How can I identify the element (of the feature
> CALL__ARG_TYPES) causing the error? I cannot pass an index or something
> to the o.e.xtext.validation.AbstractDeclarativeValidator.error(..)
> methods. Thus, as in the call above, in case of errors always the first
> item is marked (underlined), even if another item causes the error. At
> least I would expect to see all items marked.
>
> I probably could add a workaround by not referencing the TypeDecl
> directly, but using a reference element, such as
>
> Call: "call" ... argtypes+=TypeRef ("," argtypes+=TypeRef)* ...
> TypeRef: type=[TypeDecl];
>
> However, I don't like the idea of bloating the grammar if it is not
> really necessary.
>
> Do you know of a solution w/o changing the grammar?
>
> Cheers,
>
> Jens
Re: [Xtext] Custom Validation of (E)List Elements [message #631256 is a reply to message #631245] Wed, 06 October 2010 17:17 Go to previous message
Jens von Pilgrim is currently offline Jens von PilgrimFriend
Messages: 313
Registered: July 2009
Senior Member
Thank you, Sebastian! Yes, I have implemented the aforementioned
workaround. I hope to handle most type issues with the xtext-typesystem
(great work, Markus!), and using explicit type references are easy to
handle with it.

Cheers,

Jens
Previous Topic:Including external Resources in XtextResourceSet
Next Topic:How to resolve ambiguity between blocks and curly literals?
Goto Forum:
  


Current Time: Thu Apr 25 11:28:33 GMT 2024

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

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

Back to the top