-
Notifications
You must be signed in to change notification settings - Fork 2
/
TDCharacterAssembly.j
98 lines (78 loc) · 1.64 KB
/
TDCharacterAssembly.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
@import "TDAssembly.j"
@implementation TDCharacterAssembly : TDAssembly
{
}
- (id)init
{
return [self initWithString:nil];
}
- (id)initWithString:(CPString)s
{
self = [super initWithString:s];
if (self) {
defaultDelimiter = @"";
}
return self;
}
- (id)peek
{
if (index >= string.length) {
return nil;
}
return [string characterAtIndex:index];
}
- (id)next
{
var obj = [self peek];
if (obj !== nil && obj !== undefined) {
index++;
}
return obj;
}
- (BOOL)hasMore
{
return (index < string.length);
}
- (int)length
{
return string.length;
}
- (int)objectsConsumed
{
return index;
}
- (int)objectsRemaining
{
return (string.length - index);
}
- (CPString)consumedObjectsJoinedByString:(CPString)delimiter
{
return [string substringToIndex:self.objectsConsumed];
}
- (CPString)remainingObjectsJoinedByString:(CPString)delimiter
{
return [string substringFromIndex:self.objectsConsumed];
}
// overriding simply to print NSNumber objects as their unichar values
- (CPString)description
{
var s = "[";
var i = 0,
len = stack.length;
for (; i<len; i++) {
if ([obj isKindOfClass:[CPNumber class]]) { // ***this is needed for Char Assemblies
s += sprintf(@"%C", [obj integerValue]);
} else {
s += [obj description];
}
if (len - 1 != i++) {
s += ", ";
}
}
s += "]";
s += [self consumedObjectsJoinedByString:self.defaultDelimiter];
s += @"^";
s += [self remainingObjectsJoinedByString:self.defaultDelimiter];
return s;
}
@end