Printing.jrag 2.44 KB
Newer Older
1
2
aspect Printing {
  String ASTNode.PRINT_INDENT = "  ";
René Schöne's avatar
Cleanup    
René Schöne committed
3
4
5
6
7
8
9
10
11
12
13

  syn String MappingDefinitionType.prettyPrint();
  eq JavaMappingDefinitionType.prettyPrint() = getType().getName();
  eq JavaArrayMappingDefinitionType.prettyPrint() = getType().getName() + "[]";

  syn String JavaTypeUse.prettyPrint() {
    StringBuilder sb = new StringBuilder();
    generateAbstractGrammar(sb);
    return sb.toString();
  }

14
  syn String Document.prettyPrint() {
René Schöne's avatar
René Schöne committed
15
16
    StringBuilder sb = new StringBuilder();
    sb.append("# RagConnect created at ").append(java.time.Instant.now()).append("\n");
17
18
19
20
21
22
23
24
25
    for (ComplexElement element : getComplexElementList()) {
      element.prettyPrint(sb, false, "");
    }
    if (sb.charAt(sb.length() - 1) != '\n') {
      sb.append("\n");
    }
    return sb.toString();
  }
  syn StringBuilder Element.prettyPrint(StringBuilder sb, boolean printIndent, String indent);
René Schöne's avatar
René Schöne committed
26
  eq ValueElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
27
28
29
    sb.append(getValue());
    return sb;
  }
René Schöne's avatar
René Schöne committed
30
  eq StringElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
31
32
33
34
    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
35
36
37
38
39
40
41
42
    if (isEmpty()) {
      sb.append("[]");
    } else {
      for (Element element : getElementList()) {
        sb.append(indent).append("- ");
        element.prettyPrint(sb, false, indent + PRINT_INDENT);
        sb.append("\n");
      }
43
44
45
46
47
48
49
50
      // 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
51
    if (getValue().isComplex() && !getValue().isEmpty()) {
52
53
54
55
56
57
58
59
      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
60
61
62
63
64
65
66
67
68
69
    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");
      }
70
71
72
73
74
75
      // delete last newline
      sb.deleteCharAt(sb.length() - 1);
    }
    return sb;
  }
}