Skip to content

Commit

Permalink
Remove parser as argument to all its delegate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stig committed Nov 14, 2013
1 parent e8a1444 commit faaa654
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 46 deletions.
38 changes: 19 additions & 19 deletions src/main/objc/SBJsonChunkParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ - (void)pop {
}

- (void)parser:(SBJsonStreamParser *)parser found:(id)obj {
[self parser:parser found:obj isValue:NO];
[self parserFound:obj isValue:NO ];
}

- (void)parser:(SBJsonStreamParser *)parser found:(id)obj isValue:(BOOL)isValue {
- (void)parserFound:(id)obj isValue:(BOOL)isValue {
NSParameterAssert(obj);

if(processBlock&&path) {
Expand Down Expand Up @@ -170,7 +170,7 @@ - (void)parser:(SBJsonStreamParser *)parser found:(id)obj isValue:(BOOL)isValue

#pragma mark Delegate methods

- (void)parserFoundObjectStart:(SBJsonStreamParser *)parser {
- (void)parserFoundObjectStart {
++depth;
if (depth > _maxDepth)
[self maxDepthError];
Expand All @@ -182,18 +182,18 @@ - (void)parserFoundObjectStart:(SBJsonStreamParser *)parser {
currentType = SBJsonChunkObject;
}

- (void)parser:(SBJsonStreamParser *)parser foundObjectKey:(NSString*)key_ {
- (void)parserFoundObjectKey:(NSString *)key_ {
[keyStack addObject:key_];
}

- (void)parserFoundObjectEnd:(SBJsonStreamParser *)parser {
- (void)parserFoundObjectEnd {
depth--;
id value = dict;
[self pop];
[self parser:parser found:value];
[self parser:_parser found:value];
}

- (void)parserFoundArrayStart:(SBJsonStreamParser *)parser {
- (void)parserFoundArrayStart {
depth++;
if (depth > _maxDepth)
[self maxDepthError];
Expand All @@ -207,12 +207,12 @@ - (void)parserFoundArrayStart:(SBJsonStreamParser *)parser {
}
}

- (void)parserFoundArrayEnd:(SBJsonStreamParser *)parser {
- (void)parserFoundArrayEnd {
depth--;
if (depth > 1 || !supportPartialDocuments) {
id value = array;
[self pop];
[self parser:parser found:value];
[self parser:_parser found:value];
}
}

Expand All @@ -222,23 +222,23 @@ - (void)maxDepthError {
[_parser stop];
}

- (void)parser:(SBJsonStreamParser *)parser foundBoolean:(BOOL)x {
[self parser:parser found:[NSNumber numberWithBool:x] isValue:YES];
- (void)parserFoundBoolean:(BOOL)x {
[self parserFound:[NSNumber numberWithBool:x] isValue:YES ];
}

- (void)parserFoundNull:(SBJsonStreamParser *)parser {
[self parser:parser found:[NSNull null] isValue:YES];
- (void)parserFoundNull {
[self parserFound:[NSNull null] isValue:YES ];
}

- (void)parser:(SBJsonStreamParser *)parser foundNumber:(NSNumber*)num {
[self parser:parser found:num isValue:YES];
- (void)parserFoundNumber:(NSNumber *)num {
[self parserFound:num isValue:YES ];
}

- (void)parser:(SBJsonStreamParser *)parser foundString:(NSString*)string {
[self parser:parser found:string isValue:YES];
- (void)parserFoundString:(NSString *)string {
[self parserFound:string isValue:YES ];
}

- (void)parser:(SBJsonStreamParser *)parser foundError:(NSError *)err {
- (void)parserFoundError:(NSError *)err {
errorHandler(err);
}

Expand All @@ -262,7 +262,7 @@ - (NSString *)pathString {
return pathString;
}

- (BOOL)parserShouldSupportManyDocuments:(SBJsonStreamParser *)parser {
- (BOOL)parserShouldSupportManyDocuments {
return supportManyDocuments;
}

Expand Down
22 changes: 11 additions & 11 deletions src/main/objc/SBJsonStreamParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,39 @@ typedef enum {
@protocol SBJsonStreamParserDelegate < NSObject >

/// Called when object start is found
- (void)parserFoundObjectStart:(SBJsonStreamParser *)parser;
- (void)parserFoundObjectStart;

/// Called when object key is found
- (void)parser:(SBJsonStreamParser *)parser foundObjectKey:(NSString*)key;
- (void)parserFoundObjectKey:(NSString *)key;

/// Called when object end is found
- (void)parserFoundObjectEnd:(SBJsonStreamParser *)parser;
- (void)parserFoundObjectEnd;

/// Called when array start is found
- (void)parserFoundArrayStart:(SBJsonStreamParser *)parser;
- (void)parserFoundArrayStart;

/// Called when array end is found
- (void)parserFoundArrayEnd:(SBJsonStreamParser *)parser;
- (void)parserFoundArrayEnd;

/// Called when a boolean value is found
- (void)parser:(SBJsonStreamParser *)parser foundBoolean:(BOOL)x;
- (void)parserFoundBoolean:(BOOL)x;

/// Called when a null value is found
- (void)parserFoundNull:(SBJsonStreamParser *)parser;
- (void)parserFoundNull;

/// Called when a number is found
- (void)parser:(SBJsonStreamParser *)parser foundNumber:(NSNumber*)num;
- (void)parserFoundNumber:(NSNumber *)num;

/// Called when a string is found
- (void)parser:(SBJsonStreamParser *)parser foundString:(NSString*)string;
- (void)parserFoundString:(NSString *)string;

/// Called when an error occurs
- (void)parser:(SBJsonStreamParser *)parser foundError:(NSError*)err;
- (void)parserFoundError:(NSError *)err;

@optional

/// Called to determine whether to allow multiple whitespace-separated documents
- (BOOL)parserShouldSupportManyDocuments:(SBJsonStreamParser *)parser;
- (BOOL)parserShouldSupportManyDocuments;

@end

Expand Down
31 changes: 16 additions & 15 deletions src/main/objc/SBJsonStreamParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ - (NSString*)tokenName:(sbjson_token_t)token {
}

- (void)handleObjectStart {
[_delegate parserFoundObjectStart:self];
[_delegate parserFoundObjectStart];
[_stateStack addObject:_state];
_state = [SBJsonStreamParserStateObjectStart sharedInstance];
}
Expand All @@ -122,11 +122,11 @@ - (void)handleObjectEnd: (sbjson_token_t) tok {
_state = [_stateStack lastObject];
[_stateStack removeLastObject];
[_state parser:self shouldTransitionTo:tok];
[_delegate parserFoundObjectEnd:self];
[_delegate parserFoundObjectEnd];
}

- (void)handleArrayStart {
[_delegate parserFoundArrayStart:self];
[_delegate parserFoundArrayStart];
[_stateStack addObject:_state];
_state = [SBJsonStreamParserStateArrayStart sharedInstance];
}
Expand All @@ -135,7 +135,7 @@ - (void)handleArrayEnd: (sbjson_token_t) tok {
_state = [_stateStack lastObject];
[_stateStack removeLastObject];
[_state parser:self shouldTransitionTo:tok];
[_delegate parserFoundArrayEnd:self];
[_delegate parserFoundArrayEnd];
}

- (void) handleTokenNotExpectedHere: (sbjson_token_t) tok {
Expand All @@ -144,7 +144,7 @@ - (void) handleTokenNotExpectedHere: (sbjson_token_t) tok {

_state = [SBJsonStreamParserStateError sharedInstance];
id ui = @{ NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Token '%@' not expected %@", tokenName, stateName]};
[_delegate parser:self foundError:[NSError errorWithDomain:@"org.sbjson.parser" code:2 userInfo:ui]];
[_delegate parserFoundError:[NSError errorWithDomain:@"org.sbjson.parser" code:2 userInfo:ui]];
}

- (SBJsonParserStatus)parse:(NSData *)data_ {
Expand All @@ -170,7 +170,8 @@ - (SBJsonParserStatus)parse:(NSData *)data_ {

case sbjson_token_error:
_state = [SBJsonStreamParserStateError sharedInstance];
[_delegate parser:self foundError:[NSError errorWithDomain:@"org.sbjson.parser" code:3 userInfo:@{ NSLocalizedDescriptionKey : tokeniser.error }]];
[_delegate parserFoundError:[NSError errorWithDomain:@"org.sbjson.parser" code:3
userInfo:@{NSLocalizedDescriptionKey : tokeniser.error}]];
return SBJsonParserError;
break;

Expand Down Expand Up @@ -204,23 +205,23 @@ - (SBJsonParserStatus)parse:(NSData *)data_ {
break;

case sbjson_token_bool:
[_delegate parser:self foundBoolean:token[0] == 't'];
[_delegate parserFoundBoolean:token[0] == 't'];
[_state parser:self shouldTransitionTo:tok];
break;


case sbjson_token_null:
[_delegate parserFoundNull:self];
[_delegate parserFoundNull];
[_state parser:self shouldTransitionTo:tok];
break;

case sbjson_token_integer: {
const int UNSIGNED_LONG_LONG_MAX_DIGITS = 20;
if (token_len <= UNSIGNED_LONG_LONG_MAX_DIGITS) {
if (*token == '-')
[_delegate parser:self foundNumber: @(strtoll(token, NULL, 10))];
[_delegate parserFoundNumber:@(strtoll(token, NULL, 10))];
else
[_delegate parser:self foundNumber: @(strtoull(token, NULL, 10))];
[_delegate parserFoundNumber:@(strtoull(token, NULL, 10))];

[_state parser:self shouldTransitionTo:tok];
break;
Expand All @@ -229,27 +230,27 @@ - (SBJsonParserStatus)parse:(NSData *)data_ {
// FALLTHROUGH

case sbjson_token_real: {
[_delegate parser:self foundNumber: @(strtod(token, NULL))];
[_delegate parserFoundNumber:@(strtod(token, NULL))];
[_state parser:self shouldTransitionTo:tok];
break;
}

case sbjson_token_string: {
NSString *string = [[NSString alloc] initWithBytes:token length:token_len encoding:NSUTF8StringEncoding];
if ([_state needKey])
[_delegate parser:self foundObjectKey:string];
[_delegate parserFoundObjectKey:string];
else
[_delegate parser:self foundString:string];
[_delegate parserFoundString:string];
[_state parser:self shouldTransitionTo:tok];
break;
}

case sbjson_token_encoded: {
NSString *string = [self decodeStringToken:token length:token_len];
if ([_state needKey])
[_delegate parser:self foundObjectKey:string];
[_delegate parserFoundObjectKey:string];
else
[_delegate parser:self foundString:string];
[_delegate parserFoundString:string];
[_state parser:self shouldTransitionTo:tok];
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/objc/SBJsonStreamParserState.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ - (void)parser:(SBJsonStreamParser *)parser shouldTransitionTo:(sbjson_token_t)t

case sbjson_token_array_close:
case sbjson_token_object_close:
if ([parser.delegate respondsToSelector:@selector(parserShouldSupportManyDocuments:)] && [parser.delegate parserShouldSupportManyDocuments:parser])
if ([parser.delegate respondsToSelector:@selector(parserShouldSupportManyDocuments)] && [parser.delegate parserShouldSupportManyDocuments])
state = parser.state;
else
state = [SBJsonStreamParserStateComplete sharedInstance];
Expand Down

0 comments on commit faaa654

Please sign in to comment.