This repository has been archived by the owner on Apr 29, 2022. It is now read-only.
forked from itod/parsekit
/
PKParseTreeAssembler.m
184 lines (148 loc) · 5.63 KB
/
PKParseTreeAssembler.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
// 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 "PKParseTreeAssembler.h"
#import <ParseKit/ParseKit.h>
#import "PKParseTree.h"
#import "PKRuleNode.h"
#import "PKTokenNode.h"
@interface PKParseTreeAssembler ()
- (NSString *)ruleNameForSelName:(NSString *)selName withPrefix:(NSString *)pre;
- (void)didMatchRuleNamed:(NSString *)name assembly:(PKAssembly *)a;
- (void)willMatchRuleNamed:(NSString *)name assembly:(PKAssembly *)a;
- (void)parser:(PKParser *)p didMatchToken:(PKAssembly *)a;
- (PKParseTree *)currentFrom:(PKAssembly *)a;
- (void)removeUnmatchedChildrenFrom:(PKParseTree *)n;
@property (nonatomic, retain) NSMutableDictionary *ruleNames;
@property (nonatomic, copy) NSString *assemblerPrefix;
@property (nonatomic, copy) NSString *preassemblerPrefix;
@property (nonatomic, copy) NSString *suffix;
@end
@implementation PKParseTreeAssembler
- (id)init {
if (self = [super init]) {
self.ruleNames = [NSMutableDictionary dictionary];
self.preassemblerPrefix = @"parser:willMatch";
self.assemblerPrefix = @"parser:didMatch";
self.suffix = @":";
}
return self;
}
- (void)dealloc {
self.ruleNames = nil;
self.preassemblerPrefix = nil;
self.assemblerPrefix = nil;
self.suffix = nil;
[super dealloc];
}
- (BOOL)respondsToSelector:(SEL)sel {
return YES;
if ([super respondsToSelector:sel]) {
return YES;
} else {
NSString *selName = NSStringFromSelector(sel);
if ([selName hasPrefix:assemblerPrefix] && [selName hasSuffix:suffix]) {
return YES;
}
}
return NO;
}
- (id)performSelector:(SEL)sel withObject:(id)obj withObject:(id)obj1 {
NSString *selName = NSStringFromSelector(sel);
//PKParser *p = obj;
PKAssembly *a = obj1;
if ([selName hasPrefix:assemblerPrefix] && [selName hasSuffix:suffix]) {
[self didMatchRuleNamed:[self ruleNameForSelName:selName withPrefix:assemblerPrefix] assembly:a];
} else if ([selName hasPrefix:preassemblerPrefix] && [selName hasSuffix:suffix]) {
[self willMatchRuleNamed:[self ruleNameForSelName:selName withPrefix:preassemblerPrefix] assembly:a];
} else if ([super respondsToSelector:sel]) {
return [super performSelector:sel withObject:obj withObject:obj1];
} else {
NSAssert(0, @"");
}
return nil;
}
- (NSString *)ruleNameForSelName:(NSString *)selName withPrefix:(NSString *)prefix {
NSString *ruleName = [ruleNames objectForKey:selName];
if (!ruleName) {
NSUInteger prefixLen = [prefix length];
NSInteger c = ((NSInteger)[selName characterAtIndex:prefixLen]) + 32; // lowercase
NSRange r = NSMakeRange(prefixLen + 1, [selName length] - (prefixLen + [suffix length] + 1 /*:*/));
ruleName = [NSString stringWithFormat:@"%C%@", c, [selName substringWithRange:r]];
[ruleNames setObject:ruleName forKey:selName];
}
return ruleName;
}
- (void)willMatchRuleNamed:(NSString *)name assembly:(PKAssembly *)a {
PKParseTree *current = [self currentFrom:a];
[self parser:nil didMatchToken:a];
current = [current addChildRule:name];
a.target = current;
}
- (void)didMatchRuleNamed:(NSString *)name assembly:(PKAssembly *)a {
PKParseTree *current = [self currentFrom:a];
NSArray *origChildren = [[[current children] mutableCopy] autorelease];
PKParseTree *oldCurrent = nil;
while ([current isKindOfClass:[PKRuleNode class]] && ![[(id)current name] isEqualToString:name]) {
oldCurrent = [[current retain] autorelease];
a.target = [current parent];
current = [self currentFrom:a];
[self parser:nil didMatchToken:a];
}
if (oldCurrent && ![oldCurrent isMatched]) {
[(id)[current children] addObjectsFromArray:origChildren];
}
[self parser:nil didMatchToken:a];
current = [self currentFrom:a];
[self removeUnmatchedChildrenFrom:current];
[current setMatched:YES];
a.target = [current parent];
}
- (void)removeUnmatchedChildrenFrom:(PKParseTree *)n {
NSMutableArray *remove = [NSMutableArray array];
for (id child in [n children]) {
if (![child isMatched]) {
[remove addObject:child];
}
}
for (id child in remove) {
[(id)[n children] removeObject:child];
}
}
- (PKParseTree *)currentFrom:(PKAssembly *)a {
PKParseTree *current = a.target;
if (!current) {
current = [PKParseTree parseTree];
a.target = current;
}
return current;
}
- (void)parser:(PKParser *)p didMatchToken:(PKAssembly *)a {
PKParseTree *current = [self currentFrom:a];
if ([current isMatched]) return;
NSMutableArray *toks = [NSMutableArray arrayWithCapacity:[a.stack count]];
while (![a isStackEmpty]) {
id tok = [a pop];
NSAssert([tok isKindOfClass:[PKToken class]], @"");
[toks addObject:tok];
}
for (id tok in [toks reverseObjectEnumerator]) {
PKTokenNode *n = [current addChildToken:tok];
[n setMatched:YES];
}
}
@synthesize ruleNames;
@synthesize preassemblerPrefix;
@synthesize assemblerPrefix;
@synthesize suffix;
@end