5

I'm using xtext to generate an editor for a particular language. When using the editor for my new language, it has all the lovely xtext features like code-completation and coloring and so on. What I'd like to be able to do is visualise the structure of the text in my editor.

I know that xtext has an internal AST and a parse tree ( I understand that they call it a `node model') - is there any way of visualising this tree?

TheRedOne
  • 165
  • 3
  • 12
Joe
  • 4,367
  • 7
  • 33
  • 52

3 Answers3

3

A simple solution in xtend (based on the introspection done by default by EObject.toString()):

def static String dump(EObject mod_, String indent) {
    var res = indent + mod_.toString.replaceFirst ('.*[.]impl[.](.*)Impl[^(]*', '$1 ')

    for (a :mod_.eCrossReferences)
        res += ' ->' + a.toString().replaceFirst ('.*[.]impl[.](.*)Impl[^(]*', '$1 ')
    res += "\n"
    for (f :mod_.eContents) {
        res += f.dump (indent+"    ")
    }
    return res
}

Output from a call such as dump(someEObject, '') will return:

ExpressionModel 
Variable (name: i)
    Plus 
        IntConst (value: 1)
        Plus 
            IntConst (value: 2)
            Plus 
                IntConst (value: 3)
Variable (name: j)
    Plus 
        VarRef  ->Variable (name: i)
        Plus 
            IntConst (value: 4)
            Plus 
                IntConst (value: 5)
artejera
  • 1,346
  • 1
  • 11
  • 19
2

This might help you: https://github.com/OLibutzki/xtext.tools

It offers an outline for the node model and for the semantic model (AST).

1

You should check the content outline. I have customized mine but I think that the default one reflects the structure of the model.

Dan Fitch
  • 2,480
  • 2
  • 23
  • 39
Jeff MAURY
  • 11
  • 1