ASTPrinter einführen

This commit is contained in:
JanUlrich 2017-06-28 21:10:28 +02:00
parent afd3f32f23
commit 726eb60eb3
19 changed files with 361 additions and 61 deletions

View File

@ -27,7 +27,7 @@ import java.util.*;
public class JavaTXCompiler {
private List<SourceFile> sourceFiles = new ArrayList<>();
protected List<SourceFile> sourceFiles = new ArrayList<>();
public List<TypeInsert> getTypeInserts(File forFile){
ResultSet result = typeInference();

View File

@ -819,7 +819,7 @@ public class StatementGenerator {
funNParams.add(TypePlaceholder.fresh(expression.getStart())));
RefTypeOrTPHOrWildcardOrGeneric lambdaType = TypePlaceholder.fresh(expression.getStart());
//RefType lambdaType = new RefType(reg.getName("Fun"+params.getFormalparalist().size()),
//funNParams, expression.getStart());
//funNParams, name.getStart());
return new LambdaExpression(lambdaType, params, block, expression.getStart());
}
}

View File

@ -180,7 +180,7 @@ public class Java8Parser extends Parser {
"methodInvocation_lf_primary", "methodInvocation_lfno_primary", "argumentList",
"methodReference", "methodReference_lf_primary", "methodReference_lfno_primary",
"arrayCreationExpression", "dimExprs", "dimExpr", "constantExpression",
"expression", "lambdaExpression", "lambdaParameters", "inferredFormalParameterList",
"name", "lambdaExpression", "lambdaParameters", "inferredFormalParameterList",
"lambdaBody", "assignmentExpression", "assignment", "leftHandSide", "assignmentOperator",
"conditionalExpression", "conditionalOrExpression", "conditionalAndExpression",
"inclusiveOrExpression", "exclusiveOrExpression", "andExpression", "equalityExpression",

View File

@ -3,7 +3,7 @@ package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.*;
public interface ASTVisitor extends StatementVisitor{
@ -11,8 +11,6 @@ public interface ASTVisitor extends StatementVisitor{
void visit(ArgumentList argumentList);
void visit(RefTypeOrTPHOrWildcardOrGeneric refTypeOrTPHOrWildcardOrGeneric);
void visit(GenericTypeVar genericTypeVar);
void visit(FormalParameter formalParameter);
@ -21,5 +19,19 @@ public interface ASTVisitor extends StatementVisitor{
void visit(Field field);
void visit(Method field);
void visit(ParameterList formalParameters);
void visit(ClassOrInterface classOrInterface);
void visit(RefType refType);
void visit(SuperWildcardType superWildcardType);
void visit(TypePlaceholder typePlaceholder);
void visit(ExtendsWildcardType extendsWildcardType);
void visit(GenericRefType genericRefType);
}

View File

@ -2,6 +2,7 @@ package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.core.IItemWithOffset;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
@ -15,13 +16,12 @@ import java.util.List;
/**
* Stellt jede Art von Klasse dar. Auch abstrakte Klassen und Interfaces
*/
public class ClassOrInterface implements IItemWithOffset{
public class ClassOrInterface extends SyntaxTreeNode {
protected int modifiers;
protected JavaClassName name;
private List<Field> fields = new ArrayList<>();
private List<Method> methods = new ArrayList<>();
private GenericDeclarationList genericClassParameters;
private Token offset;
private RefTypeOrTPHOrWildcardOrGeneric superClass;
protected boolean isInterface;
private List<? extends RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces;
@ -29,12 +29,11 @@ public class ClassOrInterface implements IItemWithOffset{
public ClassOrInterface(int modifiers, JavaClassName name, List<Field> fielddecl, List<Method> methods, List<Constructor> constructors, GenericDeclarationList genericClassParameters,
RefTypeOrTPHOrWildcardOrGeneric superClass, Boolean isInterface, List<? extends RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces, Token offset){
this.offset = offset;
super(offset);
this.modifiers = modifiers;
this.name = name;
this.fields = fielddecl;
this.genericClassParameters = genericClassParameters;
this.offset = offset;
this.superClass = superClass;
this.isInterface = isInterface;
this.implementedInterfaces = implementedInterfaces;
@ -88,7 +87,7 @@ public class ClassOrInterface implements IItemWithOffset{
}
@Override
public Token getOffset() {
return offset;
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -23,7 +23,7 @@ import de.dhbwstuttgart.syntaxtree.statement.Block;
*/
public class Method extends Field implements IItemWithOffset, TypeScope
{
private Block block;
public final Block block;
private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken());
private ExceptionList exceptionlist;
private GenericDeclarationList generics;
@ -56,4 +56,9 @@ public class Method extends Field implements IItemWithOffset, TypeScope
public RefTypeOrTPHOrWildcardOrGeneric getReturnType() {
return this.getType();
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -32,7 +32,7 @@ public class ParameterList extends SyntaxTreeNode implements Iterable<FormalPara
@Override
public Iterator<FormalParameter> iterator() {
return null;
return formalparameter.iterator();
}
@Override

View File

@ -27,8 +27,6 @@ public interface StatementVisitor {
void visit(InstanceOf instanceOf);
void visit(InstVar instVar);
void visit(LocalVar localVar);
void visit(LocalVarDecl localVarDecl);

View File

@ -1,24 +0,0 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
public class InstVar extends Expression
{
private Expression expr;
public InstVar(Expression instanz, String name, Token offset)
{
super(instanz.getType(),offset);
expr = instanz;
}
@Override
public void accept(StatementVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,28 +1,23 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
public class LocalVar extends Statement{
protected final String expression;
public final String name;
public LocalVar(String n, RefTypeOrTPHOrWildcardOrGeneric type, Token offset)
{
super(type,offset);
this.expression = n;
this.name = n;
}
public LocalVar(Expression e1, RefTypeOrTPHOrWildcardOrGeneric type, String access)
{
super(type,e1.getOffset());
this.expression = access;
this.name = access;
}
@Override

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import org.antlr.v4.runtime.Token;
/**
@ -33,4 +34,8 @@ public class ExtendsWildcardType extends WildcardType{
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.typecheck.GenericTypeName;
import de.dhbwstuttgart.typecheck.JavaClassName;
import org.antlr.v4.runtime.Token;
@ -20,5 +21,10 @@ public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric
public String getUniqueIdentifier(){
return name.toString();
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.typecheck.JavaClassName;
import org.antlr.v4.runtime.Token;
@ -100,5 +101,9 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -4,13 +4,11 @@ import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import org.antlr.v4.runtime.Token;
public class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode{
public abstract class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode{
public RefTypeOrTPHOrWildcardOrGeneric(Token offset) {
super(offset);
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
public abstract void accept(ASTVisitor visitor);
}

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import org.antlr.v4.runtime.Token;
/**
@ -42,4 +43,9 @@ public class SuperWildcardType extends WildcardType{
public boolean isSuper() {
return true;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree.type;
import java.util.Hashtable;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
import org.antlr.v4.runtime.Token;
@ -65,4 +66,9 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric
public String getName() {
return name;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,5 +1,298 @@
package de.dhbwstuttgart.syntaxtree.visual;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.*;
import java.lang.reflect.Modifier;
import java.util.Iterator;
public class ASTPrinter {
public static String print(SourceFile toPrint){
StringBuilder output = new StringBuilder();
new OutputGenerator(output).visit(toPrint);
return output.toString();
}
private static class OutputGenerator implements ASTVisitor{
private final StringBuilder out;
OutputGenerator(StringBuilder out){
this.out = out;
}
@Override
public void visit(SourceFile sourceFile) {
for(ClassOrInterface cl : sourceFile.getClasses()){
cl.accept(this);
}
}
@Override
public void visit(ArgumentList argumentList) {
out.append("(");
Iterator<Expression> expressionIterator = argumentList.getArguments().iterator();
while(expressionIterator.hasNext()){
expressionIterator.next().accept(this);
if(expressionIterator.hasNext())out.append(", ");
}
out.append(")");
}
@Override
public void visit(GenericTypeVar genericTypeVar) {
out.append(genericTypeVar.getName());
}
@Override
public void visit(FormalParameter formalParameter) {
formalParameter.getType().accept(this);
out.append(" ");
out.append(formalParameter.getName());
}
@Override
public void visit(GenericDeclarationList genericTypeVars) {
Iterator<GenericTypeVar> genericIterator = genericTypeVars.iterator();
if(genericIterator.hasNext()){
out.append("<");
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
if(genericIterator.hasNext())out.append(", ");
}
out.append(">");
}
}
@Override
public void visit(Field field) {
field.getType().accept(this);
out.append(" ");
out.append(field.getName());
out.append(";");
}
@Override
public void visit(Method method) {
method.getType().accept(this);
out.append(" " + method.getName());
method.getParameterList().accept(this);
method.block.accept(this);
}
@Override
public void visit(ParameterList formalParameters) {
out.append("(");
Iterator<FormalParameter> genericIterator = formalParameters.getFormalparalist().iterator();
if(genericIterator.hasNext()){
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
if(genericIterator.hasNext())out.append(", ");
}
}
out.append(")");
}
@Override
public void visit(ClassOrInterface classOrInterface) {
if((Modifier.INTERFACE & classOrInterface.getModifiers()) == 1){
out.append("interface ");
}else{
out.append("class ");
}
out.append(classOrInterface.getClassName());
classOrInterface.getGenerics().accept(this);
out.append(" {\n");
for(Field f : classOrInterface.getFieldDecl()){
f.accept(this);
out.append("\n");
}
for(Method m : classOrInterface.getMethods()){
m.accept(this);
out.append("\n");
}
}
@Override
public void visit(RefType refType) {
out.append(refType.getName());
Iterator<RefTypeOrTPHOrWildcardOrGeneric> genericIterator = refType.getParaList().iterator();
if(genericIterator.hasNext()){
out.append("<");
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
if(genericIterator.hasNext())out.append(", ");
}
out.append(">");
}
}
@Override
public void visit(SuperWildcardType superWildcardType) {
out.append("? super ");
superWildcardType.getInnerType().accept(this);
}
@Override
public void visit(TypePlaceholder typePlaceholder) {
out.append("TPH "+ typePlaceholder.getName());
}
@Override
public void visit(ExtendsWildcardType extendsWildcardType) {
out.append("? extends ");
extendsWildcardType.getInnerType().accept(this);
}
@Override
public void visit(GenericRefType genericRefType) {
out.append(genericRefType.getName());
}
@Override
public void visit(LambdaExpression lambdaExpression) {
lambdaExpression.params.accept(this);
out.append(" -> ");
lambdaExpression.methodBody.accept(this);
}
@Override
public void visit(Assign assign) {
assign.lefSide.accept(this);
out.append(" = ");
assign.rightSide.accept(this);
}
@Override
public void visit(Binary binary) {
}
@Override
public void visit(Block block) {
out.append("{\n");
for(Statement stmt : block.getStatements()){
stmt.accept(this);
out.append(";\n");
}
out.append("}");
}
@Override
public void visit(CastExpr castExpr) {
}
@Override
public void visit(EmptyStmt emptyStmt) {
}
@Override
public void visit(FieldVar fieldVar) {
fieldVar.receiver.accept(this);
out.append("." + fieldVar.fieldVarName);
}
@Override
public void visit(ForStmt forStmt) {
}
@Override
public void visit(IfStmt ifStmt) {
}
@Override
public void visit(InstanceOf instanceOf) {
}
@Override
public void visit(LocalVar localVar) {
out.append(localVar.name);
}
@Override
public void visit(LocalVarDecl localVarDecl) {
localVarDecl.getType().accept(this);
out.append(" " + localVarDecl.getName());
}
@Override
public void visit(MethodCall methodCall) {
methodCall.receiver.accept(this);
out.append("."+methodCall.name);
methodCall.getArgumentList().accept(this);
}
@Override
public void visit(NewClass methodCall) {
out.append("new ");
visit((MethodCall) methodCall);
}
@Override
public void visit(NewArray newArray) {
}
@Override
public void visit(Receiver receiver) {
receiver.expr.accept(this);
}
@Override
public void visit(Return aReturn) {
out.append("return ");
aReturn.retexpr.accept(this);
}
@Override
public void visit(ReturnVoid aReturn) {
out.append("return");
}
@Override
public void visit(StaticClassName staticClassName) {
}
@Override
public void visit(Super aSuper) {
}
@Override
public void visit(This aThis) {
out.append("this");
}
@Override
public void visit(UnaryPlus unaryPlus) {
}
@Override
public void visit(WhileStmt whileStmt) {
}
@Override
public void visit(Null aNull) {
}
@Override
public void visit(Literal literal) {
}
}
}

View File

@ -118,11 +118,6 @@ public class TYPE implements StatementVisitor{
throw new NotImplementedException();
}
@Override
public void visit(InstVar instVar) {
throw new NotImplementedException();
}
@Override
public void visit(LocalVar localVar) {
// Es werden nur bei Feldvariablen Constraints generiert. Lokale Variablen sind eindeutig

View File

@ -3,6 +3,7 @@ package typeinference;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
import de.dhbwstuttgart.typedeployment.TypeInsert;
import de.dhbwstuttgart.typedeployment.TypeInsertPoint;
import de.dhbwstuttgart.typeinference.ResultSet;
@ -19,7 +20,7 @@ import java.util.List;
import static org.junit.Assert.*;
public class JavaTXCompilerTest {
public class JavaTXCompilerTest extends JavaTXCompiler {
private static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/";
private static final List<File> filesToTest = new ArrayList<>();
@ -35,10 +36,10 @@ public class JavaTXCompilerTest {
//filesToTest.add(new File(rootDirectory+"Generics.jav"));
//filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
//filesToTest.add(new File(rootDirectory+"Matrix.jav"));
JavaTXCompiler compiler = new JavaTXCompiler();
for(File f : filesToTest){
compiler.parse(f);
List<TypeInsert> result = compiler.getTypeInserts(f);
this.parse(f);
System.out.println(ASTPrinter.print(this.sourceFiles.get(sourceFiles.size()-1)));
List<TypeInsert> result = this.getTypeInserts(f);
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
for(TypeInsert tip : result){
System.out.println(tip.insert(content));