Commit 130a96ef authored by René Schöne's avatar René Schöne
Browse files

Printing without deleting newlines.

parent 2bf86903
......@@ -2,7 +2,7 @@ Document ::= <FileName> [RootElement:ComplexElement] ;
abstract Element ;
abstract ComplexElement : Element ;
MappingElement : ComplexElement ::= KeyValuePair* ;
KeyValuePair : ComplexElement ::= <Key> Value:Element ;
KeyValuePair ::= <Key> Value:Element ;
ListElement : ComplexElement ::= Element* ;
abstract SimpleElement : Element ;
ValueElement : SimpleElement ::= <Value> ;
......
......@@ -5,4 +5,10 @@ aspect Navigation {
syn boolean Element.isEmpty() = false;
eq MappingElement.isEmpty() = getNumKeyValuePair() == 0;
eq ListElement.isEmpty() = getNumElement() == 0;
inh ListElement Element.containingListElement();
eq ListElement.getElement().containingListElement() = this;
eq KeyValuePair.getValue().containingListElement() = null;
eq Document.getRootElement().containingListElement() = null;
}
aspect Printing {
String ASTNode.PRINT_INDENT = " ";
inh boolean KeyValuePair.isLast();
inh boolean Element.isLast();
eq MappingElement.getKeyValuePair(int i).isLast() = i == getNumKeyValuePair() - 1;
eq ListElement.getElement(int i).isLast() = i == getNumElement() - 1;
eq Document.getRootElement().isLast() = true;
syn boolean KeyValuePair.needTrailingNewLine() = !this.isLast();
syn boolean Element.needTrailingNewLine() = !this.isLast() || containingListElement() == null;
public String Document.prettyPrint() {
return prettyPrint(true);
}
......@@ -8,7 +16,7 @@ aspect Printing {
public String Document.prettyPrint(boolean prependCreationComment) {
StringBuilder sb = new StringBuilder();
if (prependCreationComment) {
sb.append("# RagConnect created at ").append(java.time.Instant.now()).append("\n");
sb.append("# Created at ").append(java.time.Instant.now()).append("\n");
}
if (hasRootElement()) {
getRootElement().prettyPrint(sb, false, "");
......@@ -41,21 +49,20 @@ aspect Printing {
for (Element element : getElementList()) {
sb.append(indent).append("- ");
element.prettyPrint(sb, false, indent + PRINT_INDENT);
sb.append("\n");
if (element.needTrailingNewLine()) {
sb.append("\n");
}
}
// delete last newline
sb.deleteCharAt(sb.length() - 1);
}
return sb;
}
@Override
protected StringBuilder KeyValuePair.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
if (printIndent) sb.append(indent);
sb.append(getKey()).append(":");
if (getValue().isComplex() && !getValue().isEmpty()) {
sb.append("\n");
getValue().prettyPrint(sb, true, indent + PRINT_INDENT); //);
getValue().prettyPrint(sb, true, indent + PRINT_INDENT);
} else {
sb.append(" ");
getValue().prettyPrint(sb, false, indent);
......@@ -72,11 +79,11 @@ aspect Printing {
for (KeyValuePair pair : getKeyValuePairList()) {
if (!first || printIndent) sb.append(indent);
first = false;
pair.prettyPrint(sb, false, indent); // + PRINT_INDENT
sb.append("\n");
pair.prettyPrint(sb, false, indent);
if (pair.needTrailingNewLine()) {
sb.append("\n");
}
}
// delete last newline
sb.deleteCharAt(sb.length() - 1);
}
return sb;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment