MPS. Making a simple extension for the JAVA language
Introduction
The audience of Habr is quite heterogeneous, and it is difficult to write a text that is equally suitable for experienced DSL architects and, at the same time, accessible for inquiring minds of interested students. We assume a whole series of articles about MPS in order to consistently bring our reader from simple to more complex things.
Immediately it should be noted that MPS users have two completely different roles - “ language developer ” and “ language developer ”. For the “developer in the language”, MPS is just an IDE, very close in its behavior to the majority of IDEs for familiar programming languages. Moreover, for the “developer in the language” there is a plugin that allows you to program in the language created in MPS, directly from IntelliJ IDEA. Next we focus on the role of “language developer”.
This is the first article in which we will show how an existing language, in this case Java, can be extended with new constructions in MPS. First, we will talk a little about the basic principles of MPS and introduce a few special concepts. At the end of the article there will be a screencast in which we will write our own extension of the Java language. To understand further articles, we recommend repeating these steps yourself. We promise in the comments and direct messages to help all who have difficulties. ')
Before reading this article, we recommend that you read Konstantin Solomatov habrostaty , which describes what DSL is and who needs them. It also explains how the MPS approach differs from the previous approach to the development of DSL (based on lexers and parsers), and how it relates to the projection editor.
MPS Concepts
AST As you already understood from the previous article , MPS operates with the syntax tree of the program, or AST (Abstract Syntax Tree).
This tree consists of nodes ( node ), each of which has:
Child nodes
Properties (string, numeric, logical, etc.)
Links to other nodes
Nodes in MPS are combined into models . A model in MPS is an analog package in the Java language.
Concept Each AST node has a type that determines the composition of this node, its behavior when editing, generating, etc. This type in MPS is called a concept . In a rough approximation, the concept and node are in the same relationship as the class and object in the Java language.
The concept declaration in MPS describes what data will be stored in the nodes of this concept. The rest of the concept's behavior (editor, type constraints, generator) is described in the aspects of the concept of the same name - editor, typesystem, generator ...
A language is simply a set of concepts and their aspects.
Generation Analog compilation in MPS is called generation . As a result of generation from the model files on the disk are obtained.
Generation has 2 stages: Model-to-Model transformation . At this stage, the MPS converts the source AST according to the rules described in the generator aspect of the language. The result is another AST, in a lower level language. Model-to-Text transformation . At this stage, the MPS takes the model obtained in the first step and converts each of its nodes to text, and then writes this text to a file on disk.
With the basic concepts finished, let's now get acquainted with the MPS on the example of the following problem.
Formulation of the problem
In practice, quite often you have to write a conditional operator in the form:
if (condition) { statement; }
In our example, we are going to implement a special notation for the construction in MPS that will allow us to write the same code shorter:
? condition : statement;
The new construct will be an extension of the Java language, it can be used anywhere in the code where the use of a conditional operator is permissible. Of course, you can do without this extension, but in this article our goal will not be the invention of a new language, but only a demonstration of how to create language extensions in the MPS environment.
We put MPS on our computer
This is the easiest stage. You need to go to the official website of JetBrains and download the MPS distribution. The installation process is available to any habrowser (if not available, that user is clearly not from the habrowheader). In this article I will give an example for MPS 3.0 installed on MacOS X, but on other operating systems, everything looks the same.
Next, we offer our reader to see a small screencast of how to make a simple extension of the Java language in MPS:
Summarizing
In this example, we wrote about 10 lines of code to extend the language, but the description of the process was rather lengthy. Of course, it is rather difficult to start writing your own languages ​​or extensions from scratch, but the development of the project implies a simpler approach in the future. In this article, we got to know the MPS environment more closely and now everyone can add a little bit of syntactic sugar to their Java jug.