Hi,
maybe I'm just overlooking something, but it would be very nice to be able to print out the tree that we're manipulating.
Right now I just use a TreeWalker, and print out each element, but that's without indention. So it's hard to see where the element belongs to. Is there maybe a method for this somewhere that I'm overlooking? Or else, can I get a pointer how to implement it myself?
Thanks,
~David
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
you can either call toSource() on any source element, but that won't give you a tree but a real source code representation. If you want the tree, this source code snippet could help (I write it down from my memories, don't be too picky on exact syntax/function names ;-)
class TreePrinter {
private void printIndent() {
for (int i = 0; i < indent; i++) System.out.print(" "); // imperformant solution!!!!
}
private int indent = 0;
public void dfsPrint(ProgramElement node) {
printIndent();
System.out.println(node);
if (node instanceof NonTerminalProgramElement) {
++indent;
int count = ((NonTerminalProgramElement)node).getChildCount();
for (int i=0;i<count;i++) {
ProgramElement child = ((NonTerminalProgramElement)node)node.getChild(i);
dfsPrint(child);
}
--indent;
}
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
maybe I'm just overlooking something, but it would be very nice to be able to print out the tree that we're manipulating.
Right now I just use a TreeWalker, and print out each element, but that's without indention. So it's hard to see where the element belongs to. Is there maybe a method for this somewhere that I'm overlooking? Or else, can I get a pointer how to implement it myself?
Thanks,
~David
Hey, sorry for the late reply, but thanks, that works out nicely!
Hej,
you can either call toSource() on any source element, but that won't give you a tree but a real source code representation. If you want the tree, this source code snippet could help (I write it down from my memories, don't be too picky on exact syntax/function names ;-)
class TreePrinter {
private void printIndent() {
for (int i = 0; i < indent; i++) System.out.print(" "); // imperformant solution!!!!
}
private int indent = 0;
public void dfsPrint(ProgramElement node) {
printIndent();
System.out.println(node);
if (node instanceof NonTerminalProgramElement) {
++indent;
}