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