ASTTypePrinter anfügen

This commit is contained in:
JanUlrich 2017-06-30 11:13:15 +02:00
parent 0aa18c74ba
commit 3b8ef58222
6 changed files with 723 additions and 305 deletions

View File

@ -0,0 +1,145 @@
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.*;
import java.lang.reflect.Modifier;
import java.util.Iterator;
public abstract class AbstractASTWalker implements ASTVisitor{
@Override
public void visit(SourceFile sourceFile) {
for(ClassOrInterface cl : sourceFile.getClasses()){
cl.accept(this);
}
}
@Override
public void visit(ArgumentList argumentList) {
for(Expression expr : argumentList.getArguments()){
expr.accept(this);
}
}
@Override
public void visit(FormalParameter formalParameter) {
formalParameter.getType().accept(this);
}
@Override
public void visit(GenericDeclarationList genericTypeVars) {
Iterator<GenericTypeVar> genericIterator = genericTypeVars.iterator();
if(genericIterator.hasNext()){
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
}
}
}
@Override
public void visit(Field field) {
field.getType().accept(this);
}
@Override
public void visit(Method method) {
method.getType().accept(this);
method.getParameterList().accept(this);
method.block.accept(this);
}
@Override
public void visit(ParameterList formalParameters) {
Iterator<FormalParameter> genericIterator = formalParameters.getFormalparalist().iterator();
if(genericIterator.hasNext()){
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
}
}
}
@Override
public void visit(ClassOrInterface classOrInterface) {
classOrInterface.getGenerics().accept(this);
for(Field f : classOrInterface.getFieldDecl()){
f.accept(this);
}
for(Method m : classOrInterface.getMethods()){
m.accept(this);
}
}
@Override
public void visit(RefType refType) {
Iterator<RefTypeOrTPHOrWildcardOrGeneric> genericIterator = refType.getParaList().iterator();
if(genericIterator.hasNext()){
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
}
}
}
@Override
public void visit(SuperWildcardType superWildcardType) {
superWildcardType.getInnerType().accept(this);
}
@Override
public void visit(ExtendsWildcardType extendsWildcardType) {
extendsWildcardType.getInnerType().accept(this);
}
@Override
public void visit(LambdaExpression lambdaExpression) {
lambdaExpression.params.accept(this);
lambdaExpression.methodBody.accept(this);
}
@Override
public void visit(Assign assign) {
assign.lefSide.accept(this);
assign.rightSide.accept(this);
}
@Override
public void visit(Block block) {
for(Statement stmt : block.getStatements()){
stmt.accept(this);
}
}
@Override
public void visit(FieldVar fieldVar) {
fieldVar.receiver.accept(this);
}
@Override
public void visit(LocalVarDecl localVarDecl) {
localVarDecl.getType().accept(this);
}
@Override
public void visit(MethodCall methodCall) {
methodCall.receiver.accept(this);
methodCall.getArgumentList().accept(this);
}
@Override
public void visit(NewClass methodCall) {
visit((MethodCall) methodCall);
}
@Override
public void visit(Receiver receiver) {
receiver.expr.accept(this);
}
@Override
public void visit(Return aReturn) {
aReturn.retexpr.accept(this);
}
}

View File

