Commit 96dbcea0 authored by baueko's avatar baueko
Browse files

added first version for tree for java variable declarations

parent 6f767980
......@@ -2,6 +2,8 @@ package de.in.tum.www2.cup.ast;
import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.analysis.AbstractVisitor;
import de.in.tum.www2.cup.analysis.JavaDeclarationsTreeElement;
import java_cup.runtime.XMLElement;
public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
......@@ -12,6 +14,8 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
@Override
public <T> void accept(AbstractVisitor<T> visitor, T data) {
visitor.preVisit(this, data);
JavaDeclarationsTreeElement tree = createVariableDeclarationList(javaTree);
System.out.println(tree);// TODO remove Debug
visitor.postVisit(this, data);
}
......
package de.in.tum.www2.cup.ast;
import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.analysis.AbstractVisitor;
import de.in.tum.www2.cup.analysis.JavaDeclarationsTreeElement;
import de.in.tum.www2.cup.ast.CodeBlock.BlockType;
import java_cup.runtime.ComplexSymbolFactory.Location;
import java_cup.runtime.SyntaxTreeDFS;
import java_cup.runtime.XMLElement;
import java_cup.runtime.XMLElement.Terminal;
import java.util.List;
import de.in.tum.www2.cup.Position;
public abstract class CodeBlock extends AbstractNode {
......@@ -12,7 +21,7 @@ public abstract class CodeBlock extends AbstractNode {
private int debugId;
private String blob;
private XMLElement javaTree;
protected XMLElement javaTree;
private BlockType bt;
public int getDebugId() {
......@@ -40,9 +49,68 @@ public abstract class CodeBlock extends AbstractNode {
}
}
public BlockType getBlockType() {
return bt;
}
@Override
protected void putDescription(StringBuilder builder) {
builder.append(javaTree);
}
protected static JavaDeclarationsTreeElement createVariableDeclarationList(XMLElement elem) {
XMLElementTreeTransform treeTransform = new XMLElementTreeTransform(elem.right());
SyntaxTreeDFS.dfs(elem, treeTransform);
return treeTransform.getTreeTop();
}
private static class XMLElementTreeTransform extends SyntaxTreeDFS.AbstractVisitor {
JavaDeclarationsTreeElement tree;
public JavaDeclarationsTreeElement getTreeTop() {
while (tree.getParent() != null)
tree = tree.getParent();
return tree;
}
public XMLElementTreeTransform(Location end) {
tree = new JavaDeclarationsTreeElement(end, null);
registerPostVisit("VariableDeclaratorId", (arg0, arg1) -> {
// Deklarationen in aktuellen Baum
Terminal t = (Terminal) arg0.selectById("Identifier").get(0);
tree.addDeclaration((String) t.value(), t.left());
});
registerPreVisit("Block", (arg0, arg1) -> {
// neues Element hinzufgen und absteigen
Location l = arg1.get(arg1.size() - 1).right();
JavaDeclarationsTreeElement t = new JavaDeclarationsTreeElement(l, tree);
tree.addChild(t);
tree = t;
});
registerPostVisit("Block", (arg0, arg1) -> {
// TODO fix empty trees
// baum zurcksetzen und null-Bume lschen
// wenn selber null und Kinder null/keine Kinder
// aus childrenliste des parents selbst lschen
// wenn Kinder nicht null
// Im Children von Parents eigene Kinder eintragen und umgedreht
// aus Children von Parent lschen
// (Wenn selber nicht null -> nix tun)
tree = tree.getParent();
});
}
@Override
public void defaultPost(XMLElement arg0, List<XMLElement> arg1) {
}
@Override
public void defaultPre(XMLElement arg0, List<XMLElement> arg1) {
}
}
}
package de.in.tum.www2.cup.ast;
import de.in.tum.www2.cup.analysis.AbstractVisitor;
import de.in.tum.www2.cup.analysis.JavaDeclarationsTreeElement;
import java_cup.runtime.XMLElement;
import de.in.tum.www2.cup.Range;
public class SpecialCodeBlock extends CodeBlock implements IProductionRightPart {
private BlockType bt;
public BlockType getBlockType() {
return bt;
}
public SpecialCodeBlock(int debugId, BlockType bt, String blob, Range range) {
super(debugId, bt, blob, range);
......@@ -17,6 +14,8 @@ public class SpecialCodeBlock extends CodeBlock implements IProductionRightPart
@Override
public <T> void accept(AbstractVisitor<T> visitor, T data) {
visitor.preVisit(this, data);
JavaDeclarationsTreeElement tree = createVariableDeclarationList(javaTree);
System.out.println(tree);// TODO Remove debug
visitor.postVisit(this, data);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment