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:

MyDsl.xtext

//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.
Structure:
   //First, we must declare what type of structure it is.
   type=TypeDeclaration
   //A structure can be composed of various rooms.
   (rooms+=Room)*
   //A structure has various furnitures.
   (furns+=FurnitureDeclaration)*
   //A structure can have furnitures of other rooms.
   (shares+=Share)*
   ;

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

FurnitureDeclaration:
   'FURNITURE' name=STRING
   ;
 
//We let the furnitures to be shared among rooms/structures.
Share:
   '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]
List:
   ('<-' second=[Room|STRING])? '<-' first=[Room|STRING]
   ;

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

Home.mydsl

   TYPE "Home"

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

   FURNITURE "Chair"
   FURNITURE "Table"

   SHARE "Fridge" <- "IfIAmHunger"

Bedroom.mydsl

   TYPE "Bedroom"

   FURNITURE "Bed"
   FURNITURE "Bookcase"

Kitchen.mydsl

   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!

Advertisements

About Luong
http://www.linkedin.com/in/manhluong

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