forked from itod/parsekit
/
TDRobotCommandTest.m
183 lines (140 loc) · 5 KB
/
TDRobotCommandTest.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
// Copyright 2010 Todd Ditchendorf
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "TDRobotCommandTest.h"
@interface RobotCommand : NSObject {
NSString *location;
}
@property (copy) NSString *location;
@end
@implementation RobotCommand
- (void)dealloc {
self.location = nil;
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone {
RobotCommand *c = [[RobotCommand allocWithZone:zone] init];
c->location = [location copy];
return c;
}
@synthesize location;
@end
@interface RobotPickCommand : RobotCommand {}
@end
@implementation RobotPickCommand
- (NSString *)description { return [NSString stringWithFormat:@"pick %@", self.location]; }
@end
@interface RobotPlaceCommand : RobotCommand {}
@end
@implementation RobotPlaceCommand
- (NSString *)description { return [NSString stringWithFormat:@"place %@", self.location]; }
@end
@interface RobotScanCommand : RobotCommand {}
@end
@implementation RobotScanCommand
- (NSString *)description { return [NSString stringWithFormat:@"scan %@", self.location]; }
@end
@implementation TDRobotCommandTest
// e = command*
// command = pickCommand | placeCommand | scanCommand
// pickCommand = "pick" "carrier" "from" location
// placeCommand = "place" "carrier" "at" location
// scanCommand = "scan" location
// location = Word
- (PKParser *)location {
return [PKWord word];
}
- (PKParser *)pickCommand {
PKSequence *s = [PKSequence sequence];
[s add:[[PKCaseInsensitiveLiteral literalWithString:@"pick"] discard]];
[s add:[[PKCaseInsensitiveLiteral literalWithString:@"carrier"] discard]];
[s add:[[PKCaseInsensitiveLiteral literalWithString:@"from"] discard]];
[s add:[self location]];
[s setAssembler:self selector:@selector(didMatchPickCommand:)];
return s;
}
- (PKParser *)placeCommand {
PKSequence *s = [PKSequence sequence];
[s add:[[PKCaseInsensitiveLiteral literalWithString:@"place"] discard]];
[s add:[[PKCaseInsensitiveLiteral literalWithString:@"carrier"] discard]];
[s add:[[PKCaseInsensitiveLiteral literalWithString:@"at"] discard]];
[s add:[self location]];
[s setAssembler:self selector:@selector(didMatchPlaceCommand:)];
return s;
}
- (PKParser *)scanCommand {
PKSequence *s = [PKSequence sequence];
[s add:[[PKCaseInsensitiveLiteral literalWithString:@"scan"] discard]];
[s add:[self location]];
[s setAssembler:self selector:@selector(didMatchScanCommand:)];
return s;
}
- (PKParser *)command {
PKAlternation *a = [PKAlternation alternation];
[a add:[self pickCommand]];
[a add:[self placeCommand]];
[a add:[self scanCommand]];
return a;
}
- (void)testPick {
NSString *s1 = @"pick carrier from LINE_IN";
PKTokenAssembly *a = [PKTokenAssembly assemblyWithString:s1];
PKParser *p = [self command];
PKAssembly *result = [p bestMatchFor:a];
TDNotNil(result);
TDEqualObjects(@"[]pick/carrier/from/LINE_IN^", [result description]);
id target = result.target;
TDNotNil(target);
TDEqualObjects(@"pick LINE_IN", [target description]);
}
- (void)testPlace {
NSString *s2 = @"place carrier at LINE_OUT";
PKTokenAssembly *a = [PKTokenAssembly assemblyWithString:s2];
PKParser *p = [self command];
PKAssembly *result = [p bestMatchFor:a];
TDNotNil(result);
TDEqualObjects(@"[]place/carrier/at/LINE_OUT^", [result description]);
id target = result.target;
TDNotNil(target);
TDEqualObjects(@"place LINE_OUT", [target description]);
}
- (void)testScan {
NSString *s3 = @"scan DB101_OUT";
PKTokenAssembly *a = [PKTokenAssembly assemblyWithString:s3];
PKParser *p = [self command];
PKAssembly *result = [p bestMatchFor:a];
TDNotNil(result);
TDEqualObjects(@"[]scan/DB101_OUT^", [result description]);
id target = result.target;
TDNotNil(target);
TDEqualObjects(@"scan DB101_OUT", [target description]);
}
- (void)didMatchPickCommand:(PKAssembly *)a {
RobotPickCommand *c = [[[RobotPickCommand alloc] init] autorelease];
PKToken *location = [a pop];
c.location = location.stringValue;
a.target = c;
}
- (void)didMatchPlaceCommand:(PKAssembly *)a {
RobotPlaceCommand *c = [[[RobotPlaceCommand alloc] init] autorelease];
PKToken *location = [a pop];
c.location = location.stringValue;
a.target = c;
}
- (void)didMatchScanCommand:(PKAssembly *)a {
RobotScanCommand *c = [[[RobotScanCommand alloc] init] autorelease];
PKToken *location = [a pop];
c.location = location.stringValue;
a.target = c;
}
@end