@ -1,317 +1,13 @@
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 static final String TAB = " ";
String tabs = "";
private final StringBuilder out;
OutputGenerator(StringBuilder out){
this.out = out;
}
public void tab() {
tabs += TAB;
}
public void untab() {
tabs = tabs.substring(0,tabs.length()-TAB.length());
}
@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().toString());
}
@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);
out.append("\n");
}
@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().toString());
classOrInterface.getGenerics().accept(this);
out.append(" {\n\n");
tab();
for(Field f : classOrInterface.getFieldDecl()){
out.append(tabs);
f.accept(this);
out.append("\n");
}
for(Method m : classOrInterface.getMethods()){
out.append(tabs);
m.accept(this);
out.append("\n");
}
untab();
out.append("}");
}
@Override
public void visit(RefType refType) {
out.append(refType.getName().toString());
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().toString());
}
@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) {
tab();
out.append("{\n");
for(Statement stmt : block.getStatements()){
out.append(tabs);
stmt.accept(this);
out.append(";\n");
}
untab();
out.append(tabs);
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

@ -0,0 +1,20 @@
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 ASTTypePrinter extends ASTPrinter{
public static String print(SourceFile toPrint){
StringBuilder output = new StringBuilder();
new TypeOutputGenerator(output).visit(toPrint);
return output.toString();
}
}

View File

@ -0,0 +1,306 @@
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 OutputGenerator implements ASTVisitor {
private static final String TAB = " ";
String tabs = "";
protected final StringBuilder out;
public OutputGenerator(StringBuilder out){
this.out = out;
}
public void tab() {
tabs += TAB;
}
public void untab() {
tabs = tabs.substring(0,tabs.length()-TAB.length());
}
@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().toString());
}
@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);
out.append("\n");
}
@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().toString());
classOrInterface.getGenerics().accept(this);
out.append(" {\n\n");
tab();
for(Field f : classOrInterface.getFieldDecl()){
out.append(tabs);
f.accept(this);
out.append("\n");
}
for(Method m : classOrInterface.getMethods()){
out.append(tabs);
m.accept(this);
out.append("\n");
}
untab();
out.append("}");
}
@Override
public void visit(RefType refType) {
out.append(refType.getName().toString());
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().toString());
}
@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) {
tab();
out.append("{\n");
for(Statement stmt : block.getStatements()){
out.append(tabs);
stmt.accept(this);
out.append(";\n");
}
untab();
out.append(tabs);
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

@ -0,0 +1,238 @@
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 TypeOutputGenerator extends OutputGenerator {
TypeOutputGenerator(StringBuilder out){
super(out);
}
@Override
public void visit(SourceFile sourceFile) {
super.visit(sourceFile);
}
@Override
public void visit(ArgumentList argumentList) {
super.visit(argumentList);
}
@Override
public void visit(GenericTypeVar genericTypeVar) {
super.visit(genericTypeVar);
}
@Override
public void visit(FormalParameter formalParameter) {
super.visit(formalParameter);
}
@Override
public void visit(GenericDeclarationList genericTypeVars) {
super.visit(genericTypeVars);
}
@Override
public void visit(Field field) {
super.visit(field);
}
@Override
public void visit(Method method) {
super.visit(method);
}
@Override
public void visit(ParameterList formalParameters) {
super.visit(formalParameters);
}
@Override
public void visit(ClassOrInterface classOrInterface) {
super.visit(classOrInterface);
}
@Override
public void visit(RefType refType) {
super.visit(refType);
}
@Override
public void visit(SuperWildcardType superWildcardType) {
super.visit(superWildcardType);
}
@Override
public void visit(TypePlaceholder typePlaceholder) {
super.visit(typePlaceholder);
}
@Override
public void visit(ExtendsWildcardType extendsWildcardType) {
super.visit(extendsWildcardType);
}
@Override
public void visit(GenericRefType genericRefType) {
super.visit(genericRefType);
}
@Override
public void visit(LambdaExpression lambdaExpression) {
out.append("(");
super.visit(lambdaExpression);
out.append(")");
this.out.append("::");
lambdaExpression.getType().accept(this);
}
@Override
public void visit(Assign assign) {
super.visit(assign);
}
@Override
public void visit(Binary binary) {
super.visit(binary);
}
@Override
public void visit(Block block) {
out.append("(");
super.visit(block);
out.append(")");
this.out.append("::");
block.getType().accept(this);
}
@Override
public void visit(CastExpr castExpr) {
super.visit(castExpr);
}
@Override
public void visit(EmptyStmt emptyStmt) {
super.visit(emptyStmt);
}
@Override
public void visit(FieldVar fieldVar) {
out.append("(");
super.visit(fieldVar);
out.append(")");
this.out.append("::");
fieldVar.getType().accept(this);
}
@Override
public void visit(ForStmt forStmt) {
super.visit(forStmt);
}
@Override
public void visit(IfStmt ifStmt) {
super.visit(ifStmt);
}
@Override
public void visit(InstanceOf instanceOf) {
super.visit(instanceOf);
}
@Override
public void visit(LocalVar localVar) {
out.append("(");
super.visit(localVar);
out.append(")");
this.out.append("::");
localVar.getType().accept(this);
}
@Override
public void visit(LocalVarDecl localVarDecl) {
super.visit(localVarDecl);
}
@Override
public void visit(MethodCall methodCall) {
out.append("(");
super.visit(methodCall);
out.append(")");
this.out.append("::");
methodCall.getType().accept(this);
}
@Override
public void visit(NewClass methodCall) {
super.visit(methodCall);
this.out.append("::");
methodCall.getType().accept(this);
}
@Override
public void visit(NewArray newArray) {
super.visit(newArray);
}
@Override
public void visit(Receiver receiver) {
super.visit(receiver);
}
@Override
public void visit(Return aReturn) {
super.visit(aReturn);
}
@Override
public void visit(ReturnVoid aReturn) {
super.visit(aReturn);
}
@Override
public void visit(StaticClassName staticClassName) {
super.visit(staticClassName);
}
@Override
public void visit(Super aSuper) {
super.visit(aSuper);
}
@Override
public void visit(This aThis) {
out.append("(");
super.visit(aThis);
out.append(")");
this.out.append("::");
aThis.getType().accept(this);
}
@Override
public void visit(UnaryPlus unaryPlus) {
super.visit(unaryPlus);
}
@Override
public void visit(WhileStmt whileStmt) {
super.visit(whileStmt);
}
@Override
public void visit(Null aNull) {
super.visit(aNull);
}
@Override
public void visit(Literal literal) {
super.visit(literal);
}
}

View File

@ -3,7 +3,10 @@ package typeinference;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
import de.dhbwstuttgart.syntaxtree.visual.OutputGenerator;
import de.dhbwstuttgart.typedeployment.TypeInsert;
import de.dhbwstuttgart.typedeployment.TypeInsertPoint;
import de.dhbwstuttgart.typeinference.ResultSet;
@ -38,7 +41,7 @@ public class JavaTXCompilerTest extends JavaTXCompiler {
//filesToTest.add(new File(rootDirectory+"Matrix.jav"));
for(File f : filesToTest){
this.parse(f);
System.out.println(ASTPrinter.print(this.sourceFiles.get(sourceFiles.size()-1)));
System.out.println(ASTTypePrinter.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){
@ -54,4 +57,14 @@ public class JavaTXCompilerTest extends JavaTXCompiler {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
static String showTPHInformation(Iterable<TypePlaceholder> tphs){
StringBuilder ret = new StringBuilder();
OutputGenerator gen = new OutputGenerator(ret);
for(TypePlaceholder tph : tphs){
gen.visit(tph);
}
return ret.toString();
}
}