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 ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor;
38 
39     /** <summary>Print out (most of) the events... Useful for debugging, testing...</summary> */
40     public class TraceDebugEventListener : BlankDebugEventListener {
41         ITreeAdaptor adaptor;
42 
TraceDebugEventListener(ITreeAdaptor adaptor)43         public TraceDebugEventListener(ITreeAdaptor adaptor) {
44             this.adaptor = adaptor;
45         }
46 
EnterRule(string ruleName)47         public void EnterRule(string ruleName) {
48             Console.Out.WriteLine("enterRule " + ruleName);
49         }
ExitRule(string ruleName)50         public void ExitRule(string ruleName) {
51             Console.Out.WriteLine("exitRule " + ruleName);
52         }
EnterSubRule(int decisionNumber)53         public override void EnterSubRule(int decisionNumber) {
54             Console.Out.WriteLine("enterSubRule");
55         }
ExitSubRule(int decisionNumber)56         public override void ExitSubRule(int decisionNumber) {
57             Console.Out.WriteLine("exitSubRule");
58         }
Location(int line, int pos)59         public override void Location(int line, int pos) {
60             Console.Out.WriteLine("location " + line + ":" + pos);
61         }
62 
63         #region Tree parsing stuff
64 
ConsumeNode(object t)65         public override void ConsumeNode(object t) {
66             int ID = adaptor.GetUniqueID(t);
67             string text = adaptor.GetText(t);
68             int type = adaptor.GetType(t);
69             Console.Out.WriteLine("consumeNode " + ID + " " + text + " " + type);
70         }
71 
LT(int i, object t)72         public override void LT(int i, object t) {
73             int ID = adaptor.GetUniqueID(t);
74             string text = adaptor.GetText(t);
75             int type = adaptor.GetType(t);
76             Console.Out.WriteLine("LT " + i + " " + ID + " " + text + " " + type);
77         }
78 
79         #endregion
80 
81 
82         #region AST stuff
83 
NilNode(object t)84         public override void NilNode(object t) {
85             Console.Out.WriteLine("nilNode " + adaptor.GetUniqueID(t));
86         }
87 
CreateNode(object t)88         public override void CreateNode(object t) {
89             int ID = adaptor.GetUniqueID(t);
90             string text = adaptor.GetText(t);
91             int type = adaptor.GetType(t);
92             Console.Out.WriteLine("create " + ID + ": " + text + ", " + type);
93         }
94 
CreateNode(object node, IToken token)95         public override void CreateNode(object node, IToken token) {
96             int ID = adaptor.GetUniqueID(node);
97             string text = adaptor.GetText(node);
98             int tokenIndex = token.TokenIndex;
99             Console.Out.WriteLine("create " + ID + ": " + tokenIndex);
100         }
101 
BecomeRoot(object newRoot, object oldRoot)102         public override void BecomeRoot(object newRoot, object oldRoot) {
103             Console.Out.WriteLine("becomeRoot " + adaptor.GetUniqueID(newRoot) + ", " +
104                                adaptor.GetUniqueID(oldRoot));
105         }
106 
AddChild(object root, object child)107         public override void AddChild(object root, object child) {
108             Console.Out.WriteLine("addChild " + adaptor.GetUniqueID(root) + ", " +
109                                adaptor.GetUniqueID(child));
110         }
111 
SetTokenBoundaries(object t, int tokenStartIndex, int tokenStopIndex)112         public override void SetTokenBoundaries(object t, int tokenStartIndex, int tokenStopIndex) {
113             Console.Out.WriteLine("setTokenBoundaries " + adaptor.GetUniqueID(t) + ", " +
114                                tokenStartIndex + ", " + tokenStopIndex);
115         }
116 
117         #endregion
118     }
119 }
120