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 #ifndef DEBUG_DEALLOC 28 #define DEBUG_DEALLOC 29 #endif 30 31 #import "AMutableArray.h" 32 33 @protocol Tree < NSObject, NSCopying > 34 35 //+ (id<Tree>) invalidNode; 36 37 - (id<Tree>) getChild:(NSUInteger)index; 38 - (NSUInteger) getChildCount; 39 40 // Tree tracks parent and child index now > 3.0 41 42 - (id<Tree>)getParent; 43 44 - (void) setParent:(id<Tree>)t; 45 46 /** Is there is a node above with token type ttype? */ 47 - (BOOL) hasAncestor:(NSInteger)ttype; 48 49 /** Walk upwards and get first ancestor with this token type. */ 50 - (id<Tree>) getAncestor:(NSInteger) ttype; 51 52 /** Return a list of all ancestors of this node. The first node of 53 * list is the root and the last is the parent of this node. 54 */ 55 - (AMutableArray *) getAncestors; 56 57 /** This node is what child index? 0..n-1 */ 58 - (NSInteger) getChildIndex; 59 60 - (void) setChildIndex:(NSInteger) index; 61 62 /** Set the parent and child index values for all children */ 63 - (void) freshenParentAndChildIndexes; 64 65 /** Add t as a child to this node. If t is null, do nothing. If t 66 * is nil, add all children of t to this' children. 67 */ 68 - (void) addChild:(id<Tree>) t; 69 70 /** Set ith child (0..n-1) to t; t must be non-null and non-nil node */ 71 - (void) setChild:(NSInteger)i With:(id<Tree>) t; 72 73 - (id) deleteChild:(NSInteger) i; 74 75 /** Delete children from start to stop and replace with t even if t is 76 * a list (nil-root tree). num of children can increase or decrease. 77 * For huge child lists, inserting children can force walking rest of 78 * children to set their childindex; could be slow. 79 */ 80 - (void) replaceChildrenFrom:(NSInteger)startChildIndex To:(NSInteger)stopChildIndex With:(id)t; 81 82 - (NSArray *) children; 83 // Add t as a child to this node. If t is null, do nothing. If t 84 // is nil, add all children of t to this' children. 85 86 - (void) addChildren:(NSArray *) theChildren; 87 //- (void) removeAllChildren; 88 89 // Indicates the node is a nil node but may still have children, meaning 90 // the tree is a flat list. 91 92 - (BOOL) isNil; 93 94 /** What is the smallest token index (indexing from 0) for this node 95 * and its children? 96 */ 97 - (NSInteger) getTokenStartIndex; 98 99 - (void) setTokenStartIndex:(NSInteger) index; 100 101 /** What is the largest token index (indexing from 0) for this node 102 * and its children? 103 */ 104 - (NSInteger) getTokenStopIndex; 105 - (void) setTokenStopIndex:(NSInteger) index; 106 107 - (id<Tree>) dupNode; 108 109 - (NSString *) toString; 110 111 #pragma mark Copying 112 - (id) copyWithZone:(NSZone *)aZone; // the children themselves are not copied here! 113 - (id) deepCopy; // performs a deepCopyWithZone: with the default zone 114 - (id) deepCopyWithZone:(NSZone *)aZone; 115 116 #pragma mark Tree Parser support 117 - (NSInteger)type; 118 - (NSString *)text; 119 // In case we don't have a token payload, what is the line for errors? 120 - (NSUInteger)line; 121 - (NSUInteger)charPositionInLine; 122 - (void) setCharPositionInLine:(NSUInteger)pos; 123 124 #pragma mark Informational 125 - (NSString *) treeDescription; 126 - (NSString *) description; 127 128 @end 129 130