Commit 5573cba4 authored by baueko's avatar baueko
Browse files

added classpathEntries for java parsing

parent 3730f3c9
......@@ -30,7 +30,7 @@ public class CupParser {
parser.init(context.getErrorManager(), context, inputStream);
}
public ParserResult parse() throws Exception {
public ParserResult parse(String[] classpathEntries) throws Exception {
if (result != null)
return result;
try {
......@@ -42,7 +42,7 @@ public class CupParser {
return null;
}
result = parser.getResult();
result.parseJava();
result.parseJava(classpathEntries);
// TODO add java parsing call
return result;
}
......
......@@ -2,78 +2,22 @@
package de.in.tum.www2.cup;
import java.io.FileInputStream;
import java.util.Enumeration;
import java_cup.runtime.*;
import de.in.tum.www2.cup.*;
import de.in.tum.www2.cup.analysis.DeclarationsExtractorVisitor;
import de.in.tum.www2.cup.analysis.LocationPatchVisitor;
import de.in.tum.www2.cup.ast.*;
import de.in.tum.www2.cup.internal.emit;
import de.in.tum.www2.cup.ast.ParserResult;
class Demo
{
class Demo {
public static void main(String[] blah) throws Exception {
IErrorReporter er = new NoopErrorReporter();
FileInputStream input = new FileInputStream("/Users/jroith/testcup/test.cup");
CupParser parser = new CupParser(er, input);
ParserResult res = parser.parse();
CupContext context = parser.getContext();
FileInputStream input = new FileInputStream("E:\\Dropbox\\Studium\\WiSe16\\Praktikum\\Testdateien\\Input.cup");
// System.out.println(res);
CupParser parser = new CupParser(er, input);
System.out.println("\nDeclarationVisitor:");
Declarations oldDecl = null;
DeclarationsExtractorVisitor declVisitor = new DeclarationsExtractorVisitor();
res.accept(declVisitor, null);
Declarations newDecl = declVisitor.getResult();
if (!newDecl.equals(oldDecl)) {
System.out.println("Declarations have changed. -> rescan whole document.");
}
if (!newDecl.isDeclared ("BLAH"))
System.out.println("BLAH is not a declared keyword! :-(");
if (!newDecl.isDeclaredTerminal("QUESTION"))
System.out.println("QUESTION is a declared terminal! :-)");
System.out.println("\nComputing tables ...");
LALRResult lalrResult = LALRResult.Compute(context, context.start_production);
String[] classpathEntries = new String[] { "lib\\java-cup-11b-runtime.jar" };
ParserResult res = parser.parse(classpathEntries);
for (Conflict conflict : context.getConflictManager().getConflicts()) {
System.out.println(conflict);
}
// System.out.println(lalrResult.getActionTable());
// System.out.println(lalrResult.getReduceTable());
System.out.println(res);
/*lalr_state ordered[] = new lalr_state[lalr_state.number()];
for (Enumeration s = lalr_state.all(); s.hasMoreElements(); )
{
lalr_state st = (lalr_state)s.nextElement();
ordered[st.index()] = st;
}
System.err.println("===== Viable Prefix Recognizer =====");
for (int i = 0; i<lalr_state.number(); i++)
{
if (ordered[i] == start_state) System.err.print("START ");
System.err.println(ordered[i]);
System.err.println("-------------------");
}
}
*/
System.out.println("done.");
}
}
}
......@@ -4,12 +4,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;
public ParseJavaVisitor(String[] classpathEntries) {
this.classpathEntries = classpathEntries;
}
public void postVisit(ActionCodeBlock node, Object data) {
node.parseJavaCode();
node.parseJavaCode(classpathEntries);
};
public void postVisit(SpecialCodeBlock node, Object data) {
node.parseJavaCode();
node.parseJavaCode(classpathEntries);
};
}
......@@ -27,11 +27,11 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
}
@Override
public void parseJavaCode() {
public void parseJavaCode(String[] classpathEntries) {
//
LabelToVariableVisitor visitor = new LabelToVariableVisitor(this);
this.getParent().accept(visitor, null);
JavaCompiler.parseJava(getBlob(), this);
JavaCompiler.parseJava(getBlob(), this, classpathEntries);
}
public void addVariable(LabeledProductionSymbolRef l) {
......
......@@ -45,5 +45,5 @@ public abstract class CodeBlock extends AbstractNode {
builder.append(bt + " " + tree);
}
public abstract void parseJavaCode();
public abstract void parseJavaCode(String[] classpathEntries);
}
......@@ -5,17 +5,33 @@ import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
public class JavaCompiler {
public static ASTNode parseJava(String userCode, CodeBlock codeblock) {
/**
*
* @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) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
ASTNode node;
parser.setEnvironment(new String[] { "" }, new String[] { "" }, new String[] { "UTF-8" }, true);
// String[] classpathEntries = new String[] { "lib\\java-cup-11b-runtime.jar" };// jars
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);
String dummyClass = "";
// File f = new File("");
// System.out.println(f.getAbsolutePath());
// C:\Users\Konstantin\git\cup-eclipse-plugin\CupParser
final String IMPORTS = "import java_cup.runtime.ComplexSymbolFactory.Location;";
final String DUMMY_CLASS_DEF = "public class DummyClass\n{";
final String RWING = "}";
final String DUMMY_METHOD_DEF = "\n\tpublic void dummymethod(";
......@@ -35,13 +51,13 @@ public class JavaCompiler {
break;
case ActionCode:
ActionCodeBlock block = (ActionCodeBlock) codeblock;
dummyClass = DUMMY_CLASS_DEF + DUMMY_METHOD_DEF;
// do we need a return type?
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 += ", " + "Object" + " " + l.label + "xleft"; // TODO Object should really be a Location
dummyClass += ", " + "Object" + " " + l.label + "xright"; // TODO Object should really be a Location
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";
}
......
......@@ -109,8 +109,8 @@ public class ParserResult extends AbstractNode {
throw new RuntimeException("not yet implemented.");
}
public void parseJava() {
ParseJavaVisitor visitor = new ParseJavaVisitor();
public void parseJava(String[] classpathEntries) {
ParseJavaVisitor visitor = new ParseJavaVisitor(classpathEntries);
this.accept(visitor, null);
}
......
......@@ -29,7 +29,7 @@ public class SpecialCodeBlock extends CodeBlock implements IProductionRightPart
}
@Override
public void parseJavaCode() {
public void parseJavaCode(String[] classpathEntries) {
// TODO Auto-generated method stub
}
......
Supports Markdown
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