-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTokens.java
More file actions
82 lines (61 loc) · 1.94 KB
/
Tokens.java
File metadata and controls
82 lines (61 loc) · 1.94 KB
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
package org.docopt;
import static org.docopt.Python.bool;
import static org.docopt.Python.list;
import java.util.ArrayList;
import java.util.List;
import org.docopt.Python.Re;
final class Tokens extends ArrayList<String> {
private static final long serialVersionUID = 1L;
public static Tokens withExitException(final List<String> source) {
return new Tokens(source, DocoptExitException.class);
}
public static Tokens withLanguageError(final List<String> source) {
return new Tokens(source, DocoptLanguageError.class);
}
private final Class<? extends Throwable> error;
public Tokens(final List<String> source,
final Class<? extends Throwable> error) {
// >>> self += source.split() if hasattr(source, 'split') else source
// In this implementation, source is always a list of strings, so no
// need to split.
addAll(source);
this.error = error;
}
public static Tokens fromPattern(String source) {
source = Re.sub("([\\[\\]\\(\\)\\|]|\\.\\.\\.)", " $1 ", source);
List<String> $source;
// >>> source = [s for s in re.split('\s+|(\S*<.*?>)', source) if s]
{
$source = list();
for (final String s : Re.split("\\s+|(\\S*<.*?>)", source)) {
if (bool(s)) {
$source.add(s);
}
}
}
return Tokens.withLanguageError($source);
}
public String move() {
final String result = isEmpty() ? null : remove(0);
return result;
}
public String current() {
final String result = isEmpty() ? null : get(0);
return result;
}
public Class<? extends Throwable> getError() {
return error;
}
public IllegalStateException error(final String format,
final Object... args) {
final String message = String.format(format, args);
if (error == DocoptLanguageError.class) {
throw new DocoptLanguageError(message);
}
if (error == DocoptExitException.class) {
throw new DocoptExitException(1, message, true);
}
return new IllegalStateException("Unexpected exception: "
+ error.getClass().getName());
}
}