Commit 6d421bde authored by baueko's avatar baueko

removed Mouse and XMLElements, added eclipse ast visitor for variable

declarations 
parent 377feea8
......@@ -2,9 +2,20 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="lib"/>
<classpathentry kind="lib" path="lib/Mouse-1.7.runtime.jar"/>
<classpathentry kind="lib" path="lib/Mouse-1.7.jar"/>
<classpathentry kind="lib" path="lib/java-cup-11b-runtime.jar"/>
<classpathentry exported="true" kind="lib" path="lib"/>
<classpathentry exported="true" kind="lib" path="lib/java-cup-11b-runtime.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.contenttype_3.5.100.v20160418-1621.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.jobs_3.8.0.v20160509-0411.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.resources_3.11.0.v20160503-1608.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.resources.win32.x86_64_3.5.0.v20140124-1940.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.runtime_3.12.0.v20160606-1342.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.equinox.common_3.8.0.v20160509-1230.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.equinox.preferences_3.6.1.v20160815-1406.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.jdt_3.12.1.v20160907-1200.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.jdt.core_3.12.1.v20160829-0950.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.osgi_3.11.1.v20160708-1632.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.osgi.services_3.5.100.v20160504-1419.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.osgi.util_3.3.100.v20150423-1351.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.text_3.6.0.v20160503-1849.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -17,8 +17,7 @@ import de.in.tum.www2.cup.ast.TerminalDeclaration;
import de.in.tum.www2.cup.ast.TypedNonTerminalDeclaration;
import de.in.tum.www2.cup.ast.TypedTerminalDeclaration;
public class DeclarationsExtractorVisitor extends Visitor<Object>
{
public class DeclarationsExtractorVisitor extends Visitor<Object> {
private Declarations result = new Declarations();
public Declarations getResult() {
......@@ -26,10 +25,10 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
}
@Override
public void postVisit (ProductionRight node, Object data) {
public void postVisit(ProductionRight node, Object data) {
Production parent = (Production) node.getParent();
int index=0;
int index = 0;
for (ProductionRight pr : parent.getRightHandSides()) {
if (pr == node)
break;
......@@ -40,19 +39,19 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
if (parent.getName() == null)
return;
builder.append (parent.getName().name);
builder.append ("::");
builder.append (parent.getRightHandSides().size());
builder.append ("::");
builder.append (index);
builder.append(parent.getName().name);
builder.append("::");
builder.append(parent.getRightHandSides().size());
builder.append("::");
builder.append(index);
String key = builder.toString();
HashMap<String,HashSet<String>> locals = result.getLocals();
HashMap<String, HashSet<String>> locals = result.getLocals();
if (!locals.containsKey(key))
locals.put(key, new HashSet<String>());
HashSet<String> set = locals.get(key);
for (IProductionRightPart part : node.getList()) {
......@@ -61,10 +60,11 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
set.add(lpsr.label);
}
}
}
@Override
public void postVisit (TerminalDeclaration node, Object data) {
public void postVisit(TerminalDeclaration node, Object data) {
if (node != null && node.getTerminals() != null) {
for (Terminal t : node.getTerminals())
if (t != null)
......@@ -73,7 +73,7 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
}
@Override
public void postVisit (NonTerminalDeclaration node, Object data) {
public void postVisit(NonTerminalDeclaration node, Object data) {
if (node != null && node.getNonTerminals() != null) {
for (NonTerminal nt : node.getNonTerminals())
if (nt != null)
......@@ -82,7 +82,7 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
}
@Override
public void postVisit (TypedNonTerminalDeclaration node, Object data) {
public void postVisit(TypedNonTerminalDeclaration node, Object data) {
// TODO: OAOO!
if (node != null && node.getNonTerminals() != null) {
for (NonTerminal nt : node.getNonTerminals())
......@@ -90,9 +90,9 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
result.getNonTerminals().add(nt.getNameStringOrNull());
}
}
@Override
public void postVisit (TypedTerminalDeclaration node, Object data) {
public void postVisit(TypedTerminalDeclaration node, Object data) {
// TODO: OAOO!
if (node != null && node.getTerminals() != null) {
for (Terminal nt : node.getTerminals())
......
package de.in.tum.www2.cup.analysis;
import java.util.LinkedList;
import java.util.List;
import java_cup.runtime.ComplexSymbolFactory.Location;
public class JavaDeclarationsTreeElement {
public void setParent(JavaDeclarationsTreeElement parent) {
this.parent = parent;
}
List<Pair> variables;
Location end;
List<JavaDeclarationsTreeElement> children;
JavaDeclarationsTreeElement parent;
public JavaDeclarationsTreeElement(Location end, JavaDeclarationsTreeElement parent) {
this.end = end;
this.parent = parent;
}
public List<JavaDeclarationsTreeElement> getChildren() {
return children;
}
public int childrenLength() {
return children.size();
}
public JavaDeclarationsTreeElement getParent() {
return parent;
}
public List<Pair> getVariables() {
return variables;
}
public void addDeclaration(String name, Location start) {
if (variables == null) {
variables = new LinkedList<>();
}
variables.add(new Pair(name, start));
}
public JavaDeclarationsTreeElement addChild(JavaDeclarationsTreeElement child) {
if (children == null) {
children = new LinkedList<>();
}
children.add(child);
return child;
}
class Pair {
String name;
Location start;
public Pair(String name, Location start) {
this.name = name;
this.start = start;
}
@Override
public String toString() {
return "<" + name + ", " + start + ">";
}
}
// TODO Fix Output
@Override
public String toString() {
return "vars:" + variables + " childs:" + children + " endPos:[" + end.getLine() + "," + end.getColumn() + "]";
}
}
\ No newline at end of file
package de.in.tum.www2.cup.analysis;
import de.in.tum.www2.cup.ast.*;
import java_cup.runtime.XMLElement;
public abstract class Visitor<T> extends AbstractVisitor<T>
{
......@@ -35,6 +36,7 @@ public abstract class Visitor<T> extends AbstractVisitor<T>
@Override public T preVisit (TerminalDeclaration node, T data) { return data; }
@Override public T preVisit (TypedNonTerminalDeclaration node, T data) { return data; }
@Override public T preVisit (TypedTerminalDeclaration node, T data) { return data; }
@Override public void postVisit (ActionCodeBlock node, T data) { }
@Override public void postVisit (ClassName node, T data) { }
......
package de.in.tum.www2.cup.analysis;
import de.in.tum.www2.cup.ast.*;
import java_cup.runtime.XMLElement;
public abstract class WildcardVisitor<T> extends AbstractVisitor<T>
{
......@@ -39,6 +40,9 @@ public abstract class WildcardVisitor<T> extends AbstractVisitor<T>
public T preVisit (TerminalDeclaration node, T data) { return preVisitWildcard(node, data); }
public T preVisit (TypedNonTerminalDeclaration node, T data) { return preVisitWildcard(node, data); }
public T preVisit (TypedTerminalDeclaration node, T data) { return preVisitWildcard(node, data); }
public T preVisit (XMLElement.Terminal node, T data) { throw new UnsupportedOperationException(); }
public T preVisit (XMLElement.NonTerminal node, T data) { throw new UnsupportedOperationException(); }
public T preVisit (XMLElement.Error node, T data) { throw new UnsupportedOperationException(); }
public void postVisit (ActionCodeBlock node, T data) { postVisitWildcard(node, data); }
public void postVisit (ClassName node, T data) { postVisitWildcard(node, data); }
......@@ -59,5 +63,8 @@ public abstract class WildcardVisitor<T> extends AbstractVisitor<T>
public void postVisit (TerminalDeclaration node, T data) { postVisitWildcard(node, data); }
public void postVisit (TypedNonTerminalDeclaration node, T data) { postVisitWildcard(node, data); }
public void postVisit (TypedTerminalDeclaration node, T data) { postVisitWildcard(node, data); }
public void postVisit (XMLElement.Terminal node, T data) { throw new UnsupportedOperationException(); }
public void postVisit (XMLElement.NonTerminal node, T data) { throw new UnsupportedOperationException(); }
public void postVisit (XMLElement.Error node, T data) { throw new UnsupportedOperationException(); }
}
......@@ -2,8 +2,6 @@ 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 {
......@@ -14,8 +12,6 @@ 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);
}
......@@ -23,5 +19,4 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
protected String getNodeName() {
return "ActionCodeBlock";
}
}
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 org.eclipse.jdt.core.dom.ASTNode;
import de.in.tum.www2.cup.Position;
import de.in.tum.www2.cup.Range;
public abstract class CodeBlock extends AbstractNode {
......@@ -21,14 +12,8 @@ public abstract class CodeBlock extends AbstractNode {
private int debugId;
private String blob;
protected XMLElement javaTree;
private BlockType bt;
private JavaDeclarationsTreeElement variableDeclarations;
private static JavaDeclarationsTreeElement globalParserVariableDeclarations;
public JavaDeclarationsTreeElement getVariableDeclarations() {
return variableDeclarations;
}
private ASTNode tree;
public int getDebugId() {
return debugId;
......@@ -47,14 +32,8 @@ public abstract class CodeBlock extends AbstractNode {
this.debugId = debugId;
this.bt = bt;
this.blob = blob;
// this.blob = " public class Parser{" + blob + "}";
try {
this.javaTree = JavaCompiler.parseJava(this.blob, this);
this.variableDeclarations = createVariableDeclarationList(javaTree);
} catch (Exception e) {
System.out.println(e.getMessage());
}
this.tree = JavaCompiler.parseJava(blob, this);
this.tree.accept(new LocalVariableDetector());
}
public BlockType getBlockType() {
......@@ -63,95 +42,6 @@ public abstract class CodeBlock extends AbstractNode {
@Override
protected void putDescription(StringBuilder builder) {
// builder.append(javaTree);
builder.append(variableDeclarations);
}
protected static JavaDeclarationsTreeElement createVariableDeclarationList(XMLElement elem) {
XMLElementTreeTransform treeTransform = new XMLElementTreeTransform(elem.right());
SyntaxTreeDFS.dfs(elem, treeTransform);
if (elem.getTagname().equals("Parser")) {
globalParserVariableDeclarations = treeTransform.getTreeTop();
// TODO kill global childs, copy!
}
// System.out.println("GLOBAL: " + globalParserVariableDeclarations);
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 hinzufügen und absteigen
Location l;
if (arg1.size() > 0)
l = arg1.get(arg1.size() - 1).right();
else
l = arg0.right();
JavaDeclarationsTreeElement t = new JavaDeclarationsTreeElement(l, tree);
tree.addChild(t);
tree = t;
});
registerPostVisit("Block", (arg0, arg1) -> {
if (tree.getVariables() == null) {
JavaDeclarationsTreeElement parent = tree.getParent();
int x = parent.childrenLength();
if (!(tree.getChildren() == null)) {
for (int i = 0; i < tree.childrenLength(); i++) {
parent.addChild(tree.getChildren().get(i));
tree.getChildren().get(i).setParent(parent);
for (int j = 0; j < x; j++) {
if (parent.getChildren().get(j).equals(tree)) {
parent.getChildren().remove(j);
}
}
}
}
for (int i = 0; i < x; i++) {
List<JavaDeclarationsTreeElement> parentChildren = parent.getChildren();
if (parentChildren.get(i).equals(tree)) {
parentChildren.remove(i);
}
}
}
// TODO fix empty trees
// baum zurücksetzen und null-Bäume löschen
// wenn selber null und Kinder null/keine Kinder
// aus childrenliste des parents selbst löschen
// wenn Kinder nicht null
// Im Children von Parents eigene Kinder eintragen und umgedreht
// aus Children von Parent löschen
// (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) {
}
builder.append(bt + " " + tree);
}
}
package de.in.tum.www2.cup.ast;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import java_cup.runtime.XMLElement;
import mouse.runtime.SourceString;
import java_cup.runtime.SyntaxTreeTransform;
public class JavaCompiler {
private static String[] blacklist = { "VariableDeclaratorId" };
public static XMLElement parseJava(String blob, CodeBlock codeblock) throws NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException {
SourceString src = new SourceString(blob);
public static ASTNode parseJava(String blob, CodeBlock codeblock) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(blob.toCharArray());
boolean ok = false;
ASTNode node;
ASTNode node;
switch (codeblock.getBt()) {
case Action:
case Action:
parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
node= parser.createAST(null);
//ok = parser.parse(src, "Action");
node = parser.createAST(null);
break;
case Parser:
parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
node= parser.createAST(null);
node = parser.createAST(null);
break;
case Init:
parser.setKind(ASTParser.K_STATEMENTS);
node= parser.createAST(null);
node = parser.createAST(null);
break;
case Scan:
parser.setKind(ASTParser.K_STATEMENTS);
node= parser.createAST(null);
node = parser.createAST(null);
break;
case ActionCode:
parser.setKind(ASTParser.K_STATEMENTS);
node= parser.createAST(null);
node = parser.createAST(null);
break;
default:
throw new UnsupportedOperationException("java parsing not implemented: unknown blocktype");
}
/*if (ok) {
JavaSemantics sem = parser.semantics();
System.out.println(" -> Compilation succeeded");
XMLElement elem = (XMLElement) sem.rule.rhs(0).get();
// Remove unwanted unary branch chains from the syntax tree
List<String> blacklistList = Arrays.asList(blacklist);
// elem = SyntaxTreeTransform.removeUnaryChainsBlacklist(elem,
// blacklistList);
// elem = SyntaxTreeTransform.removeUnaryChains(elem);
System.out.println(elem);
return elem;
}*/
// TODO add exception
throw new RuntimeException("Java parsing failed.");
return node;
}
}
This diff is collapsed.
package de.in.tum.www2.cup.ast;
import java_cup.runtime.ComplexSymbolFactory.Location;
import java.util.LinkedList;
import java.util.List;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java_cup.runtime.XMLElement;
import java_cup.runtime.XMLElement.Terminal;
import mouse.runtime.Phrase;
public interface JavaSemanticsUsercode {
Phrase lhs();
Phrase rhs(int i);
// -------------------------------------------------------------------
// Identifier = !Keyword Letter LetterOrDigit* Spacing
// -------------------------------------------------------------------
default void Identifier() {
XMLElement x = (XMLElement) lhs().get();
lhs().put(new XMLElement.Terminal(
new Location(lhs().getStartLine(), lhs().getStartColumn(), lhs().getStartPos()), "Identifier",
lhs().text().trim(), new Location(lhs().getEndLine(), lhs().getEndColumn(), lhs().getEndPos())));
// System.out.println("\n TEST: " + x);
}
// -------------------------------------------------------------------
// CLASS = "class" !LetterOrDigit Spacing
// INTERFACE = "interface" !LetterOrDigit Spacing
// LPAR = "(" Spacing
// RPAR = ")" Spacing
// LWING = "{" Spacing
// RWING = "}" Spacing
// SEMI = ";" Spacing
// Spacing = ([ \t\r\n\f]+ / "/*" _*+ "*/" / "//" _*+ [\r\n])*
// -------------------------------------------------------------------
default void Suppress() {
lhs().put(null);
}
default void VariableDeclaratorId() {
XMLElement x = (XMLElement) lhs().get();
List<String> tokenList = new LinkedList<>();
lhs().put(new TerminalWithTokenList(
new Location(lhs().getStartLine(), lhs().getStartColumn(), lhs().getStartPos()), "VariableDeclaratorId",
lhs().text().trim(), new Location(lhs().getEndLine(), lhs().getEndColumn(), lhs().getEndPos()),
tokenList));
}
}
class TerminalWithTokenList extends Terminal {
List<String> tokenlist;
public TerminalWithTokenList(Location l, String symbolname, Location r, List<String> tokenList) {
this(l, symbolname, null, r, tokenList);
}
public TerminalWithTokenList(Location l, String symbolname, Object i, Location r, List<String> tokenList) {
super(l, symbolname, i, r);
this.tokenlist = tokenList;
}
}
package de.in.tum.www2.cup.ast;
import java.util.Iterator;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.FieldDeclaration;