Commit 1bb3ec41 authored by Konrad Pröll's avatar Konrad Pröll
Browse files

resolved bindings

parent 9938f086
......@@ -30,7 +30,7 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
//
LabelToVariableVisitor visitor = new LabelToVariableVisitor(this);
this.getParent().accept(visitor, null);
JavaCompiler.parseJava(getBlob(), this);
}
public void addVariable(LabeledProductionSymbolRef l) {
......
package de.in.tum.www2.cup.ast;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import de.in.tum.www2.cup.ast.CodeBlock.BlockType;
public class JavaCompiler {
//private static List<Pair<ASTNode, BlockType>> nodes = new LinkedList<>();
private static ASTNode javaTree;
/*public static void doTreeMagic() {
// TODO add root node (Parser class)
// TODO add nodes -> action code in class Cup$Parser$actions
AST a = AST.newAST(AST.JLS8);
// public class Parser
TypeDeclaration td = a.newTypeDeclaration();
Modifier m = a.newModifier(ModifierKeyword.PUBLIC_KEYWORD);
td.modifiers().add(m);
td.setName(a.newSimpleName("Parser"));
// public void user_init() throws java.lang.Exception
MethodDeclaration userInit = a.newMethodDeclaration();
userInit.modifiers().add(a.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
userInit.setName(a.newSimpleName("user_init"));
// class CUP$Parser$actions
TypeDeclaration parserActions = a.newTypeDeclaration();
parserActions.setName(a.newSimpleName("CUP$Parser$actions"));
// public java_cup.runtime.Symbol scan() throws java.lang.Exception
MethodDeclaration scanWith = a.newMethodDeclaration();
scanWith.setName(a.newSimpleName("scan"));
scanWith.modifiers().add(a.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
scanWith.setReturnType2(a.newSimpleType(a.newQualifiedName(
a.newQualifiedName(a.newName("java_cup"), a.newSimpleName("runtime")), a.newSimpleName("Symbol"))));
// add stuff to Parser class body
td.bodyDeclarations().add(userInit);
for (Pair<ASTNode, BlockType> p : nodes) {
if (p.right == BlockType.Parser) {
TypeDeclaration td2 = (TypeDeclaration) p.left;
td2.delete();
System.out.println(td2.bodyDeclarations().toString());
td.bodyDeclarations().addAll(td2.bodyDeclarations());
// TODO fix this
}
}
td.bodyDeclarations().add(parserActions);
td.bodyDeclarations().add(scanWith);
int i;
}*/
public static ASTNode parseJava(String blob, CodeBlock codeblock) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(blob.toCharArray());
ASTNode node;
parser.setUnitName("TODO: Edit name");
switch (codeblock.getBt()) {
case Action:
parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
node = parser.createAST(null);
break;
case Parser:
parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
blob="public class DummyClass{"+blob+"}";
parser.setEnvironment(new String[] {"/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar"}, new String[] {""}, new String[] { "UTF-8"}, true);
parser.setResolveBindings(true);
parser.setSource(blob.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
node = parser.createAST(null);
node.accept(new LocalVariableDetector());
break;
case Init:
parser.setKind(ASTParser.K_STATEMENTS);
node = parser.createAST(null);
break;
case Scan:
parser.setKind(ASTParser.K_STATEMENTS);
blob="public class DummyClass{\n\tpublic void dummymethod()\n\t{"+blob+"\n\t}\n}";
parser.setEnvironment(new String[] {"/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar"}, new String[] {""}, new String[] { "UTF-8"}, true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(blob.toCharArray());
parser.setResolveBindings(true);
node = parser.createAST(null);
node.accept(new LocalVariableDetector());
break;
case ActionCode:
parser.setKind(ASTParser.K_STATEMENTS);
blob="public class DummyClass{\n\tpublic void dummymethod("+"Integer e"+")\n\t{"+blob+"\n\t}\n}";
parser.setEnvironment(new String[] {"/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar"}, new String[] {""}, new String[] { "UTF-8"}, true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(blob.toCharArray());
parser.setResolveBindings(true);
node = parser.createAST(null);
node.accept(new LocalVariableDetector());
break;
default:
throw new UnsupportedOperationException("java parsing not implemented: unknown blocktype");
}
return node;
}
private static String getVars() {
// TODO Auto-generated method stub
return null;
}
public static class Pair<L, R> {
private final L left;
private final R right;
public Pair(L left, R right) {
this.left = left;
this.right = right;
}
public L getLeft() {
return left;
}
public R getRight() {
return right;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair))
return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) && this.right.equals(pairo.getRight());
}
}
}
......@@ -4,6 +4,7 @@ import java.util.Iterator;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
......@@ -13,6 +14,8 @@ public class LocalVariableDetector extends ASTVisitor {
public boolean visit(FieldDeclaration node) {
for (Iterator iter = node.fragments().iterator(); iter.hasNext();) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) iter.next();
IBinding test= fragment.resolveBinding();
System.out.println("Juhu");
}
return false;
......@@ -22,6 +25,8 @@ public class LocalVariableDetector extends ASTVisitor {
public boolean visit(VariableDeclarationStatement node) {
for (Iterator iter = node.fragments().iterator(); iter.hasNext();) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) iter.next();
IBinding test= fragment.resolveBinding();
System.out.println("Juhu");
}
return false;
......
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