Skip to content
Snippets Groups Projects
Commit 5611db4d authored by René Schöne's avatar René Schöne
Browse files

WIP dumpAst 0.3.2

- working on dumping yaml
parent cfc5dc39
Branches
No related tags found
No related merge requests found
Pipeline #7941 passed
[submodule "relast.preprocessor"]
path = relast.preprocessor
url = ../relast-preprocessor.git
[submodule "dumpAst/src/main/jastadd/mustache"]
path = dumpAst/src/main/jastadd/mustache
url = ../mustache
......@@ -61,6 +61,7 @@ test {
}
File dumpAstGrammar = file('./src/main/jastadd/DumpAst.relast')
File mustacheGrammar = file('./src/main/jastadd/mustache/Mustache.relast')
task relast(type: JavaExec) {
group = 'Build'
......@@ -77,7 +78,7 @@ task relast(type: JavaExec) {
args = [
"../libs/relast.jar",
dumpAstGrammar,
// "./src/main/jastadd/MustacheNodes.relast",
mustacheGrammar,
"--listClass=java.util.ArrayList",
"--jastAddList=JastAddList",
"--useJastAddNames",
......@@ -87,7 +88,8 @@ task relast(type: JavaExec) {
]
inputs.files(file("../libs/relast.jar"),
dumpAstGrammar)
dumpAstGrammar,
mustacheGrammar)
outputs.files(file("./src/gen/jastadd/DumpAst.ast"),
file("./src/gen/jastadd/DumpAst.jadd"),
file("./src/gen/jastadd/DumpAstRefResolver.jadd"),
......
DumpAst ::= DumpNode* <PackageName> BuildConfig PrintConfig ;
rel DumpAst.RootNode -> DumpNode ;
BuildConfig ::= <TypeIgnore> <TokenIgnore> <ChildIgnore> <AttributeInclude> <NonterminalAttributeInclude> <RelationIgnore> <IncludeEmptyString:boolean> <Debug:boolean> ;
PrintConfig ::= <Scale:double> <Version> Header* ;
Header ::= <Value> ;
......
......@@ -180,8 +180,8 @@ import java.lang.String;aspect GenerationFrontend {
return this;
}
public DumpBuilder dumpAsYaml(java.nio.file.Path destination) throws java.io.IOException {
String content = build().toYaml();
public DumpBuilder dumpAsYaml(java.nio.file.Path destination, boolean prependCreationComment) throws java.io.IOException {
String content = build().toYaml(prependCreationComment);
try (java.io.Writer writer = java.nio.file.Files.newBufferedWriter(destination)) {
writer.write(content);
}
......@@ -201,6 +201,7 @@ aspect GenerationBackend {
protected DumpNode DumpAst.transform(TransformationTransferInformation tti, Object obj)
throws java.lang.reflect.InvocationTargetException, IllegalAccessException, NoSuchMethodException {
DumpNode result = transform(tti, obj, Source.ROOT);
setRootNode(result);
// post-process relationTargetsUnprocessed
boolean someAreUnprocessed = true;
while (someAreUnprocessed) {
......@@ -538,8 +539,62 @@ aspect GenerationBackend {
return sb.toString();
}
syn String DumpAst.toYaml() {
return "TODO :)";
syn String DumpAst.toYaml(boolean prependCreationComment) {
Document doc = new Document();
doc.setRootElement(getRootNode().toYaml());
return doc.prettyPrint(prependCreationComment);
}
syn MappingElement DumpNode.toYaml() {
MappingElement result = new MappingElement();
// tokens are key-value-pairs
for (DumpToken token : getDumpTokenList()) {
if (token.isDumpValueToken()) {
result.put(cleanLabel(token.label()), makeValueElement(token.asDumpValueToken().getValue()));
} else {
result.put(cleanLabel(token.label()), token.asDumpReferenceToken().getValue().toYaml());
}
}
// children
for (DumpChildNode child : getDumpChildNodeList()) {
ListElement list = new ListElement();
for (DumpNode inner : child.innerNodes(true)) {
list.add(inner.toYaml());
}
if (child.isList()) {
result.put(cleanLabel(child.label()), list);
} else if (list.getNumElement() == 1) {
result.put(cleanLabel(child.label()), list.getElement(0));
}
}
// relations
for (DumpRelation relation : getDumpRelationList()) {
ListElement list = new ListElement();
for (DumpNode inner : relation.innerNodes(true)) {
list.add(inner.toYaml());
}
if (relation.isList()) {
result.put(cleanLabel(relation.label()), list);
} else if (list.getNumElement() == 1) {
result.put(cleanLabel(relation.label()), list.getElement(0));
}
}
return result;
}
private static ComplexElement ASTNode.HELPER_COMPLEX_ELEMENT = new MappingElement();
protected static Element ASTNode.makeValueElement(Object obj) {
if (obj instanceof Integer) {
return ValueElement.of((int) obj);
} else if (obj instanceof Boolean) {
return ValueElement.of((boolean) obj);
} else {
return HELPER_COMPLEX_ELEMENT.makeStringElement(obj.toString());
}
}
private String DumpNode.cleanLabel(String originalLabel) {
return originalLabel.replace("()", "");
}
public class AppendableWriter extends java.io.Writer {
......
......@@ -13,6 +13,10 @@ aspect Navigation {
syn DumpValueToken DumpToken.asDumpValueToken() = null;
eq DumpValueToken.asDumpValueToken() = this;
// --- asDumpReferenceToken ---
syn DumpReferenceToken DumpToken.asDumpReferenceToken() = null;
eq DumpReferenceToken.asDumpReferenceToken() = this;
// --- buildConfig ---
inh BuildConfig DumpNode.buildConfig();
eq DumpAst.getChild().buildConfig() = getBuildConfig();
......
Subproject commit c10bed0d03e3fa18b8133ce1de48de7646899615
......@@ -13,7 +13,7 @@ buildscript {
}
dependencies {
runtime group: 'org.jastadd', name: 'jastadd', version: '2.3.4'
// runtime group: 'org.jastadd', name: 'jastadd', version: '2.3.4'
testImplementation project(':dumpAst')
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.4.0'
......
package de.tudresden.inf.st.jastadd.testDumper;
import org.jastadd.testDumper.ast.A;
import org.jastadd.testDumper.ast.B;
import org.jastadd.testDumper.ast.C;
import org.jastadd.testDumper.ast.Root;
public class TestDumperMain {
public static void main(String[] args) {
Root root = new Root();
root.setName("Root1");
A a = new A();
a.setName("A2");
B b1 = new B();
b1.setName("B3");
C c = new C();
c.setName("C4");
c.setRawReference(a);
b1.setOneA(a);
B b2 = new B();
b2.setName("B5");
C myC = new C();
myC.setName("C6");
a.setMyC(myC);
root.setA(a);
root.addB(b1);
root.addB(b2);
root.setC(c);
TestUtils.ExposingDumpBuilder builder = new TestUtils.ExposingDumpBuilder(root);
builder.includeAttributes("simpleAttr")
.includeNonterminalAttributes("getCalculated");
System.out.println(">> PlantUml");
System.out.println(builder.build().toPlantUml());
System.out.println(">> YAML begin");
System.out.println(builder.build().toYaml(true));
System.out.println(">> YAML end");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment