My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes  
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
//
// PKPlistParser.m
// ParseKit
//
// Created by Todd Ditchendorf on 12/9/08.
// Copyright 2009 Todd Ditchendorf. All rights reserved.
//

#import "TDPlistParser.h"
#import "NSString+ParseKitAdditions.h"

//{
// 0 = 0;
// dictKey = {
// bar = foo;
// };
// 47 = 0;
// IntegerKey = 1;
// 47.7 = 0;
// <null> = <null>;
// ArrayKey = (
// "one one",
// two,
// three
// );
// "Null Key" = <null>;
// emptyDictKey = {
// };
// StringKey = String;
// "1.0" = 1;
// YESKey = 1;
// "NO Key" = 0;
//}


// dict = '{' dictContent '}'
// dictContent = keyValuePair*
// keyValuePair = key '=' value ';'
// key = num | string | null
// value = num | string | null | array | dict
// null = '<null>'
// string = Word | QuotedString
// num = Number

// array = '(' arrayContent ')'
// arrayContent = Empty | actualArray
// actualArray = value commaValue*
// commaValue = ',' value

static NSString *kTDPlistNullString = @"<null>";

@interface PKParser (PKParserFactoryAdditionsFriend)
- (void)setTokenizer:(PKTokenizer *)t;
@end

@interface PKCollectionParser ()
@property (nonatomic, readwrite, retain) NSMutableArray *subparsers;
@end

@interface TDPlistParser ()
@property (nonatomic, retain) PKToken *curly;
@property (nonatomic, retain) PKToken *paren;
@end

@implementation TDPlistParser

- (id)init {
self = [super init];
if (self != nil) {

self.tokenizer = [PKTokenizer tokenizer];
// add '<null>' as a multichar symbol
[self.tokenizer.symbolState add:kTDPlistNullString];

self.curly = [PKToken tokenWithTokenType:PKTokenTypeSymbol stringValue:@"{" floatValue:0.];
self.paren = [PKToken tokenWithTokenType:PKTokenTypeSymbol stringValue:@"(" floatValue:0.];
[self add:[PKEmpty empty]];
[self add:self.arrayParser];
[self add:self.dictParser];
}
return self;
}


- (void)dealloc {
// avoid retain cycle leaks by releasing the subparsers of all collection parsers
dictParser.subparsers = nil;
keyValuePairParser.subparsers = nil;
arrayParser.subparsers = nil;
commaValueParser.subparsers = nil;
keyParser.subparsers = nil;
valueParser.subparsers = nil;
stringParser.subparsers = nil;

self.tokenizer = nil;
self.dictParser = nil;
self.keyValuePairParser = nil;
self.arrayParser = nil;
self.commaValueParser = nil;
self.keyParser = nil;
self.valueParser = nil;
self.stringParser = nil;
self.numParser = nil;
self.nullParser = nil;
self.curly = nil;
self.paren = nil;
[super dealloc];
}


- (id)parse:(NSString *)s {
PKTokenAssembly *a = [PKTokenAssembly assemblyWithTokenizer:self.tokenizer];

// parse
PKAssembly *res = [self completeMatchFor:a];

// pop the built result off the assembly's stack and return.
// this will be an array or a dictionary or nil
return [res pop];
}


// dict = '{' dictContent '}'
// dictContent = keyValuePair*
- (PKCollectionParser *)dictParser {
if (!dictParser) {
self.dictParser = [PKTrack track];
[dictParser add:[PKSymbol symbolWithString:@"{"]]; // dont discard. serves as fence
[dictParser add:[PKRepetition repetitionWithSubparser:self.keyValuePairParser]];
[dictParser add:[[PKSymbol symbolWithString:@"}"] discard]];
[dictParser setAssembler:self selector:@selector(didMatchDict:)];
}
return dictParser;
}


// keyValuePair = key '=' value ';'
- (PKCollectionParser *)keyValuePairParser {
if (!keyValuePairParser) {
self.keyValuePairParser = [PKTrack track];
[keyValuePairParser add:self.keyParser];
[keyValuePairParser add:[[PKSymbol symbolWithString:@"="] discard]];
[keyValuePairParser add:self.valueParser];
[keyValuePairParser add:[[PKSymbol symbolWithString:@";"] discard]];
}
return keyValuePairParser;
}


// array = '(' arrayContent ')'
// arrayContent = Empty | actualArray
// actualArray = value commaValue*
- (PKCollectionParser *)arrayParser {
if (!arrayParser) {
self.arrayParser = [PKTrack track];
[arrayParser add:[PKSymbol symbolWithString:@"("]]; // dont discard. serves as fence

PKAlternation *arrayContent = [PKAlternation alternation];
[arrayContent add:[PKEmpty empty]];

PKSequence *actualArray = [PKSequence sequence];
[actualArray add:self.valueParser];
[actualArray add:[PKRepetition repetitionWithSubparser:self.commaValueParser]];

[arrayContent add:actualArray];
[arrayParser add:arrayContent];
[arrayParser add:[[PKSymbol symbolWithString:@")"] discard]];
[arrayParser setAssembler:self selector:@selector(didMatchArray:)];
}
return arrayParser;
}


