Get rid of leaf_data_binary_data
I added a new parser "adapter" that creates ad-hoc x3::rule parser which
can coerce attributes to whatever I want. Before this, I had to
explicitly create x3::rule myself. This will shave off some lines and
will hopefully be useful in more places (my next target is
leaf_data_identityRef_data).
Change-Id: I6af83433df47e3158bd328db111fd1059cce3d45
diff --git a/src/common_parsers.hpp b/src/common_parsers.hpp
index eac8e61..8da90a6 100644
--- a/src/common_parsers.hpp
+++ b/src/common_parsers.hpp
@@ -34,6 +34,22 @@
auto const space_separator_def =
x3::omit[x3::no_skip[x3::space]];
+template <typename CoerceTo>
+struct as_type {
+ template <typename...> struct Tag{};
+
+ template <typename ParserType>
+ auto operator[](ParserType p) const {
+ return x3::rule<Tag<CoerceTo, ParserType>, CoerceTo> {"as"} = x3::as_parser(p);
+ }
+};
+
+// The `as` parser creates an ad-hoc x3::rule with the attribute specified with `CoerceTo`.
+// Example usage: as<std::string>[someParser]
+// someParser will have its attribute coerced to std::string
+// https://github.com/boostorg/spirit/issues/530#issuecomment-584836532
+template <typename CoerceTo> const as_type<CoerceTo> as{};
+
BOOST_SPIRIT_DEFINE(node_identifier)
BOOST_SPIRIT_DEFINE(module)
BOOST_SPIRIT_DEFINE(module_identifier)
diff --git a/src/leaf_data.hpp b/src/leaf_data.hpp
index 2f84a05..0bac962 100644
--- a/src/leaf_data.hpp
+++ b/src/leaf_data.hpp
@@ -22,7 +22,6 @@
x3::rule<struct leaf_data_class<yang::Binary>, binary_> const leaf_data_binary = "leaf_data_binary";
x3::rule<struct leaf_data_class<yang::Decimal>, double> const leaf_data_decimal = "leaf_data_decimal";
x3::rule<struct leaf_data_class<yang::String>, std::string> const leaf_data_string = "leaf_data_string";
-x3::rule<struct leaf_data_class_binary, std::string> const leaf_data_binary_data = "leaf_data_binary_data";
x3::rule<struct leaf_data_identityRef_data_class, identityRef_> const leaf_data_identityRef_data = "leaf_data_identityRef_data";
using x3::char_;
@@ -43,15 +42,8 @@
'\'' >> *(char_-'\'') >> '\'' |
'\"' >> *(char_-'\"') >> '\"';
-// This intermediate rule is neccessary for coercing to std::string.
-// TODO: check if I can do the coercing right in the grammar with `as{}` from
-// https://github.com/boostorg/spirit/issues/530#issuecomment-584836532
-// This would shave off some more lines.
-auto const leaf_data_binary_data_def =
- +(x3::alnum | char_('+') | char_('/')) >> -char_('=') >> -char_('=');
-
auto const leaf_data_binary_def =
- leaf_data_binary_data;
+ as<std::string>[+(x3::alnum | char_('+') | char_('/')) >> -char_('=') >> -char_('=')];
auto const leaf_data_identityRef_data_def =
-module >> node_identifier;
@@ -207,7 +199,6 @@
BOOST_SPIRIT_DEFINE(leaf_data_enum)
BOOST_SPIRIT_DEFINE(leaf_data_string)
-BOOST_SPIRIT_DEFINE(leaf_data_binary_data)
BOOST_SPIRIT_DEFINE(leaf_data_binary)
BOOST_SPIRIT_DEFINE(leaf_data_identityRef_data)
BOOST_SPIRIT_DEFINE(leaf_data_identityRef)