Thursday, December 17, 2009

Xtext - how to start

Sometimes is better to have screwdriver than swiss knife. At least people from the are of DSL think that. Well, I have decided to follow this path. I need a screwdriver. In our world one of the options is to use Xtext. Xtext is a framework for development of textual domain specific languages (DSLs).

What does it mean?

You can build a grammar description and with the use of the framework get a:
  1. EMF model related to this grammar.
  2. Fully functional text editor.
  3. Scaffold for the generation tool

What for?

I need to record some data in structured form. I need model. Text editor is more convincing.
But this is only my motivation. There is a sea of use cases.

How to start ?

You can download Eclispe Modeling tools distribution from Eclipse downloads site. Or use update site.
Then you can follow Xtext documentation. And this is the reason for this post. Although, you don't need to spend weeks on learning Xtext principles to build usable tools, still there are some things missing from the documentation.
You have created project, you have written your grammar. Now is a generation step. Is build successful ? Not really. If you look at the console view, you can find why.
You are generating without ANTLR. It is highly recommended to download and use the plug-in 'de.itemis.xtext.antlr' using the update site http://download.itemis.com/updates/milestones
Of course solution is to download the plug-in. After that you can follow the getting started tutorial.
Code is generated and now is time for building your XPand templates to generate Java code from your model. How to do that? There is no "generate sth.." button in your target environment.
The simplest way is to import your xxx.generator plug-in project from source to target. You are almost done. The only thing left is to add required dependencies to imported project. It is also helpful to add XPand nature to this project. After that you can follow tutorial, build XPand templates, and generate Java code.
Hope this short note will help somebody to start the adventure with the screwdriver even smoother ;)
Next time I will show you how to change editor default coloring.

1 comment:

Peter Friese said...

Check out my blog posts Getting started with Xtext, part 2 and Getting started with Xtext to learn how to write a DSL and how to start the code generator to transform your model into code using Xpand. If you want to learn more about Xpand, you might want to attend this EclipseCon 2010 session on Xpand (I hope to get it accepted). There will definitely be a tutorial on Xtext at EclipseCon 2010 - mark your calendar!