Added Lots of Documentation
Added some nice to haves Improved and Unified General Code Layout
This commit is contained in:
@@ -8,25 +8,27 @@
|
||||
|
||||
#import "MPExpressionParser.h"
|
||||
|
||||
#import "MPExpression.h"
|
||||
#import "MPTerm.h"
|
||||
#import "MPToken.h"
|
||||
#import "MPExpression.h"
|
||||
#import "MPParsedExpression.h"
|
||||
|
||||
#import "MPTerm.h"
|
||||
#import "MPSumTerm.h"
|
||||
#import "MPProductTerm.h"
|
||||
#import "MPFactorialTerm.h"
|
||||
#import "MPElementaryFunctionTerm.h"
|
||||
#import "MPFunctionTerm.h"
|
||||
#import "MPPowerTerm.h"
|
||||
#import "MPParsedExpression.h"
|
||||
#import "MPNegatedTerm.h"
|
||||
|
||||
#import "MPFunctionTerm.h"
|
||||
#import "MPElementaryFunctionTerm.h"
|
||||
#import "MPNumber.h"
|
||||
#import "MPVariable.h"
|
||||
#import "MPFactorialTerm.h"
|
||||
#import "MPPowerTerm.h"
|
||||
|
||||
|
||||
#define success() state = 0
|
||||
#define fail() self.errorTokenIndex = self.currentTokenIndex; state = -1
|
||||
|
||||
|
||||
|
||||
@interface MPExpressionParser ()
|
||||
|
||||
@property (nonatomic, strong) NSArray *tokens;
|
||||
@@ -46,6 +48,8 @@
|
||||
|
||||
@end
|
||||
|
||||
|
||||
|
||||
@implementation MPExpressionParser
|
||||
|
||||
- (instancetype)initWithExpression:(MPExpression *)expression
|
||||
@@ -58,12 +62,14 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
- (MPParsedExpression *)parse:(NSArray *__autoreleasing *)errors
|
||||
{
|
||||
return [self parseExpectingVariableDefinition:NO
|
||||
errors:errors];
|
||||
}
|
||||
|
||||
|
||||
- (MPParsedExpression *)parseExpectingVariableDefinition:(BOOL)flag
|
||||
errors:(NSArray *__autoreleasing *)errors
|
||||
{
|
||||
@@ -120,6 +126,7 @@
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
- (void)runParserMachine
|
||||
{
|
||||
NSInteger state = 1;
|
||||
@@ -251,6 +258,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (void)skipWhitespaces
|
||||
{
|
||||
while ([self currentToken] != nil && [self currentToken].tokenType == MPWhitespaceToken) {
|
||||
@@ -258,6 +266,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (id<MPToken>)currentToken
|
||||
{
|
||||
if (self.currentTokenIndex >= self.tokens.count) {
|
||||
@@ -266,11 +275,13 @@
|
||||
return self.tokens[self.currentTokenIndex];
|
||||
}
|
||||
|
||||
|
||||
- (void)nextToken
|
||||
{
|
||||
self.currentTokenIndex++;
|
||||
}
|
||||
|
||||
|
||||
- (NSMutableArray *)errors
|
||||
{
|
||||
if (!_errors) {
|
||||
@@ -279,6 +290,7 @@
|
||||
return _errors;
|
||||
}
|
||||
|
||||
|
||||
- (NSMutableArray *)summands
|
||||
{
|
||||
if (!_summands) {
|
||||
@@ -287,6 +299,7 @@
|
||||
return _summands;
|
||||
}
|
||||
|
||||
|
||||
- (NSMutableArray *)factors
|
||||
{
|
||||
if (!_factors) {
|
||||
@@ -295,6 +308,7 @@
|
||||
return _factors;
|
||||
}
|
||||
|
||||
|
||||
- (NSMutableArray *)functionStack
|
||||
{
|
||||
if (!_functionStack) {
|
||||
@@ -303,6 +317,7 @@
|
||||
return _functionStack;
|
||||
}
|
||||
|
||||
|
||||
- (NSMutableArray *)valueGroup
|
||||
{
|
||||
if (!_valueGroup) {
|
||||
@@ -311,6 +326,7 @@
|
||||
return _valueGroup;
|
||||
}
|
||||
|
||||
|
||||
- (BOOL)parseOperatorList:(id<MPToken>)token // Returns YES if list is overall negative
|
||||
{
|
||||
NSString *operatorString = [[token.stringValue stringByReplacingOccurrencesOfString:@" " withString:@""]
|
||||
@@ -318,17 +334,20 @@
|
||||
return (operatorString.length & 1) == 1;
|
||||
}
|
||||
|
||||
|
||||
- (NSDecimalNumber *)parseNumber:(id<MPToken>)token
|
||||
{
|
||||
return [NSDecimalNumber decimalNumberWithString:token.stringValue
|
||||
locale:[NSLocale currentLocale]];
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)parseVariable:(id<MPToken>)token
|
||||
{
|
||||
return token.stringValue;
|
||||
}
|
||||
|
||||
|
||||
- (void)collapseSummand
|
||||
{
|
||||
if (self.factors.count > 0) {
|
||||
@@ -342,6 +361,7 @@
|
||||
self.summandNegative = NO;
|
||||
}
|
||||
|
||||
|
||||
- (void)collapseFactor
|
||||
{
|
||||
if (self.valueGroup.count > 0) {
|
||||
@@ -360,6 +380,7 @@
|
||||
self.factorNegative = NO;
|
||||
}
|
||||
|
||||
|
||||
- (void)runSuffixMachine
|
||||
{
|
||||
BOOL checkMore = YES;
|
||||
@@ -388,6 +409,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (void)addErrorWithCode:(NSInteger)code
|
||||
localizedDescription:(NSString *)description
|
||||
useRange:(BOOL)flag
|
||||
@@ -408,6 +430,7 @@
|
||||
MPErrorRangeKey: [NSValue valueWithRange:errorRange]}]];
|
||||
}
|
||||
|
||||
|
||||
- (BOOL)errorOccured
|
||||
{
|
||||
[self skipWhitespaces];
|
||||
|
||||
Reference in New Issue
Block a user