1 /* 2 * [The "BSD licence"] 3 * Copyright (c) 2005-2008 Terence Parr 4 * All rights reserved. 5 * 6 * Conversion to C#: 7 * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 namespace Antlr.Runtime.Debug 34 { 35 using Antlr.Runtime.JavaExtensions; 36 37 using Console = System.Console; 38 using IOException = System.IO.IOException; 39 using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; 40 using TreeParser = Antlr.Runtime.Tree.TreeParser; 41 42 public class DebugTreeParser : TreeParser 43 { 44 /** <summary>Who to notify when events in the parser occur.</summary> */ 45 public IDebugEventListener dbg = null; 46 47 /** <summary> 48 * Used to differentiate between fixed lookahead and cyclic DFA decisions 49 * while profiling. 50 * </summary> 51 */ 52 public bool isCyclicDecision = false; 53 54 /** <summary> 55 * Create a normal parser except wrap the token stream in a debug 56 * proxy that fires consume events. 57 * </summary> 58 */ DebugTreeParser( ITreeNodeStream input, IDebugEventListener dbg, RecognizerSharedState state )59 public DebugTreeParser( ITreeNodeStream input, IDebugEventListener dbg, RecognizerSharedState state ) 60 : base( input is DebugTreeNodeStream ? input : new DebugTreeNodeStream( input, dbg ), state ) 61 { 62 SetDebugListener(dbg); 63 } 64 DebugTreeParser( ITreeNodeStream input, RecognizerSharedState state )65 public DebugTreeParser( ITreeNodeStream input, RecognizerSharedState state ) 66 : base( input is DebugTreeNodeStream ? input : new DebugTreeNodeStream( input, null ), state ) 67 { 68 } 69 DebugTreeParser( ITreeNodeStream input, IDebugEventListener dbg )70 public DebugTreeParser( ITreeNodeStream input, IDebugEventListener dbg ) 71 : this( input is DebugTreeNodeStream ? input : new DebugTreeNodeStream( input, dbg ), dbg, null ) 72 { 73 } 74 75 public override IDebugEventListener DebugListener 76 { 77 get 78 { 79 return dbg; 80 } 81 } 82 83 /** <summary> 84 * Provide a new debug event listener for this parser. Notify the 85 * input stream too that it should send events to this listener. 86 * </summary> 87 */ SetDebugListener(IDebugEventListener value)88 public virtual void SetDebugListener(IDebugEventListener value) 89 { 90 if (input is DebugTreeNodeStream) 91 ((DebugTreeNodeStream)input).DebugListener = value; 92 93 this.dbg = value; 94 } 95 ReportError( IOException e )96 public virtual void ReportError( IOException e ) 97 { 98 Console.Error.WriteLine( e ); 99 ExceptionExtensions.PrintStackTrace( e, Console.Error ); 100 } 101 ReportError( RecognitionException e )102 public override void ReportError( RecognitionException e ) 103 { 104 dbg.RecognitionException( e ); 105 } 106 GetMissingSymbol( IIntStream input, RecognitionException e, int expectedTokenType, BitSet follow )107 protected override object GetMissingSymbol( IIntStream input, 108 RecognitionException e, 109 int expectedTokenType, 110 BitSet follow ) 111 { 112 object o = base.GetMissingSymbol( input, e, expectedTokenType, follow ); 113 dbg.ConsumeNode( o ); 114 return o; 115 } 116 BeginResync()117 public override void BeginResync() 118 { 119 dbg.BeginResync(); 120 } 121 EndResync()122 public override void EndResync() 123 { 124 dbg.EndResync(); 125 } 126 BeginBacktrack( int level )127 public virtual void BeginBacktrack( int level ) 128 { 129 dbg.BeginBacktrack( level ); 130 } 131 EndBacktrack( int level, bool successful )132 public virtual void EndBacktrack( int level, bool successful ) 133 { 134 dbg.EndBacktrack( level, successful ); 135 } 136 } 137 } 138