forked from itod/parsekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEBNFParser.m
433 lines (361 loc) · 12.8 KB
/
EBNFParser.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// 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 "EBNFParser.h"
#import "NSString+ParseKitAdditions.h"
/*
statement = exprOrAssignment ';'
exprOrAssignment = expression | assigment
assigment = declaration '=' expression
declaration = '$' Word
variable = '$' Word
expression = term orTerm*
term = factor nextFactor*
orTerm = '|' term
factor = phrase | phraseStar | phraseQuestion | phrasePlus
nextFactor = factor
phrase = atomicValue | '(' expression ')'
phraseStar = phrase '*'
phraseQuestion = phrase '?'
phrasePlus = phrase '+'
atomicValue = Word | Number | QuotedString | variable
*/
static NSString * const kEBNFEqualsString = @"=";
static NSString * const kEBNFVariablePrefix = @"$";
static NSString * const kEBNFVariableSuffix = @"";
@interface PKParser (PKParserFactoryAdditionsFriend)
- (void)setTokenizer:(PKTokenizer *)t;
@end
@interface EBNFParser ()
- (void)addSymbolString:(NSString *)s toTokenizer:(PKTokenizer *)t;
- (void)didMatchWord:(PKAssembly *)a;
- (void)didMatchNum:(PKAssembly *)a;
- (void)didMatchQuotedString:(PKAssembly *)a;
- (void)didMatchStar:(PKAssembly *)a;
- (void)didMatchQuestion:(PKAssembly *)a;
- (void)didMatchPlus:(PKAssembly *)a;
- (void)didMatchAnd:(PKAssembly *)a;
- (void)didMatchOr:(PKAssembly *)a;
- (void)didMatchAssignment:(PKAssembly *)a;
- (void)didMatchVariable:(PKAssembly *)a;
@end
@implementation EBNFParser
- (id)init {
if ([super initWithSubparser:self.statementParser]) {
self.tokenizer = [PKTokenizer tokenizer];
[self addSymbolString:kEBNFEqualsString toTokenizer:self.tokenizer];
[self addSymbolString:kEBNFVariablePrefix toTokenizer:self.tokenizer];
[self addSymbolString:kEBNFVariableSuffix toTokenizer:self.tokenizer];
}
return self;
}
- (void)dealloc {
self.tokenizer = nil;
self.statementParser = nil;
self.exprOrAssignmentParser = nil;
self.assignmentParser = nil;
self.declarationParser = nil;
self.variableParser = nil;
self.expressionParser = nil;
self.termParser = nil;
self.orTermParser = nil;
self.factorParser = nil;
self.nextFactorParser = nil;
self.phraseParser = nil;
self.phraseStarParser = nil;
self.phraseQuestionParser = nil;
self.phrasePlusParser = nil;
self.atomicValueParser = 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];
}
- (void)addSymbolString:(NSString *)s toTokenizer:(PKTokenizer *)t {
if ([s length]) {
NSInteger c = [s characterAtIndex:0];
[t setTokenizerState:t.symbolState from:c to:c];
[t.symbolState add:s];
}
}
// statement = exprOrAssignment ';'
- (PKCollectionParser *)statementParser {
if (!statementParser) {
self.statementParser = [PKTrack track];
[statementParser add:self.exprOrAssignmentParser];
[statementParser add:[[PKSymbol symbolWithString:@";"] discard]];
}
return statementParser;
}
// exprOrAssignmentParser = expression | assignment
- (PKCollectionParser *)exprOrAssignmentParser {
if (!exprOrAssignmentParser) {
self.exprOrAssignmentParser = [PKAlternation alternation];
[exprOrAssignmentParser add:self.expressionParser];
[exprOrAssignmentParser add:self.assignmentParser];
}
return exprOrAssignmentParser;
}
// declaration = variable '=' expression
- (PKCollectionParser *)assignmentParser {
if (!assignmentParser) {
self.assignmentParser = [PKTrack track];
[assignmentParser add:self.declarationParser];
[assignmentParser add:[[PKSymbol symbolWithString:kEBNFEqualsString] discard]];
[assignmentParser add:self.expressionParser];
[assignmentParser setAssembler:self selector:@selector(didMatchAssignment:)];
}
return assignmentParser;
}
// declaration = '$' Word
- (PKCollectionParser *)declarationParser {
if (!declarationParser) {
self.declarationParser = [PKTrack track];
[declarationParser add:[[PKSymbol symbolWithString:kEBNFVariablePrefix] discard]];
[declarationParser add:[PKWord word]];
if ([kEBNFVariableSuffix length]) {
[declarationParser add:[[PKSymbol symbolWithString:kEBNFVariableSuffix] discard]];
}
}
return declarationParser;
}
// variable = '$' Word
- (PKCollectionParser *)variableParser {
if (!variableParser) {
self.variableParser = [PKTrack track];
[variableParser add:[[PKSymbol symbolWithString:kEBNFVariablePrefix] discard]];
[variableParser add:[PKWord word]];
if ([kEBNFVariableSuffix length]) {
[variableParser add:[[PKSymbol symbolWithString:kEBNFVariableSuffix] discard]];
}
}
return variableParser;
}
// expression = term orTerm*
- (PKCollectionParser *)expressionParser {
if (!expressionParser) {
self.expressionParser = [PKSequence sequence];
[expressionParser add:self.termParser];
[expressionParser add:[PKRepetition repetitionWithSubparser:self.orTermParser]];
}
return expressionParser;
}
// term = factor nextFactor*
- (PKCollectionParser *)termParser {
if (!termParser) {
self.termParser = [PKSequence sequence];
[termParser add:self.factorParser];
[termParser add:[PKRepetition repetitionWithSubparser:self.nextFactorParser]];
}
return termParser;
}
// orTerm = '|' term
- (PKCollectionParser *)orTermParser {
if (!orTermParser) {
self.orTermParser = [PKTrack track];
[orTermParser add:[[PKSymbol symbolWithString:@"|"] discard]];
[orTermParser add:self.termParser];
[orTermParser setAssembler:self selector:@selector(didMatchOr:)];
}
return orTermParser;
}
// factor = phrase | phraseStar | phraseQuestion | phrasePlus
- (PKCollectionParser *)factorParser {
if (!factorParser) {
self.factorParser = [PKAlternation alternation];
[factorParser add:self.phraseParser];
[factorParser add:self.phraseStarParser];
[factorParser add:self.phraseQuestionParser];
[factorParser add:self.phrasePlusParser];
}
return factorParser;
}
// nextFactor = factor
- (PKCollectionParser *)nextFactorParser {
if (!nextFactorParser) {
self.nextFactorParser = [PKAlternation alternation];
[nextFactorParser add:self.phraseParser];
[nextFactorParser add:self.phraseStarParser];
[nextFactorParser add:self.phraseQuestionParser];
[nextFactorParser add:self.phrasePlusParser];
[nextFactorParser setAssembler:self selector:@selector(didMatchAnd:)];
}
return nextFactorParser;
}
// phrase = atomicValue | '(' expression ')'
- (PKCollectionParser *)phraseParser {
if (!phraseParser) {
PKSequence *s = [PKTrack track];
[s add:[[PKSymbol symbolWithString:@"("] discard]];
[s add:self.expressionParser];
[s add:[[PKSymbol symbolWithString:@")"] discard]];
self.phraseParser = [PKAlternation alternation];
[phraseParser add:self.atomicValueParser];
[phraseParser add:s];
}
return phraseParser;
}
// phraseStar = phrase '*'
- (PKCollectionParser *)phraseStarParser {
if (!phraseStarParser) {
self.phraseStarParser = [PKSequence sequence];
[phraseStarParser add:self.phraseParser];
[phraseStarParser add:[[PKSymbol symbolWithString:@"*"] discard]];
[phraseStarParser setAssembler:self selector:@selector(didMatchStar:)];
}
return phraseStarParser;
}
// phraseQuestion = phrase '?'
- (PKCollectionParser *)phraseQuestionParser {
if (!phraseQuestionParser) {
self.phraseQuestionParser = [PKSequence sequence];
[phraseQuestionParser add:self.phraseParser];
[phraseQuestionParser add:[[PKSymbol symbolWithString:@"?"] discard]];
[phraseQuestionParser setAssembler:self selector:@selector(didMatchQuestion:)];
}
return phraseQuestionParser;
}
// phrasePlus = phrase '+'
- (PKCollectionParser *)phrasePlusParser {
if (!phrasePlusParser) {
self.phrasePlusParser = [PKSequence sequence];
[phrasePlusParser add:self.phraseParser];
[phrasePlusParser add:[[PKSymbol symbolWithString:@"+"] discard]];
[phrasePlusParser setAssembler:self selector:@selector(didMatchPlus:)];
}
return phrasePlusParser;
}
// atomicValue = Word | Number | QuotedString | Variable
- (PKCollectionParser *)atomicValueParser {
if (!atomicValueParser) {
self.atomicValueParser = [PKAlternation alternation];
PKParser *p = [PKWord word];
[p setAssembler:self selector:@selector(didMatchWord:)];
[atomicValueParser add:p];
p = [PKNumber number];
[p setAssembler:self selector:@selector(didMatchNum:)];
[atomicValueParser add:p];
p = [PKQuotedString quotedString];
[p setAssembler:self selector:@selector(didMatchQuotedString:)];
[atomicValueParser add:p];
p = self.variableParser;
[p setAssembler:self selector:@selector(didMatchVariable:)];
[atomicValueParser add:p];
}
return atomicValueParser;
}
- (void)didMatchWord:(PKAssembly *)a {
// NSLog(@"%s", _cmd);
// NSLog(@"a: %@", a);
PKToken *tok = [a pop];
[a push:[PKLiteral literalWithString:tok.stringValue]];
}
- (void)didMatchNum:(PKAssembly *)a {
// NSLog(@"%s", _cmd);
// NSLog(@"a: %@", a);
PKToken *tok = [a pop];
[a push:[PKLiteral literalWithString:tok.stringValue]];
}
- (void)didMatchQuotedString:(PKAssembly *)a {
// NSLog(@"%s", _cmd);
// NSLog(@"a: %@", a);
PKToken *tok = [a pop];
NSString *s = [tok.stringValue stringByTrimmingQuotes];
PKSequence *p = [PKSequence sequence];
PKTokenizer *t = [PKTokenizer tokenizerWithString:s];
PKToken *eof = [PKToken EOFToken];
while (eof != (tok = [t nextToken])) {
[p add:[PKLiteral literalWithString:tok.stringValue]];
}
[a push:p];
}
- (void)didMatchStar:(PKAssembly *)a {
// NSLog(@"%s", _cmd);
// NSLog(@"a: %@", a);
PKRepetition *p = [PKRepetition repetitionWithSubparser:[a pop]];
[a push:p];
}
- (void)didMatchQuestion:(PKAssembly *)a {
// NSLog(@"%s", _cmd);
// NSLog(@"a: %@", a);
PKAlternation *p = [PKAlternation alternation];
[p add:[a pop]];
[p add:[PKEmpty empty]];
[a push:p];
}
- (void)didMatchPlus:(PKAssembly *)a {
// NSLog(@"%s", _cmd);
// NSLog(@"a: %@", a);
id top = [a pop];
PKSequence *p = [PKSequence sequence];
[p add:top];
[p add:[PKRepetition repetitionWithSubparser:top]];
[a push:p];
}
- (void)didMatchAnd:(PKAssembly *)a {
// NSLog(@"%s", _cmd);
// NSLog(@"a: %@", a);
id top = [a pop];
PKSequence *p = [PKSequence sequence];
[p add:[a pop]];
[p add:top];
[a push:p];
}
- (void)didMatchOr:(PKAssembly *)a {
// NSLog(@"%s", _cmd);
// NSLog(@"a: %@", a);
id top = [a pop];
// NSLog(@"top: %@", top);
// NSLog(@"top class: %@", [top class]);
PKAlternation *p = [PKAlternation alternation];
[p add:[a pop]];
[p add:top];
[a push:p];
}
- (void)didMatchAssignment:(PKAssembly *)a {
NSLog(@"%s", _cmd);
NSLog(@"a: %@", a);
id val = [a pop];
PKToken *keyTok = [a pop];
NSMutableDictionary *table = [NSMutableDictionary dictionaryWithDictionary:a.target];
[table setObject:val forKey:keyTok.stringValue];
a.target = table;
}
- (void)didMatchVariable:(PKAssembly *)a {
// NSLog(@"%s", _cmd);
// NSLog(@"a: %@", a);
PKToken *keyTok = [a pop];
id val = [a.target objectForKey:keyTok.stringValue];
if (val) {
[a push:val];
}
}
@synthesize statementParser;
@synthesize exprOrAssignmentParser;
@synthesize assignmentParser;
@synthesize declarationParser;
@synthesize variableParser;
@synthesize expressionParser;
@synthesize termParser;
@synthesize orTermParser;
@synthesize factorParser;
@synthesize nextFactorParser;
@synthesize phraseParser;
@synthesize phraseStarParser;
@synthesize phraseQuestionParser;
@synthesize phrasePlusParser;
@synthesize atomicValueParser;
@end