Commits (1)
...@@ -2,7 +2,7 @@ Document ::= <FileName> [RootElement:ComplexElement] ; ...@@ -2,7 +2,7 @@ Document ::= <FileName> [RootElement:ComplexElement] ;
abstract Element ; abstract Element ;
abstract ComplexElement : Element ; abstract ComplexElement : Element ;
MappingElement : ComplexElement ::= KeyValuePair* ; MappingElement : ComplexElement ::= KeyValuePair* ;
KeyValuePair : ComplexElement ::= <Key> Value:Element ; KeyValuePair ::= <Key> Value:Element ;
ListElement : ComplexElement ::= Element* ; ListElement : ComplexElement ::= Element* ;
abstract SimpleElement : Element ; abstract SimpleElement : Element ;
ValueElement : SimpleElement ::= <Value> ; ValueElement : SimpleElement ::= <Value> ;
......
...@@ -5,4 +5,10 @@ aspect Navigation { ...@@ -5,4 +5,10 @@ aspect Navigation {
syn boolean Element.isEmpty() = false; syn boolean Element.isEmpty() = false;
eq MappingElement.isEmpty() = getNumKeyValuePair() == 0; eq MappingElement.isEmpty() = getNumKeyValuePair() == 0;
eq ListElement.isEmpty() = getNumElement() == 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 { aspect Printing {
String ASTNode.PRINT_INDENT = " "; 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() { public String Document.prettyPrint() {
return prettyPrint(true); return prettyPrint(true);
} }
...@@ -8,7 +16,7 @@ aspect Printing { ...@@ -8,7 +16,7 @@ aspect Printing {
public String Document.prettyPrint(boolean prependCreationComment) { public String Document.prettyPrint(boolean prependCreationComment) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (prependCreationComment) { 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()) { if (hasRootElement()) {
getRootElement().prettyPrint(sb, false, ""); getRootElement().prettyPrint(sb, false, "");
...@@ -41,21 +49,20 @@ aspect Printing { ...@@ -41,21 +49,20 @@ aspect Printing {
for (Element element : getElementList()) { for (Element element : getElementList()) {
sb.append(indent).append("- "); sb.append(indent).append("- ");
element.prettyPrint(sb, false, indent + PRINT_INDENT); 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; return sb;
} }
@Override
protected StringBuilder KeyValuePair.prettyPrint(StringBuilder sb, boolean printIndent, String indent) { protected StringBuilder KeyValuePair.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
if (printIndent) sb.append(indent); if (printIndent) sb.append(indent);
sb.append(getKey()).append(":"); sb.append(getKey()).append(":");
if (getValue().isComplex() && !getValue().isEmpty()) { if (getValue().isComplex() && !getValue().isEmpty()) {
sb.append("\n"); sb.append("\n");
getValue().prettyPrint(sb, true, indent + PRINT_INDENT); //); getValue().prettyPrint(sb, true, indent + PRINT_INDENT);
} else { } else {
sb.append(" "); sb.append(" ");
getValue().prettyPrint(sb, false, indent); getValue().prettyPrint(sb, false, indent);
...@@ -72,11 +79,11 @@ aspect Printing { ...@@ -72,11 +79,11 @@ aspect Printing {
for (KeyValuePair pair : getKeyValuePairList()) { for (KeyValuePair pair : getKeyValuePairList()) {
if (!first || printIndent) sb.append(indent); if (!first || printIndent) sb.append(indent);
first = false; first = false;
pair.prettyPrint(sb, false, indent); // + PRINT_INDENT pair.prettyPrint(sb, false, indent);
sb.append("\n"); if (pair.needTrailingNewLine()) {
sb.append("\n");
}
} }
// delete last newline
sb.deleteCharAt(sb.length() - 1);
} }
return sb; return sb;
} }
......