Skip to content

Commit

Permalink
Minor refactorings for PrettyPrinter (refs #42)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-eberle committed Nov 16, 2014
1 parent 67bb421 commit 926626d
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ public boolean isLocalMethod() {
return expression == null ? true : false;
}

public int numberOfArguments() {
return parameters == null ? 0 : parameters.length;
}

@Override
public void accept(AstVisitor visitor) {
visitor.visit(this);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/src/compiler/main/CompilerApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static int execute(String[] args) {
}

if (cmd.hasOption(PRETTY_PRINT_AST)) {
System.out.print(PrettyPrinter.get(ast));
System.out.print(PrettyPrinter.prettyPrint(ast));
}
return 0;
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion Compiler/src/compiler/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ private Expression[] parseArguments() throws IOException, ParserException {
// no expression
case RP:
default:
return null; // no ast
return new Expression[0]; // no ast
}
}

Expand Down
7 changes: 5 additions & 2 deletions Compiler/src/compiler/parser/printer/PrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import compiler.ast.AstNode;

public class PrettyPrinter {
public final class PrettyPrinter {

public static String get(AstNode astNode) {
private PrettyPrinter() { // no objects of this class allowed
}

public static String prettyPrint(AstNode astNode) {
PrettyPrinterVisitor visitor = new PrettyPrinterVisitor();
astNode.accept(visitor);
return visitor.getOutputString();
Expand Down
16 changes: 8 additions & 8 deletions Compiler/src/compiler/parser/printer/PrettyPrinterVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void visit(MethodInvocationExpression methodInvocationExpression) {
Expression[] args = methodInvocationExpression.getParameters();

// print arguments
if (args != null && args.length > 0) {
if (args.length > 0) {
int i = 0;
precedence = 0;
args[i++].accept(this);
Expand Down Expand Up @@ -404,11 +404,11 @@ private void visitNewArrayExpression(Type type, Expression expr) {
throw new IllegalArgumentException();
}
stringBuffer.append(typeString);
if (dim > 0) {
stringBuffer.append('[');
expr.accept(this);
stringBuffer.append(']');
}

stringBuffer.append('[');
expr.accept(this);
stringBuffer.append(']');

for (int i = 1; i < dim; i++) {
stringBuffer.append("[]");
}
Expand Down Expand Up @@ -460,7 +460,7 @@ public void visit(ClassDeclaration classDeclaration) {
stringBuffer.append(classDeclaration.getIdentifier());
stringBuffer.append(" {");

if (classDeclaration.getMembers() != null && classDeclaration.getMembers().size() > 0) {
if (classDeclaration.getMembers().size() > 0) {
stringBuffer.append('\n');
tabStops++;

Expand Down Expand Up @@ -499,7 +499,7 @@ public void visit(IfStatement ifStatement) {
if (numberOfTrueStatements <= 1) {
stringBuffer.append('\n');
printTabs();
} else if (numberOfTrueStatements > 1) { // if true case was a block, add space
} else { // if true case was a block, add space
stringBuffer.append(' ');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testSourceFile(Path sourceFile, Path parserFile) throws Exception {
try {
AstNode ast = parser.parse();

String printedAst = PrettyPrinter.get(ast);
String printedAst = PrettyPrinter.prettyPrint(ast);
Scanner s = new Scanner(printedAst);

int line = 1;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/tests/compiler/parser/ParseNewExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void testInvalidNewExprBegin() throws IOException {
}

private String callNewArrayExpression(Parser parser) {
return PrettyPrinter.get((NewArrayExpression) caller.call("parseNewExpression", parser, parameterTypes, args));
return PrettyPrinter.prettyPrint((NewArrayExpression) caller.call("parseNewExpression", parser, parameterTypes, args));
}

}
2 changes: 1 addition & 1 deletion Compiler/tests/compiler/parser/ParsePostfixOpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ public void accept(AstVisitor visitor) {
}

private String callArrayAccessExpression(Parser parser) {
return PrettyPrinter.get((ArrayAccessExpression) caller.call("parsePostfixOp", parser, parameterTypes, args));
return PrettyPrinter.prettyPrint((ArrayAccessExpression) caller.call("parsePostfixOp", parser, parameterTypes, args));
}
}
2 changes: 1 addition & 1 deletion Compiler/tests/compiler/parser/PrecedenceClimbingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void testTokenPrecedence2() throws IOException {
}

private String callParseExpression(Parser parser) {
return PrettyPrinter.get((Expression) privateMethodCaller.call("parseExpression", parser, new Class<?>[] { int.class }, new Object[] { 0 }));
return PrettyPrinter.prettyPrint((Expression) privateMethodCaller.call("parseExpression", parser, new Class<?>[] { int.class }, new Object[] { 0 }));
}

}

0 comments on commit 926626d

Please sign in to comment.