public static void doTreeAction(string f, AST t, string[] tokenNames) { if (t == null) { return; } if (showTree) { BaseAST.setVerboseStringConversion(true, tokenNames); ASTFactory factory = new ASTFactory(); AST r = factory.create(0, "AST ROOT"); r.setFirstChild(t); ASTFrame frame = new ASTFrame("Java AST", r); frame.ShowDialog(); //frame.Visible = true; // System.out.println(t.toStringList()); } JavaTreeParser tparse = new JavaTreeParser(); JavaRecognizer.initializeASTFactory(tparse.getASTFactory()); try { tparse.compilationUnit(t); // System.out.println("successful walk of result AST for "+f); } catch (RecognitionException e) { Console.Error.WriteLine(e.Message); Console.Error.WriteLine(e.StackTrace); } }
/// <summary> /// Make a tree from a list of nodes. The first element in the /// array is the root. If the root is null, then the tree is /// a simple list not a tree. Handles null children nodes correctly. /// For example, build(a, b, null, c) yields tree (a b c). build(null,a,b) /// yields tree (nil a b). /// </summary> /// <param name="nodes">List of Nodes.</param> /// <returns>AST Node tree.</returns> public override AST make(params AST[] nodes) { if (nodes == null || nodes.Length == 0) { return(null); } AST root = nodes[0]; AST tail = null; if (root != null) { root.setFirstChild(null); // don't leave any old pointers set } // link in children; for (int i = 1; i < nodes.Length; i++) { if (nodes[i] == null) { continue; } // ignore null nodes if (root == null) { // Set the root and set it up for a flat list root = (tail = nodes[i]); } else if (tail == null) { root.setFirstChild(nodes[i]); ((ASTNode)nodes[i]).setParent((ASTNode)root); tail = root.getFirstChild(); } else { ((ASTNode)nodes[i]).setParent((ASTNode)root); tail.setNextSibling(nodes[i]); ((ASTNode)nodes[i]).setPreviousSibling((ASTNode)tail); tail = tail.getNextSibling(); } // Chase tail to last sibling while (tail.getNextSibling() != null) { tail = tail.getNextSibling(); } } return(root); }
/// <summary> /// Duplicate AST Node tree rooted at specified AST node. Ignore it's siblings. /// </summary> /// <param name="t">Root of AST Node tree.</param> /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns> public virtual AST dupTree(AST t) { AST result = dup(t); // make copy of root // copy all children of root. if (t != null) { result.setFirstChild(dupList(t.getFirstChild())); } return(result); }
/// <summary> /// Duplicate AST Node tree rooted at specified AST node. Ignore it's siblings. /// </summary> /// <param name="t">Root of AST Node tree.</param> /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns> public override AST dupTree(AST t) { AST result = dup(t); // make copy of root // copy all children of root. if (t != null) { AST d = dupList(t.getFirstChild()); result.setFirstChild(d); ((ASTNode)d).setParent((ASTNode)result); } return(result); }