aboutsummaryrefslogtreecommitdiff
path: root/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/DebugTreeAdaptor.cs
blob: 1399b53eab818ee5f52605095d0201cbb1934320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * [The "BSD licence"]
 * Copyright (c) 2011 Terence Parr
 * All rights reserved.
 *
 * Conversion to C#:
 * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

namespace Antlr.Runtime.Debug
{
    using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor;

    /** <summary>
     *  A TreeAdaptor proxy that fires debugging events to a DebugEventListener
     *  delegate and uses the TreeAdaptor delegate to do the actual work.  All
     *  AST events are triggered by this adaptor; no code gen changes are needed
     *  in generated rules.  Debugging events are triggered *after* invoking
     *  tree adaptor routines.
     *  </summary>
     *
     *  <remarks>
     *  Trees created with actions in rewrite actions like "-&gt; ^(ADD {foo} {bar})"
     *  cannot be tracked as they might not use the adaptor to create foo, bar.
     *  The debug listener has to deal with tree node IDs for which it did
     *  not see a createNode event.  A single &lt;unknown&gt; node is sufficient even
     *  if it represents a whole tree.
     *  </remarks>
     */
    public class DebugTreeAdaptor : ITreeAdaptor
    {
        protected IDebugEventListener dbg;
        protected ITreeAdaptor adaptor;

        public DebugTreeAdaptor( IDebugEventListener dbg, ITreeAdaptor adaptor )
        {
            this.dbg = dbg;
            this.adaptor = adaptor;
        }

        public virtual object Create( IToken payload )
        {
            if ( payload.TokenIndex < 0 )
            {
                // could be token conjured up during error recovery
                return Create( payload.Type, payload.Text );
            }
            object node = adaptor.Create( payload );
            dbg.CreateNode( node, payload );
            return node;
        }

        public virtual object ErrorNode( ITokenStream input, IToken start, IToken stop,
                                RecognitionException e )
        {
            object node = adaptor.ErrorNode( input, start, stop, e );
            if ( node != null )
            {
                dbg.ErrorNode( node );
            }
            return node;
        }

        public virtual object DupTree( object tree )
        {
            object t = adaptor.DupTree( tree );
            // walk the tree and emit create and add child events
            // to simulate what dupTree has done. dupTree does not call this debug
            // adapter so I must simulate.
            SimulateTreeConstruction( t );
            return t;
        }

        /** <summary>^(A B C): emit create A, create B, add child, ...</summary> */
        protected virtual void SimulateTreeConstruction( object t )
        {
            dbg.CreateNode( t );
            int n = adaptor.GetChildCount( t );
            for ( int i = 0; i < n; i++ )
            {
                object child = adaptor.GetChild( t, i );
                SimulateTreeConstruction( child );
                dbg.AddChild( t, child );
            }
        }

        public virtual object DupNode( object treeNode )
        {
            object d = adaptor.DupNode( treeNode );
            dbg.CreateNode( d );
            return d;
        }

        public object DupNode(int type, object treeNode)
        {
            object d = adaptor.DupNode(type, treeNode);
            dbg.CreateNode(d);
            return d;
        }

        public object DupNode(object treeNode, string text)
        {
            object d = adaptor.DupNode(treeNode, text);
            dbg.CreateNode(d);
            return d;
        }

        public object DupNode(int type, object treeNode, string text)
        {
            object d = adaptor.DupNode(type, treeNode, text);
            dbg.CreateNode(d);
            return d;
        }

        public virtual object Nil()
        {
            object node = adaptor.Nil();
            dbg.NilNode( node );
            return node;
        }

        public virtual bool IsNil( object tree )
        {
            return adaptor.IsNil( tree );
        }

        public virtual void AddChild( object t, object child )
        {
            if ( t == null || child == null )
            {
                return;
            }
            adaptor.AddChild( t, child );
            dbg.AddChild( t, child );
        }

