mps generator - node macro: how to access to the parent node of the current node

Hi!

I'm a beginner in MPS. In my generator I have a loop node macro and within this macro I want to access on the parent node of the current node.

Is there a possibility to do it? Is it necessary to cast the parent node?

For instance:

Lets say I have a "Car" node and within this node I have four "Wheel" nodes. The node of my loop-node-macro is pointing to a "Wheel"-node  and I want to access to the "Car"-node. Is it possible?

I found the "parent" operation in the node macro but don't know what it's good for... .

Please post a whole small example of accessing to parent nodes within node macros.

Thanks a lot!

3 comments

I guess, node.parent : Car is what you are looking for, where ":" is a "node cast" operation. This is just a small example of using plain "smodel" language, which MPS offers for traversing the models - https://confluence.jetbrains.com/display/MPSD33/SModel+language.

Vaclav

 

1

Hi Alex!

Another option would be to pass the car as a parameter to the template that wants to use it (where currently node.parent is written) from the outside context, i.e. from where the template is called. This way it will be a little more discoverable that the template in question doesn't just translate a Wheel node into something but also depends on the context Car.

And maybe later you'll reuse the template from a context where Car is not a parent to the Wheel rather is in a more complicated relation. You'll be able to do that without changing node.parent (and potentially a number of them) in the template.

0

Please sign in to leave a comment.