forked from cappuccino/cappuccino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPScanner.j
337 lines (269 loc) · 7.66 KB
/
CPScanner.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* CPScanner.j
* Foundation
*
* Created by Emanuele Vulcano.
* Copyright 2008, Emanuele Vulcano.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import "CPCharacterSet.j"
@import "CPDictionary.j"
@import "CPString.j"
@implementation CPScanner : CPObject
{
CPString _string;
CPDictionary _locale;
int _scanLocation;
BOOL _caseSensitive;
CPCharacterSet _charactersToBeSkipped;
}
+ (id)scannerWithString:(CPString)aString
{
return [[self alloc] initWithString:aString];
}
- (id)initWithString:(CPString)aString
{
if (self = [super init])
{
_string = [aString copy];
_scanLocation = 0;
_charactersToBeSkipped = [CPCharacterSet whitespaceCharacterSet];
_caseSensitive = NO;
}
return self;
}
- (id)copy
{
var copy = [[CPScanner alloc] initWithString:[self string]];
[copy setCharactersToBeSkipped:[self charactersToBeSkipped]];
[copy setCaseSensitive:[self caseSensitive]];
[copy setLocale:[self locale]];
[copy setScanLocation:[self scanLocation]];
return copy;
}
- (CPDictionary)locale
{
return _locale;
}
- (void)setLocale:(CPDictionary)aLocale
{
_locale = aLocale;
}
- (void)setCaseSensitive:(BOOL)flag
{
_caseSensitive = flag;
}
- (BOOL)caseSensitive
{
return _caseSensitive;
}
- (CPString)string
{
return _string;
}
- (CPCharacterSet)charactersToBeSkipped
{
return _charactersToBeSkipped;
}
- (void)setCharactersToBeSkipped:(CPCharacterSet)c
{
_charactersToBeSkipped = c;
}
- (BOOL)isAtEnd
{
return _scanLocation == _string.length;
}
- (int)scanLocation
{
return _scanLocation;
}
- (void)setScanLocation:(int)aLocation
{
if (aLocation > _string.length)
aLocation = _string.length; // clamp to just after the last character
else if (aLocation < 0)
aLocation = 0; // clamp to the first
_scanLocation = aLocation;
}
// Method body for all methods that return their value by reference.
- (BOOL)_performScanWithSelector:(SEL)s withObject:(id)arg into:(id)ref
{
var ret = [self performSelector:s withObject:arg];
if (ret == nil)
return NO;
if (ref != nil)
ref(ret);
return YES;
}
/* ================================ */
/* = Scanning with CPCharacterSet = */
/* ================================ */
- (BOOL)scanCharactersFromSet:(CPCharacterSet)scanSet intoString:(id)ref
{
return [self _performScanWithSelector:@selector(scanCharactersFromSet:) withObject:scanSet into:ref];
}
- (CPString)scanCharactersFromSet:(CPCharacterSet)scanSet
{
return [self _scanWithSet:scanSet breakFlag:NO];
}
- (BOOL)scanUpToCharactersFromSet:(CPCharacterSet)scanSet intoString:(id)ref
{
return [self _performScanWithSelector:@selector(scanUpToCharactersFromSet:) withObject:scanSet into:ref];
}
- (CPString)scanUpToCharactersFromSet:(CPCharacterSet)scanSet
{
return [self _scanWithSet:scanSet breakFlag:YES];
}
// If stop == YES, it will stop when it sees a character from
// the set (scanUpToCharactersFromSet:); if stop == NO, it will
// stop when it sees a character NOT from the set
// (scanCharactersFromSet:).
- (CPString)_scanWithSet:(CPCharacterSet)scanSet breakFlag:(BOOL)stop
{
if ([self isAtEnd])
return nil;
var current = [self scanLocation],
str = nil;
while (current < _string.length)
{
var c = (_string.charAt(current));
if ([scanSet characterIsMember:c] == stop)
break;
if (![_charactersToBeSkipped characterIsMember:c])
{
if (!str)
str = '';
str += c;
}
current++;
}
if (str)
[self setScanLocation:current];
return str;
}
/* ==================== */
/* = Scanning strings = */
/* ==================== */
- (void)_movePastCharactersToBeSkipped
{
var current = [self scanLocation],
string = [self string],
toSkip = [self charactersToBeSkipped];
while (current < string.length)
{
if (![toSkip characterIsMember:string.charAt(current)])
break;
current++;
}
[self setScanLocation:current];
}
- (BOOL)scanString:(CPString)aString intoString:(id)ref
{
return [self _performScanWithSelector:@selector(scanString:) withObject:aString into:ref];
}
- (CPString)scanString:(CPString)s
{
[self _movePastCharactersToBeSkipped];
if ([self isAtEnd])
return nil;
var currentStr = [self string].substr([self scanLocation], s.length);
if ((_caseSensitive && currentStr != s) || (!_caseSensitive && (currentStr.toLowerCase() != s.toLowerCase())))
{
return nil;
}
else
{
[self setScanLocation:[self scanLocation] + s.length];
return s;
}
}
- (BOOL)scanUpToString:(CPString)aString intoString:(id)ref
{
return [self _performScanWithSelector:@selector(scanUpToString:) withObject:aString into:ref];
}
- (CPString)scanUpToString:(CPString)s
{
var current = [self scanLocation],
str = [self string],
captured = nil;
while (current < str.length)
{
var currentStr = str.substr(current, s.length);
if (currentStr == s || (!_caseSensitive && currentStr.toLowerCase() == s.toLowerCase()))
break;
if (!captured)
captured = '';
captured += str.charAt(current);
current++;
}
if (captured)
[self setScanLocation:current];
// evil private method use!
// this method is defined in the category on CPString
// in CPCharacterSet.j
if ([self charactersToBeSkipped])
captured = [captured _stringByTrimmingCharactersInSet:[self charactersToBeSkipped] options:_CPCharacterSetTrimAtBeginning];
return captured;
}
/* ==================== */
/* = Scanning numbers = */
/* ==================== */
- (float)scanWithParseFunction:(Function)parseFunction
{
[self _movePastCharactersToBeSkipped];
var str = [self string],
loc = [self scanLocation];
if ([self isAtEnd])
return 0;
var s = str.substring(loc, str.length),
f = parseFunction(s);
if (isNaN(f))
return nil;
loc += (""+f).length;
var i = 0;
while (!isNaN(parseFloat(str.substring(loc+i, str.length))))
{i++;}
[self setScanLocation:loc + i];
return f;
}
- (float)scanFloat
{
return [self scanWithParseFunction:parseFloat];
}
- (int)scanInt
{
return [self scanWithParseFunction:parseInt];
}
- (BOOL)scanInt:(int)intoInt
{
return [self _performScanWithSelector:@selector(scanInt) withObject:nil into:intoInt];
}
- (BOOL)scanFloat:(float)intoFloat
{
return [self _performScanWithSelector:@selector(scanFloat) withObject:nil into:intoFloat];
}
- (BOOL)scanDouble:(float)intoDouble
{
return [self scanFloat:intoDouble];
}
/* ========= */
/* = Debug = */
/* ========= */
- (void)description
{
return [super description] + " {" + CPStringFromClass([self class]) + ", state = '" + ([self string].substr(0, _scanLocation) + "{{ SCAN LOCATION ->}}" + [self string].substr(_scanLocation)) + "'; }";
}
@end