1// [The "BSD licence"] 2// Copyright (c) 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 "RecognizerSharedState.h" 28#import "CharStream.h" 29#import "CommonToken.h" 30#import "MismatchedTokenException.h" 31#import "MismatchedRangeException.h" 32 33@implementation RecognizerSharedState 34 35@synthesize following; 36@synthesize _fsp; 37@synthesize errorRecovery; 38@synthesize lastErrorIndex; 39@synthesize failed; 40@synthesize syntaxErrors; 41@synthesize backtracking; 42@synthesize ruleMemo; 43@synthesize token; 44@synthesize type; 45@synthesize channel; 46@synthesize tokenStartLine; 47@synthesize tokenStartCharPositionInLine; 48@synthesize tokenStartCharIndex; 49@synthesize text; 50 51+ (RecognizerSharedState *) newRecognizerSharedState 52{ 53 return [[[RecognizerSharedState alloc] init] retain]; 54} 55 56+ (RecognizerSharedState *) newRecognizerSharedStateWithRuleLen:(NSInteger)aLen 57{ 58 return [[[RecognizerSharedState alloc] initWithRuleLen:aLen] retain]; 59} 60 61+ (RecognizerSharedState *) newRecognizerSharedState:(RecognizerSharedState *)aState 62{ 63 return [[[RecognizerSharedState alloc] initWithState:aState] retain]; 64} 65 66- (id) init 67{ 68 HashRule *aHashRule; 69 if ((self = [super init]) != nil ) { 70 following = [[AMutableArray arrayWithCapacity:10] retain]; 71 _fsp = -1; 72 errorRecovery = NO; // are we recovering? 73 lastErrorIndex = -1; 74 failed = NO; // indicate that some match failed 75 syntaxErrors = 0; 76 backtracking = 0; // the level of backtracking 77 tokenStartCharIndex = -1; 78 tokenStartLine = 0; 79 int cnt = 200; 80 ruleMemo = [[RuleStack newRuleStack:cnt] retain]; 81 for (int i = 0; i < cnt; i++ ) { 82 aHashRule = [[HashRule newHashRuleWithLen:17] retain]; 83 [ruleMemo addObject:aHashRule]; 84 } 85#ifdef DONTUSEYET 86 token = state.token; 87 tokenStartCharIndex = state.tokenStartCharIndex; 88 tokenStartCharPositionInLine = state.tokenStartCharPositionInLine; 89 channel = state.channel; 90 type = state.type; 91 text = state.text; 92#endif 93 } 94 return self; 95} 96 97- (id) initWithRuleLen:(NSInteger)aLen 98{ 99 HashRule *aHashRule; 100 if ((self = [super init]) != nil ) { 101 following = [[AMutableArray arrayWithCapacity:10] retain]; 102 _fsp = -1; 103 errorRecovery = NO; // are we recovering? 104 lastErrorIndex = -1; 105 failed = NO; // indicate that some match failed 106 syntaxErrors = 0; 107 backtracking = 0; // the level of backtracking 108 tokenStartCharIndex = -1; 109 tokenStartLine = 0; 110 ruleMemo = [[RuleStack newRuleStack:aLen] retain]; 111 for (int i = 0; i < aLen; i++ ) { 112 aHashRule = [[HashRule newHashRuleWithLen:17] retain]; 113 [ruleMemo addObject:aHashRule]; 114 } 115#ifdef DONTUSEYET 116 token = state.token; 117 tokenStartCharIndex = state.tokenStartCharIndex; 118 tokenStartCharPositionInLine = state.tokenStartCharPositionInLine; 119 channel = state.channel; 120 type = state.type; 121 text = state.text; 122#endif 123 } 124 return self; 125} 126 127- (id) initWithState:(RecognizerSharedState *)aState 128{ 129 HashRule *aHashRule; 130 if ( [following count] < [aState.following count] ) { 131 // following = new BitSet[state.following.size]; 132 } 133 [following setArray:aState.following]; 134 _fsp = aState._fsp; 135 errorRecovery = aState.errorRecovery; 136 lastErrorIndex = aState.lastErrorIndex; 137 failed = aState.failed; 138 syntaxErrors = aState.syntaxErrors; 139 backtracking = aState.backtracking; 140 if ( aState.ruleMemo == nil ) { 141 int cnt = 200; 142 ruleMemo = [[RuleStack newRuleStack:cnt] retain]; 143 for (int i = 0; i < cnt; i++ ) { 144 aHashRule = [[HashRule newHashRuleWithLen:17] retain]; 145 [ruleMemo addObject:aHashRule]; 146 } 147 } 148 else { 149 ruleMemo = aState.ruleMemo; 150 if ( [ruleMemo count] == 0 ) { 151 int cnt = [ruleMemo length]; 152 for (int i = 0; i < cnt; i++ ) { 153 [ruleMemo addObject:[[HashRule newHashRuleWithLen:17] retain]]; 154 } 155 } 156 else { 157 [ruleMemo addObjectsFromArray:aState.ruleMemo]; 158 } 159 } 160 token = aState.token; 161 tokenStartCharIndex = aState.tokenStartCharIndex; 162 tokenStartCharPositionInLine = aState.tokenStartCharPositionInLine; 163 tokenStartLine = aState.tokenStartLine; 164 channel = aState.channel; 165 type = aState.type; 166 text = aState.text; 167 return( self ); 168} 169 170- (void) dealloc 171{ 172#ifdef DEBUG_DEALLOC 173 NSLog( @"called dealloc in RecognizerSharedState" ); 174#endif 175 if ( token ) [token release]; 176 if ( following ) [following release]; 177 if ( ruleMemo ) [ruleMemo release]; 178 [super dealloc]; 179} 180 181// token stuff 182#pragma mark Tokens 183 184- (id<Token>)getToken 185{ 186 return token; 187} 188 189- (void) setToken: (id<Token>) aToken 190{ 191 if (token != aToken) { 192 [aToken retain]; 193 if ( token ) [token release]; 194 token = aToken; 195 } 196} 197 198- (NSUInteger)channel 199{ 200 return channel; 201} 202 203- (void) setChannel:(NSUInteger) theChannel 204{ 205 channel = theChannel; 206} 207 208- (NSUInteger) getTokenStartLine 209{ 210 return tokenStartLine; 211} 212 213- (void) setTokenStartLine:(NSUInteger) theTokenStartLine 214{ 215 tokenStartLine = theTokenStartLine; 216} 217 218- (NSUInteger) charPositionInLine 219{ 220 return tokenStartCharPositionInLine; 221} 222 223- (void) setCharPositionInLine:(NSUInteger) theCharPosition 224{ 225 tokenStartCharPositionInLine = theCharPosition; 226} 227 228- (NSInteger) getTokenStartCharIndex; 229{ 230 return tokenStartCharIndex; 231} 232 233- (void) setTokenStartCharIndex:(NSInteger) theTokenStartCharIndex 234{ 235 tokenStartCharIndex = theTokenStartCharIndex; 236} 237 238// error handling 239- (void) reportError:(RecognitionException *)e 240{ 241 NSLog(@"%@", e.name); 242} 243 244- (AMutableArray *) getFollowing 245{ 246 return following; 247} 248 249- (void)setFollowing:(AMutableArray *)aFollow 250{ 251 if ( following != aFollow ) { 252 if ( following ) [following release]; 253 [aFollow retain]; 254 } 255 following = aFollow; 256} 257 258- (RuleStack *) getRuleMemo 259{ 260 return ruleMemo; 261} 262 263- (void)setRuleMemo:(RuleStack *)aRuleMemo 264{ 265 if ( ruleMemo != aRuleMemo ) { 266 if ( ruleMemo ) [ruleMemo release]; 267 [aRuleMemo retain]; 268 } 269 ruleMemo = aRuleMemo; 270} 271 272- (BOOL) isErrorRecovery 273{ 274 return errorRecovery; 275} 276 277- (void) setIsErrorRecovery: (BOOL) flag 278{ 279 errorRecovery = flag; 280} 281 282 283- (BOOL) getFailed 284{ 285 return failed; 286} 287 288- (void) setFailed:(BOOL)flag 289{ 290 failed = flag; 291} 292 293 294- (NSInteger) backtracking 295{ 296 return backtracking; 297} 298 299- (void) setBacktracking:(NSInteger) value 300{ 301 backtracking = value; 302} 303 304- (void) increaseBacktracking 305{ 306 backtracking++; 307} 308 309- (void) decreaseBacktracking 310{ 311 backtracking--; 312} 313 314- (BOOL) isBacktracking 315{ 316 return backtracking > 0; 317} 318 319 320- (NSInteger) lastErrorIndex 321{ 322 return lastErrorIndex; 323} 324 325- (void) setLastErrorIndex:(NSInteger) value 326{ 327 lastErrorIndex = value; 328} 329 330 331@end 332