Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewing the parse tree/node model/AST in xtext

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?

like image 274
Joe Avatar asked Dec 04 '12 10:12

Joe


3 Answers

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)
like image 155
artejera Avatar answered Oct 11 '22 16:10

artejera


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

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

like image 38
Oliver Libutzki Avatar answered Oct 11 '22 16:10

Oliver Libutzki


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

like image 28
Jeff MAURY Avatar answered Oct 11 '22 15:10

Jeff MAURY