aboutsummaryrefslogtreecommitdiff
path: root/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BufferedTreeNodeStream.cs
blob: 3b5a01ecc5b60ccbafc7e5e091fb54ad7c0a5f4b (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
 * [The "BSD licence"]
 * Copyright (c) 2005-2008 Terence Parr
 * All rights reserved.
 *
 * Conversion to C#:
 * Copyright (c) 2008-2009 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.Tree
{
    using System.Collections.Generic;

    using Console = System.Console;
    using IList = System.Collections.IList;
    using InvalidOperationException = System.InvalidOperationException;
    using StringBuilder = System.Text.StringBuilder;

    /** <summary>A buffered stream of tree nodes.  Nodes can be from a tree of ANY kind.</summary>
     *
     *  This node stream sucks all nodes out of the tree specified in
     *  the constructor during construction and makes pointers into
     *  the tree using an array of Object pointers. The stream necessarily
     *  includes pointers to DOWN and UP and EOF nodes.
     *
     *  This stream knows how to mark/release for backtracking.
     *
     *  This stream is most suitable for tree interpreters that need to
     *  jump around a lot or for tree parsers requiring speed (at cost of memory).
     *  There is some duplicated functionality here with UnBufferedTreeNodeStream
     *  but just in bookkeeping, not tree walking etc...
     *
     *  TARGET DEVELOPERS:
     *
     *  This is the old CommonTreeNodeStream that buffered up entire node stream.
     *  No need to implement really as new CommonTreeNodeStream is much better
     *  and covers what we need.
     *
     *  @see CommonTreeNodeStream
     */
    public class BufferedTreeNodeStream : ITreeNodeStream, ITokenStreamInformation
    {
        public const int DEFAULT_INITIAL_BUFFER_SIZE = 100;
        public const int INITIAL_CALL_STACK_SIZE = 10;

        protected sealed class StreamIterator : IEnumerator<object>
        {
            BufferedTreeNodeStream _outer;
            int _index;

            public StreamIterator( BufferedTreeNodeStream outer )
            {
                _outer = outer;
                _index = -1;
            }

            #region IEnumerator<object> Members

            public object Current
            {
                get
                {
                    if ( _index < _outer.nodes.Count )
                        return _outer.nodes[_index];

                    return _outer.eof;
                }
            }

            #endregion

            #region IDisposable Members

            public void Dispose()
            {
            }

            #endregion

            #region IEnumerator Members

            public bool MoveNext()
            {
                if ( _index < _outer.nodes.Count )
                    _index++;

                return _index < _outer.nodes.Count;
            }

            public void Reset()
            {
                _index = -1;
            }

            #endregion
        }

        // all these navigation nodes are shared and hence they
        // cannot contain any line/column info

        protected object down;
        protected object up;
        protected object eof;

        /** <summary>The complete mapping from stream index to tree node.
         *  This buffer includes pointers to DOWN, UP, and EOF nodes.
         *  It is built upon ctor invocation.  The elements are type
         *  Object as we don't what the trees look like.</summary>
         *
         *  Load upon first need of the buffer so we can set token types
         *  of interest for reverseIndexing.  Slows us down a wee bit to
         *  do all of the if p==-1 testing everywhere though.
         */
        protected IList nodes;

        /** <summary>Pull nodes from which tree?</summary> */
        protected object root;

        /** <summary>IF this tree (root) was created from a token stream, track it.</summary> */
        protected ITokenStream tokens;

        /** <summary>What tree adaptor was used to build these trees</summary> */
        ITreeAdaptor adaptor;

        /** <summary>Reuse same DOWN, UP navigation nodes unless this is true</summary> */
        bool uniqueNavigationNodes = false;

        /** <summary>The index into the nodes list of the current node (next node
         *  to consume).  If -1, nodes array not filled yet.</summary>
         */
        protected int p = -1;

        /** <summary>Track the last mark() call result value for use in rewind().</summary> */
        protected int lastMarker;

        /** <summary>Stack of indexes used for push/pop calls</summary> */
        protected Stack<int> calls;

        public BufferedTreeNodeStream( object tree )
            : this( new CommonTreeAdaptor(), tree )
        {
        }

        public BufferedTreeNodeStream( ITreeAdaptor adaptor, object tree )
            : this( adaptor, tree, DEFAULT_INITIAL_BUFFER_SIZE )
        {
        }

        public BufferedTreeNodeStream( ITreeAdaptor adaptor, object tree, int initialBufferSize )
        {
            this.root = tree;
            this.adaptor = adaptor;
            nodes = new List<object>( initialBufferSize );
            down = adaptor.Create( TokenTypes.Down, "DOWN" );
            up = adaptor.Create( TokenTypes.Up, "UP" );
            eof = adaptor.Create( TokenTypes.EndOfFile, "EOF" );
        }

        #region Properties

        public virtual int Count
        {
            get
            {
                if ( p == -1 )
                {
                    throw new InvalidOperationException( "Cannot determine the Count before the buffer is filled." );
                }
                return nodes.Count;
            }
        }

        public virtual object TreeSource
        {
            get
            {
                return root;
            }
        }

        public virtual string SourceName
        {
            get
            {
                return TokenStream.SourceName;
            }
        }

        public virtual ITokenStream TokenStream
        {
            get
            {
                return tokens;
            }
            set
            {
                tokens = value;
            }
        }

        public virtual ITreeAdaptor TreeAdaptor
        {
            get
            {
                return adaptor;
            }
            set
            {
                adaptor = value;
            }
        }

        public virtual bool UniqueNavigationNodes
        {
            get
            {
                return uniqueNavigationNodes;
            }
            set
            {
                uniqueNavigationNodes = value;
            }
        }

        public virtual IToken LastToken
        {
            get
            {
                return TreeAdaptor.GetToken(LB(1));
            }
        }

        public virtual IToken LastRealToken
        {
            get
            {
                int i = 0;
                IToken token;
                do
                {
                    i++;
                    token = TreeAdaptor.GetToken(LB(i));
                } while (token != null && token.Line <= 0);

                return token;
            }
        }

        public virtual int MaxLookBehind
        {
            get
            {
                return int.MaxValue;
            }
        }

        #endregion

        /** Walk tree with depth-first-search and fill nodes buffer.
         *  Don't do DOWN, UP nodes if its a list (t is isNil).
         */
        protected virtual void FillBuffer()
        {
            FillBuffer( root );
            //Console.Out.WriteLine( "revIndex=" + tokenTypeToStreamIndexesMap );
            p = 0; // buffer of nodes intialized now
        }

        public virtual void FillBuffer( object t )
        {
            bool nil = adaptor.IsNil( t );
            if ( !nil )
            {
                nodes.Add( t ); // add this node
            }
            // add DOWN node if t has children
            int n = adaptor.GetChildCount( t );
            if ( !nil && n > 0 )
            {
                AddNavigationNode( TokenTypes.Down );
            }
            // and now add all its children
            for ( int c = 0; c < n; c++ )
            {
                object child = adaptor.GetChild( t, c );
                FillBuffer( child );
            }
            // add UP node if t has children
            if ( !nil && n > 0 )
            {
                AddNavigationNode( TokenTypes.Up );
            }
        }

        /** What is the stream index for node? 0..n-1
         *  Return -1 if node not found.
         */
        protected virtual int GetNodeIndex( object node )
        {
            if ( p == -1 )
            {
                FillBuffer();
            }
            for ( int i = 0; i < nodes.Count; i++ )
            {
                object t = nodes[i];
                if ( t == node )
                {
                    return i;
                }
            }
            return -1;
        }

        /** As we flatten the tree, we use UP, DOWN nodes to represent
         *  the tree structure.  When debugging we need unique nodes
         *  so instantiate new ones when uniqueNavigationNodes is true.
         */
        protected virtual void AddNavigationNode( int ttype )
        {
            object navNode = null;
            if ( ttype == TokenTypes.Down )
            {
                if ( UniqueNavigationNodes )
                {
                    navNode = adaptor.Create( TokenTypes.Down, "DOWN" );
                }
                else
                {
                    navNode = down;
                }
            }
            else
            {
                if ( UniqueNavigationNodes )
                {
                    navNode = adaptor.Create( TokenTypes.Up, "UP" );
                }
                else
                {
                    navNode = up;
                }
            }
            nodes.Add( navNode );
        }

        public virtual object this[int i]
        {
            get
            {
                if ( p == -1 )
                {
                    throw new InvalidOperationException( "Cannot get the node at index i before the buffer is filled." );
                }
                return nodes[i];
            }
        }

        public virtual object LT( int k )
        {
            if ( p == -1 )
            {
                FillBuffer();
            }
            if ( k == 0 )
            {
                return null;
            }
            if ( k < 0 )
            {
                return LB( -k );
            }
            //System.out.print("LT(p="+p+","+k+")=");
            if ( ( p + k - 1 ) >= nodes.Count )
            {
                return eof;
            }
            return nodes[p + k - 1];
        }

        public virtual object GetCurrentSymbol()
        {
            return LT( 1 );
        }

#if false
        public virtual object getLastTreeNode()
        {
            int i = Index;
            if ( i >= size() )
            {
                i--; // if at EOF, have to start one back
            }
            Console.Out.WriteLine( "start last node: " + i + " size==" + nodes.Count );
            while ( i >= 0 &&
                ( adaptor.getType( this[i] ) == TokenTypes.EOF ||
                 adaptor.getType( this[i] ) == TokenTypes.UP ||
                 adaptor.getType( this[i] ) == TokenTypes.DOWN ) )
            {
                i--;
            }
            Console.Out.WriteLine( "stop at node: " + i + " " + nodes[i] );
            return nodes[i];
        }
#endif

        /** <summary>Look backwards k nodes</summary> */
        protected virtual object LB( int k )
        {
            if ( k == 0 )
            {
                return null;
            }
            if ( ( p - k ) < 0 )
            {
                return null;
            }
            return nodes[p - k];
        }

        public virtual void Consume()
        {
            if ( p == -1 )
            {
                FillBuffer();
            }
            p++;
        }

        public virtual int LA( int i )
        {
            return adaptor.GetType( LT( i ) );
        }

        public virtual int Mark()
        {
            if ( p == -1 )
            {
                FillBuffer();
            }
            lastMarker = Index;
            return lastMarker;
        }

        public virtual void Release( int marker )
        {
            // no resources to release
        }

        public virtual int Index
        {
            get
            {
                return p;
            }
        }

        public virtual void Rewind( int marker )
        {
            Seek( marker );
        }

        public virtual void Rewind()
        {
            Seek( lastMarker );
        }

        public virtual void Seek( int index )
        {
            if ( p == -1 )
            {
                FillBuffer();
            }
            p = index;
        }

        /** <summary>
         *  Make stream jump to a new location, saving old location.
         *  Switch back with pop().
         *  </summary>
         */
        public virtual void Push( int index )
        {
            if ( calls == null )
            {
                calls = new Stack<int>();
            }
            calls.Push( p ); // save current index
            Seek( index );
        }

        /** <summary>
         *  Seek back to previous index saved during last push() call.
         *  Return top of stack (return index).
         *  </summary>
         */
        public virtual int Pop()
        {
            int ret = calls.Pop();
            Seek( ret );
            return ret;
        }

        public virtual void Reset()
        {
            p = 0;
            lastMarker = 0;
            if ( calls != null )
            {
                calls.Clear();
            }
        }

        public virtual IEnumerator<object> Iterator()
        {
            if ( p == -1 )
            {
                FillBuffer();
            }

            return new StreamIterator( this );
        }

        // TREE REWRITE INTERFACE

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

        /** <summary>Used for testing, just return the token type stream</summary> */
        public virtual string ToTokenTypeString()
        {
            if ( p == -1 )
            {
                FillBuffer();
            }
            StringBuilder buf = new StringBuilder();
            for ( int i = 0; i < nodes.Count; i++ )
            {
                object t = nodes[i];
                buf.Append( " " );
                buf.Append( adaptor.GetType( t ) );
            }
            return buf.ToString();
        }

        /** <summary>Debugging</summary> */
        public virtual string ToTokenString( int start, int stop )
        {
            if ( p == -1 )
            {
                FillBuffer();
            }
            StringBuilder buf = new StringBuilder();
            for ( int i = start; i < nodes.Count && i <= stop; i++ )
            {
                object t = nodes[i];
                buf.Append( " " );
                buf.Append( adaptor.GetToken( t ) );
            }
            return buf.ToString();
        }

        public virtual string ToString( object start, object stop )
        {
            Console.Out.WriteLine( "toString" );
            if ( start == null || stop == null )
            {
                return null;
            }
            if ( p == -1 )
            {
                throw new InvalidOperationException( "Buffer is not yet filled." );
            }
            //Console.Out.WriteLine( "stop: " + stop );
            if ( start is CommonTree )
                Console.Out.Write( "toString: " + ( (CommonTree)start ).Token + ", " );
            else
                Console.Out.WriteLine( start );
            if ( stop is CommonTree )
                Console.Out.WriteLine( ( (CommonTree)stop ).Token );
            else
                Console.Out.WriteLine( stop );
            // if we have the token stream, use that to dump text in order
            if ( tokens != null )
            {
                int beginTokenIndex = adaptor.GetTokenStartIndex( start );
                int endTokenIndex = adaptor.GetTokenStopIndex( stop );
                // if it's a tree, use start/stop index from start node
                // else use token range from start/stop nodes
                if ( adaptor.GetType( stop ) == TokenTypes.Up )
                {
                    endTokenIndex = adaptor.GetTokenStopIndex( start );
                }
                else if ( adaptor.GetType( stop ) == TokenTypes.EndOfFile )
                {
                    endTokenIndex = Count - 2; // don't use EOF
                }
                return tokens.ToString( beginTokenIndex, endTokenIndex );
            }
            // walk nodes looking for start
            object t = null;
            int i = 0;
            for ( ; i < nodes.Count; i++ )
            {
                t = nodes[i];
                if ( t == start )
                {
                    break;
                }
            }
            // now walk until we see stop, filling string buffer with text
            StringBuilder buf = new StringBuilder();
            t = nodes[i];
            while ( t != stop )
            {
                string text = adaptor.GetText( t );
                if ( text == null )
                {
                    text = " " + adaptor.GetType( t ).ToString();
                }
                buf.Append( text );
                i++;
                t = nodes[i];
            }
            // include stop node too
            string text2 = adaptor.GetText( stop );
            if ( text2 == null )
            {
                text2 = " " + adaptor.GetType( stop ).ToString();
            }
            buf.Append( text2 );
            return buf.ToString();
        }
    }
}