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