aboutsummaryrefslogtreecommitdiff
path: root/java/com/google/turbine/tree/Pretty.java
blob: b693a42b83b2c905e39a1632d33dab068e729d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.turbine.tree;

import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.turbine.tree.Tree.Anno;
import com.google.turbine.tree.Tree.ClassLiteral;
import com.google.turbine.tree.Tree.Ident;
import com.google.turbine.tree.Tree.ModDecl;
import com.google.turbine.tree.Tree.ModDirective;
import com.google.turbine.tree.Tree.ModExports;
import com.google.turbine.tree.Tree.ModOpens;
import com.google.turbine.tree.Tree.ModProvides;
import com.google.turbine.tree.Tree.ModRequires;
import com.google.turbine.tree.Tree.ModUses;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** A pretty-printer for {@link Tree}s. */
public class Pretty implements Tree.Visitor<Void, Void> {

  static String pretty(Tree tree) {
    Pretty pretty = new Pretty();
    tree.accept(pretty, null);
    return pretty.sb.toString();
  }

  private final StringBuilder sb = new StringBuilder();
  int indent = 0;
  boolean newLine = false;

  void printLine() {
    append('\n');
    newLine = true;
  }

  void printLine(String line) {
    if (!newLine) {
      append('\n');
    }
    append(line).append('\n');
    newLine = true;
  }

  Pretty append(char c) {
    if (c == '\n') {
      newLine = true;
    } else if (newLine) {
      sb.append(Strings.repeat(" ", indent * 2));
      newLine = false;
    }
    sb.append(c);
    return this;
  }

  Pretty append(String s) {
    if (newLine) {
      sb.append(Strings.repeat(" ", indent * 2));
      newLine = false;
    }
    sb.append(s);
    return this;
  }

  @Override
  public Void visitIdent(Ident ident, Void input) {
    sb.append(ident.value());
    return null;
  }

  @Override
  public Void visitWildTy(Tree.WildTy wildTy, Void input) {
    printAnnos(wildTy.annos());
    append('?');
    if (wildTy.lower().isPresent()) {
      append(" super ");
      wildTy.lower().get().accept(this, null);
    }
    if (wildTy.upper().isPresent()) {
      append(" extends ");
      wildTy.upper().get().accept(this, null);
    }
    return null;
  }

  @Override
  public Void visitArrTy(Tree.ArrTy arrTy, Void input) {
    arrTy.elem().accept(this, null);
    if (!arrTy.annos().isEmpty()) {
      append(' ');
      printAnnos(arrTy.annos());
    }
    append("[]");
    return null;
  }

  @Override
  public Void visitPrimTy(Tree.PrimTy primTy, Void input) {
    append(primTy.tykind().toString());
    return null;
  }

  @Override
  public Void visitVoidTy(Tree.VoidTy primTy, Void input) {
    append("void");
    return null;
  }

  @Override
  public Void visitClassTy(Tree.ClassTy classTy, Void input) {
    if (classTy.base().isPresent()) {
      classTy.base().get().accept(this, null);
      append('.');
    }
    printAnnos(classTy.annos());
    append(classTy.name().value());
    if (!classTy.tyargs().isEmpty()) {
      append('<');
      boolean first = true;
      for (Tree t : classTy.tyargs()) {
        if (!first) {
          append(", ");
        }
        t.accept(this, null);
        first = false;
      }
      append('>');
    }
    return null;
  }

  @Override
  public Void visitLiteral(Tree.Literal literal, Void input) {
    append(literal.value().toString());
    return null;
  }

  @Override
  public Void visitTypeCast(Tree.TypeCast typeCast, Void input) {
    append('(');
    typeCast.ty().accept(this, null);
    append(") ");
    typeCast.expr().accept(this, null);
    return null;
  }

