ASTPrinter verbessern

This commit is contained in:
JanUlrich 2017-06-29 01:19:13 +02:00
parent 726eb60eb3
commit 0aa18c74ba

View File

@ -19,13 +19,22 @@ public class ASTPrinter {
} }
private static class OutputGenerator implements ASTVisitor{ private static class OutputGenerator implements ASTVisitor{
private static final String TAB = " ";
String tabs = "";
private final StringBuilder out; private final StringBuilder out;
OutputGenerator(StringBuilder out){ OutputGenerator(StringBuilder out){
this.out = out; this.out = out;
} }
public void tab() {
tabs += TAB;
}
public void untab() {
tabs = tabs.substring(0,tabs.length()-TAB.length());
}
@Override @Override
public void visit(SourceFile sourceFile) { public void visit(SourceFile sourceFile) {
for(ClassOrInterface cl : sourceFile.getClasses()){ for(ClassOrInterface cl : sourceFile.getClasses()){
@ -46,7 +55,7 @@ public class ASTPrinter {
@Override @Override
public void visit(GenericTypeVar genericTypeVar) { public void visit(GenericTypeVar genericTypeVar) {
out.append(genericTypeVar.getName()); out.append(genericTypeVar.getName().toString());
} }
@Override @Override
@ -83,6 +92,7 @@ public class ASTPrinter {
out.append(" " + method.getName()); out.append(" " + method.getName());
method.getParameterList().accept(this); method.getParameterList().accept(this);
method.block.accept(this); method.block.accept(this);
out.append("\n");
} }
@Override @Override
@ -105,22 +115,27 @@ public class ASTPrinter {
}else{ }else{
out.append("class "); out.append("class ");
} }
out.append(classOrInterface.getClassName()); out.append(classOrInterface.getClassName().toString());
classOrInterface.getGenerics().accept(this); classOrInterface.getGenerics().accept(this);
out.append(" {\n"); out.append(" {\n\n");
tab();
for(Field f : classOrInterface.getFieldDecl()){ for(Field f : classOrInterface.getFieldDecl()){
out.append(tabs);
f.accept(this); f.accept(this);
out.append("\n"); out.append("\n");
} }
for(Method m : classOrInterface.getMethods()){ for(Method m : classOrInterface.getMethods()){
out.append(tabs);
m.accept(this); m.accept(this);
out.append("\n"); out.append("\n");
} }
untab();
out.append("}");
} }
@Override @Override
public void visit(RefType refType) { public void visit(RefType refType) {
out.append(refType.getName()); out.append(refType.getName().toString());
Iterator<RefTypeOrTPHOrWildcardOrGeneric> genericIterator = refType.getParaList().iterator(); Iterator<RefTypeOrTPHOrWildcardOrGeneric> genericIterator = refType.getParaList().iterator();
if(genericIterator.hasNext()){ if(genericIterator.hasNext()){
out.append("<"); out.append("<");
@ -151,7 +166,7 @@ public class ASTPrinter {
@Override @Override
public void visit(GenericRefType genericRefType) { public void visit(GenericRefType genericRefType) {
out.append(genericRefType.getName()); out.append(genericRefType.getName().toString());
} }
@Override @Override
@ -175,11 +190,15 @@ public class ASTPrinter {
@Override @Override
public void visit(Block block) { public void visit(Block block) {
tab();
out.append("{\n"); out.append("{\n");
for(Statement stmt : block.getStatements()){ for(Statement stmt : block.getStatements()){
out.append(tabs);
stmt.accept(this); stmt.accept(this);
out.append(";\n"); out.append(";\n");
} }
untab();
out.append(tabs);
out.append("}"); out.append("}");
} }