001: package javaparser;
002:
003: import java.io.*;
004: import javaparser.javacc_gen.*;
005:
006: import java.util.*;
007: import javax.swing.tree.*;
008:
009: /** This builds the complete syntax tree as the tokens come from the trace methods of the parser.
010: */
011: public class RAWSyntaxTree implements ParserOutputProcessor {
012: private RAWParserTreeNode actual; // root at the beginning
013:
014: public RAWParserTreeNode root;
015:
016: public final String javaName;
017:
018: public RAWSyntaxTree(String javaName) {
019: actual = new RAWParserTreeNode("");
020: this .javaName = javaName;
021: root = actual;
022: }
023:
024: int level = 0;
025:
026: public void addNode(String name) {
027: level++;
028: RAWParserTreeNode n = RAWParserTreeNodeFactory.getInstance()
029: .create(name);
030: actual.add(n);
031: actual = n;
032: }
033:
034: /**
035: * Called by the parser when it returns from a previously
036: * encountered node (for which it has called addNode)
037: */
038: public void returnFromNode(String name) {
039: level--;
040: //System.out.println("return from "+name);
041: actual = actual.parent;
042: }
043:
044: /**
045: * Called by the parser when it has encountered a token
046: * at the current treeposition, which is defined by all
047: * already received calls of addNode() and returnFromNode().
048: */
049: public void addLeaf(final Token t) {
050: if (t.kind == JavaParserConstants.EOF)
051: return;
052:
053: RAWParserTreeNode nn = RAWParserTreeNodeFactory.getInstance()
054: .create(t);
055: actual.add(nn);
056:
057: //System.out.println("add leaf "+t+" ("+t.kind+")");
058: }
059:
060: public void terminateRecurse() {
061: RAWParserTreeNodeFactory.terminateRecurse(root);
062: }
063:
064: public static void main(String[] arguments) throws Exception {
065: //testOldRAWTree();
066:
067: testNewRAWTree();
068: }
069:
070: /** two times quicker using RAWTreeNode as MutableTN...
071: OLd tree: 483 171 156 187 171
072:
073: New tree:
074:
075: With recycling: 109 78 62 62 63 62 78 62 156 62
076: RAWParserTreeNodeFactory: 57709 free, 519291 reused.
077:
078: Without 109 124 94 124 78 125 77 125 78 125
079: Directcreat 109 109 78 124 78 125 78 124 78 125
080:
081: */
082: public static void testNewRAWTree() throws Exception {
083: System.out.println("NEW Tree");
084: for (int i = 0; i < 10; i++) {
085: FileReader sr = new FileReader(
086: new File(
087: "C:/sources/other/Script/src/tide/editor/maineditorframe.java"));
088: JavaParser pa = new JavaParser(sr);
089: pa.disable_tracing();
090:
091: long t0 = System.currentTimeMillis();
092: RAWSyntaxTree st = new RAWSyntaxTree("??javaName??");
093: pa.parserOutputProcessor = st;
094: pa.CompilationUnit();
095: System.out.print(" " + (System.currentTimeMillis() - t0)
096: + " ");
097:
098: st.terminateRecurse();
099: }
100:
101: System.out.println("\n"
102: + RAWParserTreeNodeFactory.getInstance());
103: }
104: /*
105: public static void testOldRAWTree() throws Exception
106: {
107: System.out.println("OLD Tree");
108: for(int i=0; i<5; i++)
109: {
110: FileReader sr = new FileReader(new File("C:/sources/other/Script/src/tide/editor/maineditorframe.java"));
111: JavaParser pa = new JavaParser(sr);
112: pa.disable_tracing();
113:
114: long t0 = System.currentTimeMillis();
115: SyntaxTree st = new SyntaxTree("??javaName??");
116: pa.parserOutputProcessor = st;
117: pa.CompilationUnit();
118: System.out.print(" "+(System.currentTimeMillis()-t0)+" ");
119: }
120: System.out.println();
121: }*/
122:
123: }
|