forked from gavinkwoe/todparsekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKToken.m
233 lines (177 loc) · 5.12 KB
/
PKToken.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
//
// PKToken.m
// ParseKit
//
// Created by Todd Ditchendorf on 1/20/06.
// Copyright 2009 Todd Ditchendorf. All rights reserved.
//
#import <ParseKit/PKToken.h>
#import <ParseKit/PKTypes.h>
@interface PKTokenEOF : PKToken {}
+ (PKTokenEOF *)instance;
@end
@implementation PKTokenEOF
static PKTokenEOF *EOFToken = nil;
+ (PKTokenEOF *)instance {
@synchronized(self) {
if (!EOFToken) {
[[self alloc] init]; // assignment not done here
}
}
return EOFToken;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (!EOFToken) {
EOFToken = [super allocWithZone:zone];
return EOFToken; // assignment and return on first allocation
}
}
return nil; //on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (void)release {
// do nothing
}
- (id)autorelease {
return self;
}
- (NSUInteger)retainCount {
return UINT_MAX; // denotes an object that cannot be released
}
- (NSString *)description {
return [NSString stringWithFormat:@"<PKTokenEOF %p>", self];
}
- (NSString *)debugDescription {
return [self description];
}
- (NSUInteger)offset {
return -1;
}
@end
@interface PKToken ()
- (BOOL)isEqual:(id)obj ignoringCase:(BOOL)ignoringCase;
@property (nonatomic, readwrite, getter=isNumber) BOOL number;
@property (nonatomic, readwrite, getter=isQuotedString) BOOL quotedString;
@property (nonatomic, readwrite, getter=isSymbol) BOOL symbol;
@property (nonatomic, readwrite, getter=isWord) BOOL word;
@property (nonatomic, readwrite, getter=isWhitespace) BOOL whitespace;
@property (nonatomic, readwrite, getter=isComment) BOOL comment;
@property (nonatomic, readwrite, getter=isDelimitedString) BOOL delimitedString;
@property (nonatomic, readwrite) CGFloat floatValue;
@property (nonatomic, readwrite, copy) NSString *stringValue;
@property (nonatomic, readwrite) PKTokenType tokenType;
@property (nonatomic, readwrite, copy) id value;
@property (nonatomic, readwrite) NSUInteger offset;
@end
@implementation PKToken
+ (PKToken *)EOFToken {
return [PKTokenEOF instance];
}
+ (id)tokenWithTokenType:(PKTokenType)t stringValue:(NSString *)s floatValue:(CGFloat)n {
return [[[self alloc] initWithTokenType:t stringValue:s floatValue:n] autorelease];
}
// designated initializer
- (id)initWithTokenType:(PKTokenType)t stringValue:(NSString *)s floatValue:(CGFloat)n {
//NSParameterAssert(s);
if (self = [super init]) {
self.tokenType = t;
self.stringValue = s;
self.floatValue = n;
self.number = (PKTokenTypeNumber == t);
self.quotedString = (PKTokenTypeQuotedString == t);
self.symbol = (PKTokenTypeSymbol == t);
self.word = (PKTokenTypeWord == t);
self.whitespace = (PKTokenTypeWhitespace == t);
self.comment = (PKTokenTypeComment == t);
self.delimitedString = (PKTokenTypeDelimitedString == t);
}
return self;
}
- (void)dealloc {
self.stringValue = nil;
self.value = nil;
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone {
return [self retain]; // tokens are immutable
}
- (NSUInteger)hash {
return [stringValue hash];
}
- (BOOL)isEqual:(id)obj {
return [self isEqual:obj ignoringCase:NO];
}
- (BOOL)isEqualIgnoringCase:(id)obj {
return [self isEqual:obj ignoringCase:YES];
}
- (BOOL)isEqual:(id)obj ignoringCase:(BOOL)ignoringCase {
if (![obj isMemberOfClass:[PKToken class]]) {
return NO;
}
PKToken *tok = (PKToken *)obj;
if (tokenType != tok.tokenType) {
return NO;
}
if (self.isNumber) {
return floatValue == tok.floatValue;
} else {
if (ignoringCase) {
return (NSOrderedSame == [stringValue caseInsensitiveCompare:tok.stringValue]);
} else {
return [stringValue isEqualToString:tok.stringValue];
}
}
}
- (id)value {
if (!value) {
id v = nil;
if (self.isNumber) {
v = [NSNumber numberWithFloat:floatValue];
} else {
v = stringValue;
}
self.value = v;
}
return value;
}
- (NSString *)debugDescription {
NSString *typeString = nil;
if (self.isNumber) {
typeString = @"Number";
} else if (self.isQuotedString) {
typeString = @"Quoted String";
} else if (self.isSymbol) {
typeString = @"Symbol";
} else if (self.isWord) {
typeString = @"Word";
} else if (self.isWhitespace) {
typeString = @"Whitespace";
} else if (self.isComment) {
typeString = @"Comment";
} else if (self.isDelimitedString) {
typeString = @"Delimited String";
}
return [NSString stringWithFormat:@"<%@ %C%@%C>", typeString, 0x00AB, self.value, 0x00BB];
}
- (NSString *)description {
return stringValue;
}
@synthesize number;
@synthesize quotedString;
@synthesize symbol;
@synthesize word;
@synthesize whitespace;
@synthesize comment;
@synthesize delimitedString;
@synthesize floatValue;
@synthesize stringValue;
@synthesize tokenType;
@synthesize value;
@synthesize offset;
@end