-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPattern.java
More file actions
212 lines (174 loc) · 5.24 KB
/
Pattern.java
File metadata and controls
212 lines (174 loc) · 5.24 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package org.docopt;
import static org.docopt.Python.count;
import static org.docopt.Python.list;
import static org.docopt.Python.set;
import static org.docopt.Python.split;
import java.util.Arrays;
import java.util.List;
abstract class Pattern {
static class MatchResult {
private final boolean match;
private final List<LeafPattern> left;
private final List<LeafPattern> collected;
public MatchResult(final boolean match, final List<LeafPattern> left,
final List<LeafPattern> collected) {
this.match = match;
this.left = left;
this.collected = collected;
}
public boolean matched() {
return match;
}
public List<LeafPattern> getLeft() {
return left;
}
public List<LeafPattern> getCollected() {
return collected;
}
}
@SuppressWarnings("unchecked")
private static final List<Class<? extends BranchPattern>> PARENTS = Arrays
.asList(Required.class, Optional.class, OptionsShortcut.class,
Either.class, OneOrMore.class);
/**
* Expand pattern into an (almost) equivalent one, but with single Either.
*
* Example: ((-a | -b) (-c | -d)) => (-a -c | -a -d | -b -c | -b -d) Quirks:
* [-a] => (-a), (-a...) => (-a -a)
*/
private static Either transform(final Pattern pattern) {
final List<List<Pattern>> result = list();
List<List<Pattern>> groups;
// >>> groups = [[pattern]]
{
// Can't use "groups = list(list(pattern))" since the argument is
// iterable.
groups = list();
groups.add(list(pattern));
}
while (!groups.isEmpty()) {
final List<Pattern> children = groups.remove(0);
BranchPattern child = null;
// "If any parent type is the same type as the type of any child, select the first child that is of a type in parents."
// >>> if any(t in map(type, children) for t in parents):
// >>> child = [c for c in children if type(c) in parents][0]
// TODO: I think that the "if" clause is redundant; instead, we just
// try to get the child directly.
for (final Pattern c : children) {
if (PARENTS.contains(c.getClass())) {
child = (BranchPattern) c;
break;
}
}
// See above for changes from python implementation.
if (child != null) {
children.remove(child);
if (child.getClass() == Either.class) {
for (final Pattern c : child.getChildren()) {
// >>> groups.append([c] + children)
final List<Pattern> group = list(c);
group.addAll(children);
groups.add(group);
}
}
else if (child.getClass() == OneOrMore.class) {
// >>> groups.append(child.children * 2 + children)
final List<Pattern> group = list(child.getChildren());
group.addAll(child.getChildren());
group.addAll(children);
groups.add(group);
}
else {
// >>> groups.append(child.children + children)
final List<Pattern> group = list(child.getChildren());
group.addAll(children);
groups.add(group);
}
}
else {
result.add(children);
}
}
// >>> return Either(*[Required(*e) for e in result])
{
final List<Required> required = list();
for (final List<Pattern> e : result) {
required.add(new Required(e));
}
return new Either(required);
}
}
public Pattern fix() {
fixIdentities(null);
fixRepeatingArguments();
return this;
}
/**
* Make pattern-tree tips point to same object if they are equal.
*/
private void fixIdentities(List<Pattern> uniq) {
// >>> if not hasattr(self, 'children')
if (!(this instanceof BranchPattern)) {
return;
}
if (uniq == null) {
uniq = list(set(flat()));
}
final List<Pattern> children = ((BranchPattern) this).getChildren();
for (int i = 0; i < children.size(); i++) {
final Pattern child = children.get(i);
if (!(child instanceof BranchPattern)) {
assert uniq.contains(child);
children.set(i, uniq.get(uniq.indexOf(child)));
}
else {
child.fixIdentities(uniq);
}
}
}
/**
* Fix elements that should accumulate/increment values.
*/
private void fixRepeatingArguments() {
List<List<Pattern>> either;
// >>> either = [list(child.children) for child in
// transform(self).children]
{
either = list();
for (final Pattern child : transform(this).getChildren()) {
either.add(list(((Required) child).getChildren()));
}
}
for (final List<Pattern> $case : either) {
// >>> for e in [child for child in case if case.count(child) > 1]
for (final Pattern child : $case) { // ^^^
if (count($case, child) > 1) { // ^^^
final LeafPattern e = (LeafPattern) child; // ^^^
if ((e.getClass() == Argument.class)
|| ((e.getClass() == Option.class) && ((Option) e)
.getArgCount() != 0)) {
if (e.getValue() == null) {
e.setValue(list());
}
else if (!(e.getValue() instanceof List)) {
e.setValue(split(e.getValue().toString()));
}
}
if ((e.getClass() == Command.class)
|| ((e.getClass() == Option.class) && ((Option) e)
.getArgCount() == 0)) {
e.setValue(0);
}
}
}
}
}
protected abstract List<Pattern> flat(Class<?>... types);
protected abstract MatchResult match(List<LeafPattern> left,
List<LeafPattern> collected);
protected MatchResult match(final List<LeafPattern> left) {
return match(left, null);
}
@Override
public abstract boolean equals(Object obj);
}