Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » is xTend to generate no-java code? code to download?
is xTend to generate no-java code? code to download? [message #869890] Fri, 04 May 2012 16:21 Go to next message
Aran A is currently offline Aran AFriend
Messages: 30
Registered: January 2011
Member
Hello,

I'm learning about Domain-Specific Languages (DSLs), and I would like to practise a little bit with a tool (I have enough with the theory Sad ). Just to see how to define a new language and how to generate code from it.

I see that xText could be a very good option. I have been reading the documentation, and seeing the tutorials, I have also practised a little bit on my own, and now I think that I have a clear idea about the part related to the definition of a new DSL. However, what I don't see very well is the part related to code generation.

As I understand, in theory, with a DSL we could generate any type of code (depending on our needs). As code, I understand: html, java, ant, xml, etc. What I see with Xtend (the language integrated in xText for code generation) is that the generated code is Java code. That is, the DSL designer writes Xtend code (quite similar to Java) and then, the generator included in xText generates the corresponding Java code. (1) Is my reasoning correct?

Now, as in the theory I understand that from a DSL any type of code can be generated, (2) could I generate no-java code using Xtend? I have watched a video explainig how to construct html code (in vimeo.com/39819318). So, I suppose that the answer is 'yes', but my third doubt is (3) using Xtend to generate no-java code, is it a good practise? in order to do that (generating html, ant, or whatever no-java code), would it be better to use another language or tool? any recommendation?

Finally, I have found very helpful the videos explaining different options of xText and Xtend. Specially that one related to HTML and one implementing Martin Fowler's DSL. My question is (4) would it be possible to download the code of those examples? It would be a beginning for me, just to get my fingers dirty and to practise on my own.

I would appreciate if someone would be able to answer my former questions ((1) to (4)).

Best regards,

Aran
Re: is xTend to generate no-java code? code to download? [message #869897 is a reply to message #869890] Fri, 04 May 2012 16:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13667
Registered: July 2009
Senior Member
Hi, in short: Xtend can be and is used to generate every text you
want: java code, c code, documentation, ....


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: is xTend to generate no-java code? code to download? [message #869903 is a reply to message #869897] Fri, 04 May 2012 17:40 Go to previous message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey,

I also just started with Xtext and it's pretty nice Smile

(1) You can basically create any possible output with Xtend. Here is how the process works:

def compile(Product product, Class clazz) '''
package «generatorExtensions.packageName(product)»;

«clazz.^public.publicRep» «clazz.abstractFinal» class «clazz.name» «clazz.extendsClause»{
    «FOR f : clazz.fields»
        «f.fieldRep»
    «ENDFOR»

    «FOR m : clazz.methods»
        «m.methodRep»
    «ENDFOR»
}
'''


This is peace of code written in Xtend will be translated to Java code. The Java code is going to have a lot of string concatenation, that is kind of exhausting to write with Java (usually you create a StringBuilder and append strings). Check out this video vimeo.com/26046484. It shows Xtend code as well as the translation into Java. Actually, you don't need to Xtend to generate the output from the abstract syntax tree(AST). You could traverse the AST and produce your desired output with plain Java code. This process string concatenation process is tedious with Java, so Xtend was created. That is what I guess Smile

(2) Yes, you can ... want ever you want. It is basically just a text file. Since Eclipse is performed with Java, usually the Virtual Machine is used to execute your DSL. If you like for instance, you can generate C# files. Then you just need to make sure that the user has .Net framework installed, where you would execute the files. The main point is that you don't want to reinvent the wheel. So if your DSL needs somehow to be executed, take a programming language that is already present. This implies that the files need to have the right syntax, file extension and so on. If you could not used an existed language, you would need to create binary code (basically zeros and ones) Smile And your operation system would execute that code.

(3) You can use plain Java and the AST to generate your desired output. I also just spend some time with Xtend and it is similar to Java, but still different in detail. So it needs time to get into, but it is still better the do all the output generation with Java. Image all the StringBuilder stuff Wink

(4) Can you share the link? Just omit http or www

Kon
Previous Topic:ouverture de modele xmi !
Next Topic:Split Grammar into two files
Goto Forum:
  


Current Time: Thu Dec 03 08:44:34 GMT 2020

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

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

Back to the top