From 9810e3bb4b57a77dbc034ada7c05ba78263e9e3a Mon Sep 17 00:00:00 2001 From: Robert Haschke Date: Sat, 9 Feb 2019 16:12:17 +0100 Subject: [PATCH] wip: understand event-based, incremental parsing with libyaml --- .../properties/CMakeLists.txt | 2 +- .../properties/property_from_yaml.cpp | 46 +++++++++++++++++-- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/visualization/motion_planning_tasks/properties/CMakeLists.txt b/visualization/motion_planning_tasks/properties/CMakeLists.txt index 9632287d..8742141e 100644 --- a/visualization/motion_planning_tasks/properties/CMakeLists.txt +++ b/visualization/motion_planning_tasks/properties/CMakeLists.txt @@ -5,7 +5,7 @@ set(SOURCES ) include(FindPkgConfig) -pkg_check_modules(YAML yaml-cpp>=0.5) +pkg_check_modules(YAML yaml-0.1) if (YAML_FOUND) # Only cmake > 3.12 provides XXX_LINK_LIBRARIES. Find the absolute path manually find_library(YAML_LIBRARIES ${YAML_LIBRARIES} PATHS ${YAML_LIBRARY_DIRS}) diff --git a/visualization/motion_planning_tasks/properties/property_from_yaml.cpp b/visualization/motion_planning_tasks/properties/property_from_yaml.cpp index ecf3fdbe..ce7b581c 100644 --- a/visualization/motion_planning_tasks/properties/property_from_yaml.cpp +++ b/visualization/motion_planning_tasks/properties/property_from_yaml.cpp @@ -35,7 +35,7 @@ /* Author: Robert Haschke */ #include "property_factory.h" -#include +#include #include #include @@ -48,7 +48,7 @@ namespace moveit_rviz_plugin { * As we cannot know the required data type for a field from YAML parsing, * we only distinguish numbers (FloatProperty) and all other YAML scalars (StringProperty). */ - +#if 0 // Try to set numeric or arbitrary scalar value from YAML node. Needs to match old's type. void setScalarValue(rviz::Property* old, const YAML::Node& node) { @@ -183,6 +183,9 @@ rviz::Property* createFromNode(const QString& name, const QString& description, result->setReadOnly(true); return result; } +#endif + +inline std::string indent(unsigned int depth) { return std::string(2*depth, ' '); } rviz::Property* PropertyFactory::createDefault(const std::string& name, const std::string& type, const std::string& description, const std::string& value, @@ -190,10 +193,45 @@ rviz::Property* PropertyFactory::createDefault(const std::string& name, const st { QString qname = QString::fromStdString(name); QString qdesc = QString::fromStdString(description); + + yaml_parser_t parser; + yaml_parser_initialize(&parser); + yaml_parser_set_input_string(&parser, reinterpret_cast(value.c_str()), value.size()); try { - return createFromNode(qname, qdesc, YAML::Load(value), old); + unsigned int depth=0; + bool proceed = true; + while (proceed) { + yaml_event_t event; + if (!yaml_parser_parse(&parser, &event)) { + yaml_event_delete(&event); + throw std::runtime_error(parser.problem); + break; + } + + switch(event.type) + { + case YAML_NO_EVENT: std::cout << indent(depth) << "No event!" << std::endl; break; + /* Stream start/end */ + case YAML_STREAM_START_EVENT: std::cout << indent(depth++) << "STREAM START" << std::endl; break; + case YAML_STREAM_END_EVENT: std::cout << indent(--depth) << "STREAM END" << std::endl; break; + /* Block delimeters */ + case YAML_DOCUMENT_START_EVENT: std::cout << indent(depth++) << "Start Document" << std::endl; break; + case YAML_DOCUMENT_END_EVENT: std::cout << indent(--depth) << "End Document" << std::endl; break; + case YAML_SEQUENCE_START_EVENT: std::cout << indent(depth++) << "Start Sequence" << std::endl; break; + case YAML_SEQUENCE_END_EVENT: std::cout << indent(--depth) << "End Sequence" << std::endl; break; + case YAML_MAPPING_START_EVENT: std::cout << indent(depth++) << "Start Mapping" << std::endl; break; + case YAML_MAPPING_END_EVENT: std::cout << indent(--depth) << "End Mapping" << std::endl; break; + /* Data */ + case YAML_ALIAS_EVENT: std::cout << indent(depth) << "alias anchor=" << event.data.alias.anchor << std::endl; break; + case YAML_SCALAR_EVENT: std::cout << indent(depth) << "scalar: " << event.data.scalar.value << std::endl; break; + } + proceed = event.type != YAML_STREAM_END_EVENT; + yaml_event_delete(&event); + } + return nullptr; } catch (const std::exception &e) { - return createFromNode(qname, qdesc, dummyNode("YAML parse error"), old); + std::cout << e.what() << std::endl; + return nullptr; } }