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