Skip to content
This repository
tree: c1cf850271
Fetching contributors…

Cannot retrieve contributors at this time

file 269 lines (237 sloc) 8.41 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
//
// YKParser.m
// YAMLKit
//
// Created by Patrick Thomson on 12/29/08.
//

#import "YKParser.h"
#import "YKConstants.h"


static BOOL _isBooleanTrue(NSString *aString);
static BOOL _isBooleanFalse(NSString *aString);

@interface YKParser (YKParserPrivateMethods)

- (id)_interpretObjectFromEvent:(yaml_event_t)event;
- (NSError *)_constructErrorFromParser:(yaml_parser_t *)p;

@end

@implementation YKParser

@synthesize readyToParse;

- (void)reset
{
    if(fileInput) {
        fclose(fileInput);
    }
yaml_parser_delete(&parser);
    memset(&parser, 0, sizeof(parser));
}

- (BOOL)readFile:(NSString *)path
{
    [self reset];
    fileInput = fopen([path fileSystemRepresentation], "r");
    readyToParse = ((fileInput != NULL) && (yaml_parser_initialize(&parser)));
    if(readyToParse)
yaml_parser_set_input_file(&parser, fileInput);
return readyToParse;
}

- (BOOL)readString:(NSString *)str
{
    [self reset];
    stringInput = [str UTF8String];
    readyToParse = yaml_parser_initialize(&parser);
    if(readyToParse)
yaml_parser_set_input_string(&parser, (const unsigned char *)stringInput, [str length]);
    return readyToParse;
}

- (NSArray *)parse
{
return [self parseWithError:NULL];
}

- (NSArray *)parseWithError:(NSError **)e
{
    yaml_event_t event;
    int done = 0;
    id obj, temp;
    NSMutableArray *stack = [NSMutableArray array];
if(!readyToParse) {
if(![[stack lastObject] isKindOfClass:[NSMutableDictionary class]]){
if(e != NULL) {
*e = [self _constructErrorFromParser:NULL];
return nil;
}
}
}
    
    while(!done) {
        if(!yaml_parser_parse(&parser, &event)) {
if(e != NULL) {
*e = [self _constructErrorFromParser:&parser];
}
            return nil;
}
        done = (event.type == YAML_STREAM_END_EVENT);
        switch(event.type) {
            case YAML_SCALAR_EVENT:
obj = [self _interpretObjectFromEvent:event];
                temp = [stack lastObject];
                
                if([temp isKindOfClass:[NSArray class]]) {
                    [temp addObject:obj];
                } else if([temp isKindOfClass:[NSDictionary class]]) {
                    [stack addObject:obj];
                } else if([temp isKindOfClass:[NSString class]] || [temp isKindOfClass:[NSValue class]]) {
                    [temp retain];
                    [stack removeLastObject];
                    if(![[stack lastObject] isKindOfClass:[NSMutableDictionary class]]){
if(e != NULL) {
*e = [self _constructErrorFromParser:NULL];
return nil;
}
}
[[stack lastObject] setObject:obj forKey:temp];
                }
                
                break;
            case YAML_SEQUENCE_START_EVENT:
                [stack addObject:[NSMutableArray array]];
                break;
            case YAML_MAPPING_START_EVENT:
                [stack addObject:[NSMutableDictionary dictionary]];
                break;
            case YAML_SEQUENCE_END_EVENT:
            case YAML_MAPPING_END_EVENT:
// TODO: Check for retain count errors.
                temp = [stack lastObject];
                [stack removeLastObject];

                id last = [stack lastObject];
if(last == nil) {
[stack addObject:temp];
break;
} else if([last isKindOfClass:[NSArray class]]) {
                    [last addObject:temp];
                } else if ([last isKindOfClass:[NSDictionary class]]) {
                    [stack addObject:temp];
                } else if ([last isKindOfClass:[NSString class]] || [last isKindOfClass:[NSNumber class]]) {
                    obj = [[stack lastObject] retain];
                    [stack removeLastObject];
                    if(![[stack lastObject] isKindOfClass:[NSMutableDictionary class]]){
if(e != NULL) {
*e = [self _constructErrorFromParser:NULL];
return nil;
}
}
                    [[stack lastObject] setObject:temp forKey:obj];
                }
                break;
            case YAML_NO_EVENT:
                break;
            default:
                break;
        }
        yaml_event_delete(&event);
    }
    return stack;
}

// TODO: oof, add tag support.

