Change the outline, part 1

The missing child

If you look at the outline, with the source files provided, you’ll probably see that a child is missing, there is no node that represent the shared fridge:

To know why, we have to dig a little bit in the code that generates the nodes. According to the user guide, Xtext create for us a convenience class, the Transformer (in our case MyDSLTransformer), that extends AbstractDeclarativeSemanticModelTransformer. We have to touch this class when we want to change the structure of the outline, so the creation of the nodes must go through this class and its super classes. With a little bit of tracing, aided by a bunch of breakpoints, we would see that:

  • In AbstractDeclarativeSemanticModelTransformer there is the overrided method createOutlineNode.
  • The createOutlineNode version of DefaultSemanticModelTransformer is still overrided.
  • Finally in AbstractSemanticModelTransformer our creator method is abstract.
  • In this class createOutlineNode is called by transformSemanticNode (which in turn is called by the method that create the whole tree – transformSemanticNode).
  • transformSemanticNode create a node only if consumeSemanticNode returns true.
  • In the least override of consumeSemanticNode, in AbstractDeclarativeSemanticModelTransformer, we can read that it returns false if the labelProvider doesn’t return anything for that particular EObject.

So we have to go to the LabelProvider (here is the section of the user guide) (MyDslLabelProvider) and write the method that return the label for the missing nodes, in our case we have to provide the label for Share.
MyDslLabelProvider.java

//...
String text(Share element)
   {
   if(element.getName()!=null)
      return element.getName().getName();
   return "SHARE not resolved.";
   }
//...

Compile, run and then you should see the (no more) missing node:

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!

Use the right language: Xtext

Particular problems may need particular tools to be solved.

In computer science the right tool can be a programming (or a specification) language designed and tailored for a narrow family of problems, in other words, a Domain Specific Language (DSL). If the language that you need does exists, then you’re done (well, you have already something to start with). But what if the language doesn’t exist? Or the current languages don’t suit your taste?

You can create one, maybe with an IDE that supports it.

If that’s the case, then I strongly suggest you to look at Xtext.

Xtext is a framework that help you create an advanced IDE, based on Eclipse, of a language that you create, defining its grammar. You’ve got most of the things you might expect nowadays from an IDE: syntax highlighting, code completition, validation, ecc.. There are other ways but I think that Xtext is one of the most complete suite for this kind of task. Also, it is one of the most active, developed and maintained by a group of itemis AG (it’s open source). I’m using Xtext since 0.7.2 (back in those dark ages, when there were no “classSplitting = true” options…) and I think that they are doing a great job.

Xtext has a growing user base, even in industries.

If you’re interested, take a look at the homepage, user guide and the community forum, then start to build your own language!