        public virtual object BecomeRoot( object newRoot, object oldRoot )
        {
            object n = adaptor.BecomeRoot( newRoot, oldRoot );
            dbg.BecomeRoot( newRoot, oldRoot );
            return n;
        }

        public virtual object RulePostProcessing( object root )
        {
            return adaptor.RulePostProcessing( root );
        }

        public virtual void AddChild( object t, IToken child )
        {
            object n = this.Create( child );
            this.AddChild( t, n );
        }

        public virtual object BecomeRoot( IToken newRoot, object oldRoot )
        {
            object n = this.Create( newRoot );
            adaptor.BecomeRoot( n, oldRoot );
            dbg.BecomeRoot( newRoot, oldRoot );
            return n;
        }

        public virtual object Create( int tokenType, IToken fromToken )
        {
            object node = adaptor.Create( tokenType, fromToken );
            dbg.CreateNode( node );
            return node;
        }

        public virtual object Create( int tokenType, IToken fromToken, string text )
        {
            object node = adaptor.Create( tokenType, fromToken, text );
            dbg.CreateNode( node );
            return node;
        }

        public virtual object Create( int tokenType, string text )
        {
            object node = adaptor.Create( tokenType, text );
            dbg.CreateNode( node );
            return node;
        }

        public object Create(IToken fromToken, string text)
        {
            object node = adaptor.Create(fromToken, text);
            dbg.CreateNode(node);
            return node;
        }

        public virtual int GetType( object t )
        {
            return adaptor.GetType( t );
        }

        public virtual void SetType( object t, int type )
        {
            adaptor.SetType( t, type );
        }

        public virtual string GetText( object t )
        {
            return adaptor.GetText( t );
        }

        public virtual void SetText( object t, string text )
        {
            adaptor.SetText( t, text );
        }

        public virtual IToken GetToken( object t )
        {
            return adaptor.GetToken( t );
        }

        public virtual void SetTokenBoundaries( object t, IToken startToken, IToken stopToken )
        {
            adaptor.SetTokenBoundaries( t, startToken, stopToken );
            if ( t != null && startToken != null && stopToken != null )
            {
                dbg.SetTokenBoundaries(
                    t, startToken.TokenIndex,
                    stopToken.TokenIndex );
            }
        }

        public virtual int GetTokenStartIndex( object t )
        {
            return adaptor.GetTokenStartIndex( t );
        }

        public virtual int GetTokenStopIndex( object t )
        {
            return adaptor.GetTokenStopIndex( t );
        }

        public virtual object GetChild( object t, int i )
        {
            return adaptor.GetChild( t, i );
        }

        public virtual void SetChild( object t, int i, object child )
        {
            adaptor.SetChild( t, i, child );
        }

        public virtual object DeleteChild( object t, int i )
        {
            return DeleteChild( t, i );
        }

        public virtual int GetChildCount( object t )
        {
            return adaptor.GetChildCount( t );
        }

        public virtual int GetUniqueID( object node )
        {
            return adaptor.GetUniqueID( node );
        }

        public virtual object GetParent( object t )
        {
            return adaptor.GetParent( t );
        }

        public virtual int GetChildIndex( object t )
        {
            return adaptor.GetChildIndex( t );
        }

        public virtual void SetParent( object t, object parent )
        {
            adaptor.SetParent( t, parent );
        }

        public virtual void SetChildIndex( object t, int index )
        {
            adaptor.SetChildIndex( t, index );
        }

        public virtual void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t )
        {
            adaptor.ReplaceChildren( parent, startChildIndex, stopChildIndex, t );
        }

        #region support

        public virtual IDebugEventListener GetDebugListener()
        {
            return dbg;
        }

        public virtual void SetDebugListener( IDebugEventListener dbg )
        {
            this.dbg = dbg;
        }

        public virtual ITreeAdaptor GetTreeAdaptor()
        {
            return adaptor;
        }

        #endregion
    }
}