Expressions in baseLanguage behave surprisingly !

I am using the baseLanguage as the basis for my own langauge.  Last week I had to import expressions from an XML file into the baseLanguage.  I read the XML, and create the AST in the baseLanguage.
For the expression
    <int value=“1”>
        <int value=“2”>
        <int value=“3”>

From this I programmatically create the baseLanguage expression with the following AST:
        leftExpression: IntegerConstant 1
        rightExpression: MulExpression
 leftExpression: IntegerConstant 2
 rightExpression: IntegerConstant 3

When I do a “check model” on the above expression, MPS gives an error message saying “Bad priority of operations”. When I open the file I see the correct expression rendered (I use a fraction notation):

but then the editor is performing some automatic action that rewrites the AST and suddenly it looks like:

The baseLanguage expression now looks like:
        leftExpression: DivExpression
 leftExpression: IntegerConstant 1
 rightExpression: IntegerConstant 2
        rightExpression: IntegerConstant 3

This behavior is totally surprising. The AST I created is a correct representation of the XML, but when the AST is rendered in the editor, the editor starts rewriting the AST. If you look at the standard baseLanguage syntax the original AST would look like
1 / 2 * 3

And then I started to understand what wa shappening....

The problem with this automatic rewriting of the AST is that I need to insert Parenthesized expression in the AST wherever MPS would rewrite the AST. That is quite a lot of complex work and I need to take into account all priorities for this.

Up to yesterday I assumed that the baseLanguage concepts were a metamodel of the Java language, now I come to another conclusion: the baseLanguage concepts are a metamodel of the Java textual syntax. And that is something entirely different!

Anyone creating baseLanguage expressions programmatically should be aware of this.


Please sign in to leave a comment.