1 package persistence.antlr.debug; 2 3 7 8 import persistence.antlr.*; 9 import persistence.antlr.collections.impl.BitSet; 10 11 import java.util.Stack ; 12 13 19 public class ParseTreeDebugParser extends LLkParser { 20 23 protected Stack currentParseTreeRoot = new Stack (); 24 25 28 protected ParseTreeRule mostRecentParseTreeRoot = null; 29 30 31 protected int numberOfDerivationSteps = 1; 33 public ParseTreeDebugParser(int k_) { 34 super(k_); 35 } 36 37 public ParseTreeDebugParser(ParserSharedInputState state, int k_) { 38 super(state,k_); 39 } 40 41 public ParseTreeDebugParser(TokenBuffer tokenBuf, int k_) { 42 super(tokenBuf, k_); 43 } 44 45 public ParseTreeDebugParser(TokenStream lexer, int k_) { 46 super(lexer,k_); 47 } 48 49 public ParseTree getParseTree() { 50 return mostRecentParseTreeRoot; 51 } 52 53 public int getNumberOfDerivationSteps() { 54 return numberOfDerivationSteps; 55 } 56 57 public void match(int i) throws MismatchedTokenException, TokenStreamException { 58 addCurrentTokenToParseTree(); 59 super.match(i); 60 } 61 62 public void match(BitSet bitSet) throws MismatchedTokenException, TokenStreamException { 63 addCurrentTokenToParseTree(); 64 super.match(bitSet); 65 } 66 67 public void matchNot(int i) throws MismatchedTokenException, TokenStreamException { 68 addCurrentTokenToParseTree(); 69 super.matchNot(i); 70 } 71 72 79 protected void addCurrentTokenToParseTree() throws TokenStreamException { 80 if (inputState.guessing>0) { 81 return; 82 } 83 ParseTreeRule root = (ParseTreeRule)currentParseTreeRoot.peek(); 84 ParseTreeToken tokenNode = null; 85 if ( LA(1)==Token.EOF_TYPE ) { 86 tokenNode = new ParseTreeToken(new persistence.antlr.CommonToken("EOF")); 87 } 88 else { 89 tokenNode = new ParseTreeToken(LT(1)); 90 } 91 root.addChild(tokenNode); 92 } 93 94 95 public void traceIn(String s) throws TokenStreamException { 96 if (inputState.guessing>0) { 97 return; 98 } 99 ParseTreeRule subRoot = new ParseTreeRule(s); 100 if ( currentParseTreeRoot.size()>0 ) { 101 ParseTreeRule oldRoot = (ParseTreeRule)currentParseTreeRoot.peek(); 102 oldRoot.addChild(subRoot); 103 } 104 currentParseTreeRoot.push(subRoot); 105 numberOfDerivationSteps++; 106 } 107 108 109 public void traceOut(String s) throws TokenStreamException { 110 if (inputState.guessing>0) { 111 return; 112 } 113 mostRecentParseTreeRoot = (ParseTreeRule)currentParseTreeRoot.pop(); 114 } 115 116 } 117 | Popular Tags |