Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: v1.0.1
Fetching contributors…

Cannot retrieve contributors at this time

file 120 lines (86 sloc) 2.403 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
//
// TDCharacterAssembly.m
// TDParseKit
//
// Created by Todd Ditchendorf on 7/13/08.
// Copyright 2008 Todd Ditchendorf. All rights reserved.
//

#import "TDCharacterAssembly.h"

@interface TDAssembly ()
@property (nonatomic, readwrite, retain) NSString *defaultDelimiter;
@end

@implementation TDCharacterAssembly

- (id)init {
    return [self initWithString:nil];
}


- (id)initWithString:(NSString *)s {
    self = [super initWithString:s];
    if (self) {
        self.defaultDelimiter = @"";
    }
    return self;
}


- (void)dealloc {
    [super dealloc];
}


- (id)copyWithZone:(NSZone *)zone {
    TDCharacterAssembly *a = (TDCharacterAssembly *)[super copyWithZone:zone];
    return a;
}


- (id)peek {
    if (index >= string.length) {
        return nil;
    }
    NSInteger c = [string characterAtIndex:index];
    return [NSNumber numberWithInteger:c];
}


- (id)next {
    id obj = [self peek];
    if (obj) {
        index++;
    }
    return obj;
}


- (BOOL)hasMore {
    return (index < string.length);
}


- (NSUInteger)length {
    return string.length;
}


- (NSUInteger)objectsConsumed {
    return index;
}


- (NSUInteger)objectsRemaining {
    return (string.length - index);
}


- (NSString *)consumedObjectsJoinedByString:(NSString *)delimiter {
    NSParameterAssert(delimiter);
    return [string substringToIndex:self.objectsConsumed];
}


- (NSString *)remainingObjectsJoinedByString:(NSString *)delimiter {
    NSParameterAssert(delimiter);
    return [string substringFromIndex:self.objectsConsumed];
}


// overriding simply to print NSNumber objects as their unichar values
- (NSString *)description {
    NSMutableString *s = [NSMutableString string];
    [s appendString:@"["];
    
    NSInteger i = 0;
    NSInteger len = stack.count;
    
    for (id obj in self.stack) {
        if ([obj isKindOfClass:[NSNumber class]]) { // ***this is needed for Char Assemblies
            [s appendFormat:@"%C", [obj integerValue]];
        } else {
            [s appendString:[obj description]];
        }
        if (len - 1 != i++) {
            [s appendString:@", "];
        }
    }
    
    [s appendString:@"]"];
    
    [s appendString:[self consumedObjectsJoinedByString:self.defaultDelimiter]];
    [s appendString:@"^"];
    [s appendString:[self remainingObjectsJoinedByString:self.defaultDelimiter]];
    
    return [[s copy] autorelease];
}

@end
Something went wrong with that request. Please try again.