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*/ 28/** Template overrides to add debugging to normal Python output; 29 * If ASTs are built, then you'll also get ASTDbg.stg loaded. 30 */ 31 32@outputFile.imports() ::= << 33<@super.imports()> 34from antlr3.debug import * 35>> 36 37@genericParser.args() ::= << 38debug_socket = kwargs.pop('debug_socket', None) 39port = kwargs.pop('port', None) 40>> 41 42@genericParser.init() ::= << 43self.ruleLevel = 0 44 45if self._dbg is None: 46 <createListenerAndHandshake()> 47 48>> 49 50createListenerAndHandshake() ::= << 51<if(TREE_PARSER)> 52proxy = DebugEventSocketProxy(self, adaptor=self.input.getTreeAdaptor(), 53 debug=debug_socket, port=port)<\n> 54<else> 55proxy = DebugEventSocketProxy(self, debug=debug_socket, port=port)<\n> 56<endif> 57self.setDebugListener(proxy) 58proxy.handshake() 59 60>> 61 62@genericParser.members() ::= << 63<if(grammar.grammarIsRoot)> 64ruleNames = [ 65 "invalidRule", <grammar.allImportedRules:{rST | "<rST.name>"}; wrap="\n ", separator=", "> 66 ]<\n> 67<endif> 68decisionCanBacktrack = [ 69 False, # invalid decision 70 <grammar.decisions:{d | <if(d.dfa.hasSynPred)>True<else>False<endif>}; wrap="\n ", separator=", "> 71 ] 72<if(grammar.grammarIsRoot)> <! grammar imports other grammar(s) !> 73def getRuleLevel(self): 74 return self.ruleLevel 75 76def incRuleLevel(self): 77 self.ruleLevel += 1 78 79def decRuleLevel(self): 80 self.ruleLevel -= 1 81 82<if(profile)> 83 <ctorForProfilingRootGrammar()> 84<else> 85 <ctorForRootGrammar()> 86<endif> 87<ctorForPredefinedListener()> 88<else> <! imported grammar !> 89def getRuleLevel(self): 90 return <grammar.delegators:{g| <g:delegateName()>}>.getRuleLevel() 91 92def incRuleLevel(self): 93 <grammar.delegators:{g| <g:delegateName()>}>.incRuleLevel() 94 95def decRuleLevel(self): 96 <grammar.delegators:{g| <g:delegateName()>}>.decRuleLevel() 97 98<ctorForDelegateGrammar()> 99<endif> 100<if(profile)> 101FIXME(2) 102public boolean alreadyParsedRule(IntStream input, int ruleIndex) { 103 ((Profiler)self._dbg).examineRuleMemoization(input, ruleIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]); 104 return super.alreadyParsedRule(input, ruleIndex); 105}<\n> 106FIXME(3) 107public void memoize(IntStream input, 108 int ruleIndex, 109 int ruleStartIndex) 110{ 111 ((Profiler)self._dbg).memoize(input, ruleIndex, ruleStartIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]); 112 super.memoize(input, ruleIndex, ruleStartIndex); 113}<\n> 114<endif> 115def evalPredicate(self, result, predicate): 116 self._dbg.semanticPredicate(result, predicate) 117 return result 118<\n> 119>> 120 121ctorForRootGrammar() ::= << 122<! bug: can't use <@super.members()> cut-n-paste instead !> 123<! Same except we add port number and profile stuff if root grammar !> 124<! 125public <name>(<inputStreamType> input) { 126 this(input, DebugEventSocketProxy.DEFAULT_DEBUGGER_PORT, new RecognizerSharedState()); 127} 128public <name>(<inputStreamType> input, int port, RecognizerSharedState state) { 129 super(input, state); 130 <parserCtorBody()> 131 <createListenerAndHandshake()> 132 <grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>(input, self._dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n"> 133 <@finally()> 134}<\n> 135!> 136>> 137 138ctorForProfilingRootGrammar() ::= << 139<! bug: can't use <@super.members()> cut-n-paste instead !> 140public <name>(<inputStreamType> input) { 141 this(input, new Profiler(null), new RecognizerSharedState()); 142} 143public <name>(<inputStreamType> input, DebugEventListener self.dbg, RecognizerSharedState state) { 144 super(input, self.dbg, state); 145 Profiler p = (Profiler)self.dbg; 146 p.setParser(this); 147 <parserCtorBody()> 148 <grammar.directDelegates: 149 {g|<g:delegateName()> = new <g.recognizerName>(input, self.dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n"> 150 <@finally()> 151} 152<\n> 153>> 154 155/** Basically we don't want to set any dbg listeners are root will have it. */ 156ctorForDelegateGrammar() ::= << 157<! 158public <name>(<inputStreamType> input, DebugEventListener self.dbg, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>) { 159 super(input, dbg, state); 160 <parserCtorBody()> 161 <grammar.directDelegates: 162 {g|<g:delegateName()> = new <g.recognizerName>(input, this, this.state<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n"> 163}<\n> 164!> 165>> 166 167ctorForPredefinedListener() ::= << 168<! 169public <name>(<inputStreamType> input, DebugEventListener dbg) { 170 <@superClassRef>super(input, dbg, new RecognizerSharedState());<@end> 171<if(profile)> 172 Profiler p = (Profiler)dbg; 173 p.setParser(this); 174<endif> 175 <parserCtorBody()> 176 <grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>(input, self._dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n"> 177 <@finally()> 178}<\n> 179!> 180>> 181 182@genericParser.superClassName() ::= "Debug<@super.superClassName()>" 183 184@rule.body() ::= << 185try: 186 self._dbg.enterRule(self.getGrammarFileName(), "<ruleName>") 187 if self.getRuleLevel() == 0: 188 self._dbg.commence(); 189 self.incRuleLevel() 190 <! ST uses zero-based columns, we want one-base !> 191 self._dbg.location(<ruleDescriptor.tree.line>, <ruleDescriptor.tree.charPositionInLine>+1) 192 193 <@super.body()> 194 195 self._dbg.location(<ruleDescriptor.EORNode.line>, <ruleDescriptor.EORNode.charPositionInLine>+1) 196finally: 197 self._dbg.exitRule(self.getGrammarFileName(), "<ruleName>") 198 self.decRuleLevel() 199 if self.getRuleLevel() == 0: 200 self._dbg.terminate() 201 202>> 203 204@synpred.start() ::= "self._dbg.beginBacktrack(self._state.backtracking)" 205 206@synpred.stop() ::= "self._dbg.endBacktrack(self._state.backtracking, success)" 207 208// Common debug event triggers used by region overrides below 209 210enterSubRule() ::= 211 "try { self._dbg.enterSubRule(<decisionNumber>);<\n>" 212 213exitSubRule() ::= 214 "} finally {self._dbg.exitSubRule(<decisionNumber>);}<\n>" 215 216enterDecision() ::= 217 "try { self._dbg.enterDecision(<decisionNumber>);<\n>" 218 219exitDecision() ::= 220 "} finally {self._dbg.exitDecision(<decisionNumber>);}<\n>" 221 222enterAlt(n) ::= "self._dbg.enterAlt(<n>)<\n>" 223 224// Region overrides that tell various constructs to add debugging triggers 225 226@block.body() ::= << 227try: 228 self._dbg.enterSubRule(<decisionNumber>) 229 <@super.body()> 230finally: 231 self._dbg.exitSubRule(<decisionNumber>) 232>> 233 234@blockBody.decision() ::= << 235try: 236 self._dbg.enterDecision( 237 <decisionNumber>, self.decisionCanBacktrack[<decisionNumber>]) 238 <@super.decision()> 239finally: 240 self._dbg.exitDecision(<decisionNumber>) 241>> 242 243@ruleBlock.decision() ::= << 244try: 245 self._dbg.enterDecision( 246 <decisionNumber>, self.decisionCanBacktrack[<decisionNumber>]) 247 <@super.decision()> 248finally: 249 self._dbg.exitDecision(<decisionNumber>) 250>> 251 252@ruleBlockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>" 253 254@blockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>" 255 256@positiveClosureBlock.loopBody() ::= << 257try: 258 self._dbg.enterSubRule(<decisionNumber>) 259 <@super.loopBody()> 260finally: 261 self._dbg.exitSubRule(<decisionNumber>)<\n> 262>> 263 264@positiveClosureBlockLoop.decisionBody() ::= << 265try: 266 self._dbg.enterDecision( 267 <decisionNumber>, self.decisionCanBacktrack[<decisionNumber>]) 268 <@super.decisionBody()> 269finally: 270 self._dbg.exitDecision(<decisionNumber>) 271>> 272 273@positiveClosureBlockLoop.earlyExitException() ::= 274 "self._dbg.recognitionException(eee)<\n>" 275 276@closureBlock.loopBody() ::= << 277try: 278 self._dbg.enterSubRule(<decisionNumber>) 279 <@super.loopBody()> 280finally: 281 self._dbg.exitSubRule(<decisionNumber>)<\n> 282>> 283 284@closureBlockLoop.decisionBody() ::= << 285try: 286 self._dbg.enterDecision( 287 <decisionNumber>, self.decisionCanBacktrack[<decisionNumber>]) 288 <@super.decisionBody()> 289finally: 290 self._dbg.exitDecision(<decisionNumber>) 291>> 292 293@altSwitchCase.prealt() ::= "<enterAlt(altNum)>" 294 295@element.prematch() ::= 296 "self._dbg.location(<e.line>, <e.pos>)" 297 298@matchSet.mismatchedSetException() ::= 299 "self._dbg.recognitionException(mse)" 300 301@dfaState.noViableAltException() ::= "self._dbg.recognitionException(nvae)" 302 303@dfaStateSwitch.noViableAltException() ::= "self._dbg.recognitionException(nvae)" 304 305dfaDecision(decisionNumber,description) ::= << 306try: 307 self.isCyclicDecision = True 308 <super.dfaDecision(...)> 309 310except NoViableAltException, nvae: 311 self._dbg.recognitionException(nvae) 312 raise 313 314>> 315 316@cyclicDFA.errorMethod() ::= << 317def error(self, nvae): 318 self._dbg.recognitionException(nvae) 319 320>> 321 322/** Force predicate validation to trigger an event */ 323evalPredicate(pred,description) ::= << 324self.evalPredicate(<pred>,"<description>") 325>> 326