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 using Antlr.Runtime.JavaExtensions; 35 36 using Console = System.Console; 37 using IOException = System.IO.IOException; 38 39 public class DebugParser : Parser { 40 /** <summary>Who to notify when events in the parser occur.</summary> */ 41 public IDebugEventListener dbg = null; 42 43 /** <summary> 44 * Used to differentiate between fixed lookahead and cyclic DFA decisions 45 * while profiling. 46 * </summary> 47 */ 48 public bool isCyclicDecision = false; 49 50 /** <summary> 51 * Create a normal parser except wrap the token stream in a debug 52 * proxy that fires consume events. 53 * </summary> 54 */ DebugParser(ITokenStream input, IDebugEventListener dbg, RecognizerSharedState state)55 public DebugParser(ITokenStream input, IDebugEventListener dbg, RecognizerSharedState state) 56 : base(input is DebugTokenStream ? input : new DebugTokenStream(input, dbg), state) { 57 SetDebugListener(dbg); 58 } 59 DebugParser(ITokenStream input, RecognizerSharedState state)60 public DebugParser(ITokenStream input, RecognizerSharedState state) 61 : base(input is DebugTokenStream ? input : new DebugTokenStream(input, null), state) { 62 } 63 DebugParser(ITokenStream input, IDebugEventListener dbg)64 public DebugParser(ITokenStream input, IDebugEventListener dbg) 65 : this(input is DebugTokenStream ? input : new DebugTokenStream(input, dbg), dbg, null) { 66 } 67 68 public override IDebugEventListener DebugListener { 69 get { 70 return dbg; 71 } 72 } 73 74 /** <summary> 75 * Provide a new debug event listener for this parser. Notify the 76 * input stream too that it should send events to this listener. 77 * </summary> 78 */ SetDebugListener(IDebugEventListener value)79 public virtual void SetDebugListener(IDebugEventListener value) { 80 DebugTokenStream debugTokenStream = input as DebugTokenStream; 81 if (debugTokenStream != null) 82 debugTokenStream.DebugListener = value; 83 84 dbg = value; 85 } 86 ReportError(IOException e)87 public virtual void ReportError(IOException e) { 88 Console.Error.WriteLine(e); 89 ExceptionExtensions.PrintStackTrace(e, Console.Error); 90 } 91 BeginResync()92 public override void BeginResync() { 93 dbg.BeginResync(); 94 base.BeginResync(); 95 } 96 EndResync()97 public override void EndResync() { 98 dbg.EndResync(); 99 base.EndResync(); 100 } 101 BeginBacktrack(int level)102 public virtual void BeginBacktrack(int level) { 103 dbg.BeginBacktrack(level); 104 } 105 EndBacktrack(int level, bool successful)106 public virtual void EndBacktrack(int level, bool successful) { 107 dbg.EndBacktrack(level, successful); 108 } 109 ReportError(RecognitionException e)110 public override void ReportError(RecognitionException e) { 111 base.ReportError(e); 112 dbg.RecognitionException(e); 113 } 114 } 115 } 116