1// [The "BSD licence"]
2// Copyright (c) 2006-2007 Kay Roepke 2010 Alan Condit
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9//    notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11//    notice, this list of conditions and the following disclaimer in the
12//    documentation and/or other materials provided with the distribution.
13// 3. The name of the author may not be used to endorse or promote products
14//    derived from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27@protocol ANTLRTree < NSObject, NSCopying >
28
29//+ (id<ANTLRTree>) invalidNode;
30
31- (id<ANTLRTree>) getChild:(NSUInteger)index;
32- (NSUInteger) getChildCount;
33
34// Tree tracks parent and child index now > 3.0
35
36- (id<ANTLRTree>)getParent;
37
38- (void) setParent:(id<ANTLRTree>)t;
39
40/** Is there is a node above with token type ttype? */
41- (BOOL) hasAncestor:(NSInteger)ttype;
42
43/** Walk upwards and get first ancestor with this token type. */
44- (id<ANTLRTree>) getAncestor:(NSInteger) ttype;
45
46/** Return a list of all ancestors of this node.  The first node of
47 *  list is the root and the last is the parent of this node.
48 */
49- (NSMutableArray *) getAncestors;
50
51/** This node is what child index? 0..n-1 */
52- (NSInteger) getChildIndex;
53
54- (void) setChildIndex:(NSInteger) index;
55
56/** Set the parent and child index values for all children */
57- (void) freshenParentAndChildIndexes;
58
59/** Add t as a child to this node.  If t is null, do nothing.  If t
60 *  is nil, add all children of t to this' children.
61 */
62- (void) addChild:(id<ANTLRTree>) t;
63
64/** Set ith child (0..n-1) to t; t must be non-null and non-nil node */
65- (void) setChild:(NSInteger)i With:(id<ANTLRTree>) t;
66
67- (id) deleteChild:(NSInteger) i;
68
69/** Delete children from start to stop and replace with t even if t is
70 *  a list (nil-root tree).  num of children can increase or decrease.
71 *  For huge child lists, inserting children can force walking rest of
72 *  children to set their childindex; could be slow.
73 */
74- (void) replaceChildrenFrom:(NSInteger)startChildIndex To:(NSInteger)stopChildIndex With:(id)t;
75
76- (NSArray *) getChildren;
77// Add t as a child to this node.  If t is null, do nothing.  If t
78//  is nil, add all children of t to this' children.
79
80- (void) addChildren:(NSArray *) theChildren;
81//- (void) removeAllChildren;
82
83// Indicates the node is a nil node but may still have children, meaning
84// the tree is a flat list.
85
86- (BOOL) isNil;
87
88/**  What is the smallest token index (indexing from 0) for this node
89 *   and its children?
90 */
91- (NSInteger) getTokenStartIndex;
92
93- (void) setTokenStartIndex:(NSInteger) index;
94
95/**  What is the largest token index (indexing from 0) for this node
96 *   and its children?
97 */
98- (NSInteger) getTokenStopIndex;
99- (void) setTokenStopIndex:(NSInteger) index;
100
101- (id<ANTLRTree>) dupNode;
102
103- (NSString *) toString;
104
105#pragma mark Copying
106- (id) copyWithZone:(NSZone *)aZone;	// the children themselves are not copied here!
107- (id) deepCopy;					// performs a deepCopyWithZone: with the default zone
108- (id) deepCopyWithZone:(NSZone *)aZone;
109
110#pragma mark Tree Parser support
111- (NSInteger) getType;
112- (NSString *) getText;
113// In case we don't have a token payload, what is the line for errors?
114- (NSInteger) getLine;
115- (NSInteger) getCharPositionInLine;
116- (void) setCharPositionInLine:(NSInteger)pos;
117
118#pragma mark Informational
119- (NSString *) treeDescription;
120- (NSString *) description;
121
122@end
123
124