Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » passing additional info to generator
passing additional info to generator [message #1273852] Thu, 20 March 2014 16:36 Go to next message
klaas thoelen is currently offline klaas thoelenFriend
Messages: 2
Registered: March 2014
Junior Member
Hi,

I'm wondering what is the best way to pass additional info to an Xtext generator to influence what code exactly is generated?

What I'd like to do is generate code from my DSL, but depending on some variable (which is not part of my DSL file, but e.g. a checkbox in a gui somewhere) select what code to generate. This would allow me to specify externally to only generate getters but not the setters, or for which target language to generate code (given I have generators for multiple languages).

I read about the possibility to have multiple generators, and this might be part of the solution. But my question is then how can I make the selection based upon some variable external to my DSL.

Best regards,
Klaas
Re: passing additional info to generator [message #1273917 is a reply to message #1273852] Thu, 20 March 2014 18:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13313
Registered: July 2009
Senior Member
Hi what about writing a util and inject it into the generator,
In the ui you could bind a version of that util that accesses eclipse preferences for example


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: passing additional info to generator [message #1274256 is a reply to message #1273917] Fri, 21 March 2014 07:45 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 248
Registered: June 2013
Senior Member
perhaps a preference page would help you.

See http://xtextcasts.org/episodes/21-preference-page


OR (siehe Eclipse Magazin 3.14 Seite 31):
add in your DSL grammar some "generator_options":
Model:
('generator_options' options+=Option? (',' options+=Option)*)*
modules+=Module+;
Option: name=ID ('=' value=STRING)?;


And evaluate them in the generator:

def universalPreparation(Model sm) {
loadMyOptions(sm.options)
return ""
}

/* now the main thing */
override doGenerate(Resource resource, IFileSystemAccess fsa) {
var String scriptFileName = resource.getURI().lastSegment.toString();
scriptfileName = scriptFileName;
universalPreparation(resource.contents.head as Model)
...;
}


And the generator.xtend has the following utilities:

// *** advanced Option utilities
static Map<String, String> myOptions
= new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);

def loadMyOptions(EList<Option> options) {
myOptions.clear
for (Option option : options) {
myOptions.put(option.name, option.value?:"")
}
return ("")
}

def getMyOption(String key) {
return myOptions.get(key) ?: ""
}

def boolean isMyOption(String key) {
return myOptions.containsKey(key)
}

def boolean isMyOption(String key, String compareValue) {
compareValue.equals(getMyOption(key))
}

def boolean isMyOptionIgnoreCase(String key, String compareValue) {
compareValue.equalsIgnoreCase(getMyOption(key))
}

[Updated on: Fri, 21 March 2014 08:55]

Report message to a moderator

Re: passing additional info to generator [message #1275122 is a reply to message #1274256] Sat, 22 March 2014 14:47 Go to previous messageGo to next message
klaas thoelen is currently offline klaas thoelenFriend
Messages: 2
Registered: March 2014
Junior Member
Hi,

Thanks for the quick responses!! It took me some time to explore the options.

I opted for the first response of creating a util object and injecting it into the generator.
In fact, I use a singleton util which stores the generation settings for each generation action. It is also injected into a Handler I wrote to guide the generation. I set the generation settings there and they are nicely read out in doGenerate.

A preference page is a good idea as well, but for my purposes it is a bit too far hidden away. And for including generator_options in my grammar, that is actually what I used before and was trying to avoid now.

Best regards,
Klaas
Re: passing additional info to generator [message #1276161 is a reply to message #1273852] Mon, 24 March 2014 07:08 Go to previous message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
I would use dependency injection, where this info is provided by Eclipse
preferences when executed in Eclipse and by other means when executed
elsewhere.

Am 20/03/14 19:41, schrieb klaas thoelen:
> Hi,
>
> I'm wondering what is the best way to pass additional info to an Xtext
> generator to influence what code exactly is generated?
>
> What I'd like to do is generate code from my DSL, but depending on some
> variable (which is not part of my DSL file, but e.g. a checkbox in a gui
> somewhere) select what code to generate. This would allow me to specify
> externally to only generate getters but not the setters, or for which
> target language to generate code (given I have generators for multiple
> languages).
>
> I read about the possibility to have multiple generators, and this might
> be part of the solution. But my question is then how can I make the
> selection based upon some variable external to my DSL.
>
> Best regards,
> Klaas


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Previous Topic:Version compatibility
Next Topic:Exceptions at startup when running test workspace from IDE
Goto Forum:
  


Current Time: Sun Jan 19 18:42:00 GMT 2020

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

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

Back to the top