Added Evaluation Context and Math Rules
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
#import "MPElementParser.h"
|
||||
#import "NSRegularExpression+MPParsingAdditions.h"
|
||||
#import "MPMathRules.h"
|
||||
|
||||
#import "MPParsedFactor.h"
|
||||
#import "MPParsedNumber.h"
|
||||
@@ -21,6 +22,11 @@
|
||||
@property (nonatomic) NSString *input;
|
||||
@property (nonatomic) NSUInteger parsePosition;
|
||||
|
||||
- (BOOL)allowsImplicitMultiplication;
|
||||
- (BOOL)maximumOperatorChainLength;
|
||||
- (BOOL)maximumOperatorChainLengthInMultiplication;
|
||||
- (BOOL)maximumOperatorChainLengthInFunction;
|
||||
|
||||
- (void)setError:(MPParseError *)error;
|
||||
|
||||
- (BOOL)isAtEnd;
|
||||
@@ -30,7 +36,9 @@
|
||||
- (NSRange)parseWhitespaces;
|
||||
- (NSRange)parseMultiplicationSymbol;
|
||||
- (MPParsedOperator *)parseOperators;
|
||||
- (id<MPParsedFactor>)parseValue;
|
||||
- (MPParsedNumber *)parseNumber;
|
||||
- (MPParsedVariable *)parseVariable;
|
||||
|
||||
@end
|
||||
|
||||
@@ -38,36 +46,25 @@
|
||||
MPParseError *__autoreleasing *outError;
|
||||
}
|
||||
|
||||
static BOOL useUserDefaults;
|
||||
|
||||
#pragma mark Creation Methods
|
||||
|
||||
+ (void)initialize
|
||||
#pragma mark Helpers
|
||||
- (BOOL)allowsImplicitMultiplication
|
||||
{
|
||||
useUserDefaults = YES;
|
||||
return [MPMathRules sharedRules].allowsImplicitMultiplication;
|
||||
}
|
||||
|
||||
- (instancetype)init
|
||||
- (BOOL)maximumOperatorChainLength
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.allowsImplicitMultiplications = NO;
|
||||
self.maximumOperatorChainLength = 2;
|
||||
self.maximumOperatorChainLengthInMultiplication = 1;
|
||||
}
|
||||
return self;
|
||||
return [MPMathRules sharedRules].maximumOperatorChainLength;
|
||||
}
|
||||
|
||||
#pragma mark Properties
|
||||
|
||||
+ (BOOL)isUsingDefaultValuesFromUserDefaults
|
||||
- (BOOL)maximumOperatorChainLengthInFunction
|
||||
{
|
||||
return useUserDefaults;
|
||||
return [MPMathRules sharedRules].maximumOperatorChainLengthInFunction;
|
||||
}
|
||||
|
||||
+ (void)setUsingDefaultValuesFromUserDefaults:(BOOL)flag
|
||||
- (BOOL)maximumOperatorChainLengthInMultiplication
|
||||
{
|
||||
useUserDefaults = flag;
|
||||
return [MPMathRules sharedRules].maximumOperatorChainLengthInMultiplication;
|
||||
}
|
||||
|
||||
#pragma mark Parsing Methods
|
||||
@@ -136,10 +133,10 @@ static BOOL useUserDefaults;
|
||||
// NSRange functionRange = ...
|
||||
// BOOL hasFunction = functionRange.location != NSNotFound;
|
||||
|
||||
MPParsedNumber *number = [self parseNumber];
|
||||
id<MPParsedFactor> value = [self parseValue];
|
||||
|
||||
|
||||
if (!number.exists) {
|
||||
if (!value.exists) {
|
||||
if ([self isAtEnd] && nextFactor != nil) {
|
||||
if (hasMultiplicationSymbol) {
|
||||
if (operators.numberOfOperators > self.maximumOperatorChainLengthInFunction) {
|
||||
@@ -155,13 +152,11 @@ static BOOL useUserDefaults;
|
||||
[products addObject:currentProduct];
|
||||
currentProduct = [[MPParsedProduct alloc] init];
|
||||
[currentProduct addFactor:operators];
|
||||
} else if (self.allowsImplicitMultiplications) {
|
||||
NSLog(@"Here I am");
|
||||
} else if (self.allowsImplicitMultiplication) {
|
||||
if (!currentProduct) {
|
||||
currentProduct = [[MPParsedProduct alloc] init];
|
||||
}
|
||||
} else {
|
||||
NSLog(@"ERR");
|
||||
self.error = MPParseError(NSMakeRange(self.parsePosition, 0), @"Implicit Multiplication not allowed.");
|
||||
return nil;
|
||||
}
|
||||
@@ -184,7 +179,7 @@ static BOOL useUserDefaults;
|
||||
return nil;
|
||||
}
|
||||
[currentProduct addFactor:operators];
|
||||
[currentProduct addFactor:number];
|
||||
[currentProduct addFactor:value];
|
||||
} else {
|
||||
self.error = MPParseError(multiplicationSymbolRange, @"Unexpected Symbol. Expected Number.");
|
||||
return nil;
|
||||
@@ -198,13 +193,13 @@ static BOOL useUserDefaults;
|
||||
[products addObject:currentProduct];
|
||||
}
|
||||
currentProduct = [[MPParsedProduct alloc] initWithFactor:operators];
|
||||
[currentProduct addFactor:number];
|
||||
[currentProduct addFactor:value];
|
||||
} else if (!currentProduct) {
|
||||
currentProduct = [[MPParsedProduct alloc] initWithFactor:number];
|
||||
} else if (self.allowsImplicitMultiplications) {
|
||||
[currentProduct addFactor:number];
|
||||
currentProduct = [[MPParsedProduct alloc] initWithFactor:value];
|
||||
} else if (self.allowsImplicitMultiplication) {
|
||||
[currentProduct addFactor:value];
|
||||
} else {
|
||||
self.error = MPParseError(NSMakeRange(number.range.location, 0), @"Implicit Multiplication not allowed.");
|
||||
self.error = MPParseError(NSMakeRange(value.range.location, 0), @"Implicit Multiplication not allowed.");
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
@@ -294,6 +289,18 @@ static NSRegularExpression *operatorsRegex;
|
||||
inString:self.input];
|
||||
}
|
||||
|
||||
- (id<MPParsedFactor>)parseValue
|
||||
{
|
||||
MPParsedNumber *parsedNumber = [self parseNumber];
|
||||
if (!parsedNumber.exists) {
|
||||
MPParsedVariable *parsedVariable = [self parseVariable];
|
||||
if (parsedVariable.exists) {
|
||||
return parsedVariable;
|
||||
}
|
||||
}
|
||||
return parsedNumber;
|
||||
}
|
||||
|
||||
- (MPParsedNumber *)parseNumber
|
||||
{
|
||||
NSString *decimalSeparatorRegexString = [NSRegularExpression escapedPatternForString:[[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator]];
|
||||
@@ -314,4 +321,25 @@ static NSRegularExpression *operatorsRegex;
|
||||
inString:self.input];
|
||||
}
|
||||
|
||||
static NSRegularExpression *variableRegex;
|
||||
- (MPParsedVariable *)parseVariable
|
||||
{
|
||||
if (!variableRegex) {
|
||||
variableRegex = [NSRegularExpression regularExpressionWithPattern:@"\\A\\s*([A-Za-z])\\s*"
|
||||
options:0
|
||||
error:NULL];
|
||||
}
|
||||
NSTextCheckingResult *match = [variableRegex firstMatchInString:self.input
|
||||
fromIndex:self.parsePosition];
|
||||
NSRange matchRange;
|
||||
if (!match) {
|
||||
matchRange = NSMakeRange(NSNotFound, 0);
|
||||
} else {
|
||||
self.parsePosition = NSMaxRange(match.range);
|
||||
matchRange = [match rangeAtIndex:1];
|
||||
}
|
||||
return [[MPParsedVariable alloc] initWithRange:matchRange
|
||||
inString:self.input];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user