Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext generate visitor and traversal(Generate pre-order traversal with visit.. and visitEnd... methods)
Xtext generate visitor and traversal [message #1771847] Thu, 31 August 2017 10:32 Go to next message
Gabor Szoke is currently offline Gabor SzokeFriend
Messages: 1
Registered: August 2017
Junior Member
I would like to generate a visitor and a pre-order traversal algorithm starting from the root node (DefaultRule). Given the simple statemachine example, i would like to have something similar to this in the end:

interface MyDslVisitor {
    void visitStatemachine(Statemachine sm);
    void visitEndStatemachine(Statemachine sm);
    void visitState(State s);
    void visitEndState(State s);
    void visitTransition(Transition t);
    void visitEndTransition(Transition t);

class MyDslPreOrderTraversal {
  void traverse(Statemachine sm, MyDslVisitor visitor) {
      for (State s : sm.getStates()) {
         for (Transition t : s.getTransitions()) {
             // ...

Example usage of this visitor could be a pretty-printer, where each child is indented.
class PrettyPrinter implements MyDslVisitor {
    int indent = 0;
    void visitStatemachine(Statemachine sm) {
    void visitEndStatemachine(Statemachine sm) {}
    void visitState(State s) {
       indent += 2;
    void visitEndState(State s) {
       indent -= 2;
    void visitTransition(Transition t) {
       indent += 2;
    void visitEndTransition(Transition t) {
       indent -= 2;
    void println(String str) {
        System.out.println(Strings.repeat(" ", indent) + str);

An output would look like this:
        transition A -> B
        transition B -> C
        transition B -> A
        transition C -> A

I tried to come up with an overly simplified example, to show where I got stuck. I looked at the generated MyDslSwitch but it won't suffice if I would like to achieve a pretty-printer such as above. (Obviously, I want to use it in a more complicated tree traversal with stacks and stuff.)

Is there any way to hook onto the mwe2 workflow and generate something similar to the MyDslSwitch?
Re: Xtext generate visitor and traversal [message #1771918 is a reply to message #1771847] Fri, 01 September 2017 06:37 Go to previous message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

You could write a generator fragment and add it to your language configuration. The StandardLanguage allows to add fragments to it. Start investigating StandardLanguage and GrammarAccessFragment2.
Previous Topic:Trim SemanticRegion (Formatting)
Next Topic:Error When Using Import and Mixin
Goto Forum:

Current Time: Mon Jul 04 06:57:09 GMT 2022

Powered by FUDForum. Page generated in 0.02256 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top