Working on type parsing error handling.

This commit is contained in:
Steve Nygard 2011-06-18 17:19:31 -06:00
parent d2d87b7d65
commit 6932c5c773
10 changed files with 115 additions and 123 deletions

View File

@ -56,7 +56,7 @@
{
if (hasParsedType == NO) {
CDTypeParser *parser;
NSError *error;
NSError *error = nil;
parser = [[CDTypeParser alloc] initWithType:type];
parsedType = [[parser parseType:&error] retain];

View File

@ -76,7 +76,7 @@
{
if (hasParsedType == NO) {
CDTypeParser *parser;
NSError *error;
NSError *error = nil;
parser = [[CDTypeParser alloc] initWithType:type];
parsedMethodTypes = [[parser parseMethodType:&error] retain];

View File

@ -170,7 +170,7 @@ static BOOL debug = NO;
scanner = [[NSScanner alloc] initWithString:attributeString];
if ([scanner scanString:@"T" intoString:NULL]) {
NSError *error;
NSError *error = nil;
NSRange typeRange;
CDTypeParser *parser;

View File

@ -554,7 +554,7 @@ static BOOL debugMerge = NO;
- (id)copyWithZone:(NSZone *)zone;
{
CDTypeParser *parser;
NSError *error;
NSError *error = nil;
NSString *str;
CDType *copiedType;

View File

@ -114,7 +114,7 @@ static BOOL debug = NO;
NSString *specialCase;
CDTypeParser *aParser;
CDType *resultType;
NSError *error;
NSError *error = nil;
// Special cases: char -> BOOLs, 1 bit ints -> BOOL too?
specialCase = [self _specialCaseVariable:name type:type];
@ -134,7 +134,7 @@ static BOOL debug = NO;
//NSLog(@"resultType: %p", resultType);
if (resultType == nil) {
NSLog(@"Couldn't parse type: %@", [error myExplanation]);
NSLog(@"Couldn't parse type: %@", [[error userInfo] objectForKey:CDErrorKey_LocalizedLongDescription]);
[aParser release];
//NSLog(@"< %s", __cmd);
return nil;
@ -171,7 +171,7 @@ static BOOL debug = NO;
{
CDTypeParser *aParser;
NSArray *methodTypes;
NSError *error;
NSError *error = nil;
NSMutableDictionary *typeDict;
NSMutableArray *parameterTypes;
@ -265,7 +265,7 @@ static BOOL debug = NO;
CDTypeParser *aParser;
NSArray *methodTypes;
NSMutableString *resultString;
NSError *error;
NSError *error = nil;
aParser = [[CDTypeParser alloc] initWithType:type];
methodTypes = [aParser parseMethodType:&error];

View File

@ -9,8 +9,13 @@
@class CDMethodType, CDType, CDTypeLexer, CDTypeName;
extern NSString *CDSyntaxError;
extern NSString *CDTypeParserErrorDomain;
extern NSString *CDExceptionName_SyntaxError;
extern NSString *CDErrorDomain_TypeParser;
extern NSString *CDErrorKey_Type;
extern NSString *CDErrorKey_RemainingString;
extern NSString *CDErrorKey_MethodOrVariable;
extern NSString *CDErrorKey_LocalizedLongDescription;
#define CDTypeParserCode_Default 0
#define CDTypeParserCode_SyntaxError 1

View File

@ -12,8 +12,14 @@
#import "CDTypeLexer.h"
#import "NSString-Extensions.h"
NSString *CDSyntaxError = @"Syntax Error";
NSString *CDTypeParserErrorDomain = @"CDTypeParserErrorDomain";
NSString *CDExceptionName_SyntaxError = @"CDExceptionName_SyntaxError";
NSString *CDErrorDomain_TypeParser = @"CDErrorDomain_TypeParser";
NSString *CDErrorKey_Type = @"CDErrorKey_Type";
NSString *CDErrorKey_RemainingString = @"CDErrorKey_RemainingString";
NSString *CDErrorKey_MethodOrVariable = @"CDErrorKey_MethodOrVariable";
NSString *CDErrorKey_LocalizedLongDescription = @"CDErrorKey_LocalizedLongDescription";
static BOOL debug = NO;
@ -61,34 +67,30 @@ static NSString *CDTokenDescription(int token)
{
NSArray *result;
*error = nil;
@try {
lookahead = [lexer scanNextToken];
result = [self _parseMethodType];
}
@catch (NSException *exception) {
NSDictionary *userInfo;
int code;
// Obviously I need to figure out a sane method of dealing with errors here. This is not.
if ([[exception name] isEqual:CDSyntaxError]) {
code = CDTypeParserCode_SyntaxError;
userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:@"Syntax Error", @"reason",
[NSString stringWithFormat:@"Syntax Error, %@:\n\t type: %@\n\tremaining: %@",
[exception reason], [lexer string], [lexer remainingString]], @"explanation",
[lexer string], @"type",
[lexer remainingString], @"remaining string",
nil];
} else {
code = CDTypeParserCode_Default;
userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:[exception reason], @"reason",
[lexer string], @"type",
[lexer remainingString], @"remaining string",
nil];
if (error != NULL) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
int code;
[userInfo setObject:[lexer string] forKey:CDErrorKey_Type];
[userInfo setObject:[lexer remainingString] forKey:CDErrorKey_RemainingString];
[userInfo setObject:@"method" forKey:CDErrorKey_MethodOrVariable];
[userInfo setObject:[NSString stringWithFormat:@"%@:\n\t type: %@\n\tremaining: %@", [exception reason], [lexer string], [lexer remainingString]] forKey:CDErrorKey_LocalizedLongDescription];
if ([exception name] == CDExceptionName_SyntaxError) {
code = CDTypeParserCode_SyntaxError;
[userInfo setObject:@"Syntax Error" forKey:NSLocalizedDescriptionKey];
[userInfo setObject:[exception reason] forKey:NSLocalizedFailureReasonErrorKey];
} else {
code = CDTypeParserCode_Default;
[userInfo setObject:[exception reason] forKey:NSLocalizedFailureReasonErrorKey];
}
*error = [NSError errorWithDomain:CDErrorDomain_TypeParser code:code userInfo:userInfo];
}
*error = [NSError errorWithDomain:CDTypeParserErrorDomain code:code userInfo:userInfo];
[userInfo release];
result = nil;
}
@ -100,34 +102,30 @@ static NSString *CDTokenDescription(int token)
{
CDType *result;
*error = nil;
@try {
lookahead = [lexer scanNextToken];
result = [self _parseType];
}
@catch (NSException *exception) {
NSDictionary *userInfo;
int code;
// Obviously I need to figure out a sane method of dealing with errors here. This is not.
if ([[exception name] isEqual:CDSyntaxError]) {
code = CDTypeParserCode_SyntaxError;
userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:@"Syntax Error", @"reason",
[NSString stringWithFormat:@"%@:\n\t type: %@\n\tremaining: %@",
[exception reason], [lexer string], [lexer remainingString]], @"explanation",
[lexer string], @"type",
[lexer remainingString], @"remaining string",
nil];
} else {
code = CDTypeParserCode_Default;
userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:[exception reason], @"reason",
[lexer string], @"type",
[lexer remainingString], @"remaining string",
nil];
if (error != NULL) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
int code;
[userInfo setObject:[lexer string] forKey:CDErrorKey_Type];
[userInfo setObject:[lexer remainingString] forKey:CDErrorKey_RemainingString];
[userInfo setObject:@"variable" forKey:CDErrorKey_MethodOrVariable];
[userInfo setObject:[NSString stringWithFormat:@"%@:\n\t type: %@\n\tremaining: %@", [exception reason], [lexer string], [lexer remainingString]] forKey:CDErrorKey_LocalizedLongDescription];
if ([exception name] == CDExceptionName_SyntaxError) {
code = CDTypeParserCode_SyntaxError;
[userInfo setObject:@"Syntax Error" forKey:NSLocalizedDescriptionKey];
[userInfo setObject:[exception reason] forKey:NSLocalizedFailureReasonErrorKey];
} else {
code = CDTypeParserCode_Default;
[userInfo setObject:[exception reason] forKey:NSLocalizedFailureReasonErrorKey];
}
*error = [NSError errorWithDomain:CDErrorDomain_TypeParser code:code userInfo:userInfo];
}
*error = [NSError errorWithDomain:CDTypeParserErrorDomain code:code userInfo:userInfo];
[userInfo release];
result = nil;
}
@ -151,7 +149,7 @@ static NSString *CDTokenDescription(int token)
[lexer setState:newState];
lookahead = [lexer scanNextToken];
} else {
[NSException raise:CDSyntaxError format:@"expected token %@, got %@",
[NSException raise:CDExceptionName_SyntaxError format:@"expected token %@, got %@",
CDTokenDescription(token),
CDTokenDescription(lookahead)];
}
@ -159,7 +157,7 @@ static NSString *CDTokenDescription(int token)
- (void)error:(NSString *)errorString;
{
[NSException raise:CDSyntaxError format:@"%@", errorString];
[NSException raise:CDExceptionName_SyntaxError format:@"%@", errorString];
}
- (NSArray *)_parseMethodType;
@ -329,7 +327,7 @@ static NSString *CDTokenDescription(int token)
result = [[CDType alloc] initSimpleType:simpleType];
} else {
result = nil;
[NSException raise:CDSyntaxError format:@"expected (many things), got %@", CDTokenDescription(lookahead)];
[NSException raise:CDExceptionName_SyntaxError format:@"expected (many things), got %@", CDTokenDescription(lookahead)];
}
return [result autorelease];

View File

@ -7,6 +7,6 @@
@interface NSError (CDExtensions)
- (NSString *)myExplanation;
@end
extern NSString *NSErrorDomain_ClassDump;

View File

@ -5,16 +5,8 @@
#import "NSError-CDExtensions.h"
NSString *NSErrorDomain_ClassDump = @"com.codethecode.MachObjC.ErrorDomain";
@implementation NSError (CDExtensions)
// The normal methods confuse me, and it's late.
- (NSString *)myExplanation;
{
NSString *str = [[self userInfo] objectForKey:@"explanation"];
if (str != nil)
return str;
return [self description];
}
@end

View File

@ -39,22 +39,14 @@ enum {
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUInteger formatType = CDFormatIvar;
int ch;
BOOL errorFlag = NO;
CDTypeFormatter *ivarTypeFormatter;
CDTypeFormatter *methodTypeFormatter;
NSUInteger index;
ivarTypeFormatter = [[CDTypeFormatter alloc] init];
CDTypeFormatter *ivarTypeFormatter = [[CDTypeFormatter alloc] init];
[ivarTypeFormatter setShouldExpand:YES];
[ivarTypeFormatter setShouldAutoExpand:YES];
[ivarTypeFormatter setBaseLevel:0];
//[ivarTypeFormatter setDelegate:self];
methodTypeFormatter = [[CDTypeFormatter alloc] init];
CDTypeFormatter *methodTypeFormatter = [[CDTypeFormatter alloc] init];
[methodTypeFormatter setShouldExpand:NO];
[methodTypeFormatter setShouldAutoExpand:NO];
[methodTypeFormatter setBaseLevel:0];
@ -71,20 +63,25 @@ int main(int argc, char *argv[])
exit(0);
}
NSUInteger formatType = CDFormatIvar;
int ch;
BOOL errorFlag = NO;
while ( (ch = getopt_long(argc, argv, "bm", longopts, NULL)) != -1) {
switch (ch) {
case 'b':
formatType = CDFormatBalance;
break;
case 'm':
formatType = CDFormatMethod;
break;
case '?':
default:
errorFlag = YES;
break;
case 'b':
formatType = CDFormatBalance;
break;
case 'm':
formatType = CDFormatMethod;
break;
case '?':
default:
errorFlag = YES;
break;
}
}
@ -94,55 +91,55 @@ int main(int argc, char *argv[])
}
switch (formatType) {
case CDFormatIvar: printf("Format as ivars\n"); break;
case CDFormatMethod: printf("Format as methods\n"); break;
case CDFormatBalance: printf("Format as balance\n"); break;
case CDFormatIvar: printf("Format as ivars\n"); break;
case CDFormatMethod: printf("Format as methods\n"); break;
case CDFormatBalance: printf("Format as balance\n"); break;
}
for (index = optind; index < argc; index++) {
NSString *arg;
NSString *input;
NSError *error;
NSArray *lines;
NSString *name, *type;
for (NSUInteger index = optind; index < argc; index++) {
arg = [NSString stringWithFileSystemRepresentation:argv[index]];
NSString *arg = [NSString stringWithFileSystemRepresentation:argv[index]];
printf("======================================================================\n");
printf("File: %s\n", argv[index]);
input = [[NSString alloc] initWithContentsOfFile:arg encoding:NSUTF8StringEncoding error:&error];
lines = [input componentsSeparatedByString:@"\n"];
NSError *error = nil;
NSString *input = [[NSString alloc] initWithContentsOfFile:arg encoding:NSUTF8StringEncoding error:&error];
if (error != nil) {
NSLog(@"input error: %@", error);
NSLog(@"localizedFailureReason: %@", [error localizedFailureReason]);
}
name = type = nil;
NSArray *lines = [input componentsSeparatedByString:@"\n"];
NSString *name = nil;
NSString *type = nil;
for (NSString *line in lines) {
if ([line hasPrefix:@"//"] || [line length] == 0) {
printf("%s\n", [line UTF8String]);
continue;
}
if (name == nil)
if (name == nil) {
name = line;
else if (type == nil) {
} else if (type == nil) {
NSString *str;
type = line;
switch (formatType) {
case CDFormatIvar:
str = [ivarTypeFormatter formatVariable:name type:type symbolReferences:nil];
break;
case CDFormatMethod:
str = [methodTypeFormatter formatMethodName:name type:type symbolReferences:nil];
break;
case CDFormatBalance: {
CDBalanceFormatter *balance;
balance = [[CDBalanceFormatter alloc] initWithString:type];
str = [balance format];
[balance release];
}
case CDFormatIvar:
str = [ivarTypeFormatter formatVariable:name type:type symbolReferences:nil];
break;
case CDFormatMethod:
str = [methodTypeFormatter formatMethodName:name type:type symbolReferences:nil];
break;
case CDFormatBalance: {
CDBalanceFormatter *balance = [[CDBalanceFormatter alloc] initWithString:type];
str = [balance format];
[balance release];
}
}
if (str == nil)
printf("Error formatting type.\n");