-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathOneOrMore.java
More file actions
51 lines (37 loc) · 926 Bytes
/
OneOrMore.java
File metadata and controls
51 lines (37 loc) · 926 Bytes
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
package org.docopt;
import static org.docopt.Python.list;
import java.util.List;
final class OneOrMore extends BranchPattern {
public OneOrMore(final List<? extends Pattern> children) {
super(children);
}
@Override
protected MatchResult match(final List<LeafPattern> left,
List<LeafPattern> collected) {
assert getChildren().size() == 1;
if (collected == null) {
collected = list();
}
List<LeafPattern> l = left;
List<LeafPattern> c = collected;
List<LeafPattern> l_ = null;
final boolean matched = true;
int times = 0;
while (matched) {
final MatchResult m = getChildren().get(0).match(l, c);
l = m.getLeft();
c = m.getCollected();
if (m.matched()) {
times++;
}
if ((l == null) ? (l_ == null) : l.equals(l_)) {
break;
}
l_ = l;
}
if (times >= 1) {
return new MatchResult(true, l, c);
}
return new MatchResult(false, left, collected);
}
}