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