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#import "ANTLRTree.h" 28#import "ANTLRCommonToken.h" 29#import "AMutableArray.h" 30 31@protocol ANTLRBaseTree <ANTLRTree> 32 33+ (id<ANTLRBaseTree>) INVALID_NODE; 34 35+ (id<ANTLRBaseTree>) newTree; 36+ (id<ANTLRBaseTree>) newTree:(id<ANTLRBaseTree>)node; 37 38- (id<ANTLRBaseTree>) init; 39- (id<ANTLRBaseTree>) initWith:(id<ANTLRBaseTree>)node; 40 41- (id<ANTLRBaseTree>) getChild:(NSUInteger)i; 42- (AMutableArray *)children; 43- (void) setChildren:(AMutableArray *)anArray; 44- (id<ANTLRBaseTree>)getFirstChildWithType:(NSInteger)type; 45- (NSUInteger) getChildCount; 46 47// Add t as a child to this node. If t is null, do nothing. If t 48// is nil, add all children of t to this' children. 49 50- (void) addChild:(id<ANTLRBaseTree>) tree; 51- (void) addChildren:(NSArray *) theChildren; 52//- (void) removeAllChildren; 53 54- (void) setChild:(NSInteger) i With:(id<ANTLRBaseTree>)t; 55- (id) deleteChild:(NSInteger) i; 56- (AMutableArray *) createChildrenList; 57- (void) replaceChildrenFrom:(NSInteger)startChildIndex To:(NSInteger)stopChildIndex With:(id) t; 58// Indicates the node is a nil node but may still have children, meaning 59// the tree is a flat list. 60 61- (BOOL) isNil; 62- (NSInteger) getTokenStartIndex; 63- (void) setTokenStartIndex:(NSInteger) index; 64- (NSInteger) getTokenStopIndex; 65- (void) setTokenStopIndex:(NSInteger) index; 66 67- (void) freshenParentAndChildIndexes; 68- (void) freshenParentAndChildIndexes:(NSInteger) offset; 69- (void) sanityCheckParentAndChildIndexes; 70- (void) sanityCheckParentAndChildIndexes:(id<ANTLRBaseTree>) parent At:(NSInteger) i; 71 72- (NSInteger) getChildIndex; 73- (void) setChildIndex:(NSInteger)i; 74 75- (id<ANTLRBaseTree>)getAncestor:(NSInteger)ttype; 76- (AMutableArray *)getAncestors; 77 78#pragma mark Copying 79- (id) copyWithZone:(NSZone *)aZone; // the children themselves are not copied here! 80- (id) deepCopy; // performs a deepCopyWithZone: with the default zone 81- (id) deepCopyWithZone:(NSZone *)aZone; 82 83#pragma mark Tree Parser support 84- (NSInteger)type; 85- (NSString *)text; 86// In case we don't have a token payload, what is the line for errors? 87- (NSUInteger)line; 88- (NSUInteger)charPositionInLine; 89 90 91#pragma mark Informational 92- (NSString *) treeDescription; 93- (NSString *) description; 94 95- (NSString *) toString; 96- (NSString *) toStringTree; 97 98@property (retain) AMutableArray *children; 99@property (retain) NSException *anException; 100 101@end 102 103@interface ANTLRBaseTree : NSObject <ANTLRTree> 104{ 105 __strong AMutableArray *children; 106 __strong NSException *anException; 107} 108 109+ (id<ANTLRBaseTree>) INVALID_NODE; 110+ (id<ANTLRBaseTree>) newTree; 111+ (id<ANTLRBaseTree>) newTree:(id<ANTLRBaseTree>)node; 112 113- (id<ANTLRBaseTree>) init; 114- (id<ANTLRBaseTree>) initWith:(id<ANTLRBaseTree>)node; 115 116- (id<ANTLRBaseTree>) getChild:(NSUInteger)i; 117- (AMutableArray *)children; 118- (void) setChildren:(AMutableArray *)anArray; 119- (id<ANTLRBaseTree>)getFirstChildWithType:(NSInteger)type; 120- (NSUInteger) getChildCount; 121 122//- (void) removeAllChildren; 123 124// Add t as a child to this node. If t is null, do nothing. If t 125// is nil, add all children of t to this' children. 126 127- (void) addChild:(id<ANTLRBaseTree>) tree; 128- (void) addChildren:(NSArray *) theChildren; 129 130- (void) setChild:(NSUInteger) i With:(id<ANTLRBaseTree>)t; 131- (id) deleteChild:(NSUInteger) idx; 132- (AMutableArray *) createChildrenList; 133- (void) replaceChildrenFrom:(NSInteger)startChildIndex To:(NSInteger)stopChildIndex With:(id) t; 134// Indicates the node is a nil node but may still have children, meaning 135 // the tree is a flat list. 136 137- (BOOL) isNil; 138- (NSInteger) getTokenStartIndex; 139- (void) setTokenStartIndex:(NSInteger) index; 140- (NSInteger) getTokenStopIndex; 141- (void) setTokenStopIndex:(NSInteger) index; 142 143- (void) freshenParentAndChildIndexes; 144- (void) freshenParentAndChildIndexes:(NSInteger) offset; 145- (void) sanityCheckParentAndChildIndexes; 146- (void) sanityCheckParentAndChildIndexes:(id<ANTLRBaseTree>)parent At:(NSInteger) i; 147 148- (NSInteger) getChildIndex; 149- (void) setChildIndex:(NSInteger)i; 150 151- (BOOL) hasAncestor:(NSInteger) ttype; 152- (id<ANTLRBaseTree>)getAncestor:(NSInteger)ttype; 153- (AMutableArray *)getAncestors; 154 155- (id) copyWithZone:(NSZone *)aZone; 156- (id) deepCopy; // performs a deepCopyWithZone: with the default zone 157- (id) deepCopyWithZone:(NSZone *)aZone; 158 159 // Return a token type; needed for tree parsing 160- (NSInteger)type; 161- (NSString *)text; 162 163 // In case we don't have a token payload, what is the line for errors? 164- (NSUInteger)line; 165- (NSUInteger)charPositionInLine; 166- (void) setCharPositionInLine:(NSUInteger)pos; 167 168- (NSString *) treeDescription; 169- (NSString *) description; 170- (NSString *) toString; 171- (NSString *) toStringTree; 172 173@property (retain) AMutableArray *children; 174@property (retain) NSException *anException; 175 176@end 177 178@interface ANTLRTreeNavigationNode : ANTLRBaseTree { 179} 180- (id) init; 181- (id) copyWithZone:(NSZone *)aZone; 182@end 183 184@interface ANTLRTreeNavigationNodeDown : ANTLRTreeNavigationNode { 185} 186+ (ANTLRTreeNavigationNodeDown *) getNavigationNodeDown; 187- (id) init; 188- (NSInteger) tokenType; 189- (NSString *) description; 190@end 191 192@interface ANTLRTreeNavigationNodeUp : ANTLRTreeNavigationNode { 193} 194+ (ANTLRTreeNavigationNodeUp *) getNavigationNodeUp; 195- (id) init; 196- (NSInteger) tokenType; 197- (NSString *) description; 198@end 199 200@interface ANTLRTreeNavigationNodeEOF : ANTLRTreeNavigationNode { 201} 202+ (ANTLRTreeNavigationNodeEOF *) getNavigationNodeEOF; 203- (id) init; 204- (NSInteger) tokenType; 205- (NSString *) description; 206@end 207 208extern ANTLRTreeNavigationNodeDown *navigationNodeDown; 209extern ANTLRTreeNavigationNodeUp *navigationNodeUp; 210extern ANTLRTreeNavigationNodeEOF *navigationNodeEOF; 211