forked from gavinkwoe/todparsekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKNumberState.m
258 lines (216 loc) · 5.88 KB
/
PKNumberState.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
//
// PKNumberState.m
// ParseKit
//
// Created by Todd Ditchendorf on 1/20/06.
// Copyright 2009 Todd Ditchendorf. All rights reserved.
//
#import <ParseKit/PKNumberState.h>
#import <ParseKit/PKReader.h>
#import <ParseKit/PKToken.h>
#import <ParseKit/PKTokenizer.h>
#import <ParseKit/PKSymbolState.h>
#import <ParseKit/PKTypes.h>
@interface PKToken ()
@property (nonatomic, readwrite) NSUInteger offset;
@end
@interface PKTokenizerState ()
- (void)resetWithReader:(PKReader *)r;
- (PKTokenizerState *)nextTokenizerStateFor:(PKUniChar)c tokenizer:(PKTokenizer *)t;
- (void)append:(PKUniChar)c;
- (NSString *)bufferedString;
@end
@interface PKNumberState ()
- (CGFloat)absorbDigitsFromReader:(PKReader *)r;
- (CGFloat)value;
- (void)parseLeftSideFromReader:(PKReader *)r;
- (void)parseRightSideFromReader:(PKReader *)r;
- (void)parseExponentFromReader:(PKReader *)r;
- (void)reset:(PKUniChar)cin;
- (void)checkForHex:(PKReader *)r;
- (void)checkForOctal;
@end
@implementation PKNumberState
- (PKToken *)nextTokenFromReader:(PKReader *)r startingWith:(PKUniChar)cin tokenizer:(PKTokenizer *)t {
NSParameterAssert(r);
NSParameterAssert(t);
[self resetWithReader:r];
isNegative = NO;
originalCin = cin;
if ('-' == cin) {
isNegative = YES;
cin = [r read];
[self append:'-'];
} else if ('+' == cin) {
cin = [r read];
[self append:'+'];
}
[self reset:cin];
if ('.' == c) {
[self parseRightSideFromReader:r];
} else {
[self parseLeftSideFromReader:r];
if (isDecimal) {
[self parseRightSideFromReader:r];
}
}
// erroneous ., +, -, or 0x
if (!gotADigit) {
if (isHex) {
[r unread];
return [PKToken tokenWithTokenType:PKTokenTypeNumber stringValue:@"0" floatValue:0.0];
} else {
if (isNegative && PKEOF != c) { // ??
[r unread];
}
return [t.symbolState nextTokenFromReader:r startingWith:originalCin tokenizer:t];
}
}
if (PKEOF != c) {
[r unread];
}
if (isNegative) {
floatValue = -floatValue;
}
PKToken *tok = [PKToken tokenWithTokenType:PKTokenTypeNumber stringValue:[self bufferedString] floatValue:[self value]];
tok.offset = offset;
return tok;
}
- (CGFloat)value {
CGFloat result = (CGFloat)floatValue;
NSUInteger i = 0;
for ( ; i < exp; i++) {
if (isNegativeExp) {
result /= (CGFloat)10.0;
} else {
result *= (CGFloat)10.0;
}
}
return (CGFloat)result;
}
- (CGFloat)absorbDigitsFromReader:(PKReader *)r {
CGFloat divideBy = 1.0;
CGFloat v = 0.0;
BOOL isHexAlpha = NO;
while (1) {
isHexAlpha = NO;
if (allowsHexadecimalNotation) {
[self checkForHex:r];
if (c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F') {
isHexAlpha = YES;
}
}
if (isdigit(c) || isHexAlpha) {
[self append:c];
len++;
gotADigit = YES;
if (allowsOctalNotation) {
[self checkForOctal];
}
if (isHexAlpha) {
if (c >= 'a' && c <= 'f') {
c = toupper(c);
}
c -= 7;
}
v = v * base + (c - '0');
c = [r read];
if (isFraction) {
divideBy *= base;
}
} else {
break;
}
}
if (isFraction) {
v = v / divideBy;
}
return (CGFloat)v;
}
- (void)parseLeftSideFromReader:(PKReader *)r {
isFraction = NO;
floatValue = [self absorbDigitsFromReader:r];
}
- (void)parseRightSideFromReader:(PKReader *)r {
if ('.' == c) {
PKUniChar n = [r read];
BOOL nextIsDigit = isdigit(n);
if (PKEOF != n) {
[r unread];
}
if (nextIsDigit || allowsTrailingDot) {
[self append:'.'];
if (nextIsDigit) {
c = [r read];
isFraction = YES;
floatValue += [self absorbDigitsFromReader:r];
}
}
}
if (allowsScientificNotation) {
[self parseExponentFromReader:r];
}
}
- (void)parseExponentFromReader:(PKReader *)r {
NSParameterAssert(r);
if ('e' == c || 'E' == c) {
PKUniChar e = c;
c = [r read];
BOOL hasExp = isdigit(c);
isNegativeExp = ('-' == c);
BOOL positiveExp = ('+' == c);
if (!hasExp && (isNegativeExp || positiveExp)) {
c = [r read];
hasExp = isdigit(c);
}
if (PKEOF != c) {
[r unread];
}
if (hasExp) {
[self append:e];
if (isNegativeExp) {
[self append:'-'];
} else if (positiveExp) {
[self append:'+'];
}
c = [r read];
isFraction = NO;
exp = [self absorbDigitsFromReader:r];
}
}
}
- (void)reset:(PKUniChar)cin {
c = cin;
firstNum = cin;
gotADigit = NO;
isFraction = NO;
isDecimal = YES;
isHex = NO;
len = 0;
base = (CGFloat)10.0;
floatValue = (CGFloat)0.0;
exp = (CGFloat)0.0;
isNegativeExp = NO;
}
- (void)checkForHex:(PKReader *)r {
if ('x' == c && '0' == firstNum && !isFraction && 1 == len) {
[self append:c];
len++;
c = [r read];
isDecimal = NO;
base = (CGFloat)16.0;
isHex = YES;
gotADigit = NO;
}
}
- (void)checkForOctal {
if ('0' == firstNum && !isFraction && isDecimal && 2 == len) {
isDecimal = NO;
base = (CGFloat)8.0;
}
}
@synthesize allowsTrailingDot;
@synthesize allowsScientificNotation;
@synthesize allowsOctalNotation;
@synthesize allowsHexadecimalNotation;
@end