What can I do with DSL languages generated inside JetBrains MPS?

1k Views Asked by At

I've just started a couple of hours ago reading about DSL modeling.

But right now, I'm tied to using the JetBrains MPS IDE or it's plugin for JetBrains Intellij Idea and I'd like to know how can I export those DSL models to something available to use for e.g. console applications or whatever (in case it's possible or it makes sense).

2

There are 2 best solutions below

0
On

You can define a generator which transforms a sentence (file, AST) of your language into another MPS language. The target language must exist in MPS first.

Alternatively, you could generate text with the TextGen aspect, but that is more suitable to just print the textual representation of your language. If you would like something more sophisticated (like generating text code of another language), you can use plaintextgen language from MPS-extensions or mbeddr.platform.

If you want to input (import) a textual program into MPS , you can code a paste handler where you could put your parser, or you can change the format in which the AST is stored (from XML to maybe directly your language, but this would again require a parser to read) with custom persistence.

I am currently working on a solution which enables to import an MPS language from a YAJCo model (model-based parser generator, where the input is not a grammar, but Java classes representing the semantic model). Then you can import a sentence (file) which creates and populates a model (AST). From the program in MPS you can generate Java source code which fills the original Java classes. So if you want a textual MPS language and use the IDE but then export the AST into Java objects you can use, maybe YtM is for you.

0
On

You can do several things already in MPS without exporting the models:

  • Analyze the models to check for errors, business rule violations or inconsistencies.
  • Interpret the models then display the result of the interpretation in MPS directly. Useful if you implement a specification and an example/test of that specification, then you can run tests in MPS and show the results as green/red highlight, for example.
  • Define a generator to translate the model into text (executable code or input for a tool such as Liquibase to create database schemas for example).

If you're looking to export your data from MPS for use in a different application there are two approaches I would recommend:

  1. The simplest way: NodeSerializer from MPS-extensions. I have more details on how to use it in a blog post. This lets you quickly export your data in a rather nice XML structure.
  2. The most flexible approach: writing a custom exporter by using the MPS Open API to recursively traverse a node tree. You can output any format you want (XML, JSON, YAML, etc.) and customize the output as you like.

Here are two more approaches that you could be considering but that I would NOT recommend:

  1. Accessing the model (*.mps) files directly. While they are already in XML format, their structure is adapted to MPS' needs. It is normalized, meaning that a given piece of information is generally only stored once, and it also encodes node IDs in a particular way to save space. The format is also undocumented and could change in the future (although it hasn't changed for the past several years).

  2. Using the MPS generator to convert your DSL to MPS' built-in XML language, jetbrains.mps.core.xml. I don’t recommend using the MPS generator because the generator’s sweet spot is translating between two different MPS languages, e.g. from your custom DSL to Java. If you try writing generator rules to convert anything to XML you would hit a few problems that are possible to overcome but totally unnecessary.