add line info to astnodes
This commit is contained in:
@ -8,12 +8,14 @@ import java.util.Objects;
|
||||
|
||||
public class ASTNode {
|
||||
|
||||
private final int line;
|
||||
private String name;
|
||||
private String value;
|
||||
private List<ASTNode> children = new ArrayList<>();
|
||||
|
||||
public ASTNode(String name) {
|
||||
public ASTNode(String name, int line) {
|
||||
this.name = name;
|
||||
this.line = line;
|
||||
this.value = "";
|
||||
}
|
||||
|
||||
@ -29,6 +31,15 @@ public class ASTNode {
|
||||
return this.children;
|
||||
}
|
||||
|
||||
public void setChildren(List<ASTNode> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public void setChildren(ASTNode... children) {
|
||||
this.children = new ArrayList<>();
|
||||
this.children.addAll(Arrays.asList(children));
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
@ -37,13 +48,21 @@ public class ASTNode {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.name, this.value, this.children, this.line);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.name, this.value, this.children);
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof ASTNode) {
|
||||
return this.name.equals(((ASTNode) obj).name)
|
||||
&& this.value.equals(((ASTNode) obj).value)
|
||||
&& this.children.equals(((ASTNode) obj).children)
|
||||
&& this.line == ((ASTNode) obj).line;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// toString() und print() von hier: https://stackoverflow.com/a/8948691
|
||||
@ -54,19 +73,14 @@ public class ASTNode {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void setChildren(List<ASTNode> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public void setChildren(ASTNode... children) {
|
||||
this.children = new ArrayList<>();
|
||||
this.children.addAll(Arrays.asList(children));
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
private void print(StringBuilder buffer, String prefix, String childrenPrefix) {
|
||||
buffer.append(prefix);
|
||||
buffer.append(this.name);
|
||||
@ -86,17 +100,6 @@ public class ASTNode {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof ASTNode) {
|
||||
return this.name.equals(((ASTNode) obj).name)
|
||||
&& this.value.equals(((ASTNode) obj).value)
|
||||
&& this.children.equals(((ASTNode) obj).children);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public long size() {
|
||||
int s = 0;
|
||||
|
||||
@ -106,4 +109,8 @@ public class ASTNode {
|
||||
|
||||
return s + 1;
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return this.line;
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ class ASTTest {
|
||||
|
||||
@Test
|
||||
void testOneNode() {
|
||||
ASTNode root = new ASTNode("Wurzel");
|
||||
ASTNode root = new ASTNode("Wurzel", 1);
|
||||
|
||||
AST tree = new AST(root);
|
||||
System.out.println(tree);
|
||||
@ -18,9 +18,9 @@ class ASTTest {
|
||||
|
||||
@Test
|
||||
void testThreeNodesBinary() {
|
||||
ASTNode root = new ASTNode("Wurzel");
|
||||
ASTNode childA = new ASTNode("A");
|
||||
ASTNode childB = new ASTNode("B");
|
||||
ASTNode root = new ASTNode("Wurzel", 1);
|
||||
ASTNode childA = new ASTNode("A", 1);
|
||||
ASTNode childB = new ASTNode("B", 1);
|
||||
|
||||
root.addChild(childA);
|
||||
root.addChild(childB);
|
||||
@ -33,9 +33,9 @@ class ASTTest {
|
||||
|
||||
@Test
|
||||
void testThreeNodesLinear() {
|
||||
ASTNode root = new ASTNode("Wurzel");
|
||||
ASTNode childA = new ASTNode("A");
|
||||
ASTNode childB = new ASTNode("B");
|
||||
ASTNode root = new ASTNode("Wurzel", 1);
|
||||
ASTNode childA = new ASTNode("A", 1);
|
||||
ASTNode childB = new ASTNode("B", 1);
|
||||
|
||||
root.addChild(childA);
|
||||
childA.addChild(childB);
|
||||
|
Reference in New Issue
Block a user