-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathANTLRAST.m
458 lines (411 loc) · 12.9 KB
/
ANTLRAST.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* ANTLRAST.m
Copyright (C) 1998, 1999 Helge Hess and Manuel Guesdon
All rights reserved.
Authors: Helge Hess <[email protected]>
Manuel Guesdon <[email protected]>
This file is part of the ANTLR Objective-C Library.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
We disclaim all warranties with regard to this software, including all
implied warranties of merchantability and fitness, in no event shall
we be liable for any special, indirect or consequential damages or any
damages whatsoever resulting from loss of use, data or profits, whether in
an action of contract, negligence or other tortious action, arising out of
or in connection with the use or performance of this software.
---------------------------------------------------------------------------
This code is based on ANTLR toolkit (http://www.antlr.org)
from MageLang Institute:
"We encourage users to develop software with ANTLR. However,
we do ask that credit is given to us for developing
ANTLR. By "credit", we mean that if you use ANTLR or
incorporate any source code into one of your programs
(commercial product, research project, or otherwise) that
you acknowledge this fact somewhere in the documentation,
research report, etc... If you like ANTLR and have
developed a nice tool with the output, please mention that
you developed it using ANTLR."
*/
#include "ANTLRCommon.h"
#include "ANTLRBitSet.h"
#include "ANTLRAST.h"
#include "ANTLRASTFactory.h"
#include "ANTLRASTEnumerator.h"
#include "ANTLRASTNULLType.h"
BOOL verboseStringConversion = NO;
NSString** tokenNames = NULL;
ANTLRDefAST ANTLRASTNULL=nil;
@implementation ANTLRAST
//--------------------------------------------------------------------
+(void)initialize
{
ANTLRASTNULL=[ASTNULLType new];
};
//--------------------------------------------------------------------
#if !ANTLR_GC
+(void)dealloc
{
DESTROY(ANTLRASTNULL);
};
#endif
//------------------------------------------------------------------------------
#if !ANTLR_GC
-(void)dealloc
{
DESTROY(down);
DESTROY(right);
[super dealloc];
};
#endif
//--------------------------------------------------------------------
-(id)copyWithZone:(NSZone*)_zone
{
ANTLRDefAST clone = [[isa allocWithZone:_zone] init];
[clone setFirstChild:[down copyWithZone:_zone]];
[clone setNextSibling:[right copyWithZone:_zone]];
return clone;
};
//------------------------------------------------------------------------------
-(void)addChild:(ANTLRDefAST)_ast // Add a (rightmost) child to this node
{
LOGObjectFnStart();
if (_ast)
{
ANTLRAST *tmp=down;
if (tmp)
{
while (tmp->right)
tmp=(ANTLRAST *)tmp->right;
[tmp setNextSibling:_ast];
}
else
[self setFirstChild:_ast];
};
LOGObjectFnStop();
};
//------------------------------------------------------------------------------
-(void)doWorkForFindAll:(NSMutableArray*) v
target:(ANTLRDefAST)target
partialMatch:(BOOL)partialMatch
{
// Start walking sibling lists, looking for matches.
ANTLRDefAST sibling=nil;
LOGObjectFnStart();
for (sibling=self;
sibling;
sibling=[sibling nextSibling])
{
if ( (partialMatch && [sibling equalsTreePartial:target])
|| (!partialMatch && [sibling equalsTree:target]))
[v addObject:sibling];
// regardless of match or not, check any children for matches
if ([sibling firstChild])
[[sibling firstChild] doWorkForFindAll:v
target:target
partialMatch:partialMatch];
};
LOGObjectFnStop();
};
//------------------------------------------------------------------------------
-(BOOL) equalsList:(ANTLRDefAST)t
{
ANTLRDefAST sibling;
// the empty tree is not a match of any non-null tree.
if (t)
{
// Otherwise, start walking sibling lists. First mismatch, return NO.
for (sibling = self;
sibling && t;
sibling = [sibling nextSibling], t = [t nextSibling])
{
// as a quick optimization, check roots first.
if (![sibling isEqualToAST:t])
return NO;
else
{
// if roots match, do full list match test on children.
if ([sibling firstChild])
{
if (![[sibling firstChild] equalsList:[t firstChild]])
return NO;
}
// sibling has no kids, make sure t doesn't either
else if ([t firstChild])
return NO;
};
};
if (!sibling && !t)
return YES;
};
// one sibling list has more than the other
return NO;
};
//------------------------------------------------------------------------------
// Is 'sub' a subtree of this list?
// The siblings of the root are NOT ignored.
-(BOOL) equalsListPartial:(ANTLRDefAST) sub
{
// the empty tree is always a subset of any tree.
if (sub)
{
ANTLRDefAST sibling;
// Otherwise, start walking sibling lists. First mismatch, return NO.
for (sibling=self;
sibling && sub;
sibling=[sibling nextSibling], sub=[sub nextSibling])
{
// as a quick optimization, check roots first.
if ([sibling tokenType] != [sub tokenType])
return NO;
else if ([sibling firstChild]) // if roots match, do partial list match test on children.
{
if (![[sibling firstChild] equalsListPartial:[sub firstChild]] )
return NO;
};
};
if (!sibling && sub)
// nothing left to match in this tree, but subtree has more
return NO;
};
// either both are null or sibling has more, but subtree doesn't
return YES;
};
//------------------------------------------------------------------------------
// Is tree rooted at 'this' equal to 't'? The siblings
// of 'this' are ignored.
-(BOOL)equalsTree:(ANTLRDefAST)t
{
// check roots first.
if (![self isEqualToAST:t])
return NO;
else if ([self firstChild]) // if roots match, do full list match test on children.
{
if (![[self firstChild] equalsList:[t firstChild]])
return NO;
}
else if ([t firstChild]) // sibling has no kids, make sure t doesn't either
return NO;
return YES;
};
//------------------------------------------------------------------------------
-(BOOL)equalsTreePartial:(ANTLRDefAST)sub
{
// the empty tree is always a subset of any tree.
if (sub)
{
// check roots first.
if (![self isEqualToAST:sub])
return NO;
else if ([self firstChild]) // if roots match, do full list partial match test on children.
{
if (![[self firstChild] equalsListPartial:[sub firstChild]])
return NO;
};
};
return YES;
}
//------------------------------------------------------------------------------
// Walk the tree looking for all exact subtree matches. Return
// an ASTEnumerator that lets the caller walk the list
// of subtree roots found herein.
-(ANTLRASTEnumerator*)findAll:(ANTLRDefAST)target
{
ANTLRASTEnumerator* enumerator=nil;
LOGObjectFnStart();
// the empty tree cannot result in an enumeration
if (target)
{
NSMutableArray* roots = AUTORELEASE([NSMutableArray new]);
[self doWorkForFindAll:roots
target:target
partialMatch:NO]; // find all matches recursively
enumerator= [[ANTLRASTEnumerator alloc] initWithNodes:roots];
};
LOGObjectFnStop();
return enumerator;
};
//------------------------------------------------------------------------------
// Walk the tree looking for all subtrees. Return
// an ASTEnumerator that lets the caller walk the list
// of subtree roots found herein.
-(ANTLRASTEnumerator*) findAllPartial:(ANTLRDefAST) sub
{
ANTLRASTEnumerator* enumerator=nil;
LOGObjectFnStart();
// the empty tree cannot result in an enumeration
if (sub)
{
NSMutableArray* roots = AUTORELEASE([NSMutableArray new]);
[self doWorkForFindAll:roots
target:sub
partialMatch:YES]; // find all matches recursively
enumerator= [[ANTLRASTEnumerator alloc] initWithNodes:roots];
};
LOGObjectFnStop();
return enumerator;
};
//------------------------------------------------------------------------------
// Remove all children
-(void)removeChildren
{
LOGObjectFnStart();
ASSIGN(down,nil);
LOGObjectFnStop();
};
//------------------------------------------------------------------------------
+(void)setVerboseStringConversion:(BOOL)verbose
withNames:(NSString**) names
{
verboseStringConversion = verbose;
tokenNames = names;
};
//------------------------------------------------------------------------------
-(BOOL)isEqualToAST:(ANTLRDefAST)_ast
{
if (!_ast)
return NO;
else
return [self tokenType] == [_ast tokenType];
}
//------------------------------------------------------------------------------
// Set/Get the first child of this node; null if no children
-(void)setFirstChild:(ANTLRDefAST)_child
{
LOGObjectFnStart();
ASSIGN(down,_child);
LOGObjectFnStop();
};
//------------------------------------------------------------------------------
-(ANTLRDefAST)firstChild
{
return down;
};
//------------------------------------------------------------------------------
// Set/Get the next sibling in line after this one
-(void)setNextSibling:(ANTLRDefAST)_next
{
LOGObjectFnStart();
ASSIGN(right,_next);
LOGObjectFnStop();
};
//------------------------------------------------------------------------------
-(ANTLRDefAST)nextSibling
{
return right;
};
//------------------------------------------------------------------------------
// Set/Get the token text for this node
-(void)setText:(NSString *)_text
{
};
//------------------------------------------------------------------------------
-(NSString*)text
{
return @"";
};
//------------------------------------------------------------------------------
// Set/Get the token type for this node
- (void)setTokenType:(ANTLRTokenType)_type;
{
};
//------------------------------------------------------------------------------
- (ANTLRTokenType)tokenType
{
return 0;
};
//------------------------------------------------------------------------------
-(void)setWithToken:(ANTLRDefToken)_token
{
LOGObjectFnStart();
[self setTokenType:[_token tokenType]];
[self setText:[_token text]];
LOGObjectFnStop();
};
//------------------------------------------------------------------------------
-(void)setWithAST:(ANTLRDefAST)_AST
{
LOGObjectFnStart();
[self setTokenType:[_AST tokenType]];
[self setText:[_AST text]];
LOGObjectFnStop();
};
//------------------------------------------------------------------------------
-(NSString*)description
{
return [self toString];
};
//------------------------------------------------------------------------------
-(NSString*)toString
{
// if verbose and type name not same as text (keyword probably)
if ( verboseStringConversion &&
[[self text] caseInsensitiveCompare:tokenNames[[self tokenType]]]!=NSOrderedSame &&
[[self text] caseInsensitiveCompare:[[tokenNames[[self tokenType]] stringByDeletingSuffix:@"\""] stringByDeletingPrefix:@"\""]]!=NSOrderedSame)
{
NSString* string=[NSString stringWithFormat:@"[%@,<%@>]",
[self text],
tokenNames[[self tokenType]]];
//return string.toString();
return string;
}
else
return [self text];
}
//------------------------------------------------------------------------------
// Print out a child-sibling tree in LISP notation
-(NSString*)toStringList
{
return [self toStringListWithSiblingSeparator:@" "
openSeparator:@" ("
closeSeparator:@" )"];
};
//------------------------------------------------------------------------------
-(NSString*)toStringListWithSiblingSeparator:(NSString*)_siblingSep
openSeparator:(NSString*)_openSep
closeSeparator:(NSString*)_closeSep
{
ANTLRDefAST t = self;
NSString* ts=AUTORELEASE([NSString new]);
LOGObjectFnStart();
if ([t firstChild])
ts=[ts stringByAppendingString:_openSep];
ts=[ts stringByAppendingFormat:@"%@%@",
_siblingSep,
[self toString]];
if ([t firstChild])
ts=[ts stringByAppendingString:
[[t firstChild]
toStringListWithSiblingSeparator:_siblingSep
openSeparator:_openSep
closeSeparator:_closeSep]];
if ([t firstChild])
ts=[ts stringByAppendingString:_closeSep];
if ([t nextSibling])
ts=[ts stringByAppendingString:
[[t nextSibling]
toStringListWithSiblingSeparator:_siblingSep
openSeparator:_openSep
closeSeparator:_closeSep]];
LOGObjectFnStop();
return ts;
}
//------------------------------------------------------------------------------
-(NSString*)toStringTree
{
ANTLRDefAST t = self;
NSString* ts=AUTORELEASE([NSString new]);
LOGObjectFnStart();
if ([t firstChild])
ts=[ts stringByAppendingString:@" ("];
ts=[ts stringByAppendingFormat:@" %@",
[self toString]];
if ([t firstChild])
ts=[ts stringByAppendingString:[[t firstChild] toStringList]];
if ([t firstChild])
ts=[ts stringByAppendingString:@" )"];
LOGObjectFnStop();
return ts;
};
@end