1 package antlr; 2 3 9 10 import java.util.NoSuchElementException ; 11 12 import antlr.collections.AST; 13 import antlr.collections.impl.BitSet; 14 15 public class TreeParser { 16 21 public static ASTNULLType ASTNULL = new ASTNULLType(); 22 23 24 protected AST _retTree; 25 26 27 29 30 32 protected TreeParserSharedInputState inputState; 33 34 35 protected String [] tokenNames; 36 37 38 protected AST returnAST; 39 40 41 protected ASTFactory astFactory = new ASTFactory(); 42 43 44 protected int traceDepth = 0; 45 46 public TreeParser() { 47 inputState = new TreeParserSharedInputState(); 48 } 49 50 51 public AST getAST() { 52 return returnAST; 53 } 54 55 public ASTFactory getASTFactory() { 56 return astFactory; 57 } 58 59 public String getTokenName(int num) { 60 return tokenNames[num]; 61 } 62 63 public String [] getTokenNames() { 64 return tokenNames; 65 } 66 67 protected void match(AST t, int ttype) throws MismatchedTokenException { 68 if (t == null || t == ASTNULL || t.getType() != ttype) { 70 throw new MismatchedTokenException(getTokenNames(), t, ttype, false); 71 } 72 } 73 74 78 public void match(AST t, BitSet b) throws MismatchedTokenException { 79 if (t == null || t == ASTNULL || !b.member(t.getType())) { 80 throw new MismatchedTokenException(getTokenNames(), t, b, false); 81 } 82 } 83 84 protected void matchNot(AST t, int ttype) throws MismatchedTokenException { 85 if (t == null || t == ASTNULL || t.getType() == ttype) { 87 throw new MismatchedTokenException(getTokenNames(), t, ttype, true); 88 } 89 } 90 91 public static void panic() { 92 System.err.println("TreeWalker: panic"); 93 System.exit(1); 94 } 95 96 97 public void reportError(RecognitionException ex) { 98 System.err.println(ex.toString()); 99 } 100 101 102 public void reportError(String s) { 103 System.err.println("error: " + s); 104 } 105 106 107 public void reportWarning(String s) { 108 System.err.println("warning: " + s); 109 } 110 111 115 public void setASTFactory(ASTFactory f) { 116 astFactory = f; 117 } 118 119 120 public void setASTNodeType(String nodeType) { 121 setASTNodeClass(nodeType); 122 } 123 124 125 public void setASTNodeClass(String nodeType) { 126 astFactory.setASTNodeType(nodeType); 127 } 128 129 public void traceIndent() { 130 for (int i = 0; i < traceDepth; i++) 131 System.out.print(" "); 132 } 133 134 public void traceIn(String rname, AST t) { 135 traceDepth += 1; 136 traceIndent(); 137 System.out.println("> " + rname + 138 "(" + (t != null?t.toString():"null") + ")" + 139 ((inputState.guessing > 0)?" [guessing]":"")); 140 } 141 142 public void traceOut(String rname, AST t) { 143 traceIndent(); 144 System.out.println("< " + rname + 145 "(" + (t != null?t.toString():"null") + ")" + 146 ((inputState.guessing > 0)?" [guessing]":"")); 147 traceDepth--; 148 } 149 } 150 | Popular Tags |