Commit 31db4a26 authored by baueko's avatar baueko

fixed minor bugs, actionactioncode parsing still missing

parent 63d3343f
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.Block;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
......@@ -15,146 +14,134 @@ 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);
private static AST ast = AST.newAST(AST.JLS8);
private static CompilationUnit javaTree;
private static TypeDeclaration parserClassNode, parserActionClassNode;
private static MethodDeclaration userInitNode, scanWithNode;
private static final String PARSER_CLASS_NAME = "Parser";
private static final String USER_INIT_METHOD_NAME = "user_init";
private static final String PARSER_ACTION_CLASS_NAME = "CUP$Parser$actions";
private static final String SCAN_METHOD_NAME = "scan";
private static final String DO_ACTION_METHOD_NAME = "doAction";
static {
initTree(ast);
}
private static void initTree(AST tree) {
// public class Parser
TypeDeclaration td = a.newTypeDeclaration();
Modifier m = a.newModifier(ModifierKeyword.PUBLIC_KEYWORD);
td.modifiers().add(m);
td.setName(a.newSimpleName("Parser"));
parserClassNode = tree.newTypeDeclaration();
Modifier m = tree.newModifier(ModifierKeyword.PUBLIC_KEYWORD);
parserClassNode.modifiers().add(m);
parserClassNode.setName(tree.newSimpleName(PARSER_CLASS_NAME));
// 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"));
userInitNode = tree.newMethodDeclaration();
userInitNode.modifiers().add(tree.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
userInitNode.setName(tree.newSimpleName(USER_INIT_METHOD_NAME));
// class CUP$Parser$actions
TypeDeclaration parserActions = a.newTypeDeclaration();
parserActions.setName(a.newSimpleName("CUP$Parser$actions"));
parserActionClassNode = tree.newTypeDeclaration();
parserActionClassNode.setName(tree.newSimpleName(PARSER_ACTION_CLASS_NAME));
MethodDeclaration m2 = tree.newMethodDeclaration();
m2.setName(tree.newSimpleName(DO_ACTION_METHOD_NAME));
m2.modifiers().add(tree.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
m2.modifiers().add(tree.newModifier(ModifierKeyword.FINAL_KEYWORD));
m2.setReturnType2(tree.newSimpleType(
tree.newQualifiedName(tree.newQualifiedName(tree.newName("java_cup"), tree.newSimpleName("runtime")),
tree.newSimpleName("Symbol"))));
// Block b = tree.newBlock();
// VariableDeclarationFragment tree.newVariableDeclarationFragment();
// VariableDeclarationStatement vds =
// tree.newVariableDeclarationStatement()
// m2.setBody(b);
parserActionClassNode.bodyDeclarations().add(m2);
// 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
TypeDeclaration n = (TypeDeclaration) getNodeWithBlocktype(BlockType.Parser);
for (Object o : n.bodyDeclarations()) {
BodyDeclaration b = (BodyDeclaration) ASTNode.copySubtree(a, ((BodyDeclaration) o));
td.bodyDeclarations().add(b);
}
scanWithNode = tree.newMethodDeclaration();
scanWithNode.setName(tree.newSimpleName(SCAN_METHOD_NAME));
scanWithNode.modifiers().add(tree.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
scanWithNode.setReturnType2(tree.newSimpleType(
tree.newQualifiedName(tree.newQualifiedName(tree.newName("java_cup"), tree.newSimpleName("runtime")),
tree.newSimpleName("Symbol"))));
parserClassNode.bodyDeclarations().add(userInitNode);
parserClassNode.bodyDeclarations().add(scanWithNode);
parserClassNode.bodyDeclarations().add(parserActionClassNode);
javaTree = ast.newCompilationUnit();
javaTree.types().add(parserClassNode);
// add stuff to CupParseractions
Object n2 = getNodeWithBlocktype(BlockType.Action);
// for (Object o : n2.bodyDeclarations()) {
// BodyDeclaration b = (BodyDeclaration) ASTNode.copySubtree(a,
// ((BodyDeclaration) o));
// parserActions.bodyDeclarations().add(b);
// }
Object n3 = getNodeWithBlocktype(BlockType.Scan);
// MethodDeclaration n4 = (MethodDeclaration)
// getNodeWithBlocktype(BlockType.Init);
// for (Object o : n4.bodyDeclarations()) {
// BodyDeclaration b = (BodyDeclaration) ASTNode.copySubtree(a,
// ((BodyDeclaration) o));
// td.bodyDeclarations().add(b);
// }
// td.bodyDeclarations().add(userInit);
int i;
}
private static ASTNode getNodeWithBlocktype(BlockType bt) {
private static void addNodeToTree(BlockType bt, ASTNode node, AST ast) {
switch (bt) {
case Action:
case Init:
case Parser:
case Scan:
for (Pair<ASTNode, BlockType> p : nodes) {
if (p.right == bt)
return p.left;
TypeDeclaration n = (TypeDeclaration) node;
if (n != null)
for (Object o : n.bodyDeclarations()) {
BodyDeclaration b = (BodyDeclaration) ASTNode.copySubtree(ast, ((BodyDeclaration) o));
parserClassNode.bodyDeclarations().add(b);
}
break;
case Action:
TypeDeclaration n2 = (TypeDeclaration) node;
for (Object o : n2.bodyDeclarations()) {
BodyDeclaration b = (BodyDeclaration) ASTNode.copySubtree(ast, ((BodyDeclaration) o));
parserActionClassNode.bodyDeclarations().add(b);
}
break;
case Scan:
Block n3 = (Block) node;
Block b = (Block) ASTNode.copySubtree(ast, n3);
scanWithNode.setBody(b);
break;
case Init:
Block n4 = (Block) node;
Block b2 = (Block) ASTNode.copySubtree(ast, n4);
userInitNode.setBody(b2);
case ActionCode:
// parserActionClassNode.bodyDeclarations().add(null);
break;
default:
throw new IllegalArgumentException("this method is useless for actionaction block types");
throw new UnsupportedOperationException("not yet implemented");
}
return null;
}
public static ASTNode parseJava(String blob, CodeBlock codeblock) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(blob.toCharArray());
parser.setResolveBindings(true);
ASTNode node;
switch (codeblock.getBt()) {
case Action:
parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
node = parser.createAST(null);
nodes.add(new Pair<ASTNode, CodeBlock.BlockType>(node, BlockType.Action));
break;
case Parser:
parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
node = parser.createAST(null);
nodes.add(new Pair<ASTNode, CodeBlock.BlockType>(node, BlockType.Parser));
break;
case Init:
parser.setKind(ASTParser.K_STATEMENTS);
node = parser.createAST(null);
nodes.add(new Pair<ASTNode, CodeBlock.BlockType>(node, BlockType.Init));
break;
case Scan:
parser.setKind(ASTParser.K_STATEMENTS);
node = parser.createAST(null);
nodes.add(new Pair<ASTNode, CodeBlock.BlockType>(node, BlockType.Scan));
break;
case ActionCode:
parser.setKind(ASTParser.K_STATEMENTS);
node = parser.createAST(null);
nodes.add(new Pair<ASTNode, CodeBlock.BlockType>(node, BlockType.ActionCode));
break;
default:
throw new UnsupportedOperationException("java parsing not implemented: unknown blocktype");
}
doTreeMagic();
addNodeToTree(codeblock.getBlockType(), node, ast);
return node;
}
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());
}
}
}
Markdown is supported
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