/
PKSTokenKindDescriptor.m
81 lines (55 loc) · 1.71 KB
/
PKSTokenKindDescriptor.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
//
// PKSTokenKindDescriptor.m
// ParseKit
//
// Created by Todd Ditchendorf on 3/27/13.
//
//
#import "PKSTokenKindDescriptor.h"
#import <ParseKit/PKSParser.h>
static NSMutableDictionary *sCache = nil;
@implementation PKSTokenKindDescriptor
+ (void)initialize {
if ([PKSTokenKindDescriptor class] == self) {
sCache = [[NSMutableDictionary alloc] init];
}
}
+ (PKSTokenKindDescriptor *)descriptorWithStringValue:(NSString *)s name:(NSString *)name {
PKSTokenKindDescriptor *desc = sCache[name];
if (!desc) {
desc = [[[PKSTokenKindDescriptor alloc] init] autorelease];
desc.stringValue = s;
desc.name = name;
sCache[name] = desc;
}
return desc;
}
+ (PKSTokenKindDescriptor *)anyDescriptor {
return [PKSTokenKindDescriptor descriptorWithStringValue:@"TOKEN_KIND_BUILTIN_ANY" name:@"TOKEN_KIND_BUILTIN_ANY"];
}
+ (PKSTokenKindDescriptor *)eofDescriptor {
return [PKSTokenKindDescriptor descriptorWithStringValue:@"TOKEN_KIND_BUILTIN_EOR" name:@"TOKEN_KIND_BUILTIN_EOF"];
}
+ (void)clearCache {
[sCache removeAllObjects];
}
- (void)dealloc {
self.stringValue = nil;
self.name = nil;
[super dealloc];
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@ %p '%@' %@>", [self class], self, _stringValue, _name];
}
- (BOOL)isEqual:(id)obj {
if (![obj isMemberOfClass:[self class]]) {
return NO;
}
PKSTokenKindDescriptor *that = (PKSTokenKindDescriptor *)obj;
if (![_stringValue isEqualToString:that->_stringValue]) {
return NO;
}
NSAssert([_name isEqualToString:that->_name], @"if the stringValues match, so should the names");
return YES;
}
@end