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.Tree
34 {
35     /** <summary>A stream of tree nodes, accessing nodes from a tree of some kind</summary> */
36     public interface ITreeNodeStream : IIntStream
37     {
38         /** <summary>
39          *  Get a tree node at an absolute index i; 0..n-1.
40          *  If you don't want to buffer up nodes, then this method makes no
41          *  sense for you.
42          *  </summary>
43          */
44         object this[int i]
45         {
46             get;
47         }
48 
49         /** <summary>
50          *  Get tree node at current input pointer + i ahead where i=1 is next node.
51          *  i&lt;0 indicates nodes in the past.  So LT(-1) is previous node, but
52          *  implementations are not required to provide results for k &lt; -1.
53          *  LT(0) is undefined.  For i&gt;=n, return null.
54          *  Return null for LT(0) and any index that results in an absolute address
55          *  that is negative.
56          *  </summary>
57          *
58          *  <remarks>
59          *  This is analogus to the LT() method of the TokenStream, but this
60          *  returns a tree node instead of a token.  Makes code gen identical
61          *  for both parser and tree grammars. :)
62          *  </remarks>
63          */
LT( int k )64         object LT( int k );
65 
66         /** <summary>
67          *  Where is this stream pulling nodes from?  This is not the name, but
68          *  the object that provides node objects.
69          *  </summary>
70          */
71         object TreeSource
72         {
73             get;
74         }
75 
76         /** <summary>
77          *  If the tree associated with this stream was created from a TokenStream,
78          *  you can specify it here.  Used to do rule $text attribute in tree
79          *  parser.  Optional unless you use tree parser rule text attribute
80          *  or output=template and rewrite=true options.
81          *  </summary>
82          */
83         ITokenStream TokenStream
84         {
85             get;
86         }
87 
88         /** <summary>
89          *  What adaptor can tell me how to interpret/navigate nodes and
90          *  trees.  E.g., get text of a node.
91          *  </summary>
92          */
93         ITreeAdaptor TreeAdaptor
94         {
95             get;
96         }
97 
98         /** <summary>
99          *  As we flatten the tree, we use UP, DOWN nodes to represent
100          *  the tree structure.  When debugging we need unique nodes
101          *  so we have to instantiate new ones.  When doing normal tree
102          *  parsing, it's slow and a waste of memory to create unique
103          *  navigation nodes.  Default should be false;
104          *  </summary>
105          */
106         bool UniqueNavigationNodes
107         {
108             get;
109             set;
110         }
111 
112         /** <summary>
113          *  Return the text of all nodes from start to stop, inclusive.
114          *  If the stream does not buffer all the nodes then it can still
115          *  walk recursively from start until stop.  You can always return
116          *  null or "" too, but users should not access $ruleLabel.text in
117          *  an action of course in that case.
118          *  </summary>
119          */
ToString( object start, object stop )120         string ToString( object start, object stop );
121 
122 
123         #region REWRITING TREES (used by tree parser)
124 
125         /** <summary>
126          *  Replace from start to stop child index of parent with t, which might
127          *  be a list.  Number of children may be different
128          *  after this call.  The stream is notified because it is walking the
129          *  tree and might need to know you are monkeying with the underlying
130          *  tree.  Also, it might be able to modify the node stream to avoid
131          *  restreaming for future phases.
132          *  </summary>
133          *
134          *  <remarks>
135          *  If parent is null, don't do anything; must be at root of overall tree.
136          *  Can't replace whatever points to the parent externally.  Do nothing.
137          *  </remarks>
138          */
ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t )139         void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t );
140 
141         #endregion
142 
143     }
144 }
145