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..

 

Regards

m sch

4 comments

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);
  }
  true;
}

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.

1

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

0

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

0

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

if(bla1)

if(ChuckNorris)

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

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

0

Please sign in to leave a comment.