forked from gavinkwoe/todparsekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKSymbolNode.m
76 lines (60 loc) · 2.02 KB
/
PKSymbolNode.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
//
// PKSymbolNode.m
// ParseKit
//
// Created by Todd Ditchendorf on 1/20/06.
// Copyright 2009 Todd Ditchendorf. All rights reserved.
//
#import <ParseKit/PKSymbolNode.h>
#import <ParseKit/PKSymbolRootNode.h>
@interface PKSymbolNode ()
@property (nonatomic, readwrite, retain) NSString *ancestry;
@property (nonatomic, assign) PKSymbolNode *parent; // this must be 'assign' to avoid retain loop leak
@property (nonatomic, retain) NSMutableDictionary *children;
@property (nonatomic) PKUniChar character;
@property (nonatomic, retain) NSString *string;
- (void)determineAncestry;
@end
@implementation PKSymbolNode
- (id)initWithParent:(PKSymbolNode *)p character:(PKUniChar)c {
if (self = [super init]) {
self.parent = p;
self.character = c;
self.children = [NSMutableDictionary dictionary];
// this private property is an optimization.
// cache the NSString for the char to prevent it being constantly recreated in -determinAncestry
self.string = [NSString stringWithFormat:@"%C", character];
[self determineAncestry];
}
return self;
}
- (void)dealloc {
parent = nil; // makes clang static analyzer happy
self.ancestry = nil;
self.string = nil;
self.children = nil;
[super dealloc];
}
- (void)determineAncestry {
if (PKEOF == parent.character) { // optimization for sinlge-char symbol (parent is symbol root node)
self.ancestry = string;
} else {
NSMutableString *result = [NSMutableString string];
PKSymbolNode *n = self;
while (PKEOF != n.character) {
[result insertString:n.string atIndex:0];
n = n.parent;
}
//self.ancestry = [[result copy] autorelease]; // assign an immutable copy
self.ancestry = result; // optimization
}
}
- (NSString *)description {
return [NSString stringWithFormat:@"<PKSymbolNode %@>", self.ancestry];
}
@synthesize ancestry;
@synthesize parent;
@synthesize character;
@synthesize string;
@synthesize children;
@end