Permalink
Browse files

don't hardcode recognizable operator patterns; recognize operators of…

… any length
  • Loading branch information...
1 parent f0a3f95 commit 2a6ed34d921024ee59e725738b1975dc38ac218c @davedelong committed Oct 19, 2011
Showing with 21 additions and 21 deletions.
  1. +4 −4 DDMathParser/DDMathStringToken.m
  2. +17 −17 DDMathParser/DDMathStringTokenizer.m
@@ -102,10 +102,10 @@ - (void)resolveToOperator:(DDOperator)operator {
operatorInfo = nil;
NSArray *matching = [_DDOperatorInfo infosForOperator:operator];
- if ([matching count] != 1) { return; }
-
- ambiguous = NO;
- operatorInfo = DD_RETAIN([matching objectAtIndex:0]);
+ if ([matching count] > 0) {
+ ambiguous = NO;
+ operatorInfo = DD_RETAIN([matching objectAtIndex:0]);
+ }
}
@end
@@ -442,25 +442,25 @@ - (DDMathStringToken *)_parseOperatorWithError:(NSError **)error {
unichar character = [self _nextCharacter];
NSCharacterSet *operatorCharacters = [[self class] _operatorCharacterSet];
- if ([operatorCharacters characterIsMember:character]) {
- unichar peekNext = [self _peekNextCharacter];
- if (character == '<' || character == '>' || character == '*' || character == '&' || character == '|' || character == '=') {
- // <, >, *, &, |, =
- if (peekNext == character) {
- // <<, >>, **, &&, ||, ==
- _characterIndex++;
- length++;
- }
- }
-
- if ((character == '<' || character == '>' || character == '!') && peekNext == '=' && length == 1) {
- // <=, >=, !=
- _characterIndex++;
- length++;
+
+ NSString *lastGood = nil;
+ NSUInteger lastGoodLength = length;
+
+ while ([operatorCharacters characterIsMember:character]) {
+ NSString *tmp = [NSString stringWithCharacters:(_characters+start) length:length];
+ NSArray *operators = [_DDOperatorInfo infosForOperatorToken:tmp];
+ if ([operators count] > 0) {
+ lastGood = tmp;
+ lastGoodLength = length;
}
+ character = [self _nextCharacter];
+ length++;
+ }
+
+ if (length > 0 && lastGood != nil) {
+ _characterIndex = start+lastGoodLength;
- NSString *rawToken = [NSString stringWithCharacters:(_characters + start) length:length];
- return [DDMathStringToken mathStringTokenWithToken:rawToken type:DDTokenTypeOperator];
+ return [DDMathStringToken mathStringTokenWithToken:lastGood type:DDTokenTypeOperator];
}
_characterIndex = start;

0 comments on commit 2a6ed34

Please sign in to comment.