-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.java
98 lines (84 loc) · 3.04 KB
/
Command.java
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
package ataxx;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/** All things to do with parsing commands.
* @author Aaron Lee
*/
class Command {
/** Command types. PIECEMOVE indicates a move of the form
* c0r0-c1r1. ERROR indicates a parse error in the command.
* All other commands are upper-case versions of what the
* programmer writes. */
static enum Type {
/* Start-up state only. */
AUTO("(?i)auto\\s+(red|blue)"),
BLOCK("(?i)block\\s+([a-g][1-7])"),
MANUAL("^manual\\s+(red|blue)"),
SEED("^seed\\s+([1-9]|[0-9]{2,})$"),
START,
PRINT("^print\\s+([1])"),
/* Regular moves (set-up or play) */
PASS("(?i)pass|[-]"),
PIECEMOVE("([a-g])([1-7])[-]([a-g])([1-7])"),
/* Valid at any time. */
LOAD("(?i)load\\s+(.*)"),
QUIT, CLEAR, DUMP, HELP,
/* Special "commands" internally generated. */
/** Syntax error in command. */
ERROR(".*"),
/** End of input stream. */
EOF;
/** PATTERN is a regular expression string giving the syntax of
* a command of the given type. It matches the entire command,
* assuming no leading or trailing whitespace. The groups in
* the pattern capture the operands (if any). */
Type(String pattern) {
_pattern = Pattern.compile(pattern + "$");
}
/** A Type whose pattern is the lower-case version of its name
* (with no arguments). */
Type() {
_pattern = Pattern.compile(this.toString().toLowerCase() + "$");
}
/** The Pattern descrbing syntactically correct versions of this
* type of command. */
private final Pattern _pattern;
}
/** A new Command of type TYPE with OPERANDS as its operands. */
Command(Type type, String... operands) {
_type = type;
_operands = operands;
}
/** Return the type of this Command. */
Type commandType() {
return _type;
}
/** Returns this Command's operands. */
String[] operands() {
return _operands;
}
/** Parse COMMAND, returning the command and its operands.
* COMMAND is assumed to be trimmed of all leading and
* trailing whitespace. */
static Command parseCommand(String command) {
if (command == null) {
return new Command(Type.EOF);
}
command = command.trim();
for (Type type : Type.values()) {
Matcher mat = type._pattern.matcher(command);
if (mat.matches()) {
String[] operands = new String [mat.groupCount()];
for (int i = 1; i <= operands.length; i += 1) {
operands[i - 1] = mat.group(i);
}
return new Command(type, operands);
}
}
throw new Error("Internal failure: error command did not match.");
}
/** The command name. */
private final Type _type;
/** Command arguments. */
private final String[] _operands;
}