Commit 39dcc956 authored by baueko's avatar baueko

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

Conflicts:
	CupReferencedLibraries/.classpath
parents 11fb0820 b995ccb9
......@@ -6,7 +6,6 @@ import de.in.tum.www2.cup.ast.LabeledProductionSymbolRef;
import de.in.tum.www2.cup.ast.Production;
import de.in.tum.www2.cup.ast.ProductionRight;
//TODO rename
public class LabelToVariableVisitor extends Visitor<Object> {
ActionCodeBlock block;
......
......@@ -32,13 +32,14 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
public void parseJavaCode(String[] classpathEntries) {
LabelToVariableVisitor visitor = new LabelToVariableVisitor(this);
this.getParent().accept(visitor, null);
CompilationUnit cu = (CompilationUnit) JavaCompiler.parseJava(getBlob(), this, classpathEntries);
this.cu = (CompilationUnit) JavaCompiler.parseJava(getBlob(), this, classpathEntries);
BindingCollector variableCollector = new BindingCollector();
cu.accept(variableCollector);
this.problems = cu.getProblems();
this.cu.accept(variableCollector);
this.problems = this.cu.getProblems();
this.variableMap = variableCollector.getVariableMap();
this.methodList = variableCollector.getMethodList();
this.setCupOffset(this.getRange().getBegin().getOffsetFromStart());
}
public void addVariable(LabeledProductionSymbolRef l) {
......
package de.in.tum.www2.cup.ast;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import org.eclipse.jdt.core.compiler.IProblem;
......@@ -16,21 +18,35 @@ public abstract class CodeBlock extends AbstractNode {
protected IProblem[] problems;
protected HashMap<IVariableBinding, LinkedList<SimpleName>> variableMap;
protected LinkedList<SimpleName> methodList;
private CompilationUnit cu;
protected CompilationUnit cu;
private int debugId;
private String blob;
private BlockType bt;
private ASTNode tree;
private int dummyClassOffset;
private int cupOffset;
public int getDummyClassOffset() {
return dummyClassOffset;
}
public CompilationUnit getCompilationUnit() {
return cu;
public void setDummyClassOffset(int dummyClassOffset) {
this.dummyClassOffset = dummyClassOffset;
}
public void setCompilationUnit(CompilationUnit cu) {
this.cu = cu;
public int getCupOffset() {
return cupOffset;
}
public void setCupOffset(int cupOffset) {
this.cupOffset = cupOffset;
}
public CompilationUnit getCompilationUnit() {
return cu;
}
public IProblem[] getProblems() {
return problems;
}
......@@ -67,7 +83,32 @@ public abstract class CodeBlock extends AbstractNode {
// this.tree = JavaCompiler.parseJava(blob, this);
// this.tree.accept(new LocalVariableDetector());
}
public HashSet<Integer> methodPositions()
{
HashSet<Integer> methodPositions = new HashSet<Integer>();
for (int i=0; i<this.methodList.size(); i++)
{
SimpleName elem = this.methodList.get(i);
methodPositions.add(new Integer(elem.getStartPosition() - dummyClassOffset));
}
return methodPositions;
}
public HashSet<Integer> variablePositions()
{
HashSet<Integer> variablePositions = new HashSet<Integer>();
for(Iterator iter = variableMap.keySet().iterator(); iter.hasNext();){
IVariableBinding binding = (IVariableBinding) iter.next();
//TODO: add position of
LinkedList<SimpleName> lst = variableMap.get(binding);
for(Iterator lstIter = lst.iterator(); lstIter.hasNext();){
SimpleName elem = (SimpleName) lstIter.next();
variablePositions.add(new Integer(elem.getStartPosition() - dummyClassOffset));
}
}
return variablePositions;
}
public BlockType getBlockType() {
return bt;
}
......
package de.in.tum.www2.cup.ast;
import java.util.HashMap;
import java.util.LinkedList;
import org.eclipse.jdt.core.compiler.IProblem;
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;
public class JavaCompiler {
import de.in.tum.www2.cup.ast.CodeBlock.BlockType;
public class JavaCompiler {
/**
*
* @param userCode
......@@ -41,32 +56,33 @@ public class JavaCompiler {
break;
case Init:
case Scan:
// TODO return
// 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";
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;
}
......
......@@ -34,13 +34,12 @@ public class SpecialCodeBlock extends CodeBlock implements IProductionRightPart
@Override
public void parseJavaCode(String[] classpathEntries) {
CompilationUnit cu = (CompilationUnit) JavaCompiler.parseJava(getBlob(), this, classpathEntries);
this.cu = (CompilationUnit) JavaCompiler.parseJava(getBlob(), this, classpathEntries);
BindingCollector variableCollector = new BindingCollector();
cu.accept(variableCollector);
this.cu.accept(variableCollector);
this.problems = cu.getProblems();
this.variableMap = variableCollector.getVariableMap();
this.methodList = variableCollector.getMethodList();
this.setCompilationUnit(cu);
}
}
......@@ -3,6 +3,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="/CupParser/bin/jar/CupParser.jar"/>
<classpathentry kind="lib" path="/CupParser/bin/jar/CupParser.jar" sourcepath="/CupParser/src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -7,6 +7,10 @@ import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.ITokenScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.swt.custom.StyleRange;
import de.tum.in.www2.cupplugin.syntax.CupTokenScanner;
import de.tum.in.www2.cupplugin.syntax.JavaTokenScanner;
public class CupDamagerRepairer extends DefaultDamagerRepairer {
......@@ -67,7 +71,7 @@ public class CupDamagerRepairer extends DefaultDamagerRepairer {
length= fScanner.getTokenLength();
//}
}
addRange(presentation, lastStart, length, lastAttribute);
}
}
......@@ -84,6 +84,8 @@ public class CupTextEditor extends TextEditor
public static final Color JAVA_TEXTUAL = brightBlue;
public static final Color JAVA_CUP_DEFINED = blueGreen;
public static final Color JAVA_NUMBER = purple;
public static final Color JAVA_VARIABLE = blue;
public CupTextEditor(Jumper jumper) {
super();
......
package de.tum.in.www2.cupplugin.syntax;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.stream.IntStream;
import org.eclipse.jdt.core.ToolFactory;
......@@ -16,11 +18,13 @@ import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.ITokenScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import de.in.tum.www2.cup.Position;
import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.ast.AbstractNode;
import de.in.tum.www2.cup.ast.ActionCodeBlock;
import de.in.tum.www2.cup.ast.CodeBlock;
import de.in.tum.www2.cup.ast.IProductionRightPart;
import de.in.tum.www2.cup.ast.LabeledProductionSymbolRef;
import de.in.tum.www2.cup.ast.ParserResult;
......@@ -36,6 +40,9 @@ public class JavaTokenScanner implements ITokenScanner {
private int offset;
private Range previousCodeBlockRange;
private HashSet<Integer> methodSet = new HashSet<Integer>();
private HashSet<Integer> variableSet = new HashSet<Integer>();
private int[] keywords = {
ITerminalSymbols.TokenNameabstract,
ITerminalSymbols.TokenNameassert,
......@@ -91,7 +98,7 @@ public class JavaTokenScanner implements ITokenScanner {
ITerminalSymbols.TokenNamefalse,
ITerminalSymbols.TokenNamenull};
private HashSet<String> currentLabels = null;
public JavaTokenScanner() {
......@@ -114,9 +121,14 @@ public class JavaTokenScanner implements ITokenScanner {
previousCodeBlockRange = node.getRange();
if (node instanceof ActionCodeBlock) {
if (node instanceof ActionCodeBlock) {
ActionCodeBlock codeBlock = (ActionCodeBlock) node;
methodSet = codeBlock.methodPositions();
variableSet = codeBlock.variablePositions();
AbstractNode parent = codeBlock.getParent();
if (parent instanceof ProductionRight) {
......@@ -192,10 +204,14 @@ public class JavaTokenScanner implements ITokenScanner {
static final Token javaNumberLiteral = new Token(new TextAttribute(
CupTextEditor.JAVA_NUMBER));
static final Token javaMethod = new Token(new TextAttribute(null, null, SWT.ITALIC));
static final Token javaVariable = new Token(new TextAttribute(CupTextEditor.JAVA_VARIABLE));
static final Token cupDefinedVariable = new Token(new TextAttribute(
CupTextEditor.JAVA_CUP_DEFINED, null, SWT.ITALIC));
public IToken nextToken() {
int token;
try {
......@@ -228,17 +244,24 @@ public class JavaTokenScanner implements ITokenScanner {
|| token == ITerminalSymbols.TokenNameLongLiteral)
return javaNumberLiteral;
// TODO: highlight cupVariables
if((new String(scanner.getCurrentTokenSource())).equals("RESULT")){
return cupDefinedVariable;
if(token == ITerminalSymbols.TokenNameIdentifier){
if((new String(scanner.getCurrentTokenSource())).equals("RESULT")){
return cupDefinedVariable;
}
String name = new String(scanner.getCurrentTokenSource()) ;
// - 2 because scanner works with "{:JAVACODE:}" and parser with "JAVACODE"
int offset = new Integer(scanner.getCurrentTokenStartPosition() - 2 );
if(methodSet.contains(offset)){
return javaMethod;
}
if(variableSet.contains(offset)){
//TODO: differentiate between javaVars and cupVars
return javaVariable;
}
}
// if (last.getValue() instanceof String) {
// String lastValueStr = (String) last.getValue();
// if (lastValueStr.equals("RESULT")
// || currentLabels.contains(lastValueStr))
// return cupDefinedVariable;
// }
return Token.UNDEFINED;
}
......
......@@ -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/bin/jar/CupParser.jar"/>
<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