1/* 2 * [The "BSD license"] 3 * Copyright (c) 2007-2008 Johannes Luber 4 * Copyright (c) 2005-2007 Kunle Odutola 5 * Copyright (c) 2011 Sam Harwell 6 * Copyright (c) 2011 Terence Parr 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31/** Template overrides to add debugging to normal Java output; 32 * If ASTs are built, then you'll also get ASTDbg.stg loaded. 33 */ 34 35@outputFile.debugPreprocessor() ::= "#define ANTLR_DEBUG" 36 37@outputFile.imports() ::= << 38<@super.imports()> 39using Antlr.Runtime.Debug; 40using IOException = System.IO.IOException; 41>> 42 43@genericParser.members() ::= << 44<if(grammar.grammarIsRoot)> 45public static readonly string[] ruleNames = 46 new string[] 47 { 48 "invalidRule", <grammar.allImportedRules:{rST | "<rST.name>"}; wrap="\n ", separator=", "> 49 };<\n> 50<endif> 51<if(grammar.grammarIsRoot)><! grammar imports other grammar(s) !> 52 int ruleLevel = 0; 53 public virtual int RuleLevel { get { return ruleLevel; } } 54 public virtual void IncRuleLevel() { ruleLevel++; } 55 public virtual void DecRuleLevel() { ruleLevel--; } 56<if(profile)> 57 <ctorForProfilingRootGrammar()> 58<else> 59 <ctorForRootGrammar()> 60<endif> 61<ctorForPredefinedListener()> 62<else><! imported grammar !> 63 public int RuleLevel { get { return <grammar.delegators:{g| <g:delegateName()>}>.RuleLevel; } } 64 public void IncRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.IncRuleLevel(); } 65 public void DecRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.DecRuleLevel(); } 66 <ctorForDelegateGrammar()> 67<endif> 68<if(profile)> 69public override bool AlreadyParsedRule( IIntStream input, int ruleIndex ) 70{ 71 int stopIndex = GetRuleMemoization(ruleIndex, input.Index); 72 ((Profiler)dbg).ExamineRuleMemoization(input, ruleIndex, stopIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]); 73 return base.AlreadyParsedRule(input, ruleIndex); 74}<\n> 75public override void Memoize( IIntStream input, int ruleIndex, int ruleStartIndex ) 76{ 77 ((Profiler)dbg).Memoize(input, ruleIndex, ruleStartIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]); 78 base.Memoize(input, ruleIndex, ruleStartIndex); 79}<\n> 80<endif> 81protected virtual bool EvalPredicate( bool result, string predicate ) 82{ 83 dbg.SemanticPredicate( result, predicate ); 84 return result; 85}<\n> 86>> 87 88ctorForRootGrammar() ::= << 89<! bug: can't use <@super.members()> cut-n-paste instead !> 90<! Same except we add port number and profile stuff if root grammar !> 91<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input ) 92 : this( input, DebugEventSocketProxy.DefaultDebuggerPort, new RecognizerSharedState() ) 93{ 94} 95<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input, int port, RecognizerSharedState state ) 96 : base( input, state ) 97{ 98 <parserCtorBody()> 99 <createListenerAndHandshake()> 100 <grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>( input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}> );}; separator="\n"> 101 <@finally()> 102}<\n> 103>> 104 105ctorForProfilingRootGrammar() ::= << 106<! bug: can't use <@super.members()> cut-n-paste instead !> 107<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input ) 108 : this( input, new Profiler(null), new RecognizerSharedState() ) 109{ 110} 111<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input, IDebugEventListener dbg, RecognizerSharedState state ) 112 : base( input, dbg, state ) 113{ 114 Profiler p = (Profiler)dbg; 115 p.setParser(this); 116 <parserCtorBody()> 117 <grammar.directDelegates: 118 {g|<g:delegateName()> = new <g.recognizerName>( input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}> );}; separator="\n"> 119 <@finally()> 120} 121<\n> 122>> 123 124/** Basically we don't want to set any dbg listeners are root will have it. */ 125ctorForDelegateGrammar() ::= << 126<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input, IDebugEventListener dbg, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}> ) 127 : base( input, dbg, state ) 128{ 129 <parserCtorBody()> 130 <grammar.directDelegates: 131 {g|<g:delegateName()> = new <g.recognizerName>( input, this, this.state<grammar.delegators:{g|, <g:delegateName()>}> );}; separator="\n"> 132}<\n> 133>> 134 135ctorForPredefinedListener() ::= << 136<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input, IDebugEventListener dbg ) 137 <@superClassRef>: base( input, dbg, new RecognizerSharedState() )<@end> 138{ 139<if(profile)> 140 Profiler p = (Profiler)dbg; 141 p.setParser(this); 142<endif> 143 <parserCtorBody()> 144 <grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>(input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n"> 145 <@finally()> 146}<\n> 147>> 148 149createListenerAndHandshake() ::= << 150<if(TREE_PARSER)> 151DebugEventSocketProxy proxy = new DebugEventSocketProxy( this, port, input.TreeAdaptor );<\n> 152<else> 153DebugEventSocketProxy proxy = new DebugEventSocketProxy( this, port, null );<\n> 154<endif> 155DebugListener = proxy; 156try 157{ 158 proxy.Handshake(); 159} 160catch ( IOException ioe ) 161{ 162 ReportError( ioe ); 163} 164>> 165 166@genericParser.superClassName() ::= "Debug<@super.superClassName()>" 167 168/* 169 * Many of the following rules were merged into CSharp2.stg. 170 */ 171 172@rule.preamble() ::= << 173if (RuleLevel == 0) 174 DebugListener.Commence(); 175IncRuleLevel(); 176>> 177//@rule.preamble() ::= << 178//try 179//{ 180// dbg.EnterRule( GrammarFileName, "<ruleName>" ); 181// if ( RuleLevel == 0 ) 182// { 183// dbg.Commence(); 184// } 185// IncRuleLevel(); 186// dbg.Location( <ruleDescriptor.tree.line>, <ruleDescriptor.tree.charPositionInLine> );<\n> 187//>> 188 189@rule.postamble() ::= << 190DecRuleLevel(); 191if (RuleLevel == 0) 192 DebugListener.Terminate(); 193>> 194//@rule.postamble() ::= << 195//dbg.Location(<ruleDescriptor.EORNode.line>, <ruleDescriptor.EORNode.charPositionInLine>);<\n> 196//} 197//finally 198//{ 199// dbg.ExitRule( GrammarFileName, "<ruleName>" ); 200// DecRuleLevel(); 201// if ( RuleLevel == 0 ) 202// { 203// dbg.Terminate(); 204// } 205//}<\n> 206//>> 207 208//@insertSynpreds.start() ::= "dbg.BeginBacktrack( state.backtracking );" 209//@insertSynpreds.stop() ::= "dbg.EndBacktrack( state.backtracking, success );" 210 211// Common debug event triggers used by region overrides below 212 213//enterSubRule() ::= << 214//try 215//{ 216// dbg.EnterSubRule( <decisionNumber> );<\n> 217//>> 218 219//exitSubRule() ::= << 220//} 221//finally 222//{ 223// dbg.ExitSubRule( <decisionNumber> ); 224//}<\n> 225//>> 226 227//enterDecision() ::= << 228//try 229//{ 230// dbg.EnterDecision( <decisionNumber> );<\n> 231//>> 232 233//exitDecision() ::= << 234//} 235//finally 236//{ 237// dbg.ExitDecision( <decisionNumber> ); 238//}<\n> 239//>> 240 241//enterAlt(n) ::= "dbg.EnterAlt( <n> );<\n>" 242 243// Region overrides that tell various constructs to add debugging triggers 244 245//@block.predecision() ::= "<enterSubRule()><enterDecision()>" 246 247//@block.postdecision() ::= "<exitDecision()>" 248 249//@block.postbranch() ::= "<exitSubRule()>" 250 251//@ruleBlock.predecision() ::= "<enterDecision()>" 252 253//@ruleBlock.postdecision() ::= "<exitDecision()>" 254 255//@ruleBlockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>" 256 257//@blockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>" 258 259//@positiveClosureBlock.preloop() ::= "<enterSubRule()>" 260 261//@positiveClosureBlock.postloop() ::= "<exitSubRule()>" 262 263//@positiveClosureBlock.predecision() ::= "<enterDecision()>" 264 265//@positiveClosureBlock.postdecision() ::= "<exitDecision()>" 266 267//@positiveClosureBlock.earlyExitException() ::= 268// "dbg.RecognitionException( eee<decisionNumber> );<\n>" 269 270//@closureBlock.preloop() ::= "<enterSubRule()>" 271 272//@closureBlock.postloop() ::= "<exitSubRule()>" 273 274//@closureBlock.predecision() ::= "<enterDecision()>" 275 276//@closureBlock.postdecision() ::= "<exitDecision()>" 277 278//@altSwitchCase.prealt() ::= "<enterAlt(n=i)>" 279 280//@element.prematch() ::= 281// "dbg.Location( <it.line>, <it.pos> );" 282 283//@matchSet.mismatchedSetException() ::= 284// "dbg.RecognitionException( mse );" 285 286//@dfaState.noViableAltException() ::= "dbg.RecognitionException( nvae );" 287 288//@dfaStateSwitch.noViableAltException() ::= "dbg.RecognitionException( nvae );" 289 290//dfaDecision(decisionNumber,description) ::= << 291//try 292//{ 293// isCyclicDecision = true; 294// <super.dfaDecision(...)> 295//} 296//catch ( NoViableAltException nvae ) 297//{ 298// dbg.RecognitionException( nvae ); 299// throw nvae; 300//} 301//>> 302 303//@cyclicDFA.errorMethod() ::= << 304//public override void Error( NoViableAltException nvae ) 305//{ 306// ((DebugParser)recognizer).dbg.RecognitionException( nvae ); 307//} 308//>> 309 310/** Force predicate validation to trigger an event */ 311evalPredicate(pred,description) ::= << 312EvalPredicate(<pred>, "<description>") 313>> 314