Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Adding EnumLiteralDeclaration programmatically
icon5.gif  Adding EnumLiteralDeclaration programmatically [message #636116] Fri, 29 October 2010 11:38 Go to next message
Nimesh Mising name is currently offline Nimesh Mising nameFriend
Messages: 23
Registered: March 2010
Junior Member
Hi,

Is there a way to add records to an existing enum programmatically at runtime?

I tried following code in complete_<EnumName> method. And works for code completion but is not available for grammar i.e. it gives error "no viable alternative at input 'AAAAAAAAAA'".
if (!isSet) {
    EnumLiteralDeclaration newEnumLiteralDeclaration = XtextFactory.eINSTANCE
	    .createEnumLiteralDeclaration();
    Keyword newKeyWord = XtextFactory.eINSTANCE.createKeyword();
    newKeyWord.setValue("AAAAAAAAAA");
    newEnumLiteralDeclaration.setLiteral(newKeyWord);
    ((AlternativesImpl) ((EnumRule) ruleCall.getRule())
	    .getAlternatives()).getElements().add(
	    newEnumLiteralDeclaration);
    isSet = true;
}

So obviously this is incorrect mechanism.

What I want to achieve is that for certain Enumerations like State I want the records to be loaded from database at runtime and do not want these modifications to be done in the grammar for every new record that gets added.

Thanks.
Regards,
- Nimesh
Re: Adding EnumLiteralDeclaration programmatically [message #636120 is a reply to message #636116] Fri, 29 October 2010 11:58 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Nimesh,

you cannot create enum literal declaration on the fly. That's neither
possible for the Xtext enum representation nor for sealed enums of
installed EPackages. Please refactor your grammar (e.g. use data type
rules or ID) and use content assist and validation to ensure that the
input conforms the values from the database.

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

Am 29.10.10 13:38, schrieb Nimesh:
> Hi,
>
> Is there a way to add records to an existing enum programmatically at
> runtime?
>
> I tried following code in complete_<EnumName> method. And works for code
> completion but is not available for grammar i.e. it gives error "no
> viable alternative at input 'AAAAAAAAAA'".if (!isSet) {
> EnumLiteralDeclaration newEnumLiteralDeclaration = XtextFactory.eINSTANCE
> .createEnumLiteralDeclaration();
> Keyword newKeyWord = XtextFactory.eINSTANCE.createKeyword();
> newKeyWord.setValue("AAAAAAAAAA");
> newEnumLiteralDeclaration.setLiteral(newKeyWord);
> ((AlternativesImpl) ((EnumRule) ruleCall.getRule())
> .getAlternatives()).getElements().add(
> newEnumLiteralDeclaration);
> isSet = true;
> }
> So obviously this is incorrect mechanism.
>
> What I want to achieve is that for certain Enumerations like State I
> want the records to be loaded from database at runtime and do not want
> these modifications to be done in the grammar for every new record that
> gets added.
>
> Thanks.
> Regards,
> - Nimesh
Re: Adding EnumLiteralDeclaration programmatically [message #636132 is a reply to message #636120] Fri, 29 October 2010 12:46 Go to previous messageGo to next message
Nimesh Mising name is currently offline Nimesh Mising nameFriend
Messages: 23
Registered: March 2010
Junior Member
Hi,

Thanks for the direction.

There are few such Lookups that I need in my grammar. While thinking on providing a generic solution, I thought probably if something is provided as part of XText itself it would be useful for the entire community.

So here's my thought process.

- Define a new keyword called as lookup and the grammar developers would just state a name against it.
- Generate a method called as fetch<LookupName> in normal project (not sure the class name) whose return would be List<LookupValue>.
- Each LookupValue object would have name and description.
- In the UI recognize the element of type Lookup and show code completion based on the List
- Display the description in the editor but store the name in the model
- In validation validate that the value defined against the name is available in the list.

If the requirement makes sense and it is meaningful to be added in XText instead of individual grammars then I can complete the work (with someone's help from core team course Smile) and submit a patch.

Thanks.
Regards,
- Nimesh
Re: Adding EnumLiteralDeclaration programmatically [message #636157 is a reply to message #636132] Fri, 29 October 2010 13:43 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Nimesh,

I'm afraid we do not have any plans to implement such a feature or
provide something similar as a core concept of Xtext (at least not at
the moment). However, if you are willing to contribute this as an
add-on, please feel free to host it e.g. at EclipseCode and add a
comment to https://bugs.eclipse.org/bugs/show_bug.cgi?id=328477
Especially the combination of content assist and validation against an
external resource may be interesting for others.

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

Am 29.10.10 14:46, schrieb Nimesh:
> Hi,
>
> Thanks for the direction.
>
> There are few such Lookups that I need in my grammar. While thinking on
> providing a generic solution, I thought probably if something is
> provided as part of XText itself it would be useful for the entire
> community.
>
> So here's my thought process.
>
> - Define a new keyword called as lookup and the grammar developers would
> just state a name against it.
> - Generate a method called as fetch<LookupName> in normal project (not
> sure the class name) whose return would be List<LookupValue>.
> - Each LookupValue object would have name and description.
> - In the UI recognize the element of type Lookup and show code
> completion based on the List
> - Display the description in the editor but store the name in the model
> - In validation validate that the value defined against the name is
> available in the list.
>
> If the requirement makes sense and it is meaningful to be added in XText
> instead of individual grammars then I can complete the work (with
> someone's help from core team course :)) and submit a patch.
>
> Thanks.
> Regards,
> - Nimesh
Re: Adding EnumLiteralDeclaration programmatically [message #636646 is a reply to message #636157] Tue, 02 November 2010 10:29 Go to previous message
Nimesh Mising name is currently offline Nimesh Mising nameFriend
Messages: 23
Registered: March 2010
Junior Member
Hi,

Back in office after some days Smile .

I defined the grammar for ELookup (on similar lines of enum but without the alternatives/EnumLiterals) as
LookupRule:
	'ELookup' name=ID ';'
;

I tried to use grammar mixins but then I realized that my actual grammar (e.g. Entity Grammar) needs to act like a model to this common grammar. Something on similar lines as Xtext.xtext is the grammar for my grammar (model to Xtext.xtext) Entity.xtext.

I am not sure how to go about enhancing XText grammar so that any new grammar (models to XText grammar) definition can get hooked on to this enhanced grammar (which has LookupRule).

Basically I would want to define the above one and also ability to modify AbstractRule from XText.xtext to include LookupRule i.e. the modified entry would look like
AbstractRule : ParserRule | TerminalRule | EnumRule | LookupRule;


Thanks.
Regards,
- Nimesh
Previous Topic:Keywords with whitespaces and same prefix
Next Topic:How to reduce the structure of this....
Goto Forum:
  


Current Time: Tue Apr 23 15:46:06 GMT 2024

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

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

Back to the top