// key = num | string | null
- (PKCollectionParser *)keyParser {
if (!keyParser) {
self.keyParser = [PKAlternation alternation];
[keyParser add:self.numParser];
[keyParser add:self.stringParser];
[keyParser add:self.nullParser];
}
return keyParser;
}


// value = num | string | null | array | dict
- (PKCollectionParser *)valueParser {
if (!valueParser) {
self.valueParser = [PKAlternation alternation];
[valueParser add:self.arrayParser];
[valueParser add:self.dictParser];
[valueParser add:self.stringParser];
[valueParser add:self.numParser];
[valueParser add:self.nullParser];
}
return valueParser;
}


- (PKCollectionParser *)commaValueParser {
if (!commaValueParser) {
self.commaValueParser = [PKSequence sequence];
[commaValueParser add:[[PKSymbol symbolWithString:@","] discard]];
[commaValueParser add:self.valueParser];
}
return commaValueParser;
}


// string = QuotedString | Word
- (PKCollectionParser *)stringParser {
if (!stringParser) {
self.stringParser = [PKAlternation alternation];

// we have to remove the quotes from QuotedString string values. so set an assembler method to do that
PKParser *quotedString = [PKQuotedString quotedString];
[quotedString setAssembler:self selector:@selector(didMatchQuotedString:)];
[stringParser add:quotedString];

// handle non-quoted string values (Words) in a separate assembler method for simplicity.
PKParser *word = [PKWord word];
[word setAssembler:self selector:@selector(didMatchWord:)];
[stringParser add:word];
}
return stringParser;
}


- (PKParser *)numParser {
if (!numParser) {
self.numParser = [PKNumber number];
[numParser setAssembler:self selector:@selector(didMatchNum:)];
}
return numParser;
}


// null = '<null>'
- (PKParser *)nullParser {
if (!nullParser) {
// thus must be a PKSymbol (not a PKLiteral) to match the resulting '<null>' symbol tok
self.nullParser = [PKSymbol symbolWithString:kTDPlistNullString];
[nullParser setAssembler:self selector:@selector(didMatchNull:)];
}
return nullParser;
}


- (void)didMatchDict:(PKAssembly *)a {
NSArray *objs = [a objectsAbove:self.curly];
NSInteger count = [objs count];
NSAssert1(0 == count % 2, @"in -%s, the assembly's stack's count should be a multiple of 2", _cmd);

NSMutableDictionary *res = [NSMutableDictionary dictionaryWithCapacity:count / 2.];
if (count) {
NSInteger i = 0;
for ( ; i < [objs count] - 1; i++) {
id value = [objs objectAtIndex:i++];
id key = [objs objectAtIndex:i];
[res setObject:value forKey:key];
}
}

[a pop]; // discard '{' tok
[a push:[[res copy] autorelease]];
}


- (void)didMatchArray:(PKAssembly *)a {
NSArray *objs = [a objectsAbove:self.paren];
NSMutableArray *res = [NSMutableArray arrayWithCapacity:[objs count]];

for (id obj in [objs reverseObjectEnumerator]) {
[res addObject:obj];
}

[a pop]; // discard '(' tok
[a push:[[res copy] autorelease]];
}


- (void)didMatchQuotedString:(PKAssembly *)a {
PKToken *tok = [a pop];
[a push:[tok.stringValue stringByTrimmingQuotes]];
}


- (void)didMatchWord:(PKAssembly *)a {
PKToken *tok = [a pop];
[a push:tok.stringValue];
}


- (void)didMatchNum:(PKAssembly *)a {
PKToken *tok = [a pop];
[a push:[NSNumber numberWithFloat:tok.floatValue]];
}


- (void)didMatchNull:(PKAssembly *)a {
[a pop]; // discard '<null>' tok
[a push:[NSNull null]];
}

@synthesize dictParser;
@synthesize keyValuePairParser;
@synthesize arrayParser;
@synthesize commaValueParser;
@synthesize keyParser;
@synthesize valueParser;
@synthesize stringParser;
@synthesize numParser;
@synthesize nullParser;
@synthesize curly;
@synthesize paren;
@end

Change log

r1405 by todd.ditchendorf on Oct 7, 2009   Diff
no more a.count. use [a count] instead
Go to: 
Project members, sign in to write a code review

Older revisions

r1367 by todd.ditchendorf on Sep 15, 2009   Diff
fixing 64bit build issues
r1276 by todd.ditchendorf on Aug 2, 2009   Diff
changing to didMatch and willMatch
r1177 by todd.ditchendorf on Jul 3, 2009   Diff
changing PKNum to PKNumber
All revisions of this file

File info

Size: 9157 bytes, 316 lines
Powered by Google Project Hosting