Commit baa12557 authored by baueko's avatar baueko

changed parsing to Java 8

parent 39dcc956
package de.in.tum.www2.cup.ast;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.JavaCore;
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.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IVariableBinding;
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.SimpleName;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import de.in.tum.www2.cup.ast.CodeBlock.BlockType;
public class JavaCompiler {
/**
*
* @param userCode
......@@ -32,17 +20,21 @@ public class JavaCompiler {
*/
public static ASTNode parseJava(String userCode, CodeBlock codeblock, String[] classpathEntries) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
ASTNode node;
String[] sourcepathEntries = new String[] { "" };
parser.setEnvironment(classpathEntries, sourcepathEntries, new String[] { "UTF-8" }, true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setUnitName("DummyClass");
parser.setResolveBindings(true);
Map<String, String> options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
parser.setCompilerOptions(options);
ASTNode node;
String dummyClass = "";
final String IMPORTS = "import java_cup.runtime.ComplexSymbolFactory.Location;\n";
final String DUMMY_CLASS_DEF = "public class DummyClass\n{";
final String PARSER_CLASS_DEF = "class Parser\n{";
final String RWING = "}";
final String DUMMY_METHOD_DEF = "\n\tpublic void dummymethod(";
final String DUMMY_METHOD_ARGS_END = ")\n\t{";
......@@ -51,38 +43,40 @@ public class JavaCompiler {
switch (codeblock.getBt()) {
case Action:
case Parser:
dummyClass = DUMMY_CLASS_DEF + userCode + RWING;
break;
case Parser:
dummyClass = PARSER_CLASS_DEF + userCode + RWING;
break;
case Init:
case Scan:
// TODO Add return Type for Scan with codeblock
dummyClass = DUMMY_CLASS_DEF + DUMMY_METHOD_DEF + DUMMY_METHOD_ARGS_END + userCode + DUMMY_METHOD_END
+ DUMMY_CLASS_DEF_END;
break;
case ActionCode:
ActionCodeBlock block = (ActionCodeBlock) codeblock;
dummyClass = IMPORTS + DUMMY_CLASS_DEF + DUMMY_METHOD_DEF;
// TODO return type
dummyClass += block.getReturnType() + " RESULT";
for (LabeledProductionSymbolRef l : block.getVariables()) {
dummyClass += ", " + l.getDatatype() + " " + l.label;
dummyClass += ", " + "Location" + " " + l.label + "xleft"; // TODO Object should really be a Location
dummyClass += ", " + "Location" + " " + l.label + "xright"; // TODO Object should really be a Location
dummyClass += ", " + "int" + " " + l.label + "left";
dummyClass += ", " + "int" + " " + l.label + "right";
}
dummyClass += DUMMY_METHOD_ARGS_END + "\n\t\t";
codeblock.setDummyClassOffset(dummyClass.length());
dummyClass += userCode;
dummyClass += DUMMY_METHOD_END + DUMMY_CLASS_DEF_END;
break;
default:
throw new UnsupportedOperationException("java parsing not implemented: unknown blocktype");
break;
case ActionCode:
ActionCodeBlock block = (ActionCodeBlock) codeblock;
dummyClass = IMPORTS + DUMMY_CLASS_DEF + DUMMY_METHOD_DEF;
// TODO return type
dummyClass += block.getReturnType() + " RESULT";
for (LabeledProductionSymbolRef l : block.getVariables()) {
dummyClass += ", " + l.getDatatype() + " " + l.label;
dummyClass += ", " + "Location" + " " + l.label + "xleft"; // TODO Object should really be a Location
dummyClass += ", " + "Location" + " " + l.label + "xright"; // TODO Object should really be a Location
dummyClass += ", " + "int" + " " + l.label + "left";
dummyClass += ", " + "int" + " " + l.label + "right";
}
dummyClass += DUMMY_METHOD_ARGS_END + "\n\t\t";
codeblock.setDummyClassOffset(dummyClass.length());
dummyClass += userCode;
dummyClass += DUMMY_METHOD_END + DUMMY_CLASS_DEF_END;
break;
default:
throw new UnsupportedOperationException("java parsing not implemented: unknown blocktype");
}
parser.setSource(dummyClass.toCharArray());
parser.setSource(dummyClass.toCharArray());
node = parser.createAST(null);
return node;
}
......
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