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 28 #import "ANTLRCommonToken.h" 29 30 static ANTLRCommonToken *SKIP_TOKEN; 31 static ANTLRCommonToken *EOF_TOKEN; 32 static ANTLRCommonToken *INVALID_TOKEN; 33 34 @implementation ANTLRCommonToken 35 36 static NSInteger DEFAULT_CHANNEL = ANTLRTokenChannelDefault; 37 static NSInteger INVALID_TOKEN_TYPE = ANTLRTokenTypeInvalid; 38 39 40 @synthesize text; 41 @synthesize type; 42 @synthesize line; 43 @synthesize charPositionInLine; 44 @synthesize channel; 45 @synthesize index; 46 @synthesize startIndex; 47 @synthesize stopIndex; 48 @synthesize input; 49 50 + (void) initialize 51 { 52 EOF_TOKEN = [ANTLRCommonToken newToken:ANTLRTokenTypeEOF Text:@"EOF"]; 53 SKIP_TOKEN = [ANTLRCommonToken newToken:ANTLRTokenTypeInvalid Text:@"Skip"]; 54 INVALID_TOKEN = [ANTLRCommonToken newToken:ANTLRTokenTypeInvalid Text:@"Invalid"]; 55 [EOF_TOKEN retain]; 56 [SKIP_TOKEN retain]; 57 [INVALID_TOKEN retain]; 58 } 59 60 + (ANTLRCommonToken *)INVALID_TOKEN 61 { 62 return INVALID_TOKEN; 63 } 64 65 + (NSInteger) DEFAULT_CHANNEL 66 { 67 return DEFAULT_CHANNEL; 68 } 69 70 + (NSInteger) INVALID_TOKEN_TYPE 71 { 72 return INVALID_TOKEN_TYPE; 73 } 74 75 + (ANTLRCommonToken *) newToken 76 { 77 return [[ANTLRCommonToken alloc] init]; 78 } 79 80 + (ANTLRCommonToken *) newToken:(id<ANTLRCharStream>)anInput Type:(NSInteger)aTType Channel:(NSInteger)aChannel Start:(NSInteger)aStart Stop:(NSInteger)aStop 81 { 82 return [[ANTLRCommonToken alloc] initWithInput:(id<ANTLRCharStream>)anInput Type:(NSInteger)aTType Channel:(NSInteger)aChannel Start:(NSInteger)aStart Stop:(NSInteger)aStop]; 83 } 84 85 + (ANTLRCommonToken *) newToken:(ANTLRTokenType)tokenType 86 { 87 return( [[ANTLRCommonToken alloc] initWithType:tokenType] ); 88 } 89 90 + (ANTLRCommonToken *) newToken:(NSInteger)tokenType Text:(NSString *)tokenText 91 { 92 return( [[ANTLRCommonToken alloc] initWithType:tokenType Text:tokenText] ); 93 } 94 95 + (ANTLRCommonToken *) newTokenWithToken:(ANTLRCommonToken *)fromToken 96 { 97 return( [[ANTLRCommonToken alloc] initWithToken:fromToken] ); 98 } 99 100 // return the singleton EOF Token 101 + (id<ANTLRToken>) eofToken 102 { 103 if (EOF_TOKEN == nil) { 104 EOF_TOKEN = [[ANTLRCommonToken newToken:ANTLRTokenTypeEOF Text:@"EOF"] retain]; 105 } 106 return EOF_TOKEN; 107 } 108 109 // return the singleton skip Token 110 + (id<ANTLRToken>) skipToken 111 { 112 if (SKIP_TOKEN == nil) { 113 SKIP_TOKEN = [[ANTLRCommonToken newToken:ANTLRTokenTypeInvalid Text:@"Skip"] retain]; 114 } 115 return SKIP_TOKEN; 116 } 117 118 // return the singleton skip Token 119 + (id<ANTLRToken>) invalidToken 120 { 121 if (INVALID_TOKEN == nil) { 122 INVALID_TOKEN = [[ANTLRCommonToken newToken:ANTLRTokenTypeInvalid Text:@"Invalid"] retain]; 123 } 124 return SKIP_TOKEN; 125 } 126 127 // the default channel for this class of Tokens 128 + (ANTLRTokenChannel) defaultChannel 129 { 130 return ANTLRTokenChannelDefault; 131 } 132 133 - (id) init 134 { 135 if ((self = [super init]) != nil) { 136 input = nil; 137 type = ANTLRTokenTypeInvalid; 138 channel = ANTLRTokenChannelDefault; 139 startIndex = 0; 140 stopIndex = 0; 141 } 142 return self; 143 } 144 145 // designated initializer 146 - (id) initWithInput:(id<ANTLRCharStream>)anInput 147 Type:(NSInteger)aTType 148 Channel:(NSInteger)aChannel 149 Start:(NSInteger)aStart 150 Stop:(NSInteger)aStop 151 { 152 if ((self = [super init]) != nil) { 153 input = anInput; 154 if ( input ) [input retain]; 155 type = aTType; 156 channel = aChannel; 157 startIndex = aStart; 158 stopIndex = aStop; 159 if (type == ANTLRTokenTypeEOF) 160 text = @"EOF"; 161 else 162 text = [input substringWithRange:NSMakeRange(startIndex, (stopIndex-startIndex)+1)]; 163 if ( text ) [text retain]; 164 } 165 return self; 166 } 167 168 - (id) initWithToken:(ANTLRCommonToken *)oldToken 169 { 170 if ((self = [super init]) != nil) { 171 text = [NSString stringWithString:oldToken.text]; 172 if ( text ) [text retain]; 173 type = oldToken.type; 174 line = oldToken.line; 175 index = oldToken.index; 176 charPositionInLine = oldToken.charPositionInLine; 177 channel = oldToken.channel; 178 input = oldToken.input; 179 if ( input ) [input retain]; 180 if ( [oldToken isKindOfClass:[ANTLRCommonToken class]] ) { 181 startIndex = oldToken.startIndex; 182 stopIndex = oldToken.stopIndex; 183 } 184 } 185 return self; 186 } 187 188 - (id) initWithType:(ANTLRTokenType)aTType 189 { 190 if ((self = [super init]) != nil) { 191 self.type = aTType; 192 } 193 return self; 194 } 195 196 - (id) initWithType:(ANTLRTokenType)aTType Text:(NSString *)tokenText 197 { 198 if ((self = [super init]) != nil) { 199 self.type = aTType; 200 self.text = [NSString stringWithString:tokenText]; 201 if ( text ) [text retain]; 202 } 203 return self; 204 } 205 206 - (void)dealloc 207 { 208 #ifdef DEBUG_DEALLOC 209 NSLog( @"called dealloc in ANTLRCommonToken" ); 210 #endif 211 if ( input ) [input release]; 212 if ( text ) [text release]; 213 [super dealloc]; 214 } 215 216 // create a copy, including the text if available 217 // the input stream is *not* copied! 218 - (id) copyWithZone:(NSZone *)theZone 219 { 220 ANTLRCommonToken *copy = [[[self class] allocWithZone:theZone] init]; 221 222 if (text) 223 copy.text = [text copyWithZone:nil]; 224 copy.type = type; 225 copy.line = line; 226 copy.charPositionInLine = charPositionInLine; 227 copy.channel = channel; 228 copy.index = index; 229 copy.startIndex = startIndex; 230 copy.stopIndex = stopIndex; 231 copy.input = input; 232 return copy; 233 } 234 235 236 //---------------------------------------------------------- 237 // charPositionInLine 238 //---------------------------------------------------------- 239 - (NSUInteger) charPositionInLine 240 { 241 return charPositionInLine; 242 } 243 244 - (void) setCharPositionInLine:(NSUInteger)aCharPositionInLine 245 { 246 charPositionInLine = aCharPositionInLine; 247 } 248 249 //---------------------------------------------------------- 250 // line 251 //---------------------------------------------------------- 252 - (NSUInteger) line 253 { 254 return line; 255 } 256 257 - (void) setLine:(NSUInteger)aLine 258 { 259 line = aLine; 260 } 261 262 //---------------------------------------------------------- 263 // text 264 //---------------------------------------------------------- 265 - (NSString *) text 266 { 267 if (text != nil) { 268 return text; 269 } 270 if (input == nil) { 271 return nil; 272 } 273 return [input substringWithRange:NSMakeRange(startIndex, (stopIndex-startIndex)+1)]; 274 } 275 276 - (void) setText:(NSString *)aText 277 { 278 if (text != aText) { 279 if ( text ) [text release]; 280 text = aText; 281 [text retain]; 282 } 283 } 284 285 286 //---------------------------------------------------------- 287 // type 288 //---------------------------------------------------------- 289 - (NSInteger)type 290 { 291 return type; 292 } 293 294 - (void) setType:(NSInteger)aType 295 { 296 type = aType; 297 } 298 299 //---------------------------------------------------------- 300 // channel 301 //---------------------------------------------------------- 302 - (NSUInteger)channel 303 { 304 return channel; 305 } 306 307 - (void) setChannel:(NSUInteger)aChannel 308 { 309 channel = aChannel; 310 } 311 312 313 //---------------------------------------------------------- 314 // input 315 //---------------------------------------------------------- 316 - (id<ANTLRCharStream>) input 317 { 318 return input; 319 } 320 321 - (void) setInput: (id<ANTLRCharStream>) anInput 322 { 323 if (input != anInput) { 324 if ( input ) [input release]; 325 [anInput retain]; 326 } 327 input = anInput; 328 } 329 330 331 //---------------------------------------------------------- 332 // start 333 //---------------------------------------------------------- 334 - (NSInteger) getStart 335 { 336 return startIndex; 337 } 338 339 - (void) setStart: (NSInteger) aStart 340 { 341 startIndex = aStart; 342 } 343 344 //---------------------------------------------------------- 345 // stop 346 //---------------------------------------------------------- 347 - (NSInteger) getStop 348 { 349 return stopIndex; 350 } 351 352 - (void) setStop: (NSInteger) aStop 353 { 354 stopIndex = aStop; 355 } 356 357 //---------------------------------------------------------- 358 // index 359 //---------------------------------------------------------- 360 - (NSInteger) getTokenIndex; 361 { 362 return index; 363 } 364 365 - (void) setTokenIndex: (NSInteger) aTokenIndex; 366 { 367 index = aTokenIndex; 368 } 369 370 371 // provide a textual representation for debugging 372 - (NSString *) description 373 { 374 NSString *channelStr; 375 NSMutableString *txtString; 376 377 channelStr = @""; 378 if ( channel > 0 ) { 379 channelStr = [NSString stringWithFormat:@",channel=%d\n", channel]; 380 } 381 if ([self text] != nil) { 382 txtString = [NSMutableString stringWithString:[self text]]; 383 [txtString replaceOccurrencesOfString:@"\n" withString:@"\\\\n" options:NSAnchoredSearch range:NSMakeRange(0, [txtString length])]; 384 [txtString replaceOccurrencesOfString:@"\r" withString:@"\\\\r" options:NSAnchoredSearch range:NSMakeRange(0, [txtString length])]; 385 [txtString replaceOccurrencesOfString:@"\t" withString:@"\\\\t" options:NSAnchoredSearch range:NSMakeRange(0, [txtString length])]; 386 } else { 387 txtString = [NSMutableString stringWithString:@"<no text>"]; 388 } 389 return [NSString stringWithFormat:@"[@%d, %d:%d='%@',<%d>%@,%d:%d]", index, startIndex, stopIndex, txtString, type, channelStr, line, charPositionInLine]; 390 } 391 392 - (NSString *)toString 393 { 394 return [self description]; 395 } 396 397 @end 398