1/* 2 [The "BSD license"] 3 Copyright (c) 2005-2009 Terence Parr 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 1. Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 2. Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 3. The name of the author may not be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28package org.antlr.runtime.debug; 29 30import org.antlr.runtime.RecognitionException; 31import org.antlr.runtime.Token; 32 33/** All debugging events that a recognizer can trigger. 34 * 35 * I did not create a separate AST debugging interface as it would create 36 * lots of extra classes and DebugParser has a dbg var defined, which makes 37 * it hard to change to ASTDebugEventListener. I looked hard at this issue 38 * and it is easier to understand as one monolithic event interface for all 39 * possible events. Hopefully, adding ST debugging stuff won't be bad. Leave 40 * for future. 4/26/2006. 41 */ 42public interface DebugEventListener { 43 /** Moved to version 2 for v3.1: added grammar name to enter/exit Rule */ 44 public static final String PROTOCOL_VERSION = "2"; 45 46 /** serialized version of true */ 47 public static final int TRUE = 1; 48 public static final int FALSE = 0; 49 50 /** The parser has just entered a rule. No decision has been made about 51 * which alt is predicted. This is fired AFTER init actions have been 52 * executed. Attributes are defined and available etc... 53 * The grammarFileName allows composite grammars to jump around among 54 * multiple grammar files. 55 */ 56 public void enterRule(String grammarFileName, String ruleName); 57 58 /** Because rules can have lots of alternatives, it is very useful to 59 * know which alt you are entering. This is 1..n for n alts. 60 */ 61 public void enterAlt(int alt); 62 63 /** This is the last thing executed before leaving a rule. It is 64 * executed even if an exception is thrown. This is triggered after 65 * error reporting and recovery have occurred (unless the exception is 66 * not caught in this rule). This implies an "exitAlt" event. 67 * The grammarFileName allows composite grammars to jump around among 68 * multiple grammar files. 69 */ 70 public void exitRule(String grammarFileName, String ruleName); 71 72 /** Track entry into any (...) subrule other EBNF construct */ 73 public void enterSubRule(int decisionNumber); 74 75 public void exitSubRule(int decisionNumber); 76 77 /** Every decision, fixed k or arbitrary, has an enter/exit event 78 * so that a GUI can easily track what LT/consume events are 79 * associated with prediction. You will see a single enter/exit 80 * subrule but multiple enter/exit decision events, one for each 81 * loop iteration. 82 */ 83 public void enterDecision(int decisionNumber, boolean couldBacktrack); 84 85 public void exitDecision(int decisionNumber); 86 87 /** An input token was consumed; matched by any kind of element. 88 * Trigger after the token was matched by things like match(), matchAny(). 89 */ 90 public void consumeToken(Token t); 91 92 /** An off-channel input token was consumed. 93 * Trigger after the token was matched by things like match(), matchAny(). 94 * (unless of course the hidden token is first stuff in the input stream). 95 */ 96 public void consumeHiddenToken(Token t); 97 98 /** Somebody (anybody) looked ahead. Note that this actually gets 99 * triggered by both LA and LT calls. The debugger will want to know 100 * which Token object was examined. Like consumeToken, this indicates 101 * what token was seen at that depth. A remote debugger cannot look 102 * ahead into a file it doesn't have so LT events must pass the token 103 * even if the info is redundant. 104 */ 105 public void LT(int i, Token t); 106 107 /** The parser is going to look arbitrarily ahead; mark this location, 108 * the token stream's marker is sent in case you need it. 109 */ 110 public void mark(int marker); 111 112 /** After an arbitrairly long lookahead as with a cyclic DFA (or with 113 * any backtrack), this informs the debugger that stream should be 114 * rewound to the position associated with marker. 115 */ 116 public void rewind(int marker); 117 118 /** Rewind to the input position of the last marker. 119 * Used currently only after a cyclic DFA and just 120 * before starting a sem/syn predicate to get the 121 * input position back to the start of the decision. 122 * Do not "pop" the marker off the state. mark(i) 123 * and rewind(i) should balance still. 124 */ 125 public void rewind(); 126 127 public void beginBacktrack(int level); 128 129 public void endBacktrack(int level, boolean successful); 130 131 /** To watch a parser move through the grammar, the parser needs to 132 * inform the debugger what line/charPos it is passing in the grammar. 133 * For now, this does not know how to switch from one grammar to the 134 * other and back for island grammars etc... 135 * 136 * This should also allow breakpoints because the debugger can stop 137 * the parser whenever it hits this line/pos. 138 */ 139 public void location(int line, int pos); 140 141 /** A recognition exception occurred such as NoViableAltException. I made 142 * this a generic event so that I can alter the exception hierachy later 143 * without having to alter all the debug objects. 144 * 145 * Upon error, the stack of enter rule/subrule must be properly unwound. 146 * If no viable alt occurs it is within an enter/exit decision, which 147 * also must be rewound. Even the rewind for each mark must be unwount. 148 * In the Java target this is pretty easy using try/finally, if a bit 149 * ugly in the generated code. The rewind is generated in DFA.predict() 150 * actually so no code needs to be generated for that. For languages 151 * w/o this "finally" feature (C++?), the target implementor will have 152 * to build an event stack or something. 153 * 154 * Across a socket for remote debugging, only the RecognitionException 155 * data fields are transmitted. The token object or whatever that 156 * caused the problem was the last object referenced by LT. The 157 * immediately preceding LT event should hold the unexpected Token or 158 * char. 159 * 160 * Here is a sample event trace for grammar: 161 * 162 * b : C ({;}A|B) // {;} is there to prevent A|B becoming a set 163 * | D 164 * ; 165 * 166 * The sequence for this rule (with no viable alt in the subrule) for 167 * input 'c c' (there are 3 tokens) is: 168 * 169 * commence 170 * LT(1) 171 * enterRule b 172 * location 7 1 173 * enter decision 3 174 * LT(1) 175 * exit decision 3 176 * enterAlt1 177 * location 7 5 178 * LT(1) 179 * consumeToken [c/<4>,1:0] 180 * location 7 7 181 * enterSubRule 2 182 * enter decision 2 183 * LT(1) 184 * LT(1) 185 * recognitionException NoViableAltException 2 1 2 186 * exit decision 2 187 * exitSubRule 2 188 * beginResync 189 * LT(1) 190 * consumeToken [c/<4>,1:1] 191 * LT(1) 192 * endResync 193 * LT(-1) 194 * exitRule b 195 * terminate 196 */ 197 public void recognitionException(RecognitionException e); 198 199 /** Indicates the recognizer is about to consume tokens to resynchronize 200 * the parser. Any consume events from here until the recovered event 201 * are not part of the parse--they are dead tokens. 202 */ 203 public void beginResync(); 204 205 /** Indicates that the recognizer has finished consuming tokens in order 206 * to resychronize. There may be multiple beginResync/endResync pairs 207 * before the recognizer comes out of errorRecovery mode (in which 208 * multiple errors are suppressed). This will be useful 209 * in a gui where you want to probably grey out tokens that are consumed 210 * but not matched to anything in grammar. Anything between 211 * a beginResync/endResync pair was tossed out by the parser. 212 */ 213 public void endResync(); 214 215 /** A semantic predicate was evaluate with this result and action text */ 216 public void semanticPredicate(boolean result, String predicate); 217 218 /** Announce that parsing has begun. Not technically useful except for 219 * sending events over a socket. A GUI for example will launch a thread 220 * to connect and communicate with a remote parser. The thread will want 221 * to notify the GUI when a connection is made. ANTLR parsers 222 * trigger this upon entry to the first rule (the ruleLevel is used to 223 * figure this out). 224 */ 225 public void commence(); 226 227 /** Parsing is over; successfully or not. Mostly useful for telling 228 * remote debugging listeners that it's time to quit. When the rule 229 * invocation level goes to zero at the end of a rule, we are done 230 * parsing. 231 */ 232 public void terminate(); 233 234 235 // T r e e P a r s i n g 236 237 /** Input for a tree parser is an AST, but we know nothing for sure 238 * about a node except its type and text (obtained from the adaptor). 239 * This is the analog of the consumeToken method. Again, the ID is 240 * the hashCode usually of the node so it only works if hashCode is 241 * not implemented. If the type is UP or DOWN, then 242 * the ID is not really meaningful as it's fixed--there is 243 * just one UP node and one DOWN navigation node. 244 * @param t 245 */ 246 public void consumeNode(Object t); 247 248 /** The tree parser lookedahead. If the type is UP or DOWN, 249 * then the ID is not really meaningful as it's fixed--there is 250 * just one UP node and one DOWN navigation node. 251 */ 252 public void LT(int i, Object t); 253 254 255 // A S T E v e n t s 256 257 /** A nil was created (even nil nodes have a unique ID... 258 * they are not "null" per se). As of 4/28/2006, this 259 * seems to be uniquely triggered when starting a new subtree 260 * such as when entering a subrule in automatic mode and when 261 * building a tree in rewrite mode. 262 * 263 * If you are receiving this event over a socket via 264 * RemoteDebugEventSocketListener then only t.ID is set. 265 */ 266 public void nilNode(Object t); 267 268 /** Upon syntax error, recognizers bracket the error with an error node 269 * if they are building ASTs. 270 * @param t 271 */ 272 public void errorNode(Object t); 273 274 /** Announce a new node built from token elements such as type etc... 275 * 276 * If you are receiving this event over a socket via 277 * RemoteDebugEventSocketListener then only t.ID, type, text are 278 * set. 279 */ 280 public void createNode(Object t); 281 282 /** Announce a new node built from an existing token. 283 * 284 * If you are receiving this event over a socket via 285 * RemoteDebugEventSocketListener then only node.ID and token.tokenIndex 286 * are set. 287 */ 288 public void createNode(Object node, Token token); 289 290 /** Make a node the new root of an existing root. See 291 * 292 * Note: the newRootID parameter is possibly different 293 * than the TreeAdaptor.becomeRoot() newRoot parameter. 294 * In our case, it will always be the result of calling 295 * TreeAdaptor.becomeRoot() and not root_n or whatever. 296 * 297 * The listener should assume that this event occurs 298 * only when the current subrule (or rule) subtree is 299 * being reset to newRootID. 300 * 301 * If you are receiving this event over a socket via 302 * RemoteDebugEventSocketListener then only IDs are set. 303 * 304 * @see org.antlr.runtime.tree.TreeAdaptor.becomeRoot() 305 */ 306 public void becomeRoot(Object newRoot, Object oldRoot); 307 308 /** Make childID a child of rootID. 309 * 310 * If you are receiving this event over a socket via 311 * RemoteDebugEventSocketListener then only IDs are set. 312 * 313 * @see org.antlr.runtime.tree.TreeAdaptor.addChild() 314 */ 315 public void addChild(Object root, Object child); 316 317 /** Set the token start/stop token index for a subtree root or node. 318 * 319 * If you are receiving this event over a socket via 320 * RemoteDebugEventSocketListener then only t.ID is set. 321 */ 322 public void setTokenBoundaries(Object t, int tokenStartIndex, int tokenStopIndex); 323} 324