-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTDToken.j
165 lines (134 loc) · 3.11 KB
/
TDToken.j
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
//TDTokenType
TDTokenTypeEOF = 0;
TDTokenTypeNumber = 1;
TDTokenTypeQuotedString = 2;
TDTokenTypeSymbol = 3;
TDTokenTypeWord = 4;
TDTokenTypeWhitespace = 5;
TDTokenTypeComment = 6;
@implementation TDToken : CPObject
{
float floatValue;
CPString stringValue;
TDTokenType tokenType;
BOOL number;
BOOL quotedString;
BOOL symbol;
BOOL word;
BOOL whitespace;
BOOL comment;
id value;
}
+ (TDToken)EOFToken
{
return [TDTokenEOF instance];
}
+ (id)tokenWithTokenType:(TDTokenType)t stringValue:(CPString)s floatValue:(float)n
{
return [[self alloc] initWithTokenType:t stringValue:s floatValue:n];
}
// designated initializer
- (id)initWithTokenType:(TDTokenType)t stringValue:(NSString *)s floatValue:(CGFloat)n
{
if (self = [super init]) {
tokenType = t;
stringValue = s;
floatValue = n;
number = (TDTokenTypeNumber == t);
quotedString = (TDTokenTypeQuotedString == t);
symbol = (TDTokenTypeSymbol == t);
word = (TDTokenTypeWord == t);
whitespace = (TDTokenTypeWhitespace == t);
comment = (TDTokenTypeComment == t);
}
return self;
}
- (unsigned)hash
{
return [stringValue hash];
}
- (BOOL)isEqual:(id)rhv
{
return [self isEqual:rhv ignoringCase:NO];
}
- (BOOL)isEqualIgnoringCase:(id)rhv
{
return [self isEqual:rhv ignoringCase:YES];
}
- (BOOL)isEqual:(id)rhv ignoringCase:(BOOL)ignoringCase
{
if (![rhv isMemberOfClass:[TDToken class]])
return NO;
var tok = rhv;
if (tokenType != tok.tokenType)
return NO;
if (number)
return floatValue == tok.floatValue;
else
{
if (ignoringCase)
return CPOrderedSame == [stringValue caseInsensitiveCompare:tok.stringValue];
else
return stringValue === tok.stringValue;
}
}
- (id)value
{
if (!value)
{
if (number)
value = floatValue;
else
value = stringValue;
}
return value;
}
- (CPString)debugDescription
{
/*
var 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";
}
return [NSString stringWithFormat:@"<%@ %C%@%C>", typeString, 0x00AB, self.value, 0x00BB];
*/
return [self description];
}
- (CPString)description
{
return stringValue;
}
@end
EOFToken = nil;
@implementation TDTokenEOF : TDToken
{
}
+ (TDTokenEOF)instance
{
if (!EOFToken)
EOFToken = [[self alloc] init];
return EOFToken;
}
- (id)copy
{
return self;
}
- (CPString)description
{
return [CPString stringWithFormat:@"<TDTokenEOF %p>", self];
}
- (CPString)debugDescription
{
return [self description];
}
@end