Commit 3ef9eb3c authored by Clemens Pflaum's avatar Clemens Pflaum

Merge remote-tracking branch 'origin/konstantin2' into clemens

Conflicts:
	CupParser/src/de/in/tum/www2/cup/CupParser.java
	CupParser/src/de/in/tum/www2/cup/ast/ActionCodeBlock.java
	CupParser/src/de/in/tum/www2/cup/ast/CodeBlock.java
	CupParser/src/de/in/tum/www2/cup/ast/JavaCompiler.java
	CupParser/src/de/in/tum/www2/cup/ast/SpecialCodeBlock.java
parents c40ae431 ce773342
......@@ -43,7 +43,7 @@ public class CupParser {
}
result = parser.getResult();
result.parseJava(classpathEntries, context.getErrorManager());
// TODO add java parsing call
return result;
}
......
......@@ -6,10 +6,14 @@ import de.in.tum.www2.cup.ast.LabeledProductionSymbolRef;
import de.in.tum.www2.cup.ast.Production;
import de.in.tum.www2.cup.ast.ProductionRight;
public class LabelToVariableVisitor extends Visitor<Object> {
/**
* This Visitor extracts all labeled variables of a production and stores them in the corresponding block's list. Also
* stores the type of the RETURN-variable of this production
*/
public class CupVariableLocator extends Visitor<Object> {
ActionCodeBlock block;
public LabelToVariableVisitor(ActionCodeBlock block) {
public CupVariableLocator(ActionCodeBlock block) {
this.block = block;
}
......
......@@ -8,7 +8,7 @@ import org.eclipse.jdt.core.dom.CompilationUnit;
import de.in.tum.www2.cup.ErrorManager;
import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.analysis.AbstractVisitor;
import de.in.tum.www2.cup.analysis.LabelToVariableVisitor;
import de.in.tum.www2.cup.analysis.CupVariableLocator;
public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
private List<LabeledProductionSymbolRef> variableLabels = new LinkedList<>();
......@@ -42,10 +42,11 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
}
@Override
public void parseJavaCode(String[] classpathEntries, ErrorManager errMan) {
LabelToVariableVisitor visitor = new LabelToVariableVisitor(this);
CupVariableLocator visitor = new CupVariableLocator(this);
this.getParent().accept(visitor, null);
this.cu = (CompilationUnit) JavaCompiler.parseJava(getBlob(), this, classpathEntries);
this.cu = (CompilationUnit) JavaCompiler.parseJava(this, classpathEntries);
BindingCollector variableCollector = new BindingCollector();
this.cu.accept(variableCollector);
this.variableMap = variableCollector.getVariableMap();
......
......@@ -6,7 +6,6 @@ import java.util.Iterator;
import java.util.LinkedList;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.SimpleName;
......@@ -18,80 +17,33 @@ import de.in.tum.www2.cup.Range;
public abstract class CodeBlock extends AbstractNode {
public enum BlockType {
Action, Parser, Init, Scan, ActionCode
}
protected IProblem[] problems;
protected HashMap<IVariableBinding, LinkedList<SimpleName>> variableMap;
protected LinkedList<SimpleName> methodList;
protected CompilationUnit cu;
protected CompilationUnit cu; // ASTNode of action code
private int debugId;
private String blob;
private String blob; // action code
private BlockType bt;
private ASTNode tree;
private int dummyClassOffset;
private int cupOffset;
public int getDummyClassOffset() {
return dummyClassOffset;
}
public void setDummyClassOffset(int dummyClassOffset) {
this.dummyClassOffset = dummyClassOffset;
}
public int getCupOffset() {
return cupOffset;
}
public void setCupOffset(int cupOffset) {
this.cupOffset = cupOffset;
}
public CompilationUnit getCompilationUnit() {
return cu;
}
public IProblem[] getProblems() {
return problems;
}
public HashMap<IVariableBinding, LinkedList<SimpleName>> getVariableMap() {
return variableMap;
}
public LinkedList<SimpleName> getMethodList() {
return methodList;
}
public enum BlockType {
Action, Parser, Init, Scan, ActionCode
}
public int getDebugId() {
return debugId;
}
public String getBlob() {
return blob;
}
public BlockType getBt() {
return bt;
}
public abstract void parseJavaCode(String[] classpathEntries, ErrorManager errMan);
public CodeBlock(int debugId, BlockType bt, String blob, Range range) {
super(range);
this.debugId = debugId;
this.bt = bt;
this.blob = blob;
// this.tree = JavaCompiler.parseJava(blob, this);
// this.tree.accept(new LocalVariableDetector());
}
public HashSet<Integer> methodPositions()
{
public HashSet<Integer> methodPositions() {
HashSet<Integer> methodPositions = new HashSet<Integer>();
for (int i=0; i<this.methodList.size(); i++)
{
for (int i = 0; i < this.methodList.size(); i++) {
SimpleName elem = this.methodList.get(i);
methodPositions.add(new Integer(elem.getStartPosition() - dummyClassOffset));
}
......@@ -101,7 +53,7 @@ public abstract class CodeBlock extends AbstractNode {
public HashSet<Integer> variablePositions()
{
HashSet<Integer> variablePositions = new HashSet<Integer>();
for(Iterator iter = variableMap.keySet().iterator(); iter.hasNext();){
for(Iterator<IVariableBinding> iter = variableMap.keySet().iterator(); iter.hasNext();){
IVariableBinding binding = (IVariableBinding) iter.next();
//TODO: add position of declaration
ASTNode node = this.cu.findDeclaringNode(binding);
......@@ -110,24 +62,14 @@ public abstract class CodeBlock extends AbstractNode {
variablePositions.add(offset);
}
LinkedList<SimpleName> lst = variableMap.get(binding);
for(Iterator lstIter = lst.iterator(); lstIter.hasNext();){
for(Iterator<SimpleName> lstIter = lst.iterator(); lstIter.hasNext();){
SimpleName elem = (SimpleName) lstIter.next();
variablePositions.add(new Integer(elem.getStartPosition() - dummyClassOffset));
}
}
return variablePositions;
}
public BlockType getBlockType() {
return bt;
}
@Override
protected void putDescription(StringBuilder builder) {
builder.append(bt + " " + tree);
}
public abstract void parseJavaCode(String[] classpathEntries, ErrorManager errMan);
public void reportProblems(ErrorManager errMan){
IProblem[] problems = cu.getProblems();
for(IProblem problem : problems){
......@@ -138,4 +80,56 @@ public abstract class CodeBlock extends AbstractNode {
Position.fromOffset(problem.getSourceEnd() - dummyClassOffset+ cupOffset + 2));
}
}
public BlockType getBlockType() {
return bt;
}
@Override
protected void putDescription(StringBuilder builder) {
builder.append("{" + blob + "}");
}
public int getDummyClassOffset() {
return dummyClassOffset;
}
public void setDummyClassOffset(int dummyClassOffset) {
this.dummyClassOffset = dummyClassOffset;
}
public int getCupOffset() {
return cupOffset;
}
public void setCupOffset(int cupOffset) {
this.cupOffset = cupOffset;
}
public CompilationUnit getCompilationUnit() {
return cu;
}
public IProblem[] getProblems() {
return problems;
}
public HashMap<IVariableBinding, LinkedList<SimpleName>> getVariableMap() {
return variableMap;
}
public LinkedList<SimpleName> getMethodList() {
return methodList;
}
public int getDebugId() {
return debugId;
}
public String getBlob() {
return blob;
}
public BlockType getBt() {
return bt;
}
}
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.ErrorManager;
import de.in.tum.www2.cup.ErrorSource;
import de.in.tum.www2.cup.ast.CodeBlock.BlockType;
import org.eclipse.jdt.core.dom.ASTParser;
public class JavaCompiler {
// dummy class Strings
private static final String IMPORTS = "import java_cup.runtime.ComplexSymbolFactory.Location;\n";
private static final String DUMMY_CLASS_DEF = "public class DummyClass\n{";
private static final String PARSER_CLASS_DEF = "class Parser\n{";
private static final String RWING = "}";
private static final String DUMMY_METHOD_DEF = "\n\tpublic void dummymethod(";
private static final String SCAN_METHOD_DEF = "\n\tpublic java_cup.runtime.Symbol dummymethod(";
private static final String DUMMY_METHOD_ARGS_END = ")\n\t{";
private static final String DUMMY_CLASS_DEF_END = "\n" + RWING;
private static final String DUMMY_METHOD_END = "\n\t" + RWING;
/**
*
* @param userCode
* the codeblock's java code
* @param codeblock
* @param classpathEntries
* String-array containing a path to java-cup-runtime.jar
* @return the parsed ASTNode(CompilationUnit)
*/
public static ASTNode parseJava(String userCode, CodeBlock codeblock, String[] classpathEntries) {
public static ASTNode parseJava(CodeBlock codeblock, String[] classpathEntries) {
// set parsing to Java8
ASTParser parser = ASTParser.newParser(AST.JLS8);
ASTNode node;
String[] sourcepathEntries = new String[] { "" };
Map<String, String> options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
parser.setCompilerOptions(options);
// other parsing options
String[] sourcepathEntries = new String[] { "" };// may want to add as parameter
parser.setEnvironment(classpathEntries, sourcepathEntries, new String[] { "UTF-8" }, true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setUnitName("DummyClass");
parser.setResolveBindings(true);
String dummyClass = "";
final String IMPORTS = "import java_cup.runtime.ComplexSymbolFactory.Location;\n";
final String DUMMY_CLASS_DEF = "public class DummyClass\n{";
final String RWING = "}";
final String DUMMY_METHOD_DEF = "\n\tpublic void dummymethod(";
final String DUMMY_METHOD_ARGS_END = ")\n\t{";
final String DUMMY_CLASS_DEF_END = "\n" + RWING;
final String DUMMY_METHOD_END = "\n\t" + RWING;
ASTNode node;
StringBuilder sb = new StringBuilder();
String userCode = codeblock.getBlob();
switch (codeblock.getBt()) {
case Action:
case Parser:
dummyClass = DUMMY_CLASS_DEF + userCode + RWING;
codeblock.setDummyClassOffset(DUMMY_CLASS_DEF.length());
sb.append(DUMMY_CLASS_DEF);
codeblock.setDummyClassOffset(sb.length());
sb.append(userCode); // class body
sb.append(RWING);
break;
case Parser:
sb.append(PARSER_CLASS_DEF);
codeblock.setDummyClassOffset(sb.length());
sb.append(userCode);// Parser class body
sb.append(RWING);
break;
case Init:
sb.append(DUMMY_CLASS_DEF);
sb.append(DUMMY_METHOD_DEF);
sb.append(DUMMY_METHOD_ARGS_END);
codeblock.setDummyClassOffset(sb.length());
sb.append(userCode); // method body
sb.append(DUMMY_METHOD_END);
sb.append(DUMMY_CLASS_DEF_END);
break;
case Scan:
// TODO Add return Type for Scan with codeblock
dummyClass = DUMMY_CLASS_DEF + DUMMY_METHOD_DEF + DUMMY_METHOD_ARGS_END;
codeblock.setDummyClassOffset(dummyClass.length());
dummyClass += 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");
sb.append(DUMMY_CLASS_DEF);
sb.append(SCAN_METHOD_DEF);
sb.append(DUMMY_METHOD_ARGS_END);
sb.append(userCode); // scan method body
sb.append(DUMMY_METHOD_END);
sb.append(DUMMY_CLASS_DEF_END);
break;
case ActionCode:
ActionCodeBlock block = (ActionCodeBlock) codeblock;
sb.append(IMPORTS);
sb.append(DUMMY_CLASS_DEF);
sb.append(DUMMY_METHOD_DEF);
sb.append(block.getReturnType());
sb.append(" RESULT");
for (LabeledProductionSymbolRef l : block.getVariables()) {
// add dummy location variables as parameters
sb.append(", " + l.getDatatype() + " " + l.label);
sb.append(", " + "Location" + " " + l.label + "xleft");
sb.append(", " + "Location" + " " + l.label + "xright");
sb.append(", " + "int" + " " + l.label + "left");
sb.append(", " + "int" + " " + l.label + "right");
}
sb.append(DUMMY_METHOD_ARGS_END + "\n\t\t");
codeblock.setDummyClassOffset(sb.length());
sb.append(userCode); // action method body
sb.append(DUMMY_METHOD_END);
sb.append(DUMMY_CLASS_DEF_END);
break;
default:
throw new UnsupportedOperationException("java parsing not implemented: unknown blocktype");
}
parser.setSource(dummyClass.toCharArray());
// parse dummy class
parser.setSource(sb.toString().toCharArray());
node = parser.createAST(null);
return node;
}
......
......@@ -5,7 +5,6 @@ import org.eclipse.jdt.core.dom.CompilationUnit;
import de.in.tum.www2.cup.ErrorManager;
import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.analysis.AbstractVisitor;
import de.in.tum.www2.cup.analysis.LabelToVariableVisitor;
public class SpecialCodeBlock extends CodeBlock implements IProductionRightPart {
......@@ -33,9 +32,8 @@ public class SpecialCodeBlock extends CodeBlock implements IProductionRightPart
}
@Override
public void parseJavaCode(String[] classpathEntries, ErrorManager errMan) {
this.cu = (CompilationUnit) JavaCompiler.parseJava(getBlob(), this, classpathEntries);
public void parseJavaCode(String[] classpathEntries) {
this.cu = (CompilationUnit) JavaCompiler.parseJava(this, classpathEntries);
BindingCollector variableCollector = new BindingCollector();
this.cu.accept(variableCollector);
this.variableMap = variableCollector.getVariableMap();
......
......@@ -93,9 +93,11 @@ public final class PluginUtility {
final String PATH_TO_JAVA_CUP_RUNTIME = File.separator + "tools" + File.separator + "java-cup-11b-runtime.jar";
final String PATH_TO_ECLIPSE_APPLICATION = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString();
final String PROJECTNAME = ResourcesPlugin.getWorkspace().getRoot()
.getProjects(IWorkspaceRoot.INCLUDE_HIDDEN)[0].getFullPath().toOSString();// TODO how to get right
// project?
.getProjects(IWorkspaceRoot.INCLUDE_HIDDEN)[0].getFullPath().toOSString();
final String ret = PATH_TO_ECLIPSE_APPLICATION + PROJECTNAME + PATH_TO_JAVA_CUP_RUNTIME;
// this is a hack to get the path of java-cup-runtime.jar
// only works if the jar is located in the tools-folder of the first project in workspace
// TODO get java-cup-runtime-path
return new String[] { ret };
}
......
......@@ -15,7 +15,7 @@
<classpathentry kind="lib" path="org.eclipse.osgi_3.11.1.v20160708-1632.jar"/>
<classpathentry kind="lib" path="org.eclipse.osgi.services_3.5.100.v20160504-1419.jar"/>
<classpathentry kind="lib" path="org.eclipse.osgi.util_3.3.100.v20150423-1351.jar"/>
<classpathentry kind="lib" path="org.eclipse.text_3.6.0.v20160503-1849.jar"/>
<classpathentry kind="lib" path="CupParser.jar" sourcepath="/CupParser"/>
<classpathentry kind="lib" path="org.eclipse.text_3.6.0.v20160503-1849.jar"/>
<classpathentry kind="lib" path="/CupParser/bin/jar/CupParser.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
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