5

I'm using Xtext to define my DSL. Xtext generates a parser that lets me traverse the EMF model of my input DSL. I'd like to translate this EMF model into some other tree. To do this translation, I need to traverse the tree. But, I couldn't find a visitor class for the EMF model generated by Xtext. The closest thing that I've found is a Switch class that visits a single node. I can traverse the EMF model myself and invoke the Switch class on each node that I visit. But, I wonder if there exists a visitor functionality in Xtext that implements the model traversal.

skaffman
  • 398,947
  • 96
  • 818
  • 769
reprogrammer
  • 14,298
  • 16
  • 57
  • 93

3 Answers3

3

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

The purpose of the AbstractDeclarativeValidator is to allow you to write constraints in a declarative way – as the class name already suggests.
That is instead of writing exhaustive if-else constructs or extending the generated EMF switch you just have to add the @Check annotation to any method and it will be invoked automatically when validation takes place.

That may be not exactly what you need, though.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @VonC, I had already read the thread you referred to. I'm well aware of the Switch class. I was just wandering if there is a class that implements the traversal logic, too. – reprogrammer May 01 '10 at 17:30
  • @reprogrammer: I thought so, just wanted to point out the apparent "only XText visitor" status of Switch. – VonC May 01 '10 at 18:35
3

I guess EcoreUtil.getAllProperContents(Resource, false) might be what you are looking for. You'll get a TreeIterator that will traverse the contents of the resource. Pass the result of iter.next() into a Switch and perform the transformation logic depending on the element type.

0

Hmm, is it not sufficient to use EMF API? If you want to transform the model, is using some of the transformation langauges like Xtend not possible?

Gabriel Ščerbák
  • 18,240
  • 8
  • 37
  • 52