  @Override
  public Void visitUnary(Tree.Unary unary, Void input) {
    switch (unary.op()) {
      case POST_INCR:
      case POST_DECR:
        unary.expr().accept(this, null);
        append(unary.op().toString());
        break;
      case PRE_INCR:
      case PRE_DECR:
      case UNARY_PLUS:
      case NEG:
      case NOT:
      case BITWISE_COMP:
        append(unary.op().toString());
        unary.expr().accept(this, null);
        break;
      default:
        throw new AssertionError(unary.op().name());
    }
    return null;
  }

  @Override
  public Void visitBinary(Tree.Binary binary, Void input) {
    append('(');
    binary.lhs().accept(this, null);
    append(" " + binary.op() + " ");
    binary.rhs().accept(this, null);
    append(')');
    return null;
  }

  @Override
  public Void visitConstVarName(Tree.ConstVarName constVarName, Void input) {
    append(Joiner.on('.').join(constVarName.name()));
    return null;
  }

  @Override
  public Void visitClassLiteral(ClassLiteral classLiteral, Void input) {
    classLiteral.accept(this, input);
    append(".class");
    return null;
  }

  @Override
  public Void visitAssign(Tree.Assign assign, Void input) {
    append(assign.name().value()).append(" = ");
    assign.expr().accept(this, null);
    return null;
  }

  @Override
  public Void visitConditional(Tree.Conditional conditional, Void input) {
    append("(");
    conditional.cond().accept(this, null);
    append(" ? ");
    conditional.iftrue().accept(this, null);
    append(" : ");
    conditional.iffalse().accept(this, null);
    append(")");
    return null;
  }

  @Override
  public Void visitArrayInit(Tree.ArrayInit arrayInit, Void input) {
    append('{');
    boolean first = true;
    for (Tree.Expression e : arrayInit.exprs()) {
      if (!first) {
        append(", ");
      }
      e.accept(this, null);
      first = false;
    }
    append('}');
    return null;
  }

  @Override
  public Void visitCompUnit(Tree.CompUnit compUnit, Void input) {
    if (compUnit.pkg().isPresent()) {
      compUnit.pkg().get().accept(this, null);
      printLine();
    }
    for (Tree.ImportDecl i : compUnit.imports()) {
      i.accept(this, null);
    }
    if (compUnit.mod().isPresent()) {
      printLine();
      compUnit.mod().get().accept(this, null);
    }
    for (Tree.TyDecl decl : compUnit.decls()) {
      printLine();
      decl.accept(this, null);
    }
    return null;
  }

  @Override
  public Void visitImportDecl(Tree.ImportDecl importDecl, Void input) {
    append("import ");
    if (importDecl.stat()) {
      append("static ");
    }
    append(Joiner.on('.').join(importDecl.type()));
    if (importDecl.wild()) {
      append(".*");
    }
    append(";").append('\n');
    return null;
  }

  @Override
  public Void visitVarDecl(Tree.VarDecl varDecl, Void input) {
    printVarDecl(varDecl);
    append(';');
    return null;
  }

  private void printVarDecl(Tree.VarDecl varDecl) {
    printAnnos(varDecl.annos());
    printModifiers(varDecl.mods());
    varDecl.ty().accept(this, null);
    append(' ').append(varDecl.name().value());
    if (varDecl.init().isPresent()) {
      append(" = ");
      varDecl.init().get().accept(this, null);
    }
  }

  private void printAnnos(ImmutableList<Anno> annos) {
    for (Tree.Anno anno : annos) {
      anno.accept(this, null);
      append(' ');
    }
  }

