summaryrefslogtreecommitdiff
path: root/minijava
diff options
context:
space:
mode:
authorbd-912 <bdunahu@colostate.edu>2024-03-02 22:06:02 -0700
committerbd-912 <bdunahu@colostate.edu>2024-03-02 22:06:02 -0700
commita23fdfa59b8fa2eaf58fbf6d677caf0b6cdf5a31 (patch)
treee3bbd4229a4fd035dfeaf2d91ac323e270549425 /minijava
parentcfff9d2a4784b06bce53f21713773685527e9a18 (diff)
Perfect pretty printer
Diffstat (limited to 'minijava')
-rw-r--r--minijava/PPrinter.java231
1 files changed, 147 insertions, 84 deletions
diff --git a/minijava/PPrinter.java b/minijava/PPrinter.java
index 6d69d02..e8528ab 100644
--- a/minijava/PPrinter.java
+++ b/minijava/PPrinter.java
@@ -12,6 +12,16 @@ import java.util.*;
* order. Your visitors may extend this class.
*/
public class PPrinter<R,A> extends GJDepthFirst<R,A> {
+
+ private int offset;
+
+ private void printNode(Node n, A argu) {
+ for (int i=0; i < this.offset; ++i)
+ System.out.print(".");
+ System.out.println(n.getClass().getSimpleName());
+ ++this.offset;
+ }
+
//
// User-generated visitor methods below
//
@@ -21,12 +31,14 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f1 -> ( TypeDeclaration() )*
* f2 -> <EOF>
*/
+ @SuppressWarnings("unchecked")
public R visit(Goal n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -51,8 +63,8 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f17 -> "}"
*/
public R visit(MainClass n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
@@ -71,6 +83,7 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
n.f15.accept(this, argu);
n.f16.accept(this, argu);
n.f17.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -79,9 +92,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* | ClassExtendsDeclaration()
*/
public R visit(TypeDeclaration n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -94,14 +108,15 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f5 -> "}"
*/
public R visit(ClassDeclaration n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
n.f3.accept(this, argu);
n.f4.accept(this, argu);
n.f5.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -116,8 +131,8 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f7 -> "}"
*/
public R visit(ClassExtendsDeclaration n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
@@ -126,6 +141,7 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
n.f5.accept(this, argu);
n.f6.accept(this, argu);
n.f7.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -135,11 +151,12 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f2 -> ";"
*/
public R visit(VarDeclaration n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -159,8 +176,8 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f12 -> "}"
*/
public R visit(MethodDeclaration n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
@@ -174,6 +191,7 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
n.f10.accept(this, argu);
n.f11.accept(this, argu);
n.f12.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -182,10 +200,11 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f1 -> ( FormalParameterRest() )*
*/
public R visit(FormalParameterList n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -194,10 +213,11 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f1 -> Identifier()
*/
public R visit(FormalParameter n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -206,10 +226,11 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f1 -> FormalParameter()
*/
public R visit(FormalParameterRest n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -220,9 +241,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* | Identifier()
*/
public R visit(Type n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -232,11 +254,12 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f2 -> "]"
*/
public R visit(ArrayType n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -244,9 +267,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f0 -> "boolean"
*/
public R visit(BooleanType n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -254,9 +278,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f0 -> "int"
*/
public R visit(IntegerType n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -269,9 +294,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* | PrintStatement()
*/
public R visit(Statement n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -281,11 +307,12 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f2 -> "}"
*/
public R visit(Block n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -296,12 +323,13 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f3 -> ";"
*/
public R visit(AssignmentStatement n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
n.f3.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -315,8 +343,8 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f6 -> ";"
*/
public R visit(ArrayAssignmentStatement n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
@@ -324,6 +352,7 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
n.f4.accept(this, argu);
n.f5.accept(this, argu);
n.f6.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -337,8 +366,8 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f6 -> Statement()
*/
public R visit(IfStatement n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
@@ -346,6 +375,7 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
n.f4.accept(this, argu);
n.f5.accept(this, argu);
n.f6.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -357,13 +387,14 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f4 -> Statement()
*/
public R visit(WhileStatement n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
n.f3.accept(this, argu);
n.f4.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -375,13 +406,14 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f4 -> ";"
*/
public R visit(PrintStatement n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
n.f3.accept(this, argu);
n.f4.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -397,9 +429,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* | PrimaryExpression()
*/
public R visit(Expression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -409,11 +442,12 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f2 -> PrimaryExpression()
*/
public R visit(AndExpression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -423,11 +457,12 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f2 -> PrimaryExpression()
*/
public R visit(CompareExpression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -437,11 +472,12 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f2 -> PrimaryExpression()
*/
public R visit(PlusExpression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -451,11 +487,12 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f2 -> PrimaryExpression()
*/
public R visit(MinusExpression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -465,11 +502,12 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f2 -> PrimaryExpression()
*/
public R visit(TimesExpression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -480,12 +518,13 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f3 -> "]"
*/
public R visit(ArrayLookup n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
n.f3.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -495,11 +534,12 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f2 -> "length"
*/
public R visit(ArrayLength n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -512,14 +552,15 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f5 -> ")"
*/
public R visit(MessageSend n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
n.f3.accept(this, argu);
n.f4.accept(this, argu);
n.f5.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -528,10 +569,11 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f1 -> ( ExpressionRest() )*
*/
public R visit(ExpressionList n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -540,10 +582,11 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f1 -> Expression()
*/
public R visit(ExpressionRest n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -559,9 +602,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* | BracketExpression()
*/
public R visit(PrimaryExpression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -569,9 +613,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f0 -> <INTEGER_LITERAL>
*/
public R visit(IntegerLiteral n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -579,9 +624,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f0 -> "true"
*/
public R visit(TrueLiteral n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -589,9 +635,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f0 -> "false"
*/
public R visit(FalseLiteral n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -599,9 +646,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f0 -> <IDENTIFIER>
*/
public R visit(Identifier n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -609,9 +657,10 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f0 -> "this"
*/
public R visit(ThisExpression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -623,13 +672,14 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f4 -> "]"
*/
public R visit(ArrayAllocationExpression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
n.f3.accept(this, argu);
n.f4.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -640,12 +690,13 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f3 -> ")"
*/
public R visit(AllocationExpression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
n.f3.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -654,10 +705,11 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f1 -> Expression()
*/
public R visit(NotExpression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
+ --this.offset;
return _ret;
}
@@ -667,11 +719,22 @@ public class PPrinter<R,A> extends GJDepthFirst<R,A> {
* f2 -> ")"
*/
public R visit(BracketExpression n, A argu) {
- System.out.println(n.getClass().getName());
- R _ret=null;
+ this.printNode(n, argu);
+ R _ret=null;
n.f0.accept(this, argu);
n.f1.accept(this, argu);
n.f2.accept(this, argu);
+ --this.offset;
+ return _ret;
+ }
+
+ public R visit(NodeToken n, A argu) {
+ for (int i=0; i < this.offset; ++i)
+ System.out.print(".");
+ System.out.println(n.getClass().getSimpleName() +
+ " => " +
+ n.toString());
+ R _ret=null;
return _ret;
}