-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathParseResult.java
More file actions
66 lines (49 loc) · 1.34 KB
/
ParseResult.java
File metadata and controls
66 lines (49 loc) · 1.34 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
package org.herb;
import org.herb.ast.Node;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ParseResult {
public final Node value;
private final List<Node> errors;
public final String source;
public ParseResult(Node value, List<Node> errors, String source) {
this.value = value;
this.errors = Collections.unmodifiableList(errors);
this.source = source;
}
public List<Node> recursiveErrors() {
List<Node> result = new ArrayList<>();
result.addAll(errors);
if (value != null) {
result.addAll(value.recursiveErrors());
}
return result;
}
public boolean hasErrors() {
return !recursiveErrors().isEmpty();
}
public int getErrorCount() {
return recursiveErrors().size();
}
public boolean isSuccessful() {
return errors.isEmpty();
}
@Override
public String toString() {
return String.format("ParseResult{errors=%d, source=%d chars}", errors.size(), source.length());
}
public String inspect() {
StringBuilder builder = new StringBuilder();
if (value != null) {
builder.append(value.inspect(source));
}
if (hasErrors()) {
builder.append("\n\nErrors:\n");
for (Node error : recursiveErrors()) {
builder.append(error.inspect()).append("\n");
}
}
return builder.toString();
}
}