forked from itod/parsekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTDJsonParser.m
322 lines (257 loc) · 9.41 KB
/
TDJsonParser.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2010 Todd Ditchendorf
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "TDJsonParser.h"
#import "ParseKit.h"
#import "NSString+ParseKitAdditions.h"
@interface PKParser (PKParserFactoryAdditionsFriend)
- (void)setTokenizer:(PKTokenizer *)t;
@end
@interface PKCollectionParser ()
@property (nonatomic, readwrite, retain) NSMutableArray *subparsers;
@end
@interface TDJsonParser ()
@property (nonatomic, retain) PKToken *curly;
@property (nonatomic, retain) PKToken *bracket;
@end
@implementation TDJsonParser
- (id)init {
return [self initWithIntentToAssemble:YES];
}
- (id)initWithIntentToAssemble:(BOOL)yn {
if (self = [super init]) {
shouldAssemble = yn;
self.curly = [PKToken tokenWithTokenType:PKTokenTypeSymbol stringValue:@"{" floatValue:0.0];
self.bracket = [PKToken tokenWithTokenType:PKTokenTypeSymbol stringValue:@"[" floatValue:0.0];
self.tokenizer = [PKTokenizer tokenizer];
[self.tokenizer setTokenizerState:self.tokenizer.symbolState from: '/' to: '/']; // JSON doesn't have slash slash or slash star comments
[self.tokenizer setTokenizerState:self.tokenizer.symbolState from: '\'' to: '\'']; // JSON does not have single quoted strings
[self add:self.objectParser];
[self add:self.arrayParser];
}
return self;
}
- (void)dealloc {
// yikes. this is necessary to prevent a very nasty retain cycle leak.
// to be safe, release the subparsers of all collection parsers (as they may have retain cycles in complex grammars like this one)
// technically i only need to release the valueParser.subparers in this case, but better to be paranoid than to leak.
booleanParser.subparsers = nil;
arrayParser.subparsers = nil;
objectParser.subparsers = nil;
valueParser.subparsers = nil;
commaValueParser.subparsers = nil;
propertyParser.subparsers = nil;
commaPropertyParser.subparsers = nil;
self.tokenizer = nil;
self.stringParser = nil;
self.numberParser = nil;
self.nullParser = nil;
self.booleanParser = nil;
self.arrayParser = nil;
self.objectParser = nil;
self.valueParser = nil;
self.propertyParser = nil;
self.commaPropertyParser = nil;
self.commaValueParser = nil;
self.curly = nil;
self.bracket = nil;
[super dealloc];
}
- (id)parse:(NSString *)s {
self.tokenizer.string = s;
PKTokenAssembly *a = [PKTokenAssembly assemblyWithTokenizer:self.tokenizer];
PKAssembly *result = [self completeMatchFor:a];
return [result pop];
}
- (PKParser *)stringParser {
if (!stringParser) {
self.stringParser = [PKQuotedString quotedString];
if (shouldAssemble) {
[stringParser setAssembler:self selector:@selector(didMatchString:)];
}
}
return stringParser;
}
- (PKParser *)numberParser {
if (!numberParser) {
self.numberParser = [PKNumber number];
if (shouldAssemble) {
[numberParser setAssembler:self selector:@selector(didMatchNumber:)];
}
}
return numberParser;
}
- (PKParser *)nullParser {
if (!nullParser) {
self.nullParser = [[PKLiteral literalWithString:@"null"] discard];
if (shouldAssemble) {
[nullParser setAssembler:self selector:@selector(didMatchNull:)];
}
}
return nullParser;
}
- (PKCollectionParser *)booleanParser {
if (!booleanParser) {
self.booleanParser = [PKAlternation alternation];
[booleanParser add:[PKLiteral literalWithString:@"true"]];
[booleanParser add:[PKLiteral literalWithString:@"false"]];
if (shouldAssemble) {
[booleanParser setAssembler:self selector:@selector(didMatchBoolean:)];
}
}
return booleanParser;
}
- (PKCollectionParser *)arrayParser {
if (!arrayParser) {
// array = '[' content ']'
// content = Empty | actualArray
// actualArray = value commaValue*
PKTrack *actualArray = [PKTrack track];
[actualArray add:self.valueParser];
[actualArray add:[PKRepetition repetitionWithSubparser:self.commaValueParser]];
PKAlternation *content = [PKAlternation alternation];
[content add:[PKEmpty empty]];
[content add:actualArray];
self.arrayParser = [PKSequence sequence];
[arrayParser add:[PKSymbol symbolWithString:@"["]]; // serves as fence
[arrayParser add:content];
[arrayParser add:[[PKSymbol symbolWithString:@"]"] discard]];
if (shouldAssemble) {
[arrayParser setAssembler:self selector:@selector(didMatchArray:)];
}
}
return arrayParser;
}
- (PKCollectionParser *)objectParser {
if (!objectParser) {
// object = '{' content '}'
// content = Empty | actualObject
// actualObject = property commaProperty*
// property = QuotedString ':' value
// commaProperty = ',' property
PKTrack *actualObject = [PKTrack track];
[actualObject add:self.propertyParser];
[actualObject add:[PKRepetition repetitionWithSubparser:self.commaPropertyParser]];
PKAlternation *content = [PKAlternation alternation];
[content add:[PKEmpty empty]];
[content add:actualObject];
self.objectParser = [PKSequence sequence];
[objectParser add:[PKSymbol symbolWithString:@"{"]]; // serves as fence
[objectParser add:content];
[objectParser add:[[PKSymbol symbolWithString:@"}"] discard]];
if (shouldAssemble) {
[objectParser setAssembler:self selector:@selector(didMatchObject:)];
}
}
return objectParser;
}
- (PKCollectionParser *)valueParser {
if (!valueParser) {
self.valueParser = [PKAlternation alternation];
[valueParser add:self.stringParser];
[valueParser add:self.numberParser];
[valueParser add:self.nullParser];
[valueParser add:self.booleanParser];
[valueParser add:self.arrayParser];
[valueParser add:self.objectParser];
}
return valueParser;
}
- (PKCollectionParser *)commaValueParser {
if (!commaValueParser) {
self.commaValueParser = [PKTrack sequence];
[commaValueParser add:[[PKSymbol symbolWithString:@","] discard]];
[commaValueParser add:self.valueParser];
}
return commaValueParser;
}
- (PKCollectionParser *)propertyParser {
if (!propertyParser) {
self.propertyParser = [PKSequence sequence];
[propertyParser add:[PKQuotedString quotedString]];
[propertyParser add:[[PKSymbol symbolWithString:@":"] discard]];
[propertyParser add:self.valueParser];
if (shouldAssemble) {
[propertyParser setAssembler:self selector:@selector(didMatchProperty:)];
}
}
return propertyParser;
}
- (PKCollectionParser *)commaPropertyParser {
if (!commaPropertyParser) {
self.commaPropertyParser = [PKTrack sequence];
[commaPropertyParser add:[[PKSymbol symbolWithString:@","] discard]];
[commaPropertyParser add:self.propertyParser];
}
return commaPropertyParser;
}
- (void)didMatchNull:(PKAssembly *)a {
[a push:[NSNull null]];
}
- (void)didMatchNumber:(PKAssembly *)a {
PKToken *tok = [a pop];
[a push:[NSNumber numberWithFloat:tok.floatValue]];
}
- (void)didMatchString:(PKAssembly *)a {
PKToken *tok = [a pop];
[a push:[tok.stringValue stringByTrimmingQuotes]];
}
- (void)didMatchBoolean:(PKAssembly *)a {
PKToken *tok = [a pop];
[a push:[NSNumber numberWithBool:[tok.stringValue isEqualToString:@"true"] ? YES : NO]];
}
- (void)didMatchArray:(PKAssembly *)a {
NSArray *elements = [a objectsAbove:self.bracket];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[elements count]];
for (id element in [elements reverseObjectEnumerator]) {
if (element) {
[array addObject:element];
}
}
[a pop]; // pop the [
[a push:array];
}
- (void)didMatchObject:(PKAssembly *)a {
NSArray *elements = [a objectsAbove:self.curly];
NSMutableDictionary *d = [NSMutableDictionary dictionaryWithCapacity:[elements count] / 2.];
NSInteger i = 0;
for ( ; i < [elements count] - 1; i++) {
id value = [elements objectAtIndex:i++];
NSString *key = [elements objectAtIndex:i];
if (key && value) {
[d setObject:value forKey:key];
}
}
[a pop]; // pop the {
[a push:d];
}
- (void)didMatchProperty:(PKAssembly *)a {
id value = [a pop];
PKToken *tok = [a pop];
NSString *key = [tok.stringValue stringByTrimmingQuotes];
[a push:key];
[a push:value];
}
@synthesize stringParser;
@synthesize numberParser;
@synthesize nullParser;
@synthesize booleanParser;
@synthesize arrayParser;
@synthesize objectParser;
@synthesize valueParser;
@synthesize commaValueParser;
@synthesize propertyParser;
@synthesize commaPropertyParser;
@synthesize curly;
@synthesize bracket;
@end