-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTDWordState.j
82 lines (67 loc) · 2.13 KB
/
TDWordState.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
@import "TDTokenizerState.j"
@implementation TDWordState : TDTokenizerState
{
CPArray wordChars;
}
- (id)init
{
if (self = [super init])
{
wordChars = [];
for (var i=0, len=255 ; i <= len; i++) {
[wordChars addObject:NO];
}
[self setWordChars:YES from: 'a' to: 'z'];
[self setWordChars:YES from: 'A' to: 'Z'];
[self setWordChars:YES from: '0' to: '9'];
[self setWordChars:YES from: '-' to: '-'];
[self setWordChars:YES from: '_' to: '_'];
[self setWordChars:YES from:'\'' to:'\''];
[self setWordChars:YES from: 192 to: 255];
}
return self;
}
- (void)setWordChars:(BOOL)yn from:(unsigned)start to:(unsigned)end
{
var len = wordChars.length;
if (start > len || end > len || start < 0 || end < 0) {
[CPException raise:@"TDWordStateNotSupportedException" reason:@"TDWordState only supports setting word chars for chars in the latin1 set (under 256)"];
}
for (var i=start ; i <= end; i++) {
[wordChars replaceObjectAtIndex:i withObject:yn];
}
}
- (BOOL)isWordChar:(unsigned)c
{
if (c > -1 && c < wordChars.length - 1) {
return (!![wordChars objectAtIndex:c]);
}
if (c >= 0x2000 && c <= 0x2BFF) { // various symbols
return NO;
} else if (c >= 0xFE30 && c <= 0xFE6F) { // general punctuation
return NO;
} else if (c >= 0xFE30 && c <= 0xFE6F) { // western musical symbols
return NO;
} else if (c >= 0xFF00 && c <= 0xFF65) { // symbols within Hiragana & Katakana
return NO;
} else if (c >= 0xFFF0 && c <= 0xFFFF) { // specials
return NO;
} else if (c < 0) {
return NO;
} else {
return YES;
}
}
- (TDToken)nextTokenFromReader:(TDReader)r startingWith:(unsigned)cin tokenizer:(TDTokenizer)t
{
[self reset];
var c = cin;
do {
[self append:c];
c = [r read];
} while ([self isWordChar:c]);
if (-1 != c)
[r unread];
return [TDToken tokenWithTokenType:TDTokenTypeWord stringValue:[self bufferedString] floatValue:0.0];
}
@end