Thursday, 6 August 2009

Xtext Spring Demo - Part 1

Create a new language to define Spring Beans



Let's start by downloading and installing Xtext.

Xtext is a set of plugins for Eclipse and can be obtained here in a variety of formats.

I recommend downloading the full distribution, because it includes links to the update site, and the integrated ANTLR parser bindings. This can be found here

Once installed - start the Xtext Eclipse installation in a new workspace - and we can start creating our new parser.

For this demonstration, I chose to create an alternative to the Spring XML bean definition language.

This isn't because I think the end result is particularly useful - but it does offer a great test case for a simple language and a variety of integration scenarios.

The demonstration stops some way short of being a fully-fledged replacement for Spring XML - it lacks support for most of Spring's features.

It does hint at how to go this far, though - it would be interesting to see how little code is required to replace Spring's XML bean definition loader :)

Create a new Xtext project



Create a new Xtext project by selecting the File menu, then New->Project... and choose "Xtext Project":



Give the project a name - use these examples:

Main project name: org.xtext.example.dsl.spring
Language name: org.xtext.example.dsl.spring.Spring
DSL-File extension: spring
Create generator project: yes




A set of projects are created - find the Spring.xtext file as shown and open it:



As you see - a new Xtext project starts with an example language - an entity description language.
(The Xtext language description language is actually very closely related to EBNF.)

The example is no use to us now, so let's replace that text with our Spring Bean definition language definition.

Leave the grammar and generate statements as they are, but replace the rest with:

SpringModel:
(beans+=Bean)*;

Bean:
'bean' name=ID type=STRING
(properties+=Property)* ';';

Property:
name=ID '=' (ref=[Bean]|value=STRING);




Now - we generate the Java implementation code for our language - run the GenerateSpring.mwe file - right-click on it and Run As->MWE Workflow:


The generator should run and you should see some successful looking output in the console:


Great! Let's test our new language :):

We don't need to write any Java to start with - we can test our parser with the free IDE editor generated in the last step.

Normally one would export the generated plugins and restart a copy of eclipse with these plugins loaded, but to quickly test our language - we can just launch a test instance of eclipse to do that:


Import the "generator" project (org.xtext.example.dsl.spring.generator if you used the example names) into the test workspace, and open src/model/MyModel.spring.

As before - this file is pre-populated with an example model (a valid example of the original Entity language we blew away, but invalid for our new language).

Therefore - the editor quite correctly highlights an error:



So - paste this valid Spring bean model into the file - replacing all the current text:

/*
* This is an example model
*/
bean myObject "org.xtext.example.dsl.spring.generator.test.MyType"
intAttribute = "5"
stringAttribute = "xtext is easy..."
otherAttribute = otherObject;

bean otherObject "org.xtext.example.dsl.spring.generator.test.OtherType"
someOtherAttribute = "...when you know how";



You should see the syntax highlighting in effect immediately, and notice that Java-like comments are supported:



Content assist proposals are given on Ctrl-Space - for syntax and for references and property values.
These can be customised.
Later we will see how to add Java class names proposals to the Bean "type" property...



Hit Ctrl+Shift+F.

Okay - sorry! It puts all the text on to one unreadable line.

That part isn't free - to get pretty printing we have to do a little Java coding.

We'll get to that later so hit Ctrl-Z for now and we'll write a test harness to demonstrate how you'd integrate the generated parser into your code.

Previous Next

0 comments:

Post a Comment