if else construct in MPS Jetbrains

Hello forum!

In my DSL I'd like to introduce an if-else construct


if (<condition>) {


else { }


Now if and else are separate concepts. I've tried a way that else only can be applied in my DSL, if a if concept is his pre-sibling. I used the constraint type can be child, but node is always null there (sounds logic). But there must be another way that I couldn't find at the moment.

Any help is appreciated..



m sch

Comment actions Permalink

With constraints you do not have the information necessary to evaluate the position within the parent collection, to which the "else" is being added before the "else" node is actually added to the model. Additionally constraints cannot prevent the user from deleting "if" or moving "else", anyway.

You could consider using this:

(childConcept, node, link, parentNode, operationContext)->boolean {
  if (node != null) {
    return node.prev-sibling.isInstanceOf(MyIf);

This way you at least complain about orphaned "else" nodes.


Alternatively, consider  using a checking type-system rule for this type of restriction. They allow you to also customize the error message.

Comment actions Permalink

I hadn't thought of a type-system rule. Thanks Vaclav!

Comment actions Permalink

Just wondering, why don't you follow "IfStatementConcept" with "elseIfs ElseIfConcept[0..n]" children way?

Comment actions Permalink

Thanks for your feedback Vladimir!

Yeah, I also thought of that one.

One reason was that in my DSL it's only possibly to have some predefined conditions. For example



So they don't really follow the rules of algebra.

For the sake of simplicity I just build my one quickly.


Please sign in to leave a comment.