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

Add YAML grammar and printing for it.

parent cd2ce77b
......@@ -74,6 +74,7 @@ task relast(type: JavaExec) {
"../relast.preprocessor/src/main/jastadd/RelAst.relast",
"./src/main/jastadd/RagConnect.relast",
"./src/main/jastadd/MustacheNodes.relast",
"./src/main/jastadd/YAML.relast",
"--listClass=java.util.ArrayList",
"--jastAddList=JastAddList",
"--useJastAddNames",
......@@ -83,9 +84,10 @@ task relast(type: JavaExec) {
]
inputs.files("../libs/relast.jar",
"../relast.preprocessor/src/main/jastadd/RelAST.relast",
"../relast.preprocessor/src/main/jastadd/RelAst.relast",
"./src/main/jastadd/RagConnect.relast",
"./src/main/jastadd/MustacheNodes.relast")
"./src/main/jastadd/MustacheNodes.relast",
"./src/main/jastadd/YAML.relast")
outputs.files("./src/gen/jastadd/RagConnect.ast",
"./src/gen/jastadd/RagConnect.jadd",
"./src/gen/jastadd/RagConnectRefResolver.jadd",
......
......@@ -15,6 +15,7 @@ aspect Navigation {
eq MRagConnect.getChild().containedFile() = null;
// --- containedFileName ---
eq Grammar.getChild().containedFileName() = null; // should be in PP
eq RagConnect.getChild().containedFileName() = getFileName();
eq MRagConnect.getChild().containedFileName() = null;
......
aspect Printing {
String ASTNode.PRINT_INDENT = " ";
syn String Document.prettyPrint() {
StringBuilder sb = new StringBuilder("#start\n");
for (ComplexElement element : getComplexElementList()) {
element.prettyPrint(sb, false, "");
}
if (sb.charAt(sb.length() - 1) != '\n') {
sb.append("\n");
}
sb.append("#end");
return sb.toString();
}
syn StringBuilder Element.prettyPrint(StringBuilder sb, boolean printIndent, String indent);
eq StringElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
sb.append(getValue());
return sb;
}
eq QuotedStringElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
sb.append("\"").append(getValue()).append("\"");
return sb;
}
eq ListElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
for (Element element : getElementList()) {
sb.append(indent).append("- ");
element.prettyPrint(sb, false, indent + PRINT_INDENT);
sb.append("\n");
}
if (getNumElement() > 0) {
// delete last newline
sb.deleteCharAt(sb.length() - 1);
}
return sb;
}
eq KeyValuePair.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
if (printIndent) sb.append(indent);
sb.append(getKey()).append(": ");
if (getValue().isComplex()) {
sb.append("\n");
getValue().prettyPrint(sb, true, indent + PRINT_INDENT); //);
} else {
getValue().prettyPrint(sb, false, indent);
}
return sb;
}
eq MappingElement.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
boolean first = true;
for (KeyValuePair pair : getKeyValuePairList()) {
if (!first || printIndent) sb.append(indent);
first = false;
pair.prettyPrint(sb, false, indent); // + PRINT_INDENT
sb.append("\n");
}
if (!first) {
// delete last newline
sb.deleteCharAt(sb.length() - 1);
}
return sb;
}
}
Document ::= <FileName> ComplexElement* ;
abstract Element ;
abstract ComplexElement : Element ;
MappingElement : ComplexElement ::= KeyValuePair* ;
KeyValuePair : ComplexElement ::= <Key> Value:Element ;
ListElement : ComplexElement ::= Element* ;
abstract SimpleElement : Element ;
StringElement : SimpleElement ::= <Value> ;
QuotedStringElement : SimpleElement ::= <Value> ;
aspect Navigation {
eq Document.getChild().program() = null;
eq Document.getChild().ragconnect() = null;
eq Document.getChild().containedFile() = null;
eq Document.getChild().containedFileName() = getFileName();
syn boolean Element.isComplex() = false;
eq ComplexElement.isComplex() = true;
}
package org.jastadd.ragconnect.compiler;
import org.jastadd.ragconnect.ast.*;
/**
* Testing Ros2Rag without parser.
*
* @author rschoene - Initial contribution
*/
public class SimpleMain {
private static void printManualYAML() {
Document doc = new Document();
KeyValuePair root = new KeyValuePair();
root.setKey("panda_mqtt_connector");
MappingElement firstLevel = new MappingElement();
firstLevel.addKeyValuePair(new KeyValuePair("server", new QuotedStringElement("tcp://localhost:1883")));
firstLevel.addKeyValuePair(new KeyValuePair("robot_speed_factor", new StringElement(".7")));
KeyValuePair topics = new KeyValuePair();
topics.setKey("topics");
MappingElement theTopics = new MappingElement();
theTopics.addKeyValuePair(new KeyValuePair("robotConfig", new QuotedStringElement("robotconfig")));
theTopics.addKeyValuePair(new KeyValuePair("trajectory", new QuotedStringElement("trajectory")));
theTopics.addKeyValuePair(new KeyValuePair("nextStep", new QuotedStringElement("ros2rag/nextStep")));
topics.setValue(theTopics);
firstLevel.addKeyValuePair(topics);
firstLevel.addKeyValuePair(new KeyValuePair("zone_size", new StringElement("0.5")));
KeyValuePair zones = new KeyValuePair();
zones.setKey("zones");
ListElement theZones = new ListElement();
theZones.addElement(new QuotedStringElement("1 1"));
theZones.addElement(new QuotedStringElement("0 1"));
theZones.addElement(new QuotedStringElement("-1 1"));
zones.setValue(theZones);
firstLevel.addKeyValuePair(zones);
KeyValuePair parts = new KeyValuePair();
parts.setKey("end_effectors");
KeyValuePair pandaParts = new KeyValuePair();
pandaParts.setKey("panda");
MappingElement thePanda = new MappingElement();
thePanda.addKeyValuePair(new KeyValuePair("Link0", new QuotedStringElement("panda_link0")));
thePanda.addKeyValuePair(new KeyValuePair("Link1", new QuotedStringElement("panda_link1")));
thePanda.addKeyValuePair(new KeyValuePair("Link2", new QuotedStringElement("panda_link2")));
thePanda.addKeyValuePair(new KeyValuePair("Link3", new QuotedStringElement("panda_link3")));
thePanda.addKeyValuePair(new KeyValuePair("Link4", new QuotedStringElement("panda_link4")));
thePanda.addKeyValuePair(new KeyValuePair("Link5", new QuotedStringElement("panda_link5")));
thePanda.addKeyValuePair(new KeyValuePair("Link6", new QuotedStringElement("panda_link6")));
thePanda.addKeyValuePair(new KeyValuePair("RightFinger", new QuotedStringElement("panda_rightfinger")));
thePanda.addKeyValuePair(new KeyValuePair("LeftFinger", new QuotedStringElement("panda_leftfinger")));
pandaParts.setValue(thePanda);
parts.setValue(pandaParts);
firstLevel.addKeyValuePair(parts);
KeyValuePair end_effectors = new KeyValuePair();
end_effectors.setKey("end_effectors");
KeyValuePair endEffectorParts = new KeyValuePair();
endEffectorParts.setKey("panda");
endEffectorParts.setValue(new KeyValuePair("EndEffector", new QuotedStringElement("panda_hand")));
end_effectors.setValue(endEffectorParts);
firstLevel.addKeyValuePair(end_effectors);
KeyValuePair goalPoses = new KeyValuePair();
goalPoses.setKey("goal_poses");
ListElement theGoalPoses = new ListElement();
addPose(theGoalPoses, "0.4 0.4 0.3");
addPose(theGoalPoses, "-0.4 0.4 0.3");
addPose(theGoalPoses, "-0.4 -0.4 0.3");
addPose(theGoalPoses, "0.4 0.4 0.3");
addPose(theGoalPoses, "-0.4 0.4 0.3");
addPose(theGoalPoses, "0.4 0.4 0.3");
goalPoses.setValue(theGoalPoses);
firstLevel.addKeyValuePair(goalPoses);
root.setValue(firstLevel);
doc.addComplexElement(root);
System.out.println(doc.prettyPrint());
}
private static void addPose(ListElement theGoalPoses, String position) {
MappingElement goalPose1 = new MappingElement();
goalPose1.addKeyValuePair(new KeyValuePair("position", new QuotedStringElement(position)));
goalPose1.addKeyValuePair(new KeyValuePair("orientation", new QuotedStringElement("1 1 0 0")));
goalPose1.addKeyValuePair(new KeyValuePair("work", new QuotedStringElement("20000")));
theGoalPoses.addElement(goalPose1);
}
public static void main(String[] args) {
printManualYAML();
}
}
Markdown is supported
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