-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathNode.java
More file actions
61 lines (47 loc) · 1.27 KB
/
Copy pathNode.java
File metadata and controls
61 lines (47 loc) · 1.27 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
package loop.ast;
import loop.Token;
import java.util.ArrayList;
import java.util.List;
/**
* An abstract node in the parse tree.
*
* @author [email protected] (Dhanji R. Prasanna)
*/
public abstract class Node {
// the rest of the tree under this node
protected final List<Node> children = new ArrayList<Node>();
public int sourceLine, sourceColumn;
public Node add(Node child) {
children.add(child);
return this;
}
@SuppressWarnings("unchecked")
public <T extends Node> T sourceLocation(List<Token> tokens) {
return sourceLocation(tokens.iterator().next());
}
@SuppressWarnings("unchecked")
public <T extends Node> T sourceLocation(Token start) {
this.sourceLine = start.line;
this.sourceColumn = start.column;
return (T) this;
}
@SuppressWarnings("unchecked")
public <T extends Node> T sourceLocation(Node source) {
this.sourceLine = source.sourceLine;
this.sourceColumn = source.sourceColumn;
return (T) this;
}
public List<Node> children() {
return children;
}
public Node onlyChild() {
assert children.size() == 1;
return children.get(0);
}
public abstract String toSymbol();
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
children + '}';
}
}