Archived
1

Added Documentation

This commit is contained in:
Kim Wittenburg
2014-11-10 21:45:50 +01:00
parent f4f924bd71
commit 10f0e73ad3
32 changed files with 1847 additions and 318 deletions

View File

@@ -10,67 +10,82 @@
#import "MPToken.h"
@implementation MPTokenStream {
NSUInteger _currentTokenIndex;
NSUInteger eofLocation;
BOOL *whitespaceIgnores;
NSUInteger maxWhitespaceIgnores;
NSUInteger currentWhitespaceState;
}
@interface MPTokenStream ()
@property (nonatomic, copy) NSArray *tokens;
@property (nonatomic) NSUInteger currentTokenIndex;
@property (nonatomic) NSUInteger eofLocation;
@property (nonatomic) BOOL *whitespaceIgnores;
@property (nonatomic) NSUInteger maxWhitespaceIgnores;
@property (nonatomic) NSUInteger currentWhitespaceIgnoreState;
- (void)pushWhitespaceState:(BOOL)state;
@end
@implementation MPTokenStream
- (instancetype)initWithTokens:(NSArray *)tokens
{
self = [super init];
if (self) {
self.tokens = tokens;
whitespaceIgnores = malloc(10 * sizeof(BOOL));
maxWhitespaceIgnores = 10;
currentWhitespaceState = 0;
self.whitespaceIgnores = malloc(10 * sizeof(BOOL));
self.maxWhitespaceIgnores = 10;
self.currentWhitespaceIgnoreState = 0;
[self beginAcceptingWhitespaceTokens];
_currentTokenIndex = 0;
if (tokens.count > 0) {
eofLocation = NSMaxRange([tokens.lastObject range]);
self.eofLocation = NSMaxRange([tokens.lastObject range]);
} else {
eofLocation = 0;
self.eofLocation = 0;
}
}
return self;
}
- (void)beginAcceptingWhitespaceTokens
{
[self pushWhitespaceState:YES];
}
- (void)beginIgnoringWhitespaceTokens
{
[self pushWhitespaceState:NO];
}
- (void)pushWhitespaceState:(BOOL)state
{
currentWhitespaceState++;
if (currentWhitespaceState >= maxWhitespaceIgnores) {
maxWhitespaceIgnores += 10;
BOOL *reallocatedWhitespaceIgnores = realloc(whitespaceIgnores, maxWhitespaceIgnores);
self.currentWhitespaceIgnoreState++;
if (self.currentWhitespaceIgnoreState >= self.maxWhitespaceIgnores) {
self.maxWhitespaceIgnores += 10;
BOOL *reallocatedWhitespaceIgnores = realloc(self.whitespaceIgnores, self.maxWhitespaceIgnores);
if (reallocatedWhitespaceIgnores) {
whitespaceIgnores = reallocatedWhitespaceIgnores;
self.whitespaceIgnores = reallocatedWhitespaceIgnores;
} else {
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Realloc Failed" userInfo:nil];
}
}
whitespaceIgnores[currentWhitespaceState] = state;
self.whitespaceIgnores[self.currentWhitespaceIgnoreState] = state;
}
- (void)endIgnoringOrAcceptingWhitespaceTokens
{
currentWhitespaceState--;
self.currentWhitespaceIgnoreState--;
}
- (void)skipWhitespaces
{
if (whitespaceIgnores[currentWhitespaceState]) {
if (self.whitespaceIgnores[self.currentWhitespaceIgnoreState]) {
return;
}
while (_currentTokenIndex < self.tokens.count) {
@@ -82,21 +97,24 @@
}
}
- (BOOL)hasMoreTokens
{
[self skipWhitespaces];
return _currentTokenIndex < self.tokens.count;
}
- (id<MPToken>)currentToken
{
[self skipWhitespaces];
if (_currentTokenIndex >= _tokens.count) {
return [[MPToken alloc] initEOFTokenAtLocation:eofLocation];
return nil;
}
return _tokens[_currentTokenIndex];
}
- (id<MPToken>)peekNextToken
{
NSUInteger currentTokenIndex = _currentTokenIndex;
@@ -106,15 +124,17 @@
return token;
}
- (void)currentTokenConsumed
{
[self currentToken];
++_currentTokenIndex;
}
- (void)dealloc
{
free(whitespaceIgnores);
free(self.whitespaceIgnores);
}
@end