A very simple case study

This is a case study derived from my use of Xtext in my work as a software engineer, a consultant.

I will break it in several posts that might resamble a tutorial but it is not one because I still don’t feel myself so competent to write a full fledge tutorial: for each path that I will take there will be probably other cleaver ways, or, worse, errors that I won’t see.
So, please, take everything with a grain of salt.
(If you want a well-done tutorial, look here and here .)

Also, I cannot write about the language that I’m developing, because of non-disclosure agreements. So the language on which I’ll write about is specifically created for these posts, designed for the sole purpose to reflect problems that I have encountered. Someone might call it a head fake.

Shall we start?

The language I propose is used to describe houses. A house is a structure that can contain other structures, each one with some furnitures. A furniture can be shared from one sub-structure to another.
That’s all, simple isn’t it? šŸ™‚

So after downloaded all the necessary things, and created the projects (with everything at their default values), write down the grammar:


//Use the default terminals.
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

//Generate the model.
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

//A house will be considered as a particular kind of structure.
   //First, we must declare what type of structure it is.
   //A structure can be composed of various rooms.
   //A structure has various furnitures.
   //A structure can have furnitures of other rooms.

   'TYPE' name=STRING
//A room has a name and is a structure of a particular type.
   'ROOM' name=STRING 'OF' 'TYPE' type=[TypeDeclaration|STRING]

//We let the furnitures to be shared among rooms/structures.
   'SHARE' name=[FurnitureDeclaration|STRING] (list=List)?
//This is used to define where the shared furniture come from.
//Share level is limited to two.
//{} <- [II] <- [I]
   ('<-' second=[Room|STRING])? '<-' first=[Room|STRING]

Generate everything, then launch the plug-ins.
Here are some source files:


   TYPE "Home"

   ROOM "IfIAmHunger" OF TYPE "Kitchen"
   ROOM "TheSleepingRoom" OF TYPE "Bedroom"

   FURNITURE "Chair"
   FURNITURE "Table"

   SHARE "Fridge" <- "IfIAmHunger"


   TYPE "Bedroom"

   FURNITURE "Bookcase"


   TYPE "Kitchen"
   FURNITURE "Fridge"
   FURNITURE "Hoven"

As you can see Xtext provide you with many things out of the box: syntax highlighting, code folding, an outline, ecc.. Each one is configurable according to your needs.

That’s it for this post!
Next we’ll start to customize something!


About Luong

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s