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