1 package persistence.antlr; 2 3 8 9 import persistence.antlr.collections.impl.BitSet; 10 import persistence.antlr.collections.AST; 11 import persistence.antlr.collections.impl.ASTArray; 12 13 46 47 import java.io.IOException ; 48 import java.util.Hashtable ; 49 50 import persistence.antlr.debug.MessageListener; 51 import persistence.antlr.debug.ParserListener; 52 import persistence.antlr.debug.ParserMatchListener; 53 import persistence.antlr.debug.ParserTokenListener; 54 import persistence.antlr.debug.SemanticPredicateListener; 55 import persistence.antlr.debug.SyntacticPredicateListener; 56 import persistence.antlr.debug.TraceListener; 57 58 public abstract class Parser { 59 protected ParserSharedInputState inputState; 60 61 62 64 65 protected String [] tokenNames; 66 67 68 protected AST returnAST; 69 70 75 protected ASTFactory astFactory = null; 76 77 80 protected Hashtable tokenTypeToASTClassMap = null; 81 82 private boolean ignoreInvalidDebugCalls = false; 83 84 85 protected int traceDepth = 0; 86 87 public Parser() { 88 this(new ParserSharedInputState()); 89 } 90 91 public Parser(ParserSharedInputState state) { 92 inputState = state; 93 } 94 95 99 public Hashtable getTokenTypeToASTClassMap() { 100 return tokenTypeToASTClassMap; 101 } 102 103 public void addMessageListener(MessageListener l) { 104 if (!ignoreInvalidDebugCalls) 105 throw new IllegalArgumentException ("addMessageListener() is only valid if parser built for debugging"); 106 } 107 108 public void addParserListener(ParserListener l) { 109 if (!ignoreInvalidDebugCalls) 110 throw new IllegalArgumentException ("addParserListener() is only valid if parser built for debugging"); 111 } 112 113 public void addParserMatchListener(ParserMatchListener l) { 114 if (!ignoreInvalidDebugCalls) 115 throw new IllegalArgumentException ("addParserMatchListener() is only valid if parser built for debugging"); 116 } 117 118 public void addParserTokenListener(ParserTokenListener l) { 119 if (!ignoreInvalidDebugCalls) 120 throw new IllegalArgumentException ("addParserTokenListener() is only valid if parser built for debugging"); 121 } 122 123 public void addSemanticPredicateListener(SemanticPredicateListener l) { 124 if (!ignoreInvalidDebugCalls) 125 throw new IllegalArgumentException ("addSemanticPredicateListener() is only valid if parser built for debugging"); 126 } 127 128 public void addSyntacticPredicateListener(SyntacticPredicateListener l) { 129 if (!ignoreInvalidDebugCalls) 130 throw new IllegalArgumentException ("addSyntacticPredicateListener() is only valid if parser built for debugging"); 131 } 132 133 public void addTraceListener(TraceListener l) { 134 if (!ignoreInvalidDebugCalls) 135 throw new IllegalArgumentException ("addTraceListener() is only valid if parser built for debugging"); 136 } 137 138 139 public abstract void consume() throws TokenStreamException; 140 141 142 public void consumeUntil(int tokenType) throws TokenStreamException { 143 while (LA(1) != Token.EOF_TYPE && LA(1) != tokenType) { 144 consume(); 145 } 146 } 147 148 149 public void consumeUntil(BitSet set) throws TokenStreamException { 150 while (LA(1) != Token.EOF_TYPE && !set.member(LA(1))) { 151 consume(); 152 } 153 } 154 155 protected void defaultDebuggingSetup(TokenStream lexer, TokenBuffer tokBuf) { 156 } 158 159 160 public AST getAST() { 161 return returnAST; 162 } 163 164 public ASTFactory getASTFactory() { 165 return astFactory; 166 } 167 168 public String getFilename() { 169 return inputState.filename; 170 } 171 172 public ParserSharedInputState getInputState() { 173 return inputState; 174 } 175 176 public void setInputState(ParserSharedInputState state) { 177 inputState = state; 178 } 179 180 public String getTokenName(int num) { 181 return tokenNames[num]; 182 } 183 184 public String [] getTokenNames() { 185 return tokenNames; 186 } 187 188 public boolean isDebugMode() { 189 return false; 190 } 191 192 196 public abstract int LA(int i) throws TokenStreamException; 197 198 199 public abstract Token LT(int i) throws TokenStreamException; 200 201 public int mark() { 203 return inputState.input.mark(); 204 } 205 206 210 public void match(int t) throws MismatchedTokenException, TokenStreamException { 211 if (LA(1) != t) 212 throw new MismatchedTokenException(tokenNames, LT(1), t, false, getFilename()); 213 else 214 consume(); 216 } 217 218 222 public void match(BitSet b) throws MismatchedTokenException, TokenStreamException { 223 if (!b.member(LA(1))) 224 throw new MismatchedTokenException(tokenNames, LT(1), b, false, getFilename()); 225 else 226 consume(); 228 } 229 230 public void matchNot(int t) throws MismatchedTokenException, TokenStreamException { 231 if (LA(1) == t) 232 throw new MismatchedTokenException(tokenNames, LT(1), t, true, getFilename()); 234 else 235 consume(); 237 } 238 239 245 public static void panic() { 246 System.err.println("Parser: panic"); 247 System.exit(1); 248 } 249 250 public void removeMessageListener(MessageListener l) { 251 if (!ignoreInvalidDebugCalls) 252 throw new RuntimeException ("removeMessageListener() is only valid if parser built for debugging"); 253 } 254 255 public void removeParserListener(ParserListener l) { 256 if (!ignoreInvalidDebugCalls) 257 throw new RuntimeException ("removeParserListener() is only valid if parser built for debugging"); 258 } 259 260 public void removeParserMatchListener(ParserMatchListener l) { 261 if (!ignoreInvalidDebugCalls) 262 throw new RuntimeException ("removeParserMatchListener() is only valid if parser built for debugging"); 263 } 264 265 public void removeParserTokenListener(ParserTokenListener l) { 266 if (!ignoreInvalidDebugCalls) 267 throw new RuntimeException ("removeParserTokenListener() is only valid if parser built for debugging"); 268 } 269 270 public void removeSemanticPredicateListener(SemanticPredicateListener l) { 271 if (!ignoreInvalidDebugCalls) 272 throw new IllegalArgumentException ("removeSemanticPredicateListener() is only valid if parser built for debugging"); 273 } 274 275 public void removeSyntacticPredicateListener(SyntacticPredicateListener l) { 276 if (!ignoreInvalidDebugCalls) 277 throw new IllegalArgumentException ("removeSyntacticPredicateListener() is only valid if parser built for debugging"); 278 } 279 280 public void removeTraceListener(TraceListener l) { 281 if (!ignoreInvalidDebugCalls) 282 throw new RuntimeException ("removeTraceListener() is only valid if parser built for debugging"); 283 } 284 285 286 public void reportError(RecognitionException ex) { 287 System.err.println(ex); 288 } 289 290 291 public void reportError(String s) { 292 if (getFilename() == null) { 293 System.err.println("error: " + s); 294 } 295 else { 296 System.err.println(getFilename() + ": error: " + s); 297 } 298 } 299 300 301 public void reportWarning(String s) { 302 if (getFilename() == null) { 303 System.err.println("warning: " + s); 304 } 305 else { 306 System.err.println(getFilename() + ": warning: " + s); 307 } 308 } 309 310 public void rewind(int pos) { 311 inputState.input.rewind(pos); 312 } 313 314 318 public void setASTFactory(ASTFactory f) { 319 astFactory = f; 320 } 321 322 public void setASTNodeClass(String cl) { 323 astFactory.setASTNodeType(cl); 324 } 325 326 330 public void setASTNodeType(String nodeType) { 331 setASTNodeClass(nodeType); 332 } 333 334 public void setDebugMode(boolean debugMode) { 335 if (!ignoreInvalidDebugCalls) 336 throw new RuntimeException ("setDebugMode() only valid if parser built for debugging"); 337 } 338 339 public void setFilename(String f) { 340 inputState.filename = f; 341 } 342 343 public void setIgnoreInvalidDebugCalls(boolean value) { 344 ignoreInvalidDebugCalls = value; 345 } 346 347 348 public void setTokenBuffer(TokenBuffer t) { 349 inputState.input = t; 350 } 351 352 public void traceIndent() { 353 for (int i = 0; i < traceDepth; i++) 354 System.out.print(" "); 355 } 356 357 public void traceIn(String rname) throws TokenStreamException { 358 traceDepth += 1; 359 traceIndent(); 360 System.out.println("> " + rname + "; LA(1)==" + LT(1).getText() + 361 ((inputState.guessing > 0)?" [guessing]":"")); 362 } 363 364 public void traceOut(String rname) throws TokenStreamException { 365 traceIndent(); 366 System.out.println("< " + rname + "; LA(1)==" + LT(1).getText() + 367 ((inputState.guessing > 0)?" [guessing]":"")); 368 traceDepth -= 1; 369 } 370 } 371 | Popular Tags |