Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mustache
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
JastAdd
mustache
Commits
c10bed0d
Commit
c10bed0d
authored
4 years ago
by
René Schöne
Browse files
Options
Downloads
Patches
Plain Diff
Add collapsed printing and more convinience methods.
parent
c085995f
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Helpers.jrag
+60
-4
60 additions, 4 deletions
Helpers.jrag
Mustache.relast
+1
-1
1 addition, 1 deletion
Mustache.relast
Printing.jrag
+54
-14
54 additions, 14 deletions
Printing.jrag
with
115 additions
and
19 deletions
Helpers.jrag
+
60
−
4
View file @
c10bed0d
aspect Helpers {
aspect Helpers {
// --- of ---
public static ValueElement ValueElement.of(int value) {
public static ValueElement ValueElement.of(int value) {
return new ValueElement(String.valueOf(value));
return new ValueElement(
false,
String.valueOf(value));
}
}
public static ValueElement ValueElement.of(boolean value) {
public static ValueElement ValueElement.of(boolean value) {
return new ValueElement(String.valueOf(value));
return new ValueElement(
false,
String.valueOf(value));
}
}
public static ValueElement ValueElement.of(String value) {
public static ValueElement ValueElement.of(String value) {
return new ValueElement(value);
return new ValueElement(
false,
value);
}
}
public static StringElement StringElement.of(String value) {
public static StringElement StringElement.of(String value) {
return new StringElement(value);
return new StringElement(
false,
value);
}
}
// --- addKeyValuePair ---
public void MappingElement.addKeyValuePair(String key, Element value) {
public void MappingElement.addKeyValuePair(String key, Element value) {
addKeyValuePair(new KeyValuePair(key, value));
addKeyValuePair(new KeyValuePair(key, value));
}
}
// --- put ---
public MappingElement MappingElement.put(String key, int value) {
addKeyValuePair(key, ValueElement.of(value));
return this;
}
public MappingElement MappingElement.put(String key, boolean value) {
addKeyValuePair(key, ValueElement.of(value));
return this;
}
public MappingElement MappingElement.put(String key, String value) {
addKeyValuePair(key, makeStringElement(value));
return this;
}
public MappingElement MappingElement.put(String key, Element inner) {
addKeyValuePair(key, inner);
return this;
}
// --- add ---
public ListElement ListElement.add(int value) {
addElement(ValueElement.of(value));
return this;
}
public ListElement ListElement.add(boolean value) {
addElement(ValueElement.of(value));
return this;
}
public ListElement ListElement.add(String value) {
addElement(makeStringElement(value));
return this;
}
public ListElement ListElement.add(Element inner) {
addElement(inner);
return this;
}
// --- helper methods for put/add ---
protected SimpleElement ComplexElement.makeStringElement(String value) {
// simple test, check for special characters
return containsAny(value, "[{\"\n") ?
StringElement.of(value.replace("\n", "\\n").replace("\"", "\\\"")) :
ValueElement.of(value);
}
protected boolean ComplexElement.containsAny(String s, String searchChars) {
// from https://stackoverflow.com/a/54399334/2493208
java.util.Set<Character> charsToTestFor = searchChars.chars()
.mapToObj(ch -> Character.valueOf((char) ch))
.collect(java.util.stream.Collectors.toSet());
return s.chars().anyMatch(ch -> charsToTestFor.contains(Character.valueOf((char) ch)));
}
// --- getValue ---
public java.util.Optional<Element> MappingElement.getValue(String key) {
public java.util.Optional<Element> MappingElement.getValue(String key) {
for (KeyValuePair pair : getKeyValuePairList()) {
for (KeyValuePair pair : getKeyValuePairList()) {
if (pair.getKey().equals(key)) {
if (pair.getKey().equals(key)) {
...
...
This diff is collapsed.
Click to expand it.
Mustache.relast
+
1
−
1
View file @
c10bed0d
Document ::= <FileName> [RootElement:ComplexElement] ;
Document ::= <FileName> [RootElement:ComplexElement] ;
abstract Element ;
abstract Element
::= <Collapse:boolean>
;
abstract ComplexElement : Element ;
abstract ComplexElement : Element ;
MappingElement : ComplexElement ::= KeyValuePair* ;
MappingElement : ComplexElement ::= KeyValuePair* ;
KeyValuePair ::= <Key> Value:Element ;
KeyValuePair ::= <Key> Value:Element ;
...
...
This diff is collapsed.
Click to expand it.
Printing.jrag
+
54
−
14
View file @
c10bed0d
aspect Printing {
aspect Printing {
String ASTNode.PRINT_INDENT = " ";
String ASTNode.PRINT_INDENT = " ";
// --- isLast ---
inh boolean KeyValuePair.isLast();
inh boolean KeyValuePair.isLast();
inh boolean Element.isLast();
inh boolean Element.isLast();
eq MappingElement.getKeyValuePair(int i).isLast() = i == getNumKeyValuePair() - 1;
eq MappingElement.getKeyValuePair(int i).isLast() = i == getNumKeyValuePair() - 1;
eq ListElement.getElement(int i).isLast() = i == getNumElement() - 1;
eq ListElement.getElement(int i).isLast() = i == getNumElement() - 1;
eq Document.getRootElement().isLast() = true;
eq Document.getRootElement().isLast() = true;
// --- needTrailingNewLine ---
syn boolean KeyValuePair.needTrailingNewLine() = !this.isLast();
syn boolean KeyValuePair.needTrailingNewLine() = !this.isLast();
syn boolean Element.needTrailingNewLine() = !this.isLast() || containingListElement() == null;
syn boolean Element.needTrailingNewLine() = !this.isLast() || containingListElement() == null;
// --- isCollapsed ---
inh boolean Element.isCollapsed();
inh boolean KeyValuePair.isCollapsed();
eq Document.getRootElement().isCollapsed() = getRootElement().getCollapse();
eq MappingElement.getKeyValuePair(int i).isCollapsed() = isCollapsed();
eq ListElement.getElement(int i).isCollapsed() = getElement(i).getCollapse() || isCollapsed();
eq KeyValuePair.getValue().isCollapsed() = getValue().getCollapse() || isCollapsed();
public String Document.prettyPrint() {
public String Document.prettyPrint() {
return prettyPrint(true);
return prettyPrint(true);
}
}
...
@@ -46,11 +57,22 @@ aspect Printing {
...
@@ -46,11 +57,22 @@ aspect Printing {
if (isEmpty()) {
if (isEmpty()) {
sb.append("[]");
sb.append("[]");
} else {
} else {
for (Element element : getElementList()) {
if (isCollapsed()) {
sb.append(indent).append("- ");
sb.append("[");
element.prettyPrint(sb, false, indent + PRINT_INDENT);
for (Element element : getElementList()) {
if (element.needTrailingNewLine()) {
element.prettyPrint(sb, false, indent);
sb.append("\n");
if (!element.isLast()) {
sb.append(", ");
}
}
sb.append("]");
} else {
for (Element element : getElementList()) {
sb.append(indent).append("- ");
element.prettyPrint(sb, false, indent + PRINT_INDENT);
if (element.needTrailingNewLine()) {
sb.append("\n");
}
}
}
}
}
}
}
...
@@ -59,8 +81,15 @@ aspect Printing {
...
@@ -59,8 +81,15 @@ aspect Printing {
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(":");
if (isCollapsed()) {
if (getValue().isComplexElement() && !getValue().isEmpty()) {
sb.append("\"");
}
sb.append(getKey());
if (isCollapsed()) {
sb.append("\"");
}
sb.append(":");
if (getValue().isComplexElement() && !getValue().isEmpty() && !getValue().isCollapsed()) {
sb.append("\n");
sb.append("\n");
getValue().prettyPrint(sb, true, indent + PRINT_INDENT);
getValue().prettyPrint(sb, true, indent + PRINT_INDENT);
} else {
} else {
...
@@ -75,13 +104,24 @@ aspect Printing {
...
@@ -75,13 +104,24 @@ aspect Printing {
if (isEmpty()) {
if (isEmpty()) {
sb.append("{}");
sb.append("{}");
} else {
} else {
boolean first = true;
if (isCollapsed()) {
for (KeyValuePair pair : getKeyValuePairList()) {
sb.append("{");
if (!first || printIndent) sb.append(indent);
for (KeyValuePair pair : getKeyValuePairList()) {
first = false;
pair.prettyPrint(sb, false, indent);
pair.prettyPrint(sb, false, indent);
if (!pair.isLast()) {
if (pair.needTrailingNewLine()) {
sb.append(", ");
sb.append("\n");
}
}
sb.append("}");
} else {
boolean first = true;
for (KeyValuePair pair : getKeyValuePairList()) {
if (!first || printIndent) sb.append(indent);
first = false;
pair.prettyPrint(sb, false, indent);
if (pair.needTrailingNewLine()) {
sb.append("\n");
}
}
}
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment