Commit 346b473d authored by baueko's avatar baueko

renamed JavaCompiler, added classpathentries to CupParser

parent f53a2c64
......@@ -12,6 +12,30 @@ public class CupParser {
private Parser parser;
private ParserResult result;
private static String[] classpathEntries = new String[] { "" };
private static String[] sourcepathEntries = new String[] { "" };
public static String[] getClasspathEntries() {
return classpathEntries;
}
public static String[] getSourcepathEntries() {
return sourcepathEntries;
}
/**
* Should contain a path to java-cup-runtime-jar
*
* @param classpathEntries
*/
public static void setClasspathEntries(String[] classpathEntries) {
CupParser.classpathEntries = classpathEntries;
}
public static void setSourcepathEntries(String[] sourcepathEntries) {
CupParser.sourcepathEntries = sourcepathEntries;
}
public ParserResult getResult() {
return result;
}
......@@ -30,7 +54,7 @@ public class CupParser {
parser.init(context.getErrorManager(), context, inputStream);
}
public ParserResult parse(String[] classpathEntries) throws Exception {
public ParserResult parse() throws Exception {
if (result != null)
return result;
try {
......@@ -42,7 +66,7 @@ public class CupParser {
return null;
}
result = parser.getResult();
result.parseJava(classpathEntries, context.getErrorManager());
result.parseJava(context.getErrorManager());
return result;
}
......
......@@ -5,18 +5,17 @@ import de.in.tum.www2.cup.ast.ActionCodeBlock;
import de.in.tum.www2.cup.ast.SpecialCodeBlock;
public class ParseJavaVisitor extends Visitor<Object> {
private final String[] classpathEntries;
private ErrorManager errMan;
public ParseJavaVisitor(String[] classpathEntries, ErrorManager errMan) {
this.classpathEntries = classpathEntries;
public ParseJavaVisitor(ErrorManager errMan) {
this.errMan = errMan;
}
public void postVisit(ActionCodeBlock node, Object data) {
node.parseJavaCode(classpathEntries, errMan);
node.parseJavaCode(errMan);
};
public void postVisit(SpecialCodeBlock node, Object data) {
node.parseJavaCode(classpathEntries, errMan);
node.parseJavaCode(errMan);
};
}
......@@ -16,12 +16,12 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
public List<String> getVariableLabels() {
List<String> varLabels = new LinkedList<String>();
for(LabeledProductionSymbolRef node : variableLabels){
for (LabeledProductionSymbolRef node : variableLabels) {
varLabels.add(node.label);
varLabels.add(node.label +"xleft");
varLabels.add(node.label +"xright");
varLabels.add(node.label +"left");
varLabels.add(node.label +"right");
varLabels.add(node.label + "xleft");
varLabels.add(node.label + "xright");
varLabels.add(node.label + "left");
varLabels.add(node.label + "right");
}
return varLabels;
}
......@@ -42,11 +42,10 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
}
@Override
public void parseJavaCode(String[] classpathEntries, ErrorManager errMan) {
public void parseJavaCode(ErrorManager errMan) {
CupVariableLocator visitor = new CupVariableLocator(this);
this.getParent().accept(visitor, null);
this.cu = (CompilationUnit) JavaCompiler.parseJava(this, classpathEntries);
this.cu = (CompilationUnit) JavaParser.parseJava(this);
BindingCollector variableCollector = new BindingCollector();
this.cu.accept(variableCollector);
this.variableMap = variableCollector.getVariableMap();
......
......@@ -33,7 +33,7 @@ public abstract class CodeBlock extends AbstractNode {
private int dummyClassOffset;
private int cupOffset;
public abstract void parseJavaCode(String[] classpathEntries, ErrorManager errMan);
public abstract void parseJavaCode(ErrorManager errMan);
public CodeBlock(int debugId, BlockType bt, String blob, Range range) {
super(range);
......@@ -43,7 +43,8 @@ public abstract class CodeBlock extends AbstractNode {
}
/**
* Returns a HashSet with all valid method startPositions as Integer Offset from start of CodeBlock
* Returns a HashSet with all valid method startPositions as Integer Offset from start of CodeBlock
*
* @return
*/
public HashSet<Integer> methodPositions() {
......@@ -54,50 +55,52 @@ public abstract class CodeBlock extends AbstractNode {
}
return methodPositions;
}
/**
* Returns a HashSet with all valid variable startPositions as Integer Offset from start of CodeBlock
* Returns a HashSet with all valid variable startPositions as Integer Offset from start of CodeBlock
*
* @return
*/
public HashSet<Integer> variablePositions()
{
public HashSet<Integer> variablePositions() {
HashSet<Integer> variablePositions = new HashSet<Integer>();
for(Iterator<IVariableBinding> 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
// TODO: add position of declaration
ASTNode node = this.cu.findDeclaringNode(binding);
Integer offset = new Integer (node.getStartPosition() - dummyClassOffset);
if(offset > 0){
Integer offset = new Integer(node.getStartPosition() - dummyClassOffset);
if (offset > 0) {
variablePositions.add(offset);
}
LinkedList<SimpleName> lst = variableMap.get(binding);
for(Iterator<SimpleName> 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;
}
/**
* Report Java ParseProblems of this CodeBlock
* Currently not functional because ErrorManager needs Position Objects with absolute column and lineNumbers not Offsets
* Report Java ParseProblems of this CodeBlock Currently not functional because ErrorManager needs Position Objects
* with absolute column and lineNumbers not Offsets
*
* @param errMan
*/
public void reportProblems(ErrorManager errMan){
public void reportProblems(ErrorManager errMan) {
IProblem[] problems = cu.getProblems();
for(IProblem problem : problems){
//Adding 2 here is a hack: "{:"
//TODO: Position needs line and column number
errMan.Warning(ErrorSource.Parser, problem.getMessage(),
Position.fromOffset(problem.getSourceStart() - dummyClassOffset+ cupOffset + 2),
Position.fromOffset(problem.getSourceEnd() - dummyClassOffset+ cupOffset + 2));
for (IProblem problem : problems) {
// Adding 2 here is a hack: "{:"
// TODO: Position needs line and column number
errMan.Warning(ErrorSource.Parser, problem.getMessage(),
Position.fromOffset(problem.getSourceStart() - dummyClassOffset + cupOffset + 2),
Position.fromOffset(problem.getSourceEnd() - dummyClassOffset + cupOffset + 2));
}
}
public BlockType getBlockType() {
return bt;
}
@Override
protected void putDescription(StringBuilder builder) {
builder.append("{" + blob + "}");
......
......@@ -5,9 +5,14 @@ import java.util.Map;
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.ASTParser;
public class JavaCompiler {
import de.in.tum.www2.cup.CupParser;
/**
* Class for java parsing
*/
public class JavaParser {
// 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{";
......@@ -20,20 +25,21 @@ public class JavaCompiler {
private static final String DUMMY_METHOD_END = "\n\t" + RWING;
/**
* Method for java parsing. Creates a dummy class and dummy method if necessary.
*
* @param codeblock
* @param classpathEntries
* String-array containing a path to java-cup-runtime.jar
* this codeblock's action code will be parsed
* @return the parsed ASTNode(CompilationUnit)
*/
public static ASTNode parseJava(CodeBlock codeblock, String[] classpathEntries) {
public static ASTNode parseJava(CodeBlock codeblock) {
// set parsing to Java8
ASTParser parser = ASTParser.newParser(AST.JLS8);
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.setEnvironment(CupParser.getClasspathEntries(), CupParser.getSourcepathEntries(),
new String[] { "UTF-8" }, true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setUnitName("DummyClass");
parser.setResolveBindings(true);
......@@ -49,7 +55,7 @@ public class JavaCompiler {
sb.append(userCode); // class body
sb.append(RWING);
break;
case Parser:
case Parser:
sb.append(PARSER_CLASS_DEF);
codeblock.setDummyClassOffset(sb.length());
sb.append(userCode);// Parser class body
......@@ -65,7 +71,7 @@ public class JavaCompiler {
sb.append(DUMMY_CLASS_DEF_END);
break;
case Scan:
sb.append(DUMMY_CLASS_DEF);
sb.append(SCAN_METHOD_DEF);
sb.append(DUMMY_METHOD_ARGS_END);
......
......@@ -110,8 +110,8 @@ public class ParserResult extends AbstractNode {
throw new RuntimeException("not yet implemented.");
}
public void parseJava(String[] classpathEntries, ErrorManager errMan) {
ParseJavaVisitor visitor = new ParseJavaVisitor(classpathEntries, errMan);
public void parseJava(ErrorManager errMan) {
ParseJavaVisitor visitor = new ParseJavaVisitor(errMan);
this.accept(visitor, null);
}
......
......@@ -32,16 +32,16 @@ public class SpecialCodeBlock extends CodeBlock implements IProductionRightPart
}
@Override
public void parseJavaCode(String[] classpathEntries, ErrorManager errMan) {
//Parse JavaCode to CompilationUnit
this.cu = (CompilationUnit) JavaCompiler.parseJava(this, classpathEntries);
//Collect variable and method Bindings
public void parseJavaCode(ErrorManager errMan) {
// Parse JavaCode to CompilationUnit
this.cu = (CompilationUnit) JavaParser.parseJava(this);
// Collect variable and method Bindings
BindingCollector variableCollector = new BindingCollector();
this.cu.accept(variableCollector);
this.variableMap = variableCollector.getVariableMap();
this.methodList = variableCollector.getMethodList();
this.setCupOffset(this.getRange().getBegin().getOffsetFromStart());
//Report Java parseProblems
// Report Java parseProblems
reportProblems(errMan);
}
}
......@@ -104,7 +104,8 @@ public class DocumentDidChangeJob extends Job {
try {
parserDate = new Date();
long before = tmxb.getCurrentThreadCpuTime();
result = parser.parse(PluginUtility.createClasspath());
CupParser.setClasspathEntries(PluginUtility.createClasspath());
result = parser.parse();
parseTimeNano = tmxb.getCurrentThreadCpuTime() - before;
} catch (Exception e1) {
e1.printStackTrace();
......
......@@ -156,7 +156,8 @@ public class Debugger {
InputStream in = new ByteArrayInputStream(document.get().getBytes());
CupParser parser = new CupParser(new NoopErrorReporter(), in);
try {
selfBuiltAstCache = parser.parse(PluginUtility.createClasspath());
CupParser.setClasspathEntries(PluginUtility.createClasspath());
selfBuiltAstCache = parser.parse();
// TODO: manage revisions properly?
long revision = RevisionManager.increment(document);
Model.getInstanceForDocument(document).setASTModel(selfBuiltAstCache, parser.getContext(), revision);
......
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