Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend with external templates(Configurable multi language code generator)
Xtend with external templates [message #713483] Mon, 08 August 2011 14:25 Go to next message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
Hi again.

It seems I finally finished my DSL and now I want to create a compiler/code generator for my DSL. Xtend is an awesome language for writing such code generators but I'm not sure if they meet my requirements.

My compiler should be a standalone package which I can fire via command-line or ANT task (classpath, additional configurations etc) and the compiler should also support multiple output format-plugins for different target languages.

The main problem using Xtend is: I want the templates to be flexible and it should be possible to specify a template source jar/directory where the templates get loaded from. Xtend compiles it's template files into java classes, therefore they can't get modified without recompiling the whole compiler package. It's quite annoying if you only want to modify the template and need to recompile the whole compiler or compiler-plugin.

This is the workflow I want to achive:

1. The compiler gets invoked from commandline, ant or eclipse.
2. The compiler reads the configuration
3. The compiler loads the all models of the buildpath/classpath.
4. The compiler initializes and invokes the desired target-language plugin (i.E. Java)
5. The target-language plugin wraps the original model into an output optimized-model proxy (additional iterators, additional getters etc.)
6. The target-language plugin loads the desired template files from the specified template directory/jar
7. The target-language plugin uses the loaded templates to generate the code into the desired output directory.

How would you implement such an architecture? Is there a way I can build such a dynamic-template-loading / target-language-plugin-architecture using Xtend? Should I use such a workflow or should I take another approach?

I thought of using Freemaker as an alternative to Xtend.

Greetings
Daniel



[Updated on: Mon, 08 August 2011 14:25]

Report message to a moderator

Re: Xtend with external templates [message #713526 is a reply to message #713483] Mon, 08 August 2011 15:11 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 747
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Hi Daniel,

by the usage of different Guice modules you could bind different IGenerator implementations and compiler configurations. Since your workflow does differ from the standard implementation and there is not much experience in customizing Xtend so far (what would you expect 1 month after release?), I cannot tell much on it. I'm working on a headless build with Maven Tycho at the moment, and currently the Xtend compilation is a problem, at least with Maven. I'm quite sure that such workflows are realizable, since almost everything is doable with Xtext.

I would not consider Freemarker as a real alternative. You should consider Xpand or Acceleo, which are both good code generation frameworks within the Eclipse ecosystem.

Regards,
~Karsten


Need professional support for Xtext, EMF, Eclipse IDE?
Go to: http://devhub.karakun.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Re: Xtend with external templates [message #713841 is a reply to message #713526] Tue, 09 August 2011 07:12 Go to previous messageGo to next message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
Hi Karsten.

I already thought of using Guice for the 'plugin' architecture. I'm quite new into code-injection but I really like it so far.

I think I will try to rely on Xpand since it's optimized for generating Xtext to code.

[Edit]
I think I have designed an architecture which allows me to build a quite flexible system. I have 2 types of plugins which I need to specify for my compiler. The compiler loads a Language Speicifc Plugin (i.e. Java) which requires a template plugin to specifiy (i.e. specific java templates).

The compiler parses global configurations, then the language plugin gets triggered to read it's own settings. The compiler loads the model from source-code and triggers the language plugin to start working.
The language plugin loads the template plugin, extends the language model and starts the language specific workflow using the templates provided.

It seems my system is implementable using Xtend, even if the template-plugin system is a little bit heavy for a simple template replacement.

Cheers
Daniel

[Updated on: Tue, 09 August 2011 08:55]

Report message to a moderator

Re: Xtend with external templates [message #713884 is a reply to message #713841] Tue, 09 August 2011 09:54 Go to previous message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
An additional question to this topic:

I want to place my compiler in an own project. Is there an easy way to add all required dependencies for creating an Xtend code generator? (xtext, xtend, guice, log4j ...)

I tried to add them manually but it doesn't seem to work well. Have created a "mydsl.compiler.java" project, added all libraries and added this xtend file:

package at.mydsl.compiler.java

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
import at.mydsl.compiler.MyCompilerBase // implements IGenerator
import at.mydsl.smiDef.Model

class SmiDefJavaCompiler extends SmiDefCompilerBase {
	
	String javaPackage
	
	def String getJavaPackage() {
		return this.javaPackage
	}
	
	def void setJavaPackage(String value) {
		this.javaPackage = value
	}	
	
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {		
		for(model : resource.contents.filter(typeof(Model))) {			 
		}		
	}
}


But I get an error on resource.contents: "Couldn't resolve reference to JvmIdentifiableElement 'contents'."
The same file works within the main at.mydsl project.

[Updated on: Wed, 10 August 2011 07:45]

Report message to a moderator

Previous Topic:ConcurrentModificationException while validating
Next Topic:Theoretical Question
Goto Forum:
  


Current Time: Fri Apr 16 18:41:21 GMT 2021

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

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

Back to the top