Commit 9545fc93 authored by Clemens Pflaum's avatar Clemens Pflaum

Merge remote-tracking branch 'origin/clemens'

Conflicts:
	CupParser/.classpath
	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/JavaParser.java
	CupParser/src/de/in/tum/www2/cup/ast/JavaSemantics.java
	CupReferencedLibraries/.classpath
	CupReferencedLibraries/META-INF/MANIFEST.MF
	CupReferencedLibraries/build.properties
parents b7f7883f 8550a5ab
......@@ -77,3 +77,6 @@ Network Trash Folder
Temporary Items
.apdisk
/CupParser/src/de/in/tum/www2/cup/Demo.java
\ No newline at end of file
......@@ -3,8 +3,19 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry exported="true" kind="lib" path="lib"/>
<classpathentry exported="true" kind="lib" path="lib/java-cup-11b-runtime.jar"/>
<classpathentry exported="true" kind="lib" path="lib/Mouse-1.7.runtime.jar"/>
<classpathentry exported="true" kind="lib" path="lib/Mouse-1.7.jar"/>
<classpathentry exported="true" kind="lib" path="lib/java-cup-11b-runtime.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.contenttype_3.5.100.v20160418-1621.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.jobs_3.8.0.v20160509-0411.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.resources_3.11.0.v20160503-1608.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.resources.win32.x86_64_3.5.0.v20140124-1940.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.core.runtime_3.12.0.v20160606-1342.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.equinox.common_3.8.0.v20160509-1230.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.equinox.preferences_3.6.1.v20160815-1406.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.jdt_3.12.1.v20160907-1200.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.jdt.core_3.12.1.v20160829-0950.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.osgi_3.11.1.v20160708-1632.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.osgi.services_3.5.100.v20160504-1419.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.osgi.util_3.3.100.v20150423-1351.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.text_3.6.0.v20160503-1849.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -775,7 +775,8 @@ production ::=
new Name(
(String) lhs_id,
Range.fromLocations(lhs_idxleft, lhs_idxright)
),
),
((symbol_part) this.symbols.get(lhs_id))._the_symbol._stack_type,
(List<ProductionRight>) rhs,
Range.fromLocations(lhs_idxleft, sxright)
)
......@@ -964,7 +965,7 @@ prod_part ::=
if (labid == null)
ref = new ProductionSymbolRef(new Name(symid, range), (Range) range.clone());
else
ref = new LabeledProductionSymbolRef(new Name(symid, range), labid, (Range) range.clone());
ref = new LabeledProductionSymbolRef(new Name(symid, range), labid,((symbol_part) this.symbols.get(symid))._the_symbol._stack_type, (Range) range.clone());
ast_add_rhs_part(ref);
}
:}
......
......@@ -3,48 +3,70 @@ 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;
}
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
*/
public static void setClasspathEntries(String[] classpathEntries) {
CupParser.classpathEntries = classpathEntries;
}
public static void setSourcepathEntries(String[] sourcepathEntries) {
CupParser.sourcepathEntries = sourcepathEntries;
}
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(context.getErrorManager());
return result;
}
}
package de.in.tum.www2.cup;
import java.io.File;
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();
// System.out.println(res);
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);
for (Conflict conflict : context.getConflictManager().getConflicts()) {
System.out.println(conflict);
}
// System.out.println(lalrResult.getActionTable());
// System.out.println(lalrResult.getReduceTable());
/*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.");
}
IErrorReporter er = new NoOpErrorReporter();
FileInputStream input = new FileInputStream("E:/Dropbox/Studium/WiSe16/Praktikum/Testdateien/Input.cup");
CupParser parser = new CupParser(er, input);
CupParser.setClasspathEntries(createClasspath());
ParserResult res = parser.parse();
System.out.println(res);
}
public static String[] createClasspath() {
final String PATH_TO_JAVA_CUP_RUNTIME = "tools" + File.separator + "java-cup-11b-runtime.jar";
// final String PATH_TO_JAVA_LANG = "C:\\Program Files\\Java\\jre1.8.0_45\\lib\\rt.jar";
return new String[] { PATH_TO_JAVA_CUP_RUNTIME/* , PATH_TO_JAVA_LANG */ };
}
}
package de.in.tum.www2.cup;
public class NoopErrorReporter implements IErrorReporter
public class NoOpErrorReporter implements IErrorReporter
{
public void report(ErrorType type, ErrorSource source, ErrorCode errorCode,
String message, Position start, Position end)
{
System.out.println("NoOp: you have an Error: \n" +
"start:"+start+" end:"+end+" " + message);
}
}
......
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.Production;
import de.in.tum.www2.cup.ast.ProductionRight;
/**
* 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 CupVariableLocator(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);
}
}
Production p = (Production) node.getParent();
block.setReturnType(p.getReturnType());
}
}
......@@ -17,8 +17,7 @@ import de.in.tum.www2.cup.ast.TerminalDeclaration;
import de.in.tum.www2.cup.ast.TypedNonTerminalDeclaration;
import de.in.tum.www2.cup.ast.TypedTerminalDeclaration;
public class DeclarationsExtractorVisitor extends Visitor<Object>
{
public class DeclarationsExtractorVisitor extends Visitor<Object> {
private Declarations result = new Declarations();
public Declarations getResult() {
......@@ -26,10 +25,10 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
}
@Override
public void postVisit (ProductionRight node, Object data) {
public void postVisit(ProductionRight node, Object data) {
Production parent = (Production) node.getParent();
int index=0;
int index = 0;
for (ProductionRight pr : parent.getRightHandSides()) {
if (pr == node)
break;
......@@ -40,19 +39,19 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
if (parent.getName() == null)
return;
builder.append (parent.getName().name);
builder.append ("::");
builder.append (parent.getRightHandSides().size());
builder.append ("::");
builder.append (index);
builder.append(parent.getName().name);
builder.append("::");
builder.append(parent.getRightHandSides().size());
builder.append("::");
builder.append(index);
String key = builder.toString();
HashMap<String,HashSet<String>> locals = result.getLocals();
HashMap<String, HashSet<String>> locals = result.getLocals();
if (!locals.containsKey(key))
locals.put(key, new HashSet<String>());
HashSet<String> set = locals.get(key);
for (IProductionRightPart part : node.getList()) {
......@@ -61,10 +60,11 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
set.add(lpsr.label);
}
}
}
@Override
public void postVisit (TerminalDeclaration node, Object data) {
public void postVisit(TerminalDeclaration node, Object data) {
if (node != null && node.getTerminals() != null) {
for (Terminal t : node.getTerminals())
if (t != null)
......@@ -73,7 +73,7 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
}
@Override
public void postVisit (NonTerminalDeclaration node, Object data) {
public void postVisit(NonTerminalDeclaration node, Object data) {
if (node != null && node.getNonTerminals() != null) {
for (NonTerminal nt : node.getNonTerminals())
if (nt != null)
......@@ -82,7 +82,7 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
}
@Override
public void postVisit (TypedNonTerminalDeclaration node, Object data) {
public void postVisit(TypedNonTerminalDeclaration node, Object data) {
// TODO: OAOO!
if (node != null && node.getNonTerminals() != null) {
for (NonTerminal nt : node.getNonTerminals())
......@@ -90,9 +90,9 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
result.getNonTerminals().add(nt.getNameStringOrNull());
}
}
@Override
public void postVisit (TypedTerminalDeclaration node, Object data) {
public void postVisit(TypedTerminalDeclaration node, Object data) {
// TODO: OAOO!
if (node != null && node.getTerminals() != null) {
for (Terminal nt : node.getTerminals())
......
package de.in.tum.www2.cup.analysis;
import de.in.tum.www2.cup.ErrorManager;
import de.in.tum.www2.cup.ast.ActionCodeBlock;
import de.in.tum.www2.cup.ast.SpecialCodeBlock;
public class ParseJavaVisitor extends Visitor<Object> {
private ErrorManager errMan;
public ParseJavaVisitor(ErrorManager errMan) {
this.errMan = errMan;
}
public void postVisit(ActionCodeBlock node, Object data) {
node.parseJavaCode(true, errMan);
};
public void postVisit(SpecialCodeBlock node, Object data) {
node.parseJavaCode(true, errMan);
};
}
package de.in.tum.www2.cup.analysis;
import de.in.tum.www2.cup.ast.*;
import java_cup.runtime.XMLElement;
public abstract class Visitor<T> extends AbstractVisitor<T>
{
......@@ -35,6 +36,7 @@ public abstract class Visitor<T> extends AbstractVisitor<T>
@Override public T preVisit (TerminalDeclaration node, T data) { return data; }
@Override public T preVisit (TypedNonTerminalDeclaration node, T data) { return data; }
@Override public T preVisit (TypedTerminalDeclaration node, T data) { return data; }
@Override public void postVisit (ActionCodeBlock node, T data) { }
@Override public void postVisit (ClassName node, T data) { }
......
package de.in.tum.www2.cup.analysis;
import de.in.tum.www2.cup.ast.*;
import java_cup.runtime.XMLElement;
public abstract class WildcardVisitor<T> extends AbstractVisitor<T>
{
......@@ -39,6 +40,9 @@ public abstract class WildcardVisitor<T> extends AbstractVisitor<T>
public T preVisit (TerminalDeclaration node, T data) { return preVisitWildcard(node, data); }
public T preVisit (TypedNonTerminalDeclaration node, T data) { return preVisitWildcard(node, data); }
public T preVisit (TypedTerminalDeclaration node, T data) { return preVisitWildcard(node, data); }
public T preVisit (XMLElement.Terminal node, T data) { throw new UnsupportedOperationException(); }
public T preVisit (XMLElement.NonTerminal node, T data) { throw new UnsupportedOperationException(); }
public T preVisit (XMLElement.Error node, T data) { throw new UnsupportedOperationException(); }
public void postVisit (ActionCodeBlock node, T data) { postVisitWildcard(node, data); }
public void postVisit (ClassName node, T data) { postVisitWildcard(node, data); }
......@@ -59,5 +63,8 @@ public abstract class WildcardVisitor<T> extends AbstractVisitor<T>
public void postVisit (TerminalDeclaration node, T data) { postVisitWildcard(node, data); }
public void postVisit (TypedNonTerminalDeclaration node, T data) { postVisitWildcard(node, data); }
public void postVisit (TypedTerminalDeclaration node, T data) { postVisitWildcard(node, data); }
public void postVisit (XMLElement.Terminal node, T data) { throw new UnsupportedOperationException(); }
public void postVisit (XMLElement.NonTerminal node, T data) { throw new UnsupportedOperationException(); }
public void postVisit (XMLElement.Error node, T data) { throw new UnsupportedOperationException(); }
}
package de.in.tum.www2.cup.ast;
import java.util.LinkedList;
import java.util.List;
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.CupVariableLocator;
public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
private List<LabeledProductionSymbolRef> variableLabels = new LinkedList<>();
private String returnType;
public List<String> getVariableLabels() {
List<String> varLabels = new LinkedList<String>();
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");
}
return varLabels;
}
public ActionCodeBlock(int debugId, BlockType bt, String blob, Range range) {
super(debugId, bt, blob, range);
......@@ -20,4 +41,36 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
return "ActionCodeBlock";
}
@Override
public void parseJavaCode(Boolean recalcCupVars, ErrorManager errMan) {
if(recalcCupVars){
this.variableLabels.clear();;
CupVariableLocator visitor = new CupVariableLocator(this);
this.getParent().accept(visitor, null);
}
this.cu = (CompilationUnit) JavaParser.parseJava(this);
BindingCollector variableCollector = new BindingCollector();
this.cu.accept(variableCollector);
this.variableMap = variableCollector.getVariableMap();
this.methodList = variableCollector.getMethodList();
this.setCupOffset(this.getRange().getBegin().getOffsetFromStart());
if(errMan != null)
reportProblems(errMan);
}
public void addVariable(LabeledProductionSymbolRef l) {
variableLabels.add(l);
}
public List<LabeledProductionSymbolRef> getVariables() {
return variableLabels;
}
public void setReturnType(String returnType) {
this.returnType = returnType;
}
public String getReturnType() {
return returnType;
}
}
package de.in.tum.www2.cup.ast;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
public class BindingCollector extends ASTVisitor {
private HashMap<IVariableBinding, LinkedList<SimpleName>> variableMap = new HashMap<IVariableBinding, LinkedList<SimpleName>>();
private LinkedList<SimpleName> methodList = new LinkedList<SimpleName>();
public LinkedList<SimpleName> getMethodList() {
return methodList;
}
public HashMap<IVariableBinding, LinkedList<SimpleName>> getVariableMap() {
return variableMap;
}
@Override
public boolean visit(FieldDeclaration node) {
for (Iterator iter = node.fragments().iterator(); iter.hasNext();) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) iter.next();
IVariableBinding binding= (IVariableBinding) fragment.resolveBinding();
if(binding != null)
variableMap.put( binding, new LinkedList<SimpleName>());
// System.out.println("found FieldDeclaration " + fragment.getName().getIdentifier());
}
return true;
}
@Override
public boolean visit(VariableDeclarationStatement node) {
for (Iterator iter = node.fragments().iterator(); iter.hasNext();) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) iter.next();
IVariableBinding binding= (IVariableBinding) fragment.resolveBinding();
if(binding != null)
variableMap.put( binding, new LinkedList<SimpleName>());
// System.out.println("found VariableDeclarationStatement " + fragment.getName().getIdentifier());
}
return false;
}
@Override
public boolean visit(SingleVariableDeclaration node) {
IVariableBinding binding = (IVariableBinding) node.resolveBinding();
if(binding != null){
variableMap.put( binding, new LinkedList<SimpleName>());
// System.out.println("found SingleVariableDeclaration " + binding.getName());
}
return false;
}
@Override
public boolean visit(SimpleName node) {
IBinding nodeBinding = node.resolveBinding();
if(nodeBinding instanceof IVariableBinding){
IVariableBinding binding = (IVariableBinding) nodeBinding;
if(variableMap.containsKey(binding)){
variableMap.get(binding).add(node);
}
// System.out.println("found variableBinding " + nodeBinding.getName());
}else if(nodeBinding instanceof IMethodBinding) {
methodList.add(node);
// System.out.println("found methodBinding " + nodeBinding.getName());
}else{
// System.out.println("found SimpleName " + node.getIdentifier());
}
return false;
}
}
package de.in.tum.www2.cup.ast;
import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.ast.CodeBlock.BlockType;
import java_cup.runtime.XMLElement;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import org.eclipse.jdt.core.compiler.IProblem;