Commit ddedca0e authored by baueko's avatar baueko

added visitors for java parsing

parent 3c19aa31
......@@ -3,48 +3,48 @@ package de.in.tum.www2.cup;
import java.io.InputStream;
import de.in.tum.www2.cup.ast.*;
import de.in.tum.www2.cup.ast.ParserResult;
import de.in.tum.www2.cup.internal.Parser;
import de.in.tum.www2.cup.internal.ParserAbortException;
public class CupParser
{
private CupContext context;
private Parser parser;
private ParserResult result;
public ParserResult getResult() {
return result;
}
public boolean hasParseErrors() {
return context.getErrorManager().hasParseErrors();
}
public CupContext getContext() {
return context;
}
public CupParser(IErrorReporter er, InputStream inputStream) {
context = new CupContext(er);
parser = new Parser();
parser.init(context.getErrorManager(), context, inputStream);
}
public ParserResult parse() throws Exception {
if (result != null)
return result;
try {
parser.parse();
} catch (ParserAbortException e) {
context.getErrorManager().Fatal(ErrorSource.Parser, "PARSER ABORT!");
System.out.println("PARSER ABORT!");
this.context = null;
return null;
}
result = parser.getResult();
return result;
}
public class CupParser {
private CupContext context;
private Parser parser;
private ParserResult result;
public ParserResult getResult() {
return result;
}
public boolean hasParseErrors() {
return context.getErrorManager().hasParseErrors();
}
public CupContext getContext() {
return context;
}
public CupParser(IErrorReporter er, InputStream inputStream) {
context = new CupContext(er);
parser = new Parser();
parser.init(context.getErrorManager(), context, inputStream);
}
public ParserResult parse() throws Exception {
if (result != null)
return result;
try {
parser.parse();
} catch (ParserAbortException e) {
context.getErrorManager().Fatal(ErrorSource.Parser, "PARSER ABORT!");
System.out.println("PARSER ABORT!");
this.context = null;
return null;
}
result = parser.getResult();
result.parseJava();
// TODO add java parsing call
return result;
}
}
package de.in.tum.www2.cup.analysis;
import de.in.tum.www2.cup.ast.ActionCodeBlock;
import de.in.tum.www2.cup.ast.IProductionRightPart;
import de.in.tum.www2.cup.ast.LabeledProductionSymbolRef;
import de.in.tum.www2.cup.ast.ProductionRight;
public class LabelToVariableVisitor extends Visitor<Object> {
ActionCodeBlock block;
public LabelToVariableVisitor(ActionCodeBlock block) {
this.block = block;
}
@Override
public void postVisit(ProductionRight node, Object data) {
for (IProductionRightPart part : node.getList()) {
if (part instanceof LabeledProductionSymbolRef) {
block.addVariable((LabeledProductionSymbolRef) part);
}
}
}
}
package de.in.tum.www2.cup.analysis;
import de.in.tum.www2.cup.ast.ActionCodeBlock;
import de.in.tum.www2.cup.ast.SpecialCodeBlock;
public class ParseJavaVisitor extends Visitor<Object> {
public void postVisit(ActionCodeBlock node, Object data) {
node.parseJavaCode();
};
public void postVisit(SpecialCodeBlock node, Object data) {
node.parseJavaCode();
};
}
package de.in.tum.www2.cup.ast;
import java.util.LinkedList;
import java.util.List;
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 ActionCodeBlock extends CodeBlock implements IProductionRightPart {
private List<LabeledProductionSymbolRef> variables = new LinkedList<>();
public ActionCodeBlock(int debugId, BlockType bt, String blob, Range range) {
super(debugId, bt, blob, range);
......@@ -19,4 +24,20 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
protected String getNodeName() {
return "ActionCodeBlock";
}
@Override
public void parseJavaCode() {
//
LabelToVariableVisitor visitor = new LabelToVariableVisitor(this);
this.getParent().accept(visitor, null);
}
public void addVariable(LabeledProductionSymbolRef l) {
variables.add(l);
}
public List<LabeledProductionSymbolRef> getVariables() {
return variables;
}
}
......@@ -32,8 +32,8 @@ public abstract class CodeBlock extends AbstractNode {
this.debugId = debugId;
this.bt = bt;
this.blob = blob;
this.tree = JavaCompiler.parseJava(blob, this);
this.tree.accept(new LocalVariableDetector());
// this.tree = JavaCompiler.parseJava(blob, this);
// this.tree.accept(new LocalVariableDetector());
}
public BlockType getBlockType() {
......@@ -44,4 +44,6 @@ public abstract class CodeBlock extends AbstractNode {
protected void putDescription(StringBuilder builder) {
builder.append(bt + " " + tree);
}
public abstract void parseJavaCode();
}
package de.in.tum.www2.cup.ast;
import de.in.tum.www2.cup.analysis.AbstractVisitor;
import de.in.tum.www2.cup.Range;
public class LabeledProductionSymbolRef extends ProductionSymbolRef
{
public class LabeledProductionSymbolRef extends ProductionSymbolRef {
public String label; // TODO: Name class?
private String datatype;
public LabeledProductionSymbolRef(Name name, String label, Range range) {
public String getDatatype() {
return datatype;
}
public void setDatatype(String datatype) {
this.datatype = datatype;
}
public LabeledProductionSymbolRef(Name name, String label, String datatype, Range range) {
super(name, range);
this.label = label;
this.datatype = datatype;
}
@Override protected String getNodeName() { return "LabeledProductionRef"; }
@Override
protected String getNodeName() {
return "LabeledProductionRef";
}
@Override
protected void putDescription(StringBuilder builder) {
......@@ -20,5 +31,5 @@ public class LabeledProductionSymbolRef extends ProductionSymbolRef
builder.append(" | ");
super.putDescription(builder);
}
}
......@@ -9,6 +9,7 @@ import de.in.tum.www2.cup.Position;
import de.in.tum.www2.cup.analysis.AbstractVisitor;
import de.in.tum.www2.cup.analysis.DeclarationsExtractorVisitor;
import de.in.tum.www2.cup.analysis.FindAtPositionVisitor;
import de.in.tum.www2.cup.analysis.ParseJavaVisitor;
import de.in.tum.www2.cup.analysis.RefResolutionVisitor;
import de.in.tum.www2.cup.internal.production;
......@@ -108,6 +109,11 @@ public class ParserResult extends AbstractNode {
throw new RuntimeException("not yet implemented.");
}
public void parseJava() {
ParseJavaVisitor visitor = new ParseJavaVisitor();
this.accept(visitor, null);
}
@Override
public <T> void accept(AbstractVisitor<T> visitor, T data) {
T childArg = visitor.preVisit(this, data);
......
......@@ -27,4 +27,10 @@ public class SpecialCodeBlock extends CodeBlock implements IProductionRightPart
builder.append("|");
super.putDescription(builder);
}
@Override
public void parseJavaCode() {
// TODO Auto-generated method stub
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<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="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<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="output" path="bin"/>
</classpath>
......@@ -3,7 +3,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry exported="true" kind="lib" path="java-cup-11b-runtime.jar" sourcepath="/CUP/src"/>
<classpathentry exported="true" kind="lib" path="CupParser.jar" sourcepath="/CupParser"/>
<classpathentry kind="lib" path="org.eclipse.core.contenttype_3.5.100.v20160418-1621.jar"/>
<classpathentry kind="lib" path="org.eclipse.core.jobs_3.8.0.v20160509-0411.jar"/>
<classpathentry kind="lib" path="org.eclipse.core.resources_3.11.0.v20160503-1608.jar"/>
......@@ -17,5 +16,7 @@
<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="CupParser.jar" sourcepath="/CupParser"/>
<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