Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Multiple generators
Multiple generators [message #1787296] Wed, 23 May 2018 09:33 Go to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
Hi all,

I've created my first xtext language here: https://github.com/karypid/sbelang

It's a tiny language to describe SBE encoding and generate an XML file that conforms to SBE's schema. There is a tool for generating code from that XML file here: https://github.com/real-logic/simple-binary-encoding

My next step would be to generate Java code *in addition to* the XML file (similar to what the tool I linked above does). From the documentation, I used AbstractGenerator as my target is not Java: https://github.com/karypid/sbelang/blob/master/org.sbelang.dsl.parent/org.sbelang.dsl/src/org/sbelang/dsl/generator/SbeLangDslGenerator.xtend

Now, in order to generate Java code, I want to use the model inferrer and plug into the existing facilities as per: https://www.eclipse.org/Xtext/documentation/104_jvmdomainmodel.html

My problem is that as far as I understand, xtext decides what to do based on the statement in your grammar. Mine at https://github.com/karypid/sbelang/blob/master/org.sbelang.dsl.parent/org.sbelang.dsl/src/org/sbelang/dsl/SbeLangDsl.xtext currently has:

grammar org.sbelang.dsl.SbeLangDsl with org.eclipse.xtext.common.Terminals


Apparently to use the JVM model inferrer I need to switch to "org.eclipse.xtext.xbase.Xbase" but is it REQUIRED to only have one of the two? Ideally I want to use the same grammar/language and have 2 generators (and even be able to control programmatically whether to invoke only one of them or both). How would I go about doing that? I was hoping someone can point to some existing OSS project with that structure that I could peek into for "best practice" ?
Re: Multiple generators [message #1787298 is a reply to message #1787296] Wed, 23 May 2018 09:42 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Producing Java code with the JvmModelInferrer is only one option, and yes, it would require to use Xbase. Not sure if that is what you want. You could also write a plain code generator implementing IGenerator2.

To use multiple templates you typically write one template that injects the others and dispatch to them in their doGenerate() method. That root template has to be bound to the IGenerator2 interface in the runtime Guice module (MyDslModule).
Re: Multiple generators [message #1787320 is a reply to message #1787298] Wed, 23 May 2018 16:16 Go to previous messageGo to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
Thank you for your pointers Karsten.

For now, I'm prototyping with two IGenerator2 implementations. My base generator looks like this:

class SbeLangDslGenerator extends AbstractGenerator {
    val xmlGenerator = new SbeLangDslXmlGenerator;
    val javaGenerator = new SbeLangDslJavaGenerator

    override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
        xmlGenerator.doGenerate(resource, fsa, context)
        javaGenerator.doGenerate(resource, fsa, context)
    }
}


The SbeLangDslXmlGenerator is what SbeLangDslGenerator used to be and I am producing multiple Java files from the second. I hope I understood you correctly, but even so it seems to work.

Re: Multiple generators [message #1787331 is a reply to message #1787320] Wed, 23 May 2018 19:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
are you sure your generator inherits from JvmModelGenerator?
this one is used for xbase based languages and uses a lot of injections so you should inject it to your SbeLangDslGenerator instead of using new


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Multiple generators [message #1787340 is a reply to message #1787331] Wed, 23 May 2018 20:20 Go to previous messageGo to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
For the time being I am going with the non-JVM based via pure templates, so the grammar uses org.eclipse.xtext.common.Terminals. I am not sure what the implications of this are, but I have made progress creating Java code like this.

I feel like I understand too little to make good decisions, so I plan to do a second re-implementation using the JVM model inferrer.
Re: Multiple generators [message #1787355 is a reply to message #1787340] Thu, 24 May 2018 04:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Depending on your grammar (e.g. what kind of expressions you have) and your target you won't need an jvmmodelinferrer

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Multiple generators [message #1787524 is a reply to message #1787355] Mon, 28 May 2018 08:23 Go to previous message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

You should use @Inject on the fields instead of creating them with new. You will gain more flexibility when you are consequently using dependency injection.
Previous Topic:Accessing the `IBuildContext` from the `IShouldGenerate`
Next Topic:Language A accessing B language during code generation
Goto Forum:
  


Current Time: Fri Mar 29 00:33:45 GMT 2024

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

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

Back to the top