How can I traverse the EMF object tree generated by Xtext?

I use Xtext to determine my DSL. Xtext generates a parser that allows me to navigate the EMF model of my input DSL. I would like to translate this EMF model into another tree. To make this translation, I need to go through a tree. But I could not find the visitor class for the EMF model created by Xtext. The closest I found is the Switch class, which visits a single node. I can go through the EMF model myself and call the Switch class on every node I visit. But, I wonder if there is visitor functionality in Xtext that implements model bypass.

+4
source share
3 answers

If you consider this thread , Switch ( used here for example) is pretty much the official visitor pattern for XText.
You also have a custom verification process :

The purpose of AbstractDeclarativeValidator is to let you write constraints in a declarative way - as the class name already says.
This is instead of writing exhaustive if-else constructs or extending the generated EMF switch, you just need to add the @Check annotation to any method, and it will be called automatically during validation.

This may not be exactly what you need.

+3
source

I think EcoreUtil.getAllProperContents (Resource, false) may be what you are looking for. You will get a TreeIterator that traverses the contents of the resource. Pass the result of iter.next () to Switch and execute the conversion logic depending on the type of element.

+3
source

Hmm, is it not enough to use the EMF API? If you want to transform a model using some of the langauges transforms, for example Xtend, is it impossible?

0
source

Source: https://habr.com/ru/post/1308552/


All Articles