  @Override
  public Void visitMethDecl(Tree.MethDecl methDecl, Void input) {
    for (Tree.Anno anno : methDecl.annos()) {
      anno.accept(this, null);
      printLine();
    }
    printModifiers(methDecl.mods());
    if (!methDecl.typarams().isEmpty()) {
      append('<');
      boolean first = true;
      for (Tree.TyParam t : methDecl.typarams()) {
        if (!first) {
          append(", ");
        }
        t.accept(this, null);
        first = false;
      }
      append('>');
      append(' ');
    }
    if (methDecl.ret().isPresent()) {
      methDecl.ret().get().accept(this, null);
      append(' ');
    }
    append(methDecl.name().value());
    append('(');
    boolean first = true;
    for (Tree.VarDecl param : methDecl.params()) {
      if (!first) {
        append(", ");
      }
      printVarDecl(param);
      first = false;
    }
    append(')');
    if (!methDecl.exntys().isEmpty()) {
      append(" throws ");
      first = true;
      for (Tree.Type e : methDecl.exntys()) {
        if (!first) {
          append(", ");
        }
        e.accept(this, null);
        first = false;
      }
    }
    if (methDecl.defaultValue().isPresent()) {
      append(" default ");
      methDecl.defaultValue().get().accept(this, null);
      append(";");
    } else if (methDecl.mods().contains(TurbineModifier.ABSTRACT)
        || methDecl.mods().contains(TurbineModifier.NATIVE)) {
      append(";");
    } else {
      append(" {}");
    }
    return null;
  }

  @Override
  public Void visitAnno(Tree.Anno anno, Void input) {
    append('@');
    append(Joiner.on('.').join(anno.name()));
    if (!anno.args().isEmpty()) {
      append('(');
      boolean first = true;
      for (Tree.Expression e : anno.args()) {
        if (!first) {
          append(", ");
        }
        e.accept(this, null);
        first = false;
      }
      append(')');
    }
    return null;
  }

  @Override
  public Void visitTyDecl(Tree.TyDecl tyDecl, Void input) {
    for (Tree.Anno anno : tyDecl.annos()) {
      anno.accept(this, null);
      printLine();
    }
    printModifiers(tyDecl.mods());
    switch (tyDecl.tykind()) {
      case CLASS:
        append("class");
        break;
      case INTERFACE:
        append("interface");
        break;
      case ENUM:
        append("enum");
        break;
      case ANNOTATION:
        append("@interface");
        break;
    }
    append(' ').append(tyDecl.name().value());
    if (!tyDecl.typarams().isEmpty()) {
      append('<');
      boolean first = true;
      for (Tree.TyParam t : tyDecl.typarams()) {
        if (!first) {
          append(", ");
        }
        t.accept(this, null);
        first = false;
      }
      append('>');
    }
    if (tyDecl.xtnds().isPresent()) {
      append(" extends ");
      tyDecl.xtnds().get().accept(this, null);
    }
    if (!tyDecl.impls().isEmpty()) {
      append(" implements ");
      boolean first = true;
      for (Tree.ClassTy t : tyDecl.impls()) {
        if (!first) {
          append(", ");
        }
        t.accept(this, null);
        first = false;
      }
    }
    append(" {").append('\n');
    indent++;
    switch (tyDecl.tykind()) {
      case ENUM:
        {
          List<Tree> nonConsts = new ArrayList<>();
          for (Tree t : tyDecl.members()) {
            if (t instanceof Tree.VarDecl) {
              Tree.VarDecl decl = (Tree.VarDecl) t;
              if (decl.mods().contains(TurbineModifier.ACC_ENUM)) {
                append(decl.name().value()).append(',').append('\n');
                continue;
              }
            }
            nonConsts.add(t);
          }
          printLine(";");
          boolean first = true;
          for (Tree t : nonConsts) {
            if (!first) {
              printLine();
            }
            t.accept(this, null);
            first = false;
          }
          break;
        }
      default:
        {
          boolean first = true;
          for (Tree t : tyDecl.members()) {
            if (!first) {
              printLine();
            }
            t.accept(this, null);
            first = false;
          }
          break;
        }
    }
    indent--;
    printLine("}");
    return null;
  }

