Printing.jrag 2.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
René Schöne's avatar
René Schöne committed
15
  eq ValueElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
16
17
18
    sb.append(getValue());
    return sb;
  }
René Schöne's avatar
René Schöne committed
19
  eq StringElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
20
21
22
23
    sb.append("\"").append(getValue()).append("\"");
    return sb;
  }
  eq ListElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
René Schöne's avatar
René Schöne committed
24
25
26
27
28
29
30
31
    if (isEmpty()) {
      sb.append("[]");
    } else {
      for (Element element : getElementList()) {
        sb.append(indent).append("- ");
        element.prettyPrint(sb, false, indent + PRINT_INDENT);
        sb.append("\n");
      }
32
33
34
35
36
37
38
39
      // 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(": ");
René Schöne's avatar
René Schöne committed
40
    if (getValue().isComplex() && !getValue().isEmpty()) {
41
42
43
44
45
46
47
48
      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) {
René Schöne's avatar
René Schöne committed
49
50
51
52
53
54
55
56
57
58
    if (isEmpty()) {
      sb.append("{}");
    } else {
      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");
      }
59
60
61
62
63
64
      // delete last newline
      sb.deleteCharAt(sb.length() - 1);
    }
    return sb;
  }
}