Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Grammar
Grammar [message #1111017] Tue, 17 September 2013 12:52 Go to next message
Eclipse UserFriend
I'm pretty sure this is a commonly known problem but I can't find the solution.

foo-1 should become to name: foo; version: 1
foo-bar-2.0 should become to name: foo-bar; version: 2.0
foo-2-bar-3.0.1 should become to name: foo-2-bar; version: 3.0.1

And the grammar as follows. As aspected the parser doesn't recognize the (last) dash as the starting point for VERSION_ID terminal b/c the ID terminal consumes the dash.

Model:
    identifier=Identifier;

Identifier:
    name=ID ( '-' version=VERSION_ID);

terminal VERSION_ID:
    INT ('.' INT)*;

terminal ID:
    '^'? ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '0'..'9' | '-')*;

It doesn't work even with a slightly modified VERSION_ID terminal.

Model:
    identifier=Identifier;

Identifier:
    name=ID version=VERSION_ID;

terminal ID:
    '^'? ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '0'..'9' | '-')*;

terminal VERSION_ID:
    '-' INT ('.' INT)*;


Any pointers?

Regards, Lars M.
Re: Grammar [message #1111059 is a reply to message #1111017] Tue, 17 September 2013 14:16 Go to previous messageGo to next message
Eclipse UserFriend
I don't see a simple, non-invasive way for achieving that in the grammar. As a first attempt, I would use the following construct.
Identifier: nameAndVersion=NameAndVersion;
NameAndVersion: ...; //appropriate datatype rule

Adapt the name provider to extract the actual name (without version) and calculate the version from nameAndVersion when necessary. The latter could be done as an extension method or you use an external metamodel with a corresponding getter,... the list of possibilities not being complete.

Re: Grammar [message #1111453 is a reply to message #1111059] Wed, 18 September 2013 04:31 Go to previous messageGo to next message
Eclipse UserFriend
Hi Alex,

thx for your answer.

Quote:
calculate the version ... when necessary ... could be done as an extension method

Xtends extension methods will only be used while generating source code, right? I do not generate source code or any other kind of text artifacts. So I can't use extensions methods.

My next idea ist to use a customized ValueConverter bound to the data type rule "NameAndVersion" to split the matched string into the two parts "name" and "version" (key-value pairs) and vice versa. But ecore::EMap isn't serializable und thus not usable as the return type of a data type rule. (error is: The attribute 'Identifier.nav' is not transient so it must have a data type that is serializable) It seems likely that I have to define a new meta model to be used as the return type of that data type rule.

Identifier:
    nav=NameAndVersion;
NameAndVersion returns ecore::EMap:
    NAV_IDENTIFIER;
terminal NAV_IDENTIFIER :
    '^'? ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '0'..'9' | '-')*;
Re: Grammar [message #1111475 is a reply to message #1111453] Wed, 18 September 2013 05:06 Go to previous message
Eclipse UserFriend
Extension methods will be used whenever you write Xtend code (instead of Java). If you want to stick to Java, simply use a static method for extracting the version. This will be the simplest solution. Depending on how often you need to access the version of the object, an imported metamodel with the corresponding getter will be better.
Previous Topic:GMF/xtext diagram/model creation problem
Next Topic:ValueConverter class is not getting called during runtime
Goto Forum:
  


Current Time: Sat Jul 12 02:06:06 EDT 2025

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

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

Back to the top