The Eclipse Xtext framework [1] provides a straightforward way to craft external domain specific languages. Simply define the syntax of your language and you'll end up with an Eclipse-based language-aware IDE. In this tutorial, I will illustrate how to perform the first steps towards designing your own language. You'll become familiar with the syntax of the Xtext grammar definition language and learn how to define validation rules for your concepts. For this tutorial, we will use a well known abstraction that has been addressed by different frameworks in one or the other way: the entity model. The entity model is handled differently by various frameworks to overcome the mismatch between relational databases and object-oriented software systems. Ruby On Rails [2] derives a lot of information from the database structure, Groovy's Grails [3] framework uses an internal DSL to describe the structure of the entities, and Spring Roo [4] has a strong emphasize on scaffolding.
Because it is such a common use case, Xtext 2.0 already ships with an example that demonstrates how such a domain model language looks. In this tutorial series, I want to develop that language from scratch , explaining the grammar and APIs step by step.
The first iteration of the language is pretty simple and contains a few intuitive concepts. It allows you to define packages that contain entities and data types. In turn, an entity defines properties. A property refers to a type and may be multi- or single-valued. An example file looks like this:
import com.drDobbs.common.String
package com.drDobbs {
entity Author {
name : String
many articles: Article
}
entity Article {
author: Author
title: String
}
}
package com.drDobbs.common {
datatype String
}
Getting Started
To follow this tutorial, you must ensure that Xtext is properly installed in your Eclipse IDE. Once this is done, launch the wizard that guides you through the necessary steps to create a new Xtext project:
File -> New -> Project... -> Xtext -> Xtext project
Then choose the following settings (as shown in Figure 1) on the next wizard page before you hit Finish:
- Main project name: org.example.domainmodel
- Language name: org.example.domainmodel.Domainmodel
- DSL-File extension: dmodel
The "Hello World" grammar in the Domainmodel.xtext file illustrates a trivially simple language that allows you to write a number of "Hello World!" sentences.
grammar org.example.domainmodel.Domainmodel with org.eclipse.xtext.common.Terminals generate domainmodel "http://www.example.org/domainmodel/Domainmodel" Model: greetings+=Greeting*; Greeting: 'Hello' name=ID '!';
In addition to the preamble, the grammar file contains two parser rules that specify the structure of a valid sentence of the language. It's important to realize that an Xtext grammar is not only a description of the syntax of a DSL, it's also a concise notation that describes the mapping to a type-safe object model. The types and the expected structure of these objects is called "abstract syntax." It is either derived from the grammar definition itself, or defined externally and imported into the grammar definition. These imports allow for language modularization. During this tutorial lesson, we will stick with the defaults and the generated domainmodel, which is declared in the preamble:
generate domainmodel "http://www.example.org/domainmodel/Domainmodel"


