DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more

Getting Started with Xtext

Peter Friese user avatar by
Peter Friese
·
Jun. 30, 09 · Interview
Like (0)
Save
Tweet
Share
29.09K Views

Join the DZone community and get the full member experience.

Join For Free

xtext has been released as a part of the eclipse galileo release train on june 24th, 2009. xtext is a framework for building dlss ( d omain s pecific l anguages). in fact, it can be seen as a dsl for defining dsls.

in this article, we will develop a small dsl for defining entities.

download and install

hop over to http://xtext.itemis.com and download an xtext distribution matching your platform. we've got all major platforms: windows, mac osx carbon, mac osx cocoa 64, mac osx cocoa 32, linux gtk 64, linux gtk 32.

to install, unzip the distribution file to a directory of your liking.

windows users, please make sure you unzip to a directory near to your filesystem root! eclipse contains files and folders with long names that might be hard to handle for windows.

create a new project

after launching eclipse and creating a new workspace (or using an existing one), create a new xtext project by selecting file -> new... project... -> xtext project . in this article, we're creating a dsl for describing entities, so let's go with the following settings:

  • main project name: org.xtext.example.entity
  • language name: org.xtext.example.entity
  • dsl-file extension: entity
  • create generator project: yes

click finish to let xtext create the three projects that make up your dsl:

  • org.xtext.example.entity - this project contains the dsl itself, including all back end infrastructure like the parser and the meta model.
  • org.xtext.example.entity.ui - as the name implies, this project contains all ui centric code of the dsl: the editor, the outline, content assist and so forth
  • org.xtext.example.entity.generator - this project contains a code generator which will transform the dsl scripts (aka models) you write in your dsl into something useful. in our example, we will create some pojos and daos from our models.

upon finishing creating these three projects, xtext opens a file entity.xtext , which contains a sample grammar, which we will change in a second.

define the grammar for your dsl

next up, we need to define the grammar for our dsl. to make things easier for us, let's first write down a sample model. please open org.xtext.example.entity.generator/src/model/mymodel.entity and key in the following text:

typedef string
typedef integer
typedef date mapsto java.util.date

entity person {
string name
string surname
date birthday
address home
address work
}

entity boss extends person {
person* employees
}

entity address {
string street
string number
string city
string zip
}

most of this model should be pretty obvious, but there are two things worth noting:

  • we define our own list of data types to gain a certain degree of flexibilty, i.e. to map them to different types in the host language, as can be seen for the date data type, which gets mapped to java.util.date (we might also decide to map it to java.sql.date
  • the star (*) denotes multiplicity. we might also have chosen square brackets ( person[] employees ) or something completely different - it's largely a matter of taste.

let's derive the grammar for this model. open org.xtext.example.entity/src/org/xtext/example/entity.xtext , erase its entire contents and enter the following:

grammar org.xtext.example.entity with org.eclipse.xtext.common.terminals
generate entity "http://www.xtext.org/example/entity"

the first line indicates we want the new grammar to be derived from the (built-in) grammar terminals , which defines some basic terminal rules (like string, id and so forth). if you're interested, ctrl+click on the language name to navigate to its definition.

the second line defines the name and namespace uri for our own grammar.

let's now define that our dsl supports types. add the following lines:

model:
(types+=type)*;

type:
typedef | entity;

this tells xtext that our model contains any number (i.e. 0..n , as declared by the * ) of type s. what exactly a type is needs to be specified. apparently, a type can be either a typedef or an entity :

typedef:
"typedef" name=id ("mapsto" mappedtype=javaid)?;

a typedef starts with the keyword typedef , followed by an id making up its name. following the name, we can optionally (hence the question mark at the end) add a mapsto clause. the fragment mappedtype=javaid specifies that the typedef will later have an attribute named mappedtype of type javaid. as javaid is not yet defined, we need to do so:

javaid:
name=id("." id)*;

so, a javaid is a sequence of id s and dots, making up a qualified java type name, such as java.util.date .

next, let's define how to model entities:

entity:
"entity" name=id ("extends" superentity=[entity])?
"{"
(attributes+=attribute)*
"}";

as you might have guessed, entity s start with the keyword entity , followed by an id as their name. they may optionally extends another entity. surrounding a rule call with square brackets means "this is a cross reference", i.e. a reference to an already existing element.

entity s do have attribute s (any number, to be precise), thus we have to define how attribute s look like:

attribute:
type=[type] (many?="*")? name=id;

by now, you should be able to read this rule: an attribute has a type which is a cross reference to a type (which is either a typedef or an entity ), it has an optional multiplicity indicator (the star) and - of course - if has a name.

your grammar should look like this by now:

grammar org.xtext.example.entity with org.eclipse.xtext.common.terminals

generate entity "http://www.xtext.org/example/entity"

model:
(types+=type)*;

type:
typedef | entity;

typedef:
"typedef" name=id ("mapsto" mappedtype=javaid)?;

javaid:
name=id("." id)*;

entity:
"entity" name=id ("extends" superentity=[entity])?
"{"
(attributes+=attribute)*
"}";

attribute:
type=[type] (many?="*")? name=id;

compiling the dsl

now it is time to see the fruits of our labor. but first, we need to compile our grammar. xtext will create:

  • a parser
  • a serializer
  • an ecore meta model
  • a full blown eclipse editor

from this grammar. to make this happen, please select org.xtext.example.entity/src/org/xtext/example/generateentity.mwe and select run as -> mwe workflow from the context menu. xtext will now generate the entire infrastructure for your dsl and after a few seconds you should have a shiny new dsl including a great editor.

taking it for a spin

seeing is believing, so let's take the dsl editor for a test drive. select the dsl project org.xtext.example.entity and, from the context menu, select run as -> eclipse application . a second instance of eclipse will be started.

in this new instance, create a new, empty project ( file -> new -> project... -> general -> project . create a new file sample.entity in the new project.

you can now insert the model we designed above or enter a new model:

getting started with xtext

leveraging the model

now that we've got a fancy editor for our dsl, we want to transform the models we can create with this editor into something meaningful. this, however, will be the topic of the next installment.

more info

feel free to discuss this article in the comments section of my blog . should you have any technical questions regarding xtext, we've got an excellent newsgroup where the committers answer your questions. we also offer professional (commercial) support , i.e. customized trainings to get your team up to speed. just drop us a note , we're happy to discuss the details with you.

from http://www.peterfriese.de/

entity

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Building the Next-Generation Data Lakehouse: 10X Performance
  • A Beginner's Guide to Infrastructure as Code
  • 5 Software Developer Competencies: How To Recognize a Good Programmer
  • Top 11 Git Commands That Every Developer Should Know

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: