Xtext is great (textual DSLs)

In my last project I worked on a external textual DSL for defining products in telecommunications.

At this time, the products and their parts (deep tree of “features” then composed to products) are all maintained in Excel. This is great for the “inventor” and power-user, but it’s very hard to enforce structural correctness and business rules. Also working in a team and versioning is almost impossible.

A textual DSL was chosen over a custom application, because gives much more “power” to the user, because he is editing raw data in a very feasible way.

Sad that Microsoft dropped their investment in DSLs. But, so what. We’ve got Xtext. There is only one drawback. It’s Java + Eclipse, not C# + Visual Studio.

First I had plans (and started) to write about a particular scoping problem I had to solve in this DSL, but then I moved to a different project. So instead I’ll just post the introductory part where I listed the features of Xtext, just in case this is new to you 🙂

  • Xtext is a Language Workbench for external textual DSLs.
  • Xtext builds on Java+Eclipse.
  • Xtext is a rich grammar language.
  • Xtext builds on EMF (Eclipse Modeling Framework, a rich meta-meta model).
  • Xtext derives a EMF-based meta-model from your grammar definition (Semantic Model).
  • Xtext generates a Antlr-Parser from your grammar definition and automatically converts the AST into your semantic model.
  • Xtext generates an Eclipse-based text editor including syntax highlighting, cross-reference resolving, validation, formatting, outline-view, code-completion, rename refactoring, go to definition navigation + much more.
  • Xtext offers a rich API for validating you semantic model, but shows warnings and errors in the editor (squiggles).
  • Xtext offers a rich API for configuring your DSLs formatting (spaces, line breaks, eg).
  • Xtext lets you define custom scoping – including generic support for packages/namespaces + imports.
  • Xtext comes with a base-language (Xbase) for expressions (including compilation of expressions to Java).
  • Xtext comes with a template language (Xtend) for generating artifacts from your semantic model (model-to-model or model-to-text transformations).

Resources

6 thoughts on “Xtext is great (textual DSLs)

  1. If you’re interested in DSLs in .net I am looking for help on MetaSharp. I have read about xtext and it’s very interesting, a different approach but pretty darn cool.

    At the moment metasharp is still in progress but it will have a better-than-mg parser (IMO) soon. A much more complete code generation toolset.

  2. Ok I made a short term roadmap, which definitely includes a language workbench. I used to be tied to MGrammar which was part of the evolution of MetaSharp but have since replaced it with something new and of my own creation (and superior). There are a lot of interesting differences and advantages. It is the new centerpiece of the project which has been sort of rebooted with these new ideas.

    But yes, the idea is to have an external grammar dsl that can live inside of C#/VB/etc projects and generate transformers for either text or object graphs. I am definitely envisioning a workbench that lets you experiment with the grammar you are authoring dynamically (much like mgrammar did with the 3 panels). There will be a way to provide metadata in the grammar such that it can do things like syntax hilighting.

    Can you elaborate on what you mean by semantic richness, references and “scoping++”?

    • This means, that a language-workbench should offer more than a parser.

      In an Xtext-Grammar, you target a meta-model (which can also be inferred from the grammar). So when you parse, you get a real model graph including cross-references, not just a AST on steroids.

      Example:

      Parent: Id ‘has’ children+=[Child];
      Child: Id;

      Will parse:
      myparent child1 child2

      Into:
      myparent {children=[child1, child2]}

      This is marked by the [] around the referenced rule ‘Child’ in the rule for ‘Parent’.

      Now, if you do this:

      Parent: Id ‘has’ children+=[Child | QualifiedName]
      Child: Id;
      QualifiedName: (Id “.”)* Id;

      Then children can be referenced to using a complex name. This enables for namespaces, e.g.
      Then you can implement scoping (using statements++) for defining the rules for visibility and referencability.

  3. Hi Lars,
    great post! Do you know about any ongoing efforts on migrating Xtext to .Net? This would be really cool!

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