Printing.jrag 1.94 KB
Newer Older
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
aspect Printing {
  String ASTNode.PRINT_INDENT = "  ";
  syn String Document.prettyPrint() {
    StringBuilder sb = new StringBuilder("#start\n");
    for (ComplexElement element : getComplexElementList()) {
      element.prettyPrint(sb, false, "");
    }
    if (sb.charAt(sb.length() - 1) != '\n') {
      sb.append("\n");
    }
    sb.append("#end");
    return sb.toString();
  }
  syn StringBuilder Element.prettyPrint(StringBuilder sb, boolean printIndent, String indent);
  eq StringElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
    sb.append(getValue());
    return sb;
  }
  eq QuotedStringElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
    sb.append("\"").append(getValue()).append("\"");
    return sb;
  }
  eq ListElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
    for (Element element : getElementList()) {
      sb.append(indent).append("- ");
      element.prettyPrint(sb, false, indent + PRINT_INDENT);
      sb.append("\n");
    }
    if (getNumElement() > 0) {
      // delete last newline
      sb.deleteCharAt(sb.length() - 1);
    }
    return sb;
  }
  eq KeyValuePair.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
    if (printIndent) sb.append(indent);
    sb.append(getKey()).append(": ");
    if (getValue().isComplex()) {
      sb.append("\n");
      getValue().prettyPrint(sb, true, indent + PRINT_INDENT);  //);
    } else {
      getValue().prettyPrint(sb, false, indent);
    }
    return sb;
  }
  eq MappingElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
    boolean first = true;
    for (KeyValuePair pair : getKeyValuePairList()) {
      if (!first || printIndent) sb.append(indent);
      first = false;
      pair.prettyPrint(sb, false, indent);  // + PRINT_INDENT
      sb.append("\n");
    }
    if (!first) {
      // delete last newline
      sb.deleteCharAt(sb.length() - 1);
    }
    return sb;
  }
}