Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Dynamic type of variable in grammar
Dynamic type of variable in grammar [message #1441152] Thu, 09 October 2014 10:25 Go to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
Hey,

I'm trying to implement a grammar for this:

type java.lang.String default "Something";
type java.lang.Long default 0;
type java.lang.Boolean default true;


The JvmType reference is working fine, but I'm a little unsure on how to proceed with the default values. Content assist should propose to me the values allowed for the current type only of course, and validation must make sure that no unsupported types are written there by hand...

What I have so far (incomplete I know, just to test):

TypeRefDecl:
	'type' type=JvmTypeRef ('default' (defInt=INT|defStr=STRING|defId=ID))? ';'
;


That basically allows me to write there a lot of different default values. Still this grammar "feels" somehow wrong. Also, how could I distinguish which one to read in my code now...?

Am I on the right track, or is there some method of doing this that I'm not aware of?

Cheers,
Markus
Re: Dynamic type of variable in grammar [message #1441409 is a reply to message #1441152] Thu, 09 October 2014 17:46 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Usually you would model this with real expressions and a type system to
make sure that the default are compliant with the declared type. I'm
afraid that would go beyond what could be explained here. You may want
to explore Xbase for that matter, though.

Best,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Dynamic type of variable in grammar [message #1441764 is a reply to message #1441409] Fri, 10 October 2014 06:34 Go to previous messageGo to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
Hey,

So if i had something like that:

'type' type=JvmTypeRef ('default' defaultValue=[XBase::XExpression])? ';'


How would I go for checking whether types are compatible. Is there existing functionality?

Cheers,
Markus
Re: Dynamic type of variable in grammar [message #1441769 is a reply to message #1441764] Fri, 10 October 2014 06:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you would rather have something like

'type' type=JvmTypeRef ('default' defaultValue=XExpression)? ';'


and use the jvm model inferrer to map that to a java concept (e.g. a field)
thus you would get the type checking for free


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Dynamic type of variable in grammar [message #1441770 is a reply to message #1441769] Fri, 10 October 2014 06:42 Go to previous messageGo to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
I'm not generating code (currently), but leveraging the EMF model at runtime and just want the validation. Is this still possible?

EDIT: If I do as you suggested I get: XExpression cannot be resolved to a rule also qualified with XBase::XExpression does not work.
EDIT2: ups sorry - my fault Wink

[Updated on: Fri, 10 October 2014 06:45]

Report message to a moderator

Re: Dynamic type of variable in grammar [message #1441771 is a reply to message #1441770] Fri, 10 October 2014 06:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you have to have a look at the xbase grammar which rule exaclty to pick

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Dynamic type of variable in grammar [message #1441840 is a reply to message #1441770] Fri, 10 October 2014 08:58 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Yes, using Xbase does not necessarily mean that you have to generate
code. In your scenario it may be overkill, though. As I already
mentioned, a personal in-depth intro to type systems, expressions etc
would exceed what could be done in the forum.

Best,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Dynamic type of variable in grammar [message #1441851 is a reply to message #1441840] Fri, 10 October 2014 09:14 Go to previous message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
Hey,

Thanks. I have tried implementing XBase for that, but found that (you're right) that's a little overkill. For that handfull of java.lang.* types i'll just really manually verify literal types Smile For that I partially copied the concept around XLiteral and use that now.

Cheers,
Markus
Previous Topic:Preventing code generation from marking files as deleted in VCS
Next Topic:[2.7.0] Cannot run MWE2 workflow to generate my language anymore
Goto Forum:
  


Current Time: Wed Apr 24 23:23:23 GMT 2024

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

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

Back to the top