- (id)_interpretObjectFromEvent:(yaml_event_t)event
{
NSString *stringValue = [NSString stringWithUTF8String:(const char *)event.data.scalar.value];
id obj = stringValue;

if(event.data.scalar.style == YAML_PLAIN_SCALAR_STYLE) {
NSScanner *scanner = [NSScanner scannerWithString:obj];

// Integers are automatically casted unless given a !!str tag. I think.
if([scanner scanDouble:NULL] && [scanner scanLocation] == [stringValue length]) {
obj = [NSNumber numberWithDouble:[obj doubleValue]];
} else if([scanner scanInt:NULL] && [scanner scanLocation] == [stringValue length]) {
obj = [NSNumber numberWithInt:[obj intValue]];
// FIXME: Boolean parsing here is not in accordance with the YAML standards.
} else if(_isBooleanTrue((NSString *)obj)) {
obj = [NSNumber numberWithBool:YES];
} else if(_isBooleanFalse((NSString *)obj)) {
obj = [NSNumber numberWithBool:NO];
} else if([obj isEqualToString:@"~"]) {
obj = [NSNull null];
}
// TODO: add date parsing.
}
return obj;
}

- (NSError *)_constructErrorFromParser:(yaml_parser_t *)p
{
int code = 0;
NSMutableDictionary *data = [NSMutableDictionary dictionary];

if(p != NULL) {
// actual parser error
code = p->error;
// get the string encoding.
NSStringEncoding enc = 0;
switch (p->encoding) {
case YAML_UTF8_ENCODING:
enc = NSUTF8StringEncoding;
break;
case YAML_UTF16LE_ENCODING:
enc = NSUTF16LittleEndianStringEncoding;
break;
case YAML_UTF16BE_ENCODING:
enc = NSUTF16BigEndianStringEncoding;
break;
default: break;
}
[data setObject:[NSNumber numberWithInt:enc] forKey:NSStringEncodingErrorKey];

[data setObject:[NSString stringWithUTF8String:p->problem] forKey:YKProblemDescriptionKey];
[data setObject:[NSNumber numberWithInt:p->problem_offset] forKey:YKProblemOffsetKey];
[data setObject:[NSNumber numberWithInt:p->problem_value] forKey:YKProblemValueKey];
[data setObject:[NSNumber numberWithInt:p->problem_mark.line] forKey:YKProblemLineKey];
[data setObject:[NSNumber numberWithInt:p->problem_mark.index] forKey:YKProblemIndexKey];
[data setObject:[NSNumber numberWithInt:p->problem_mark.column] forKey:YKProblemColumnKey];

[data setObject:[NSString stringWithUTF8String:p->context] forKey:YKErrorContextDescriptionKey];
[data setObject:[NSNumber numberWithInt:p->context_mark.line] forKey:YKErrorContextLineKey];
[data setObject:[NSNumber numberWithInt:p->context_mark.column] forKey:YKErrorContextColumnKey];
[data setObject:[NSNumber numberWithInt:p->context_mark.index] forKey:YKErrorContextIndexKey];

} else if(readyToParse) {
[data setObject:NSLocalizedString(@"Internal assertion failed, possibly due to specially malformed input.", @"") forKey:NSLocalizedDescriptionKey];
} else {
[data setObject:NSLocalizedString(@"YAML parser was not ready to parse.", @"") forKey:NSLocalizedFailureReasonErrorKey];
[data setObject:NSLocalizedString(@"Did you remember to call readFile: or readString:?", @"") forKey:NSLocalizedDescriptionKey];
}

return [[NSError alloc] initWithDomain:YKErrorDomain code:code userInfo:data];
}

- (void)finalize
{
yaml_parser_delete(&parser);
if(fileInput != NULL) fclose(fileInput);
[super finalize];
}

- (void)dealloc
{
yaml_parser_delete(&parser);
if(fileInput != NULL) fclose(fileInput);
    [super dealloc];
}

@end

static BOOL _isBooleanFalse(NSString *aString)
{
BOOL isFalse = NO;
const char *cstr = [aString UTF8String];
char *falseValues[] = {
"false", "False", "FALSE",
"n", "N", "NO", "No", "no",
"off", "Off", "OFF"
};
size_t length = sizeof(falseValues) / sizeof(*falseValues);
int index;
for(index = 0; index < length && !isFalse; index++) {
isFalse = strcmp(cstr, falseValues[index]) == 0;
}
return isFalse;
}

static BOOL _isBooleanTrue(NSString *aString)
{
BOOL isTrue = NO;
const char *cstr = [aString UTF8String];
char *trueValues[] = {
"true", "TRUE", "True",
"y", "Y", "Yes", "yes", "YES",
"on", "On", "ON"
};
size_t length = sizeof(trueValues) / sizeof(*trueValues);
int index;
for(index = 0; index < length && !isTrue; index++) {
isTrue = strcmp(cstr, trueValues[index]) == 0;
}
return isTrue;
}
Something went wrong with that request. Please try again.