42 namespace mtc = ::moveit::task_constructor;
55 ~ScopedYamlEvent() { yaml_event_delete(&event_); }
56 operator yaml_event_t
const&()
const {
return event_; }
57 operator yaml_event_t&() {
return event_; }
68 static const int YAML_ERROR_EVENT = 255;
70 Parser(
const std::string& value);
76 static rviz::Property* createScalar(
const QString& name,
const QString& description,
const QByteArray& value,
80 bool noError()
const {
return parser_.error == YAML_NO_ERROR; }
82 int parse(yaml_event_t& event)
const;
84 rviz::Property* process(
const yaml_event_t& event,
const QString& name,
const QString& description,
87 inline static QByteArray byteArray(
const yaml_event_t& event) {
88 assert(event.type == YAML_SCALAR_EVENT);
89 return QByteArray::fromRawData(
reinterpret_cast<const char*
>(event.data.scalar.value), event.data.scalar.length);
92 static bool setValue(
rviz::Property* old,
const QByteArray& value);
99 mutable yaml_parser_t parser_;
102 Parser::Parser(
const std::string& value) {
103 yaml_parser_initialize(&parser_);
104 yaml_parser_set_input_string(&parser_,
reinterpret_cast<const yaml_char_t*
>(
value.c_str()),
value.size());
108 yaml_parser_delete(&parser_);
111 int Parser::parse(yaml_event_t& event)
const {
112 if (!yaml_parser_parse(&parser_, &event)) {
113 return YAML_ERROR_EVENT;
122 ScopedYamlEvent event;
123 switch (parse(event)) {
124 case YAML_ERROR_EVENT:
125 return Parser::createScalar(name, description,
"YAML error", old);
126 case YAML_STREAM_END_EVENT:
130 case YAML_SEQUENCE_START_EVENT:
131 case YAML_MAPPING_START_EVENT:
132 case YAML_SCALAR_EVENT:
133 return process(event, name, description, old);
140 return createScalar(name, description,
"undefined", old);
144 rviz::Property* Parser::process(
const yaml_event_t& event,
const QString& name,
const QString& description,
146 switch (event.type) {
147 case YAML_SEQUENCE_START_EVENT:
148 return processSequence(name, description, old);
149 case YAML_MAPPING_START_EVENT:
150 return processMapping(name, description, old);
151 case YAML_SCALAR_EVENT:
152 return createScalar(name, description, byteArray(event), old);
154 throw std::runtime_error(
"Unhandled YAML event");
159 bool Parser::setValue(
rviz::Property* old,
const QByteArray& value) {
162 double v =
value.toDouble(&ok);
176 rviz::Property* Parser::createScalar(
const QString& name,
const QString& description,
const QByteArray& value,
179 if (old && setValue(old, value)) {
187 double v =
value.toDouble(&ok);
214 root = createParent(name, description, root);
217 while (!stop && noError()) {
218 ScopedYamlEvent event;
219 switch (parse(event)) {
220 case YAML_MAPPING_END_EVENT:
224 case YAML_SCALAR_EVENT: {
225 QByteArray key = byteArray(event);
226 int num =
root->numChildren();
229 while (next < num && root->childAt(next)->
getName() < key)
232 root->removeChildren(index, next - index);
233 num =
root->numChildren();
237 if (old_child && old_child->
getName() != key)
241 switch (parse(event)) {
242 case YAML_MAPPING_START_EVENT:
243 case YAML_SEQUENCE_START_EVENT:
244 case YAML_SCALAR_EVENT:
245 new_child = process(event, key,
"", old_child);
248 new_child = createScalar(key,
"", parser_.problem, old_child);
249 root->setValue(
"YAML error");
253 if (new_child != old_child)
254 root->addChild(new_child, index);
260 root->setValue(
"YAML error");
266 root->removeChildren(index,
root->numChildren() - index);
272 root = createParent(name, description, root);
275 while (!stop && noError()) {
276 ScopedYamlEvent event;
277 switch (parse(event)) {
278 case YAML_SEQUENCE_END_EVENT:
282 case YAML_MAPPING_START_EVENT:
283 case YAML_SEQUENCE_START_EVENT:
284 case YAML_SCALAR_EVENT: {
286 rviz::Property* new_child = process(event, QString(
"[%1]").arg(index),
"", old_child);
287 if (new_child != old_child)
288 root->addChild(new_child, index);
295 root->setValue(
"YAML error");
301 root->removeChildren(index,
root->numChildren() - index);
309 const std::string& description,
const std::string& value,
311 QString qname = QString::fromStdString(name);
312 QString qdesc = QString::fromStdString(description);
313 Parser parser(value);
314 return parser.process(qname, qdesc, old);