Permalink
Browse files

change to an NSError API instead of an NSException API

  • Loading branch information...
1 parent cd12b71 commit 3c188144fe61982a11b120d21dd906095b289ee7 @davedelong committed Apr 2, 2011
@@ -16,6 +16,10 @@
5508992212BCAD5C00BBC2D9 /* DDOperatorTerm.m in Sources */ = {isa = PBXBuildFile; fileRef = 5508992112BCAD5C00BBC2D9 /* DDOperatorTerm.m */; };
5508992312BCAD5C00BBC2D9 /* DDOperatorTerm.m in Sources */ = {isa = PBXBuildFile; fileRef = 5508992112BCAD5C00BBC2D9 /* DDOperatorTerm.m */; };
5508992412BCAD5C00BBC2D9 /* DDOperatorTerm.m in Sources */ = {isa = PBXBuildFile; fileRef = 5508992112BCAD5C00BBC2D9 /* DDOperatorTerm.m */; };
+ 5514EF161347EBA10010B9D8 /* DDTypes.m in Sources */ = {isa = PBXBuildFile; fileRef = 5526E9E01310EF880018B4C9 /* DDTypes.m */; };
+ 5514EF171347EBA20010B9D8 /* DDTypes.m in Sources */ = {isa = PBXBuildFile; fileRef = 5526E9E01310EF880018B4C9 /* DDTypes.m */; };
+ 5514EF181347EBB00010B9D8 /* NSNumberFormatter+DDMathParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 55DDAB2A134044680088A170 /* NSNumberFormatter+DDMathParser.m */; };
+ 5514EF191347EBB10010B9D8 /* NSNumberFormatter+DDMathParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 55DDAB2A134044680088A170 /* NSNumberFormatter+DDMathParser.m */; };
552532B712C1C92D00A942A7 /* _DDFunctionUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 552532B612C1C92D00A942A7 /* _DDFunctionUtilities.m */; };
552532B812C1C92D00A942A7 /* _DDFunctionUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 552532B612C1C92D00A942A7 /* _DDFunctionUtilities.m */; };
552532B912C1C92D00A942A7 /* _DDFunctionUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 552532B612C1C92D00A942A7 /* _DDFunctionUtilities.m */; };
@@ -28,7 +32,6 @@
5537E36312AB20F30000CFED /* DDMathStringTokenizer.m in Sources */ = {isa = PBXBuildFile; fileRef = 55BFA9BE128CDFD600A80228 /* DDMathStringTokenizer.m */; };
5537E36412AB20F30000CFED /* DDMathStringToken.m in Sources */ = {isa = PBXBuildFile; fileRef = 55F795E21293175B00EF2716 /* DDMathStringToken.m */; };
5537E36512AB20F30000CFED /* NSString+DDMathParsing.m in Sources */ = {isa = PBXBuildFile; fileRef = 55BA679F1299FA19002279A5 /* NSString+DDMathParsing.m */; };
- 5537E36612AB20F30000CFED /* NSExpression+DDMathParsing.m in Sources */ = {isa = PBXBuildFile; fileRef = 5538F3B9129CC0B30099B842 /* NSExpression+DDMathParsing.m */; };
5537E36712AB20F30000CFED /* DDMathEvaluator.m in Sources */ = {isa = PBXBuildFile; fileRef = 5554D58A12944AFD00A62529 /* DDMathEvaluator.m */; };
5537E36912AB20F30000CFED /* DDExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 55F7963612934A1400EF2716 /* DDExpression.m */; };
5537E36A12AB20F30000CFED /* _DDFunctionExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 55F9EFEA1295AF3D00F4A9FD /* _DDFunctionExpression.m */; };
@@ -37,8 +40,6 @@
5537E36F12AB21140000CFED /* Demo.m in Sources */ = {isa = PBXBuildFile; fileRef = 5537E36E12AB21140000CFED /* Demo.m */; };
5537E37312AB214D0000CFED /* DemoController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5537E37212AB214D0000CFED /* DemoController.m */; };
5537E37512AB229C0000CFED /* Demo.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5537E37412AB229C0000CFED /* Demo.xib */; };
- 5538F3BA129CC0B30099B842 /* NSExpression+DDMathParsing.m in Sources */ = {isa = PBXBuildFile; fileRef = 5538F3B9129CC0B30099B842 /* NSExpression+DDMathParsing.m */; };
- 5538F3BD129CC2450099B842 /* NSExpression+DDMathParsing.m in Sources */ = {isa = PBXBuildFile; fileRef = 5538F3B9129CC0B30099B842 /* NSExpression+DDMathParsing.m */; };
554DA12B12985DAE00433840 /* DDExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 55F7963612934A1400EF2716 /* DDExpression.m */; };
554DA12E12985DB500433840 /* _DDFunctionExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 55F9EFEA1295AF3D00F4A9FD /* _DDFunctionExpression.m */; };
554DA12F12985DB500433840 /* _DDNumberExpression.m in Sources */ = {isa = PBXBuildFile; fileRef = 55F9EFED1295AF7500F4A9FD /* _DDNumberExpression.m */; };
@@ -92,9 +93,9 @@
5508991C12BCA4F000BBC2D9 /* DDFunctionTerm.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DDFunctionTerm.m; sourceTree = "<group>"; };
5508992012BCAD5C00BBC2D9 /* DDOperatorTerm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DDOperatorTerm.h; sourceTree = "<group>"; };
5508992112BCAD5C00BBC2D9 /* DDOperatorTerm.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DDOperatorTerm.m; sourceTree = "<group>"; };
+ 5514EE291347CE9B0010B9D8 /* DDMathParserMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DDMathParserMacros.h; path = DDMathParser/DDMathParserMacros.h; sourceTree = "<group>"; };
552532B512C1C92D00A942A7 /* _DDFunctionUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _DDFunctionUtilities.h; sourceTree = "<group>"; };
552532B612C1C92D00A942A7 /* _DDFunctionUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _DDFunctionUtilities.m; sourceTree = "<group>"; };
- 5526E9DF1310EC010018B4C9 /* DDMathParserMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DDMathParserMacros.h; sourceTree = "<group>"; };
5526E9E01310EF880018B4C9 /* DDTypes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DDTypes.m; sourceTree = "<group>"; };
55325A0412A8C34E00DE9EB0 /* DDTerm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DDTerm.h; sourceTree = "<group>"; };
55325A0512A8C34E00DE9EB0 /* DDTerm.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DDTerm.m; sourceTree = "<group>"; };
@@ -104,8 +105,6 @@
5537E37112AB214D0000CFED /* DemoController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoController.h; sourceTree = "<group>"; };
5537E37212AB214D0000CFED /* DemoController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoController.m; sourceTree = "<group>"; };
5537E37412AB229C0000CFED /* Demo.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = Demo.xib; sourceTree = "<group>"; };
- 5538F3B8129CC0B30099B842 /* NSExpression+DDMathParsing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSExpression+DDMathParsing.h"; sourceTree = "<group>"; };
- 5538F3B9129CC0B30099B842 /* NSExpression+DDMathParsing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSExpression+DDMathParsing.m"; sourceTree = "<group>"; };
5554D58912944AFD00A62529 /* DDMathEvaluator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DDMathEvaluator.h; sourceTree = "<group>"; };
5554D58A12944AFD00A62529 /* DDMathEvaluator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DDMathEvaluator.m; sourceTree = "<group>"; };
5554D5EF129478E800A62529 /* DDMathEvaluator+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "DDMathEvaluator+Private.h"; sourceTree = "<group>"; };
@@ -236,7 +235,7 @@
isa = PBXGroup;
children = (
55BA673E1298ECA2002279A5 /* DDMathParser.h */,
- 5526E9DF1310EC010018B4C9 /* DDMathParserMacros.h */,
+ 5514EE291347CE9B0010B9D8 /* DDMathParserMacros.h */,
55BFA9B2128CDEA200A80228 /* Parser */,
55F7966612934F8900EF2716 /* Evaluator */,
);
@@ -273,8 +272,6 @@
55325A0312A8C34400DE9EB0 /* Precedence Parser */,
55BA679E1299FA19002279A5 /* NSString+DDMathParsing.h */,
55BA679F1299FA19002279A5 /* NSString+DDMathParsing.m */,
- 5538F3B8129CC0B30099B842 /* NSExpression+DDMathParsing.h */,
- 5538F3B9129CC0B30099B842 /* NSExpression+DDMathParsing.m */,
55DDAB29134044680088A170 /* NSNumberFormatter+DDMathParser.h */,
55DDAB2A134044680088A170 /* NSNumberFormatter+DDMathParser.m */,
);
@@ -439,7 +436,6 @@
5537E36312AB20F30000CFED /* DDMathStringTokenizer.m in Sources */,
5537E36412AB20F30000CFED /* DDMathStringToken.m in Sources */,
5537E36512AB20F30000CFED /* NSString+DDMathParsing.m in Sources */,
- 5537E36612AB20F30000CFED /* NSExpression+DDMathParsing.m in Sources */,
5537E36712AB20F30000CFED /* DDMathEvaluator.m in Sources */,
5537E36912AB20F30000CFED /* DDExpression.m in Sources */,
5537E36A12AB20F30000CFED /* _DDFunctionExpression.m in Sources */,
@@ -452,6 +448,8 @@
5508992312BCAD5C00BBC2D9 /* DDOperatorTerm.m in Sources */,
552532B812C1C92D00A942A7 /* _DDFunctionUtilities.m in Sources */,
559E92A912C521CF00942099 /* _DDDecimalFunctions.m in Sources */,
+ 5514EF171347EBA20010B9D8 /* DDTypes.m in Sources */,
+ 5514EF191347EBB10010B9D8 /* NSNumberFormatter+DDMathParser.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -471,14 +469,15 @@
55F9EFF11295AFB500F4A9FD /* _DDVariableExpression.m in Sources */,
55F9F0141295C19900F4A9FD /* NSExpression+EasyParsing.m in Sources */,
55BA67A01299FA19002279A5 /* NSString+DDMathParsing.m in Sources */,
- 5538F3BA129CC0B30099B842 /* NSExpression+DDMathParsing.m in Sources */,
55E2FCB7129DC90400CD6023 /* DDParser.m in Sources */,
55325A0612A8C34E00DE9EB0 /* DDTerm.m in Sources */,
5508991A12BCA4D300BBC2D9 /* DDGroupTerm.m in Sources */,
5508991F12BCA4F000BBC2D9 /* DDFunctionTerm.m in Sources */,
5508992412BCAD5C00BBC2D9 /* DDOperatorTerm.m in Sources */,
552532B912C1C92D00A942A7 /* _DDFunctionUtilities.m in Sources */,
559E92AA12C521CF00942099 /* _DDDecimalFunctions.m in Sources */,
+ 5514EF161347EBA10010B9D8 /* DDTypes.m in Sources */,
+ 5514EF181347EBB00010B9D8 /* NSNumberFormatter+DDMathParser.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -495,7 +494,6 @@
554DA12F12985DB500433840 /* _DDNumberExpression.m in Sources */,
554DA13012985DB500433840 /* _DDVariableExpression.m in Sources */,
55BA67A11299FAEF002279A5 /* NSString+DDMathParsing.m in Sources */,
- 5538F3BD129CC2450099B842 /* NSExpression+DDMathParsing.m in Sources */,
55325A1912A8C82C00DE9EB0 /* DDParser.m in Sources */,
55325A1A12A8C83000DE9EB0 /* DDTerm.m in Sources */,
5508991812BCA4D300BBC2D9 /* DDGroupTerm.m in Sources */,
@@ -20,20 +20,17 @@ typedef enum {
}
-+ (id) expressionFromString:(NSString *)expressionString;
++ (id) expressionFromString:(NSString *)expressionString error:(NSError **)error;
+ (id) numberExpressionWithNumber:(NSNumber *)number;
-+ (id) functionExpressionWithFunction:(NSString *)function arguments:(NSArray *)arguments;
++ (id) functionExpressionWithFunction:(NSString *)function arguments:(NSArray *)arguments error:(NSError **)error;
+ (id) variableExpressionWithVariable:(NSString *)variable;
- (DDExpressionType) expressionType;
-- (NSNumber *) evaluateWithSubstitutions:(NSDictionary *)substitutions evaluator:(DDMathEvaluator *)evaluator;
-
-- (NSExpression *) expressionValue;
-- (NSExpression *) expressionValueForEvaluator:(DDMathEvaluator *)evaluator;
+- (NSNumber *) evaluateWithSubstitutions:(NSDictionary *)substitutions evaluator:(DDMathEvaluator *)evaluator error:(NSError **)error;
- (DDExpression *) simplifiedExpression;
-- (DDExpression *) simplifiedExpressionWithEvaluator:(DDMathEvaluator *)evaluator;
+- (DDExpression *) simplifiedExpressionWithEvaluator:(DDMathEvaluator *)evaluator error:(NSError **)error;
#pragma mark Number methods
- (NSNumber *) number;
@@ -19,16 +19,16 @@
@implementation DDExpression
-+ (id) expressionFromString:(NSString *)expressionString {
- return [[DDParser parserWithString:expressionString] parsedExpression];
++ (id) expressionFromString:(NSString *)expressionString error:(NSError **)error {
+ return [[DDParser parserWithString:expressionString error:error] parsedExpressionWithError:error];
}
+ (id) numberExpressionWithNumber:(NSNumber *)number {
return [[[_DDNumberExpression alloc] initWithNumber:number] autorelease];
}
-+ (id) functionExpressionWithFunction:(NSString *)function arguments:(NSArray *)arguments {
- return [[[_DDFunctionExpression alloc] initWithFunction:function arguments:arguments] autorelease];
++ (id) functionExpressionWithFunction:(NSString *)function arguments:(NSArray *)arguments error:(NSError **)error {
+ return [[[_DDFunctionExpression alloc] initWithFunction:function arguments:arguments error:error] autorelease];
}
+ (id) variableExpressionWithVariable:(NSString *)variable {
@@ -42,21 +42,20 @@ - (DDExpressionType) expressionType {
[NSException raise:NSInvalidArgumentException format:@"this method should be overridden: %@", NSStringFromSelector(_cmd)];
return 0;
}
-- (NSNumber *) evaluateWithSubstitutions:(NSDictionary *)substitutions evaluator:(DDMathEvaluator *)evaluator {
+- (NSNumber *) evaluateWithSubstitutions:(NSDictionary *)substitutions evaluator:(DDMathEvaluator *)evaluator error:(NSError **)error {
[NSException raise:NSInvalidArgumentException format:@"this method should be overridden: %@", NSStringFromSelector(_cmd)];
return nil;
}
- (DDExpression *) simplifiedExpression {
- return [self simplifiedExpressionWithEvaluator:[DDMathEvaluator sharedMathEvaluator]];
-}
-- (DDExpression *) simplifiedExpressionWithEvaluator:(DDMathEvaluator *)evaluator {
- [NSException raise:NSInvalidArgumentException format:@"this method should be overridden: %@", NSStringFromSelector(_cmd)];
- return nil;
-}
-- (NSExpression *) expressionValue {
- return [self expressionValueForEvaluator:[DDMathEvaluator sharedMathEvaluator]];
+ NSError *error = nil;
+ DDExpression *simplified = [self simplifiedExpressionWithEvaluator:[DDMathEvaluator sharedMathEvaluator] error:&error];
+ if (error != nil) {
+ NSLog(@"error: %@", error);
+ return nil;
+ }
+ return simplified;
}
-- (NSExpression *) expressionValueForEvaluator:(DDMathEvaluator *)evaluator {
+- (DDExpression *) simplifiedExpressionWithEvaluator:(DDMathEvaluator *)evaluator error:(NSError **)error {
[NSException raise:NSInvalidArgumentException format:@"this method should be overridden: %@", NSStringFromSelector(_cmd)];
return nil;
}
@@ -8,23 +8,28 @@
#import "DDFunctionTerm.h"
#import "DDGroupTerm.h"
+#import "DDMathParserMacros.h"
@interface DDFunctionTerm ()
-- (void) groupParametersStartingAtIndex:(NSUInteger)index;
+- (void) groupParametersStartingAtIndex:(NSUInteger)index error:(NSError **)error;
@end
@implementation DDFunctionTerm
-- (id) initWithTokenizer:(DDMathStringTokenizer *)tokenizer {
- self = [super initWithTokenizer:tokenizer];
+- (id) initWithTokenizer:(DDMathStringTokenizer *)tokenizer error:(NSError **)error {
+ self = [super initWithTokenizer:tokenizer error:error];
if (self) {
if (tokenizer != nil) {
DDMathStringToken * token = [tokenizer peekNextToken];
if ([token operatorType] != DDOperatorParenthesisOpen) {
- [NSException raise:NSGenericException format:@"function not followed by an open parenthesis"];
+ if (error) {
+ *error = ERR_BADARG(@"function not followed by an open parenthesis");
+ }
+ [self release];
+ return nil;
}
[tokenizer nextToken]; //consume the opening parenthesis
@@ -33,36 +38,57 @@ - (id) initWithTokenizer:(DDMathStringTokenizer *)tokenizer {
DDMathStringToken * peek = [tokenizer peekNextToken];
if ([peek operatorType] == DDOperatorComma) {
[tokenizer nextToken]; //consume the comma
- [self groupParametersStartingAtIndex:groupingIndex++];
+ [self groupParametersStartingAtIndex:groupingIndex++ error:error];
+ if (error && *error) {
+ [self release];
+ return nil;
+ }
} else {
- DDTerm * t = [DDTerm termForTokenType:[peek tokenType] withTokenizer:tokenizer];
+ DDTerm * t = [DDTerm termForTokenType:[peek tokenType] withTokenizer:tokenizer error:error];
+ if (error && *error) {
+ [self release];
+ return nil;
+ }
[[self subTerms] addObject:t];
}
}
if (groupingIndex == 0 && [[self subTerms] count] > 0) {
//we added terms, but never grouped
- [self groupParametersStartingAtIndex:groupingIndex];
+ [self groupParametersStartingAtIndex:groupingIndex error:error];
+ if (error && *error) {
+ [self release];
+ return nil;
+ }
}
token = [tokenizer nextToken];
if ([token operatorType] != DDOperatorParenthesisClose) {
- [NSException raise:NSGenericException format:@"function does not have a close parenthesis"];
+ if (error) {
+ *error = ERR_BADARG(@"function does not have a close parenthesis");
+ }
+ [self release];
+ return nil;
}
}
}
return self;
}
-- (void) groupParametersStartingAtIndex:(NSUInteger)index {
+- (void) groupParametersStartingAtIndex:(NSUInteger)index error:(NSError **)error {
if ([[self subTerms] count] == 0) {
//we hit a comma
- [NSException raise:NSGenericException format:@"empty function parameter"];
+ if (error) {
+ *error = ERR_EVAL(@"empty function parameter");
+ }
+ return;
}
NSRange groupingRange = NSMakeRange(index, [[self subTerms] count] - index);
if (index >= [[self subTerms] count] || groupingRange.length <= 0) {
- [NSException raise:NSGenericException format:@"invalid comma placement"];
+ if (error) {
+ *error = ERR_EVAL(@"invalid comma placement");
+ }
return;
}
@@ -76,31 +102,42 @@ - (void) groupParametersStartingAtIndex:(NSUInteger)index {
}
NSArray * sub = [[self subTerms] subarrayWithRange:groupingRange];
- DDTerm * parameterGroup = [DDGroupTerm groupTermWithSubTerms:sub];
+ DDTerm * parameterGroup = [DDGroupTerm groupTermWithSubTerms:sub error:error];
+ if (error && *error) {
+ return;
+ }
[[self subTerms] replaceObjectsInRange:groupingRange withObjectsFromArray:[NSArray arrayWithObject:parameterGroup]];
}
- (NSString *) function {
return [tokenValue token];
}
-- (void) resolveWithParser:(DDParser *)parser {
- [[self subTerms] makeObjectsPerformSelector:_cmd withObject:parser];
+- (void) resolveWithParser:(DDParser *)parser error:(NSError **)error {
+ for (DDTerm *subTerm in [self subTerms]) {
+ [subTerm resolveWithParser:parser error:error];
+ if (error && *error) {
+ return;
+ }
+ }
}
- (NSString *) description {
return [NSString stringWithFormat:@"%@%@", [[self tokenValue] token], [super description]];
}
-- (DDExpression *) expression {
+- (DDExpression *) expressionWithError:(NSError **)error {
NSMutableArray * parameters = [NSMutableArray array];
for (DDTerm * param in [self subTerms]) {
- DDExpression * e = [param expression];
+ DDExpression * e = [param expressionWithError:error];
+ if (error && *error) {
+ return nil;
+ }
if (e != nil) {
[parameters addObject:e];
}
}
- return [DDExpression functionExpressionWithFunction:[self function] arguments:parameters];
+ return [DDExpression functionExpressionWithFunction:[self function] arguments:parameters error:error];
}
@end
@@ -15,7 +15,7 @@
@property (readonly) NSMutableArray * subTerms;
-+ (id) rootTermWithTokenizer:(DDMathStringTokenizer *)tokenizer;
-+ (id) groupTermWithSubTerms:(NSArray *)sub;
++ (id) rootTermWithTokenizer:(DDMathStringTokenizer *)tokenizer error:(NSError **)error;
++ (id) groupTermWithSubTerms:(NSArray *)sub error:(NSError **)error;
@end
Oops, something went wrong.

0 comments on commit 3c18814

Please sign in to comment.