Commit 1e30ed27 authored by René Schöne's avatar René Schöne
Browse files

WIP: Mustache2YAML

- finished transformation
- remove indentation from mustache templates
- store YAML in file when --printYaml is given
parent 3cd357c6
aspect Printing { aspect Printing {
String ASTNode.PRINT_INDENT = " "; String ASTNode.PRINT_INDENT = " ";
syn String Document.prettyPrint() { syn String Document.prettyPrint() {
StringBuilder sb = new StringBuilder("#start\n"); StringBuilder sb = new StringBuilder();
sb.append("# RagConnect created at ").append(java.time.Instant.now()).append("\n");
for (ComplexElement element : getComplexElementList()) { for (ComplexElement element : getComplexElementList()) {
element.prettyPrint(sb, false, ""); element.prettyPrint(sb, false, "");
} }
if (sb.charAt(sb.length() - 1) != '\n') { if (sb.charAt(sb.length() - 1) != '\n') {
sb.append("\n"); sb.append("\n");
} }
sb.append("#end");
return sb.toString(); return sb.toString();
} }
syn StringBuilder Element.prettyPrint(StringBuilder sb, boolean printIndent, String indent); syn StringBuilder Element.prettyPrint(StringBuilder sb, boolean printIndent, String indent);
......
...@@ -3,6 +3,7 @@ aspect MustacheNodesToYAML { ...@@ -3,6 +3,7 @@ aspect MustacheNodesToYAML {
Document doc = new Document(); Document doc = new Document();
MappingElement root = new MappingElement(); MappingElement root = new MappingElement();
root.addKeyValuePair("rootNodeName", StringElement.of(rootNodeName())); root.addKeyValuePair("rootNodeName", StringElement.of(rootNodeName()));
root.addKeyValuePair("closeMethod", StringElement.of(closeMethod()));
root.addKeyValuePair("usesMqtt", ValueElement.of(usesMqtt)); root.addKeyValuePair("usesMqtt", ValueElement.of(usesMqtt));
root.addKeyValuePair("usesRest", ValueElement.of(usesRest)); root.addKeyValuePair("usesRest", ValueElement.of(usesRest));
// mqtt // mqtt
...@@ -18,7 +19,7 @@ aspect MustacheNodesToYAML { ...@@ -18,7 +19,7 @@ aspect MustacheNodesToYAML {
inner.addKeyValuePair("name", StringElement.of(comp.name())); inner.addKeyValuePair("name", StringElement.of(comp.name()));
rootTypeComponents.addElement(inner); rootTypeComponents.addElement(inner);
} }
root.addKeyValuePair("rootTypeComponents", rootTypeComponents); root.addKeyValuePair("RootTypeComponents", rootTypeComponents);
// rest // rest
root.addKeyValuePair("restHandlerField", StringElement.of(restHandlerField())); root.addKeyValuePair("restHandlerField", StringElement.of(restHandlerField()));
...@@ -63,92 +64,93 @@ aspect MustacheNodesToYAML { ...@@ -63,92 +64,93 @@ aspect MustacheNodesToYAML {
return doc; return doc;
} }
syn Element MReceiveDefinition.toYAML() { syn MappingElement MEndpointDefinition.toYAML() {
MappingElement inner = new MappingElement(); MappingElement result = new MappingElement();
inner.addKeyValuePair("parentTypeName", StringElement.of(parentTypeName())); result.addKeyValuePair("parentTypeName", StringElement.of(parentTypeName()));
inner.addKeyValuePair("connectMethod", StringElement.of(connectMethod())); result.addKeyValuePair("connectMethod", StringElement.of(connectMethod()));
inner.addKeyValuePair("connectParameterName", StringElement.of(connectParameterName())); result.addKeyValuePair("connectParameterName", StringElement.of(connectParameterName()));
inner.addKeyValuePair("lastDefinitionToType", StringElement.of(lastDefinitionToType())); result.addKeyValuePair("lastDefinitionToType", StringElement.of(lastDefinitionToType()));
inner.addKeyValuePair("resultVarPrefix", StringElement.of(resultVarPrefix())); result.addKeyValuePair("resultVarPrefix", StringElement.of(resultVarPrefix()));
inner.addKeyValuePair("lastDefinitionName", StringElement.of(lastDefinitionName())); result.addKeyValuePair("lastDefinitionName", StringElement.of(lastDefinitionName()));
inner.addKeyValuePair("preemptiveReturn", StringElement.of(preemptiveReturn())); result.addKeyValuePair("preemptiveReturn", StringElement.of(preemptiveReturn()));
inner.addKeyValuePair("alwaysApply", ValueElement.of(alwaysApply())); result.addKeyValuePair("alwaysApply", ValueElement.of(alwaysApply()));
inner.addKeyValuePair("condition", StringElement.of(condition())); result.addKeyValuePair("condition", StringElement.of(
inner.addKeyValuePair("loggingEnabledForReads", ValueElement.of(loggingEnabledForReads)); condition().replace("\"", "\\\"").replace("\n", "\\n")));
inner.addKeyValuePair("tokenName", StringElement.of(tokenName())); result.addKeyValuePair("lastResult", StringElement.of(lastResult()));
inner.addKeyValuePair("lastResult", StringElement.of(lastResult())); result.addKeyValuePair("InnerMappingDefinitions", innerMappingDefinitionsAsListElement());
// inner.addKeyValuePair("first", ValueElement.of(isFirst())); result.addKeyValuePair("tokenName", StringElement.of(tokenName()));
// parentTypeName return result;
// connectMethod }
// connectParameterName
// lastDefinitionToType syn MappingElement MReceiveDefinition.toYAML() {
// resultVarPrefix MappingElement result = super.toYAML();
// lastDefinitionName result.addKeyValuePair("loggingEnabledForReads", ValueElement.of(loggingEnabledForReads));
// #InnerMappingDefinitions -> MInnerMappingDefinition return result;
// last }
// toType
// methodName syn MappingElement MSendDefinition.toYAML() {
// inputVarName MappingElement result = super.toYAML();
// preemptiveReturn result.addKeyValuePair("sender", StringElement.of(sender()));
// alwaysApply result.addKeyValuePair("lastValue", StringElement.of(lastValue()));
// condition - special chars result.addKeyValuePair("loggingEnabledForWrites", ValueElement.of(loggingEnabledForWrites));
// loggingEnabledForReads result.addKeyValuePair("updateMethod", StringElement.of(updateMethod()));
// tokenName result.addKeyValuePair("writeMethod", StringElement.of(writeMethod()));
// lastResult result.addKeyValuePair("tokenResetMethod", StringElement.of(tokenResetMethod()));
return inner; return result;
}
syn Element MSendDefinition.toYAML() {
MappingElement inner = new MappingElement();
// #SendDefinitions -> MSendDefinition
// parentTypeName
// sender
// lastValue
// connectMethod
// connectParameterName
// loggingEnabledForWrites
// updateMethod
// writeMethod
// tokenResetMethod
// lastResult
return inner;
} }
syn Element MMappingDefinition.toYAML() { syn Element MMappingDefinition.toYAML() {
MappingElement inner = new MappingElement(); MappingElement result = new MappingElement();
// #MappingDefinitions -> MMappingDefinition result.addKeyValuePair("toType", StringElement.of(toType()));
// toType result.addKeyValuePair("methodName", StringElement.of(methodName()));
// methodName result.addKeyValuePair("fromType", StringElement.of(fromType()));
// fromType result.addKeyValuePair("fromVariableName", StringElement.of(fromVariableName()));
// fromVariableName result.addKeyValuePair("content", StringElement.of(
// content - special chars content().replace("\"", "\\\"").replace("\n", "\\n")));
return inner; return result;
} }
syn Element MDependencyDefinition.toYAML() { syn Element MDependencyDefinition.toYAML() {
MappingElement inner = new MappingElement(); MappingElement result = new MappingElement();
// #DependencyDefinitions -> MDependencyDefinition result.addKeyValuePair("targetParentTypeName", StringElement.of(targetParentTypeName()));
// targetParentTypeName result.addKeyValuePair("dependencyMethod", StringElement.of(dependencyMethod()));
// dependencyMethod result.addKeyValuePair("sourceParentTypeName", StringElement.of(sourceParentTypeName()));
// sourceParentTypeName result.addKeyValuePair("internalRelationPrefix", StringElement.of(internalRelationPrefix()));
// internalRelationPrefix return result;
return inner;
} }
syn Element MTokenComponent.toYAML() { syn Element MTokenComponent.toYAML() {
MappingElement result = new MappingElement();
result.addKeyValuePair("parentTypeName", StringElement.of(parentTypeName()));
result.addKeyValuePair("name", StringElement.of(name()));
result.addKeyValuePair("javaType", StringElement.of(javaType()));
result.addKeyValuePair("internalName", StringElement.of(internalName()));
ListElement dependencyDefinitions = new ListElement();
for (MDependencyDefinition def : getDependencyDefinitionList()) {
MappingElement inner = new MappingElement(); MappingElement inner = new MappingElement();
// #TokenComponents -> MTokenComponent inner.addKeyValuePair("targetParentTypeName", StringElement.of(def.targetParentTypeName()));
// parentTypeName inner.addKeyValuePair("internalRelationPrefix", StringElement.of(def.internalRelationPrefix()));
// name MappingElement targetEndpointDefinition = new MappingElement();
// javaType targetEndpointDefinition.addKeyValuePair("updateMethod", StringElement.of(def.targetEndpointDefinition().updateMethod()));
// internalName targetEndpointDefinition.addKeyValuePair("writeMethod", StringElement.of(def.targetEndpointDefinition().writeMethod()));
// #DependencyDefinitions -> MDependencyDefinition inner.addKeyValuePair("targetEndpointDefinition", targetEndpointDefinition);
// targetParentTypeName dependencyDefinitions.addElement(inner);
// internalRelationPrefix }
// #targetEndpointDefinition -> MSendDefinition result.addKeyValuePair("DependencyDefinitions", dependencyDefinitions);
// updateMethod return result;
// writeMethod }
return inner;
ListElement MEndpointDefinition.innerMappingDefinitionsAsListElement() {
ListElement innerMappingDefinitions = new ListElement();
for (MInnerMappingDefinition def : getInnerMappingDefinitionList()) {
MappingElement inner = new MappingElement();
inner.addKeyValuePair("toType", StringElement.of(def.toType()));
inner.addKeyValuePair("methodName", StringElement.of(def.methodName()));
inner.addKeyValuePair("inputVarName", StringElement.of(def.inputVarName()));
inner.addKeyValuePair("last", ValueElement.of(def.isLast()));
innerMappingDefinitions.addElement(inner);
}
return innerMappingDefinitions;
} }
} }
...@@ -70,7 +70,9 @@ public class Compiler extends AbstractCompiler { ...@@ -70,7 +70,9 @@ public class Compiler extends AbstractCompiler {
if (optionPrintYaml.value()) { if (optionPrintYaml.value()) {
ASTNode.rootNode = ragConnect.getProgram().resolveTypeDecl(optionRootNode.value()); ASTNode.rootNode = ragConnect.getProgram().resolveTypeDecl(optionRootNode.value());
System.out.println(ragConnect.toMustache().toYAML().prettyPrint()); String yamlContent = ragConnect.toMustache().toYAML().prettyPrint();
System.out.println(yamlContent);
writeToFile(getConfiguration().outputDir().toPath().resolve("RagConnect.yml"), yamlContent);
return 0; return 0;
} }
...@@ -98,9 +100,9 @@ public class Compiler extends AbstractCompiler { ...@@ -98,9 +100,9 @@ public class Compiler extends AbstractCompiler {
} }
for (GrammarFile grammarFile : ragConnect.getProgram().getGrammarFileList()) { for (GrammarFile grammarFile : ragConnect.getProgram().getGrammarFileList()) {
Path outputFile = getConfiguration().outputDir().toPath().resolve(grammarFile.getFileName()); Path outputFile = getConfiguration().outputDir().toPath().resolve(grammarFile.getFileName());
sendToFile(outputFile, grammarFile.generateAbstractGrammar()); writeToFile(outputFile, grammarFile.generateAbstractGrammar());
} }
sendToFile(getConfiguration().outputDir().toPath().resolve("RagConnect.jadd"), ragConnect.generateAspect(optionRootNode.value())); writeToFile(getConfiguration().outputDir().toPath().resolve("RagConnect.jadd"), ragConnect.generateAspect(optionRootNode.value()));
return 0; return 0;
} }
...@@ -139,7 +141,7 @@ public class Compiler extends AbstractCompiler { ...@@ -139,7 +141,7 @@ public class Compiler extends AbstractCompiler {
System.out.println(message); System.out.println(message);
} }
private void sendToFile(Path path, String str) throws CompilerException { private void writeToFile(Path path, String str) throws CompilerException {
try (BufferedWriter writer = Files.newBufferedWriter(path)) { try (BufferedWriter writer = Files.newBufferedWriter(path)) {
writer.append(str); writer.append(str);
} catch (Exception e) { } catch (Exception e) {
......
public void {{targetParentTypeName}}.{{dependencyMethod}}({{sourceParentTypeName}} source) { public void {{targetParentTypeName}}.{{dependencyMethod}}({{sourceParentTypeName}} source) {
add{{internalRelationPrefix}}Source(source); add{{internalRelationPrefix}}Source(source);
} }
String scheme,host, path; String scheme,host, path;
java.net.URI uri; java.net.URI uri;
try { try {
uri = new java.net.URI({{connectParameterName}}); uri = new java.net.URI({{connectParameterName}});
scheme = uri.getScheme(); scheme = uri.getScheme();
host = uri.getHost(); host = uri.getHost();
path = uri.getPath(); path = uri.getPath();
} catch (java.net.URISyntaxException e) { } catch (java.net.URISyntaxException e) {
System.err.println(e.getMessage()); // Maybe re-throw error? System.err.println(e.getMessage()); // Maybe re-throw error?
return false; return false;
} }
protected static {{toType}} ASTNode.{{methodName}}({{fromType}} {{fromVariableName}}) throws Exception { protected static {{toType}} ASTNode.{{methodName}}({{fromType}} {{fromVariableName}}) throws Exception {
{{{content}}}{{!maybe print line by line to get better indentation}} {{{content}}}{{!maybe print line by line to get better indentation}}
} }
public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}) throws java.io.IOException { public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}) throws java.io.IOException {
{{>handleUri}} {{>handleUri}}
java.util.function.Consumer<byte[]> consumer = message -> { java.util.function.Consumer<byte[]> consumer = message -> {
{{> mappingApplication}} {{> mappingApplication}}
...@@ -20,4 +20,4 @@ ...@@ -20,4 +20,4 @@
System.err.println("Unknown protocol '" + scheme + "'."); System.err.println("Unknown protocol '" + scheme + "'.");
return false; return false;
} }
} }
private Runnable {{parentTypeName}}.{{sender}} = null; private Runnable {{parentTypeName}}.{{sender}} = null;
private byte[] {{parentTypeName}}.{{lastValue}} = null; private byte[] {{parentTypeName}}.{{lastValue}} = null;
public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}, boolean writeCurrentValue) { public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}, boolean writeCurrentValue) {
{{>handleUri}} {{>handleUri}}
switch (scheme) { switch (scheme) {
{{#usesMqtt}} {{#usesMqtt}}
...@@ -36,16 +36,16 @@ ...@@ -36,16 +36,16 @@
return false; return false;
} }
return true; return true;
} }
protected boolean {{parentTypeName}}.{{updateMethod}}() { protected boolean {{parentTypeName}}.{{updateMethod}}() {
{{tokenResetMethod}}(); {{tokenResetMethod}}();
{{> mappingApplication}} {{> mappingApplication}}
{{lastValue}} = {{lastResult}}; {{lastValue}} = {{lastResult}};
// normally we would return true here. unless no connect method was called so far to initialize {{sender}} yet // normally we would return true here. unless no connect method was called so far to initialize {{sender}} yet
return {{sender}} != null; return {{sender}} != null;
} }
protected void {{parentTypeName}}.{{writeMethod}}() { protected void {{parentTypeName}}.{{writeMethod}}() {
{{sender}}.run(); {{sender}}.run();
} }
public {{parentTypeName}} {{parentTypeName}}.set{{name}}({{javaType}} value) { public {{parentTypeName}} {{parentTypeName}}.set{{name}}({{javaType}} value) {
set{{internalName}}(value); set{{internalName}}(value);
{{#DependencyDefinitions}} {{#DependencyDefinitions}}
for ({{targetParentTypeName}} target : get{{internalRelationPrefix}}TargetList()) { for ({{targetParentTypeName}} target : get{{internalRelationPrefix}}TargetList()) {
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
} }
{{/DependencyDefinitions}} {{/DependencyDefinitions}}
return this; return this;
} }
public {{javaType}} {{parentTypeName}}.get{{name}}() { public {{javaType}} {{parentTypeName}}.get{{name}}() {
return get{{internalName}}(); return get{{internalName}}();
} }
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