Repository navigation
Expand file tree
/
Copy pathNode.java
More file actions
70 lines (55 loc) · 2.06 KB
/
Copy pathNode.java
File metadata and controls
70 lines (55 loc) · 2.06 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
package io.pixee.ast;
import io.pixee.lang.ConceptDescriptor;
import io.pixee.lang.LanguageDescriptor;
import io.pixee.lang.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;
public class Node extends Data {
public final ConceptDescriptor concept;
private final List<Child> children = new ArrayList<Child>();
private Node parent;
public Node(ConceptDescriptor concept) {
this.concept = concept;
}
public Node add(PropertyDescriptor pd, Data data) {
this.children.add(new Child(pd, data));
return this;
}
public Node add(String propName, Data data) {
this.children.add(new Child(propName, data));
return this;
}
public List<Node> childNodes() {
return children.stream().filter(c -> c.data() instanceof Node).map(c -> ((Node) c.data())).toList();
}
public void resolve(LanguageDescriptor lang, Node parent) {
this.parent = parent;
this.children.stream().forEach(c -> c.resolve(this));
this.childNodes().stream().forEach(c -> c.resolve(lang, this));
}
public String dump(String indent) {
StringBuilder sb = new StringBuilder();
sb.append(this.concept.name + " {\n");
for (Child c : children.stream().filter(c -> c.data() instanceof Value).toList()) {
sb.append(indent + " " + c.role().name + ": ");
sb.append(((Value) c.data()).data + "\n");
}
for (Child c : children.stream().filter(c -> c.data() instanceof Node).toList()) {
sb.append(indent + " " + c.role().name + ": ");
sb.append(((Node) c.data()).dump(indent+" "));
}
sb.append(indent + "}\n");
return sb.toString();
}
public Node parent() {
return parent;
}
public void replaceChild(Node matched, Node replacement) {
for (Child c: children) {
if (c.data() instanceof Node && c.data() == matched) {
c.setData(replacement);
}
}
replacement.resolve(this.concept.language(),this);
}
}