-
Notifications
You must be signed in to change notification settings - Fork 5
/
ParamParser.m
154 lines (110 loc) · 4.07 KB
/
ParamParser.m
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
//
// ParamParser.m
// Spike
//
// Created by Matt Mower on 17/02/2009.
// Copyright 2009 LucidMac Software. All rights reserved.
//
#import "ParamParser.h"
#import <TDParseKit/TDParseKit.h>
#import "NSString+TDParseKitAdditions.h"
#import "Parameter.h"
@interface ParamParser (PrivateMethods)
- (NSString *)grammarPath;
- (void)workOnStringAssembly:(TDAssembly *)assembly;
- (void)workOnNullAssembly:(TDAssembly *)assembly;
- (void)workOnBooleanAssembly:(TDAssembly *)assembly;
- (void)workOnNumberAssembly:(TDAssembly *)assembly;
- (void)workOnArrayAssembly:(TDAssembly *)assembly;
- (void)workOnObjectAssembly:(TDAssembly *)assembly;
- (void)workOnPropertyAssembly:(TDAssembly *)assembly;
@end
static TDToken *arrayStartToken;
static TDToken *objectStartToken;
@implementation ParamParser
+ (void)initialize {
if( objectStartToken == nil ) {
objectStartToken = [TDToken tokenWithTokenType:TDTokenTypeSymbol stringValue:@"{" floatValue:0.0];
}
if( arrayStartToken == nil ) {
arrayStartToken = [TDToken tokenWithTokenType:TDTokenTypeSymbol stringValue:@"[" floatValue:0.0];
}
}
- (id)init {
if( ( self = [super init] ) ) {
NSString *rubyHashGrammar = [NSString stringWithContentsOfFile:[self grammarPath]
encoding:NSUTF8StringEncoding
error:nil];
// NSString *rubyHashGrammar = [NSString stringWithContentsOfFile:[self grammarPath]
// encoding:NSUnicodeStringEncoding
// error:nil];
parser = [[TDParserFactory factory] parserFromGrammar:rubyHashGrammar
assembler:self
getTokenizer:&tokenizer];
}
return self;
}
@synthesize parser;
@synthesize tokenizer;
- (NSString *)grammarPath {
return [[NSBundle bundleForClass:[self class]] pathForResource:@"rubyhash"
ofType:@"grammar"];
}
- (NSArray *)parseParams:(NSString *)unparsedParams {
[[self tokenizer] setString:unparsedParams];
TDTokenAssembly *assembly = [TDTokenAssembly assemblyWithTokenizer:tokenizer];
TDAssembly *result = [[self parser] bestMatchFor:assembly];
return [result pop];
}
- (void)workOnStringAssembly:(TDAssembly *)assembly {
TDToken *tok = [assembly pop];
[assembly push:[[tok stringValue] stringByTrimmingQuotes]];
}
- (void)workOnNumberAssembly:(TDAssembly *)assembly {
TDToken *tok = [assembly pop];
[assembly push:[NSNumber numberWithFloat:[tok floatValue]]];
}
- (void)workOnNullAssembly:(TDAssembly *)assembly {
[assembly push:[NSNull null]];
}
- (void)workOnBooleanAssembly:(TDAssembly *)assembly {
TDToken *token = [assembly pop];
[assembly push:[NSNumber numberWithBool:[[token stringValue] isEqualToString:@"true"] ? YES : NO]];
}
- (void)workOnPropertyAssembly:(TDAssembly *)assembly {
id value = [assembly pop];
[assembly pop]; // discard =>
TDToken *tok = [assembly pop];
NSString *key = [[tok stringValue] stringByTrimmingQuotes];
Parameter *param = [[Parameter alloc] initWithName:key];
if( [value isKindOfClass:[NSArray class]] ) {
[param setGroupedParams:value];
} else {
[param setValue:value];
}
[assembly push:param];
}
- (void)workOnArrayAssembly:(TDAssembly *)assembly {
NSArray *elements = [assembly objectsAbove:arrayStartToken];
NSMutableString *product = [[NSMutableString alloc] init];
for( id element in [elements reverseObjectEnumerator] ) {
if( [element isKindOfClass:[TDToken class]] ) {
continue;
}
[product appendFormat:@"%@%@", [product length] > 0 ? @"," : @"", element];
}
[assembly pop]; // pop the [
[assembly push:product];
}
- (void)workOnObjectAssembly:(TDAssembly *)assembly {
NSArray *elements = [assembly objectsAbove:objectStartToken];
NSMutableArray *params = [NSMutableArray arrayWithCapacity:[elements count]];
for( id element in elements ) {
if( [element isKindOfClass:[Parameter class]] ) {
[params addObject:element];
}
}
[assembly pop]; // pop the {
[assembly push:params];
}
@end