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 "ANTLRCommonTreeAdaptor.h" 28 29@implementation ANTLRCommonTreeAdaptor 30 31+ (ANTLRCommonTree *) newEmptyTree; 32{ 33 return [ANTLRCommonTree newTree]; 34} 35 36+ (ANTLRCommonTreeAdaptor *)newTreeAdaptor 37{ 38 return[[ANTLRCommonTreeAdaptor alloc] init]; 39} 40 41- (id) init 42{ 43 self = [super init]; 44 if (self) { 45 } 46 return self; 47} 48 49/** Duplicate a node. This is part of the factory; 50 * override if you want another kind of node to be built. 51 * 52 * I could use reflection to prevent having to override this 53 * but reflection is slow. 54 */ 55- (id) dupNode:(id<ANTLRBaseTree>)t 56{ 57 if ( t==nil ) 58 return nil; 59 return [ANTLRCommonTree newTree:t]; 60} 61 62/** Tell me how to create a token for use with imaginary token nodes. 63 * For example, there is probably no input symbol associated with imaginary 64 * token DECL, but you need to create it as a payload or whatever for 65 * the DECL node as in ^(DECL type ID). 66 * 67 * This is a variant of createToken where the new token is derived from 68 * an actual real input token. Typically this is for converting '{' 69 * tokens to BLOCK etc... You'll see 70 * 71 * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; 72 * 73 * If you care what the token payload objects' type is, you should 74 * override this method and any other createToken variant. 75 */ 76- (ANTLRCommonTree *) create:(ANTLRCommonToken *)aToken 77{ 78 return [ANTLRCommonTree newTreeWithToken:aToken]; 79} 80 81/** Tell me how to create a token for use with imaginary token nodes. 82 * For example, there is probably no input symbol associated with imaginary 83 * token DECL, but you need to create it as a payload or whatever for 84 * the DECL node as in ^(DECL type ID). 85 * 86 * If you care what the token payload objects' type is, you should 87 * override this method and any other createToken variant. 88 */ 89- (ANTLRCommonTree *)createTree:(NSInteger)tokenType Text:(NSString *)text 90{ 91 return [ANTLRCommonTree newTreeWithTokenType:tokenType Text:text]; 92} 93 94- (id<ANTLRToken>)createToken:(NSInteger)tokenType Text:(NSString *)text 95{ 96 id<ANTLRToken> fromToken = [ANTLRCommonToken newToken:tokenType Text:text]; 97 return fromToken; 98} 99 100- (id<ANTLRToken>)createToken:(id<ANTLRToken>)fromToken 101{ 102 return [ANTLRCommonToken newTokenWithToken:(ANTLRCommonToken *)fromToken]; 103} 104 105/** Track start/stop token for subtree root created for a rule. 106 * Only works with Tree nodes. For rules that match nothing, 107 * seems like this will yield start=i and stop=i-1 in a nil node. 108 * Might be useful info so I'll not force to be i..i. 109 */ 110- (void) setTokenBoundaries:(id<ANTLRBaseTree>)aTree From:(id<ANTLRToken>)startToken To:(id<ANTLRToken>)stopToken 111{ 112 if ( aTree == nil ) 113 return; 114 int startTokIdx = 0; 115 int stopTokIdx = 0; 116 if ( startToken != nil ) 117 startTokIdx = [startToken getTokenIndex]; 118 if ( stopToken != nil ) 119 stopTokIdx = [stopToken getTokenIndex]; 120 [(id<ANTLRBaseTree>)aTree setTokenStartIndex:startTokIdx]; 121 [(id<ANTLRBaseTree>)aTree setTokenStopIndex:stopTokIdx]; 122} 123 124- (NSInteger)getTokenStartIndex:(id<ANTLRBaseTree>) t 125{ 126 if ( t == nil ) 127 return -1; 128 return [(id<ANTLRBaseTree>)t getTokenStartIndex]; 129} 130 131- (NSInteger)getTokenStopIndex:(id<ANTLRBaseTree>) t 132{ 133 if ( t == nil ) 134 return -1; 135 return [(id<ANTLRBaseTree>)t getTokenStopIndex]; 136} 137 138- (NSString *)getText:(ANTLRCommonTree *)t 139{ 140 if ( t == nil ) 141 return nil; 142 return t.token.text; 143} 144 145- (void)setText:(id<ANTLRBaseTree>)t Text:(NSString *)text 146{ 147 if ( t == nil ) 148 return; 149} 150 151- (NSInteger)getType:(ANTLRCommonTree *)t 152{ 153 if ( t==nil ) 154 return ANTLRTokenTypeInvalid; 155 return t.token.type; 156} 157 158- (void) setType:(id<ANTLRBaseTree>)t Type:(NSInteger)tokenType 159{ 160 if ( t==nil ) 161 return; 162} 163 164/** What is the Token associated with this node? If 165 * you are not using ANTLRCommonTree, then you must 166 * override this in your own adaptor. 167 */ 168- (id<ANTLRToken>) getToken:(ANTLRCommonTree *) t 169{ 170 if ( [t isKindOfClass:[ANTLRCommonTree class]] ) { 171 return t.token; 172 } 173 return nil; // no idea what to do 174} 175 176- (id<ANTLRBaseTree>) getChild:(id<ANTLRBaseTree>)t At:(NSInteger)i 177{ 178 if ( t == nil ) 179 return nil; 180 return [(id<ANTLRBaseTree>)t getChild:i]; 181} 182 183- (void) setChild:(id<ANTLRBaseTree>)t At:(NSInteger)i Child:(id<ANTLRBaseTree>)child 184{ 185 if ( t == nil ) 186 return; 187 [(id<ANTLRBaseTree>)t setChild:i With:child]; 188} 189 190- (id) deleteChild:(id<ANTLRBaseTree>)t Index:(NSInteger)anIndex 191{ 192 return [t deleteChild:anIndex]; 193} 194 195- (NSInteger) getChildCount:(id<ANTLRBaseTree>) t 196{ 197 if ( t == nil ) 198 return 0; 199 return [(id<ANTLRBaseTree>) t getChildCount]; 200} 201 202- (id<ANTLRBaseTree>) getParent:(id<ANTLRBaseTree>) t 203{ 204 if ( t == nil ) 205 return nil; 206 return (id<ANTLRBaseTree>)[t getParent]; 207} 208 209- (void) setParent:(id<ANTLRBaseTree>)t With:(id<ANTLRBaseTree>) parent 210{ 211 if ( t != nil ) 212 [(id<ANTLRBaseTree>) t setParent:(id<ANTLRBaseTree>)parent]; 213} 214 215- (NSInteger) getChildIndex:(id<ANTLRBaseTree>) t 216{ 217 if ( t == nil ) 218 return 0; 219 return [(id<ANTLRBaseTree>) t getChildIndex]; 220} 221 222- (void) setChildIndex:(id<ANTLRBaseTree>)t With:(NSInteger)anIndex 223{ 224 if ( t!=nil ) 225 [(id<ANTLRBaseTree>)t setChildIndex:anIndex]; 226} 227 228- (void) replaceChildren:(id<ANTLRBaseTree>)parent From:(NSInteger)startChildIndex To:(NSInteger)stopChildIndex With:(id<ANTLRBaseTree>)t 229{ 230 if ( parent != nil ) { 231 [(id<ANTLRBaseTree>)parent replaceChildrenFrom:startChildIndex To:stopChildIndex With:t]; 232 } 233} 234 235- (id) copyWithZone:(NSZone *)aZone 236{ 237 return [[[self class] allocWithZone:aZone] init]; 238} 239 240@end 241