-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathBaseNode.java
More file actions
170 lines (135 loc) Β· 4.71 KB
/
BaseNode.java
File metadata and controls
170 lines (135 loc) Β· 4.71 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
package org.herb.ast;
import org.herb.Location;
/**
* Abstract base class for all AST nodes.
*/
public abstract class BaseNode implements Node {
protected final String type;
protected final Location location;
protected final java.util.List<Node> errors;
protected BaseNode(String type, Location location, java.util.List<Node> errors) {
this.type = type;
this.location = location;
this.errors = errors != null ? errors : java.util.Collections.emptyList();
}
@Override
public String getType() {
return type;
}
@Override
public Location getLocation() {
return location;
}
public java.util.List<Node> getErrors() {
return errors;
}
@Override
public String inspect(String source) {
return inspect();
}
@Override
public <R, C> R accept(NodeVisitor<R, C> visitor, C context) {
return visitor.visitNode(this, context);
}
@Override
public <R, C> void visitChildren(NodeVisitor<R, C> visitor, C context) {
// Base implementation does nothing - subclasses override to visit their children
}
/**
* Helper to format errors for tree inspection.
*/
protected String inspectErrors(String prefix) {
if (errors == null || errors.isEmpty()) return "";
StringBuilder output = new StringBuilder();
int errorCount = errors.size();
String countLabel = errorCount == 1 ? "error" : "errors";
output.append("βββ errors: (").append(errorCount).append(" ").append(countLabel).append(")\n");
for (int i = 0; i < errors.size(); i++) {
Node error = errors.get(i);
boolean isLast = i == errors.size() - 1;
String symbol = isLast ? "βββ " : "βββ ";
String nextPrefix = isLast ? " " : "β ";
if (error != null) {
String tree = error.inspect();
if (tree.endsWith("\n")) {
tree = tree.substring(0, tree.length() - 1);
}
output.append(prefix).append(symbol).append(tree.replaceAll("\n", "\n" + prefix + nextPrefix)).append("\n");
if (!isLast) {
output.append(prefix).append(nextPrefix).append("\n");
}
}
}
output.append(prefix).append("\n");
return output.toString();
}
/**
* Helper to format an array of nodes for tree inspection.
*/
protected String inspectArray(java.util.List<Node> array, String prefix) {
return inspectArray(array, prefix, null);
}
/**
* Helper to format an array of nodes for tree inspection with source for Prism nodes.
*/
protected String inspectArray(java.util.List<Node> array, String prefix, String source) {
if (array == null) return "β
\n";
if (array.isEmpty()) return "[]\n";
StringBuilder output = new StringBuilder();
output.append(String.format("(%d item%s)\n", array.size(), array.size() == 1 ? "" : "s"));
for (int i = 0; i < array.size(); i++) {
Node item = array.get(i);
boolean isLast = i == array.size() - 1;
String symbol = isLast ? "βββ " : "βββ ";
String nextPrefix = isLast ? " " : "β ";
if (item != null) {
String tree = source != null ? item.inspect(source) : item.inspect();
if (tree.endsWith("\n")) {
tree = tree.substring(0, tree.length() - 1);
}
output.append(prefix).append(symbol).append(tree.replaceAll("\n", "\n" + prefix + nextPrefix)).append("\n");
if (!isLast) {
output.append(prefix).append(nextPrefix).append("\n");
}
} else {
output.append(prefix).append(symbol).append("null\n");
if (!isLast) {
output.append(prefix).append(nextPrefix).append("\n");
}
}
}
return output.toString();
}
/**
* Helper to format a single node for tree inspection.
*/
protected String inspectNode(Node node, String prefix) {
return inspectNode(node, prefix, null);
}
/**
* Helper to format a single node for tree inspection with source for Prism nodes.
*/
protected String inspectNode(Node node, String prefix, String source) {
if (node == null) return "β
\n";
String tree = source != null ? node.inspect(source) : node.inspect();
if (tree.endsWith("\n")) {
tree = tree.substring(0, tree.length() - 1);
}
String[] lines = tree.split("\n", -1);
if (lines.length == 0) return "β
\n";
StringBuilder result = new StringBuilder();
result.append("βββ ").append(lines[0]).append("\n");
for (int i = 1; i < lines.length; i++) {
if (lines[i].isEmpty()) {
result.append(prefix).append("\n");
} else {
result.append(prefix).append(" ").append(lines[i]).append("\n");
}
}
return result.toString();
}
@Override
public String toString() {
return String.format("%s@%s", type, location);
}
}