1 /* 2 [The "BSD licence"] 3 Copyright (c) 2005-2007 Kunle Odutola 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 13 the documentation and/or other materials provided with the 14 distribution. 15 3. The name of the author may not be used to endorse or promote products 16 derived from this software without specific prior WRITTEN permission. 17 4. Unless explicitly state otherwise, any contribution intentionally 18 submitted for inclusion in this work to the copyright owner or licensor 19 shall be under the terms and conditions of this license, without any 20 additional terms or conditions. 21 22 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 35 namespace Antlr.Runtime.Debug 36 { 37 using System; 38 using Antlr.Runtime; 39 using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor; 40 using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream; 41 42 43 /// <summary> 44 /// The default tracer mimics the traceParser behavior of ANTLR 2.x. 45 /// This listens for debugging events from the parser and implies 46 /// that you cannot debug and trace at the same time. 47 /// </summary> 48 public class Tracer : BlankDebugEventListener 49 { 50 public IIntStream input; 51 protected int level = 0; 52 Tracer(IIntStream input)53 public Tracer(IIntStream input) 54 { 55 this.input = input; 56 } 57 EnterRule(string grammarFileName, string ruleName)58 override public void EnterRule(string grammarFileName, string ruleName) 59 { 60 for (int i = 1; i <= level; i++) 61 { 62 Console.Out.Write(" "); 63 } 64 Console.Out.WriteLine("> " + grammarFileName + " " + ruleName + " lookahead(1)=" + GetInputSymbol(1)); 65 level++; 66 } 67 ExitRule(string grammarFileName, string ruleName)68 override public void ExitRule(string grammarFileName, string ruleName) 69 { 70 level--; 71 for (int i = 1; i <= level; i++) 72 { 73 Console.Out.Write(" "); 74 } 75 Console.Out.WriteLine("< " + grammarFileName + " " + ruleName + " lookahead(1)=" + GetInputSymbol(1)); 76 } 77 GetInputSymbol(int k)78 public virtual object GetInputSymbol(int k) 79 { 80 if (input is ITokenStream) 81 { 82 return ((ITokenStream) input).LT(k); 83 } 84 return (char) input.LA(k); 85 } 86 } 87 }