/
NSScanner-additions.m
295 lines (264 loc) · 8.29 KB
/
NSScanner-additions.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
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
/*
* Copyright (c) 2008-2012 Martin Hedenfalk <martin@vicoapp.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "NSScanner-additions.h"
#include "logging.h"
@implementation NSScanner (additions)
- (unichar)peek
{
if ([self isAtEnd])
return 0;
return [[self string] characterAtIndex:[self scanLocation]];
}
- (void)inc
{
if (![self isAtEnd])
[self setScanLocation:[self scanLocation] + 1];
}
- (BOOL)expectCharacter:(unichar)ch
{
if ([self peek] == ch) {
[self inc];
return YES;
}
return NO;
}
- (BOOL)scanCharacter:(unichar *)ch
{
if ([self isAtEnd])
return NO;
if (ch)
*ch = [[self string] characterAtIndex:[self scanLocation]];
[self inc];
return YES;
}
- (BOOL)scanUpToUnescapedCharacterFromSet:(NSCharacterSet *)toCharSet
appendToString:(NSMutableString *)s
stripEscapes:(BOOL)stripEscapes
{
unichar ch;
BOOL gotChar = NO;
while ([self scanCharacter:&ch]) {
if (ch == '\\') {
if ([self scanCharacter:&ch]) {
if (!stripEscapes)
[s appendString:@"\\"];
[s appendFormat:@"%C", ch];
} else
[s appendString:@"\\"];
} else if ([toCharSet characterIsMember:ch]) {
/* Don't swallow the end character. */
gotChar = YES;
[self setScanLocation:[self scanLocation] - 1];
break;
} else
[s appendFormat:@"%C", ch];
}
DEBUG(@"scanned escaped string [%@]", s);
return gotChar ? YES : NO;
}
- (BOOL)scanUpToUnescapedCharacterFromSet:(NSCharacterSet *)toCharSet
intoString:(NSString **)string
stripEscapes:(BOOL)stripEscapes
{
NSMutableString *s = [NSMutableString string];
BOOL ret = [self scanUpToUnescapedCharacterFromSet:toCharSet appendToString:s stripEscapes:stripEscapes];
if (string)
*string = s;
return ret;
}
- (BOOL)scanUpToUnescapedCharacter:(unichar)toChar
intoString:(NSString **)string
stripEscapes:(BOOL)stripEscapes
{
return [self scanUpToUnescapedCharacterFromSet:[NSCharacterSet characterSetWithRange:NSMakeRange(toChar, 1)]
intoString:string
stripEscapes:stripEscapes];
}
- (BOOL)scanUpToUnescapedCharacter:(unichar)toChar
intoString:(NSString **)string
{
/* TextMate bundle commands want backslash escapes intact. sh unescapes. */
return [self scanUpToUnescapedCharacter:toChar intoString:string stripEscapes:NO];
}
- (BOOL)scanShellVariableIntoString:(NSString **)intoString
{
NSUInteger startLocation = [self scanLocation];
BOOL initial = YES;
NSMutableCharacterSet *shellVariableSet = nil;
if (shellVariableSet == nil) {
shellVariableSet = [[NSMutableCharacterSet alloc] init];
[shellVariableSet formUnionWithCharacterSet:[NSCharacterSet characterSetWithRange:NSMakeRange('a', 'z' - 'a')]];
[shellVariableSet formUnionWithCharacterSet:[NSCharacterSet characterSetWithRange:NSMakeRange('A', 'Z' - 'A')]];
[shellVariableSet formUnionWithCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@"_"]];
}
while (![self isAtEnd]) {
if (![self scanCharactersFromSet:shellVariableSet intoString:nil])
break;
if (initial) {
[shellVariableSet formUnionWithCharacterSet:[NSCharacterSet characterSetWithRange:NSMakeRange('0', '9' - '0')]];
initial = NO;
}
}
if ([self scanLocation] == startLocation)
return NO;
if (intoString)
*intoString = [[self string] substringWithRange:NSMakeRange(startLocation, [self scanLocation] - startLocation)];
return YES;
}
- (BOOL)scanString:(NSString *)aString
{
return [self scanString:aString intoString:nil];
}
- (BOOL)scanKeyCode:(NSInteger *)intoKeyCode
{
[self setCharactersToBeSkipped:nil];
unichar ch;
if (![self scanCharacter:&ch])
return NO;
if (ch == '\\') {
/* Escaped character. */
if ([self scanCharacter:&ch]) {
if (intoKeyCode)
*intoKeyCode = ch;
return YES;
} else {
/* trailing backslash? treat as literal */
if (intoKeyCode)
*intoKeyCode = '\\';
return YES;
}
} else if (ch == '<') {
NSUInteger oldLocation = [self scanLocation];
[self setCaseSensitive:NO];
unsigned int modifiers = 0;
BOOL gotModifier;
do {
gotModifier = YES;
if ([self scanString:@"c-"] ||
[self scanString:@"ctrl-"] ||
[self scanString:@"control-"])
modifiers |= NSControlKeyMask;
else if ([self scanString:@"a-"] ||
[self scanString:@"m-"] ||
[self scanString:@"alt-"] ||
[self scanString:@"option-"] ||
[self scanString:@"meta-"])
modifiers |= NSAlternateKeyMask;
else if ([self scanString:@"s-"] ||
[self scanString:@"shift-"])
modifiers |= NSShiftKeyMask;
else if ([self scanString:@"d-"] ||
[self scanString:@"cmd-"] ||
[self scanString:@"command-"])
modifiers |= NSCommandKeyMask;
else
gotModifier = NO;
} while (gotModifier);
unichar key;
if ([self scanString:@"delete"] ||
[self scanString:@"del"])
key = NSDeleteFunctionKey;
else if ([self scanString:@"left"])
key = NSLeftArrowFunctionKey;
else if ([self scanString:@"right"])
key = NSRightArrowFunctionKey;
else if ([self scanString:@"up"])
key = NSUpArrowFunctionKey;
else if ([self scanString:@"down"])
key = NSDownArrowFunctionKey;
else if ([self scanString:@"pagedown"] ||
[self scanString:@"pgdn"])
key = NSPageDownFunctionKey;
else if ([self scanString:@"pageup"] ||
[self scanString:@"pgup"])
key = NSPageUpFunctionKey;
else if ([self scanString:@"home"])
key = NSHomeFunctionKey;
else if ([self scanString:@"end"])
key = NSEndFunctionKey;
else if ([self scanString:@"insert"] ||
[self scanString:@"ins"])
key = NSInsertFunctionKey;
else if ([self scanString:@"help"])
key = NSHelpFunctionKey;
else if ([self scanString:@"bs"] ||
[self scanString:@"backspace"])
key = 0x7F;
else if ([self scanString:@"tab"])
key = 0x09;
else if ([self scanString:@"escape"] ||
[self scanString:@"esc"])
key = 0x1B;
else if ([self scanString:@"cr"] ||
[self scanString:@"enter"] ||
[self scanString:@"return"])
key = 0x0D;
else if ([self scanString:@"space"])
key = ' ';
else if ([self scanString:@"bar"])
key = '|';
else if ([self scanString:@"lt"])
key = '<';
else if ([self scanString:@"bslash"] ||
[self scanString:@"backslash"])
key = '\\';
else if ([self scanString:@"nl"]) /* ctrl-J */
key = 0x0A;
else if ([self scanString:@"ff"]) /* ctrl-L */
key = 0x0C;
else if ([self scanString:@"nul"])
key = 0x0;
else if ([self scanCharacter:&key]) {
int f;
if ((key == 'f' || key == 'F') && [self scanInt:&f]) {
key = NSF1FunctionKey + f - 1;
} else if (modifiers == NSControlKeyMask &&
((key >= 'a' && key <= 'z') ||
key == '@' ||
(key >= '[' && key <= '_'))) {
/* ASCII control character 0x00 - 0x1F. */
key = tolower(toupper(key) - '@');
modifiers = 0;
}
} else
goto failed;
if (![self scanString:@">"])
goto failed;
if (intoKeyCode)
*intoKeyCode = modifiers | key;
return YES;
failed:
[self setScanLocation:oldLocation];
if (intoKeyCode)
*intoKeyCode = '<';
} else if (intoKeyCode)
*intoKeyCode = ch;
return YES;
}
- (void)skipWhitespace
{
[self scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:nil];
}
@end