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     using System.Collections.Generic;
36 
37     /** <summary>
38      *  What does a tree look like?  ANTLR has a number of support classes
39      *  such as CommonTreeNodeStream that work on these kinds of trees.  You
40      *  don't have to make your trees implement this interface, but if you do,
41      *  you'll be able to use more support code.
42      *  </summary>
43      *
44      *  <remarks>
45      *  NOTE: When constructing trees, ANTLR can build any kind of tree; it can
46      *  even use Token objects as trees if you add a child list to your tokens.
47      *
48      *  This is a tree node without any payload; just navigation and factory stuff.
49      *  </remarks>
50      */
51     public interface ITree
52     {
53 
GetChild( int i )54         ITree GetChild( int i );
55 
56         int ChildCount
57         {
58             get;
59         }
60 
61         // Tree tracks parent and child index now > 3.0
62 
63         ITree Parent
64         {
65             get;
66             set;
67         }
68 
69         /** <summary>Is there is a node above with token type ttype?</summary> */
HasAncestor( int ttype )70         bool HasAncestor( int ttype );
71 
72         /** <summary>Walk upwards and get first ancestor with this token type.</summary> */
GetAncestor( int ttype )73         ITree GetAncestor( int ttype );
74 
75         /** <summary>
76          *  Return a list of all ancestors of this node.  The first node of
77          *  list is the root and the last is the parent of this node.
78          *  </summary>
79          */
GetAncestors()80         IList<ITree> GetAncestors();
81 
82         /** <summary>This node is what child index? 0..n-1</summary> */
83         int ChildIndex
84         {
85             get;
86             set;
87         }
88 
89         /** <summary>Set the parent and child index values for all children</summary> */
FreshenParentAndChildIndexes()90         void FreshenParentAndChildIndexes();
91 
92         /** <summary>
93          *  Add t as a child to this node.  If t is null, do nothing.  If t
94          *  is nil, add all children of t to this' children.
95          *  </summary>
96          */
AddChild( ITree t )97         void AddChild( ITree t );
98 
99         /** <summary>Set ith child (0..n-1) to t; t must be non-null and non-nil node</summary> */
SetChild( int i, ITree t )100         void SetChild( int i, ITree t );
101 
DeleteChild( int i )102         object DeleteChild( int i );
103 
104         /** <summary>
105          *  Delete children from start to stop and replace with t even if t is
106          *  a list (nil-root tree).  num of children can increase or decrease.
107          *  For huge child lists, inserting children can force walking rest of
108          *  children to set their childindex; could be slow.
109          *  </summary>
110          */
ReplaceChildren( int startChildIndex, int stopChildIndex, object t )111         void ReplaceChildren( int startChildIndex, int stopChildIndex, object t );
112 
113         /** <summary>
114          *  Indicates the node is a nil node but may still have children, meaning
115          *  the tree is a flat list.
116          *  </summary>
117          */
118         bool IsNil
119         {
120             get;
121         }
122 
123         /** <summary>
124          *  What is the smallest token index (indexing from 0) for this node
125          *  and its children?
126          *  </summary>
127          */
128         int TokenStartIndex
129         {
130             get;
131             set;
132         }
133 
134         /** <summary>
135          *  What is the largest token index (indexing from 0) for this node
136          *  and its children?
137          *  </summary>
138          */
139         int TokenStopIndex
140         {
141             get;
142             set;
143         }
144 
DupNode()145         ITree DupNode();
146 
147         /** <summary>Return a token type; needed for tree parsing</summary> */
148         int Type
149         {
150             get;
151         }
152 
153         string Text
154         {
155             get;
156         }
157 
158         /** <summary>In case we don't have a token payload, what is the line for errors?</summary> */
159         int Line
160         {
161             get;
162         }
163 
164         int CharPositionInLine
165         {
166             get;
167         }
168 
ToStringTree()169         string ToStringTree();
170 
ToString()171         string ToString();
172     }
173 }
174