  private void printModifiers(ImmutableSet<TurbineModifier> mods) {
    List<TurbineModifier> modifiers = new ArrayList<>(mods);
    Collections.sort(modifiers);
    for (TurbineModifier mod : modifiers) {
      switch (mod) {
        case PRIVATE:
        case PROTECTED:
        case PUBLIC:
        case ABSTRACT:
        case FINAL:
        case STATIC:
        case VOLATILE:
        case SYNCHRONIZED:
        case STRICTFP:
        case NATIVE:
        case TRANSIENT:
        case DEFAULT:
        case TRANSITIVE:
          append(mod.toString()).append(' ');
          break;
        case ACC_SUPER:
        case VARARGS:
        case INTERFACE:
        case ACC_ENUM:
        case ACC_ANNOTATION:
        case ACC_SYNTHETIC:
        case ACC_BRIDGE:
          break;
      }
    }
  }

  @Override
  public Void visitTyParam(Tree.TyParam tyParam, Void input) {
    printAnnos(tyParam.annos());
    append(tyParam.name().value());
    if (!tyParam.bounds().isEmpty()) {
      append(" extends ");
      boolean first = true;
      for (Tree bound : tyParam.bounds()) {
        if (!first) {
          append(" & ");
        }
        bound.accept(this, null);
        first = false;
      }
    }
    return null;
  }

  @Override
  public Void visitPkgDecl(Tree.PkgDecl pkgDecl, Void input) {
    for (Tree.Anno anno : pkgDecl.annos()) {
      anno.accept(this, null);
      printLine();
    }
    append("package ").append(Joiner.on('.').join(pkgDecl.name())).append(';');
    return null;
  }

  @Override
  public Void visitModDecl(ModDecl modDecl, Void input) {
    for (Tree.Anno anno : modDecl.annos()) {
      anno.accept(this, null);
      printLine();
    }
    if (modDecl.open()) {
      append("open ");
    }
    append("module ").append(modDecl.moduleName()).append(" {");
    indent++;
    append('\n');
    for (ModDirective directive : modDecl.directives()) {
      directive.accept(this, null);
    }
    indent--;
    append("}\n");
    return null;
  }

  @Override
  public Void visitModRequires(ModRequires modRequires, Void input) {
    append("requires ");
    printModifiers(modRequires.mods());
    append(modRequires.moduleName());
    append(";");
    append('\n');
    return null;
  }

  @Override
  public Void visitModExports(ModExports modExports, Void input) {
    append("exports ");
    append(modExports.packageName().replace('/', '.'));
    if (!modExports.moduleNames().isEmpty()) {
      append(" to").append('\n');
      indent += 2;
      boolean first = true;
      for (String moduleName : modExports.moduleNames()) {
        if (!first) {
          append(',').append('\n');
        }
        append(moduleName);
        first = false;
      }
      indent -= 2;
    }
    append(";");
    append('\n');
    return null;
  }

  @Override
  public Void visitModOpens(ModOpens modOpens, Void input) {
    append("opens ");
    append(modOpens.packageName().replace('/', '.'));
    if (!modOpens.moduleNames().isEmpty()) {
      append(" to").append('\n');
      indent += 2;
      boolean first = true;
      for (String moduleName : modOpens.moduleNames()) {
        if (!first) {
          append(',').append('\n');
        }
        append(moduleName);
        first = false;
      }
      indent -= 2;
    }
    append(";");
    append('\n');
    return null;
  }

  @Override
  public Void visitModUses(ModUses modUses, Void input) {
    append("uses ");
    append(Joiner.on('.').join(modUses.typeName()));
    append(";");
    append('\n');
    return null;
  }

  @Override
  public Void visitModProvides(ModProvides modProvides, Void input) {
    append("provides ");
    append(Joiner.on('.').join(modProvides.typeName()));
    if (!modProvides.implNames().isEmpty()) {
      append(" with").append('\n');
      indent += 2;
      boolean first = true;
      for (ImmutableList<Ident> implName : modProvides.implNames()) {
        if (!first) {
          append(',').append('\n');
        }
        append(Joiner.on('.').join(implName));
        first = false;
      }
      indent -= 2;
    }
    append(";");
    append('\n');
    return null;
  }
}