Add parser support for bits
Change-Id: I8f36a2639d3f4911c2fb825dd3bf28956886a9a6
diff --git a/src/ast_commands.hpp b/src/ast_commands.hpp
index b863640..d0c0985 100644
--- a/src/ast_commands.hpp
+++ b/src/ast_commands.hpp
@@ -299,6 +299,7 @@
BOOST_FUSION_ADAPT_STRUCT(delete_, m_path)
BOOST_FUSION_ADAPT_STRUCT(set_, m_path, m_data)
BOOST_FUSION_ADAPT_STRUCT(enum_, m_value)
+BOOST_FUSION_ADAPT_STRUCT(bits_, m_bits)
BOOST_FUSION_ADAPT_STRUCT(binary_, m_value)
BOOST_FUSION_ADAPT_STRUCT(identityRef_, m_prefix, m_value)
BOOST_FUSION_ADAPT_STRUCT(commit_)
diff --git a/src/leaf_data.hpp b/src/leaf_data.hpp
index fb490c7..964c154 100644
--- a/src/leaf_data.hpp
+++ b/src/leaf_data.hpp
@@ -154,6 +154,31 @@
{
return std::visit(*this, leafRef.m_targetType->m_type);
}
+ bool operator()(const yang::Bits& bits) const
+ {
+ parserContext.m_suggestions.clear();
+ x3::symbols<std::string> parser;
+ for (const auto& bit : bits.m_allowedValues) {
+ parser.add(bit, bit);
+ parserContext.m_suggestions.insert(Completion{bit});
+ }
+
+ std::vector<std::string> bitsRes;
+
+ do {
+ std::string bit;
+ auto pass = parser.parse(first, last, ctx, rctx, bit);
+ if (pass) {
+ bitsRes.push_back(bit);
+ parser.remove(bit);
+ parserContext.m_suggestions.erase(Completion{bit});
+ }
+ } while (space_separator.parse(first, last, ctx, rctx, x3::unused));
+
+ attr = bits_{bitsRes};
+
+ return true;
+ }
bool operator()(const yang::Union& unionInfo) const
{
return std::any_of(unionInfo.m_unionTypes.begin(), unionInfo.m_unionTypes.end(), [this](const auto& type) {
diff --git a/src/leaf_data_type.cpp b/src/leaf_data_type.cpp
index b7a3876..fe6fe04 100644
--- a/src/leaf_data_type.cpp
+++ b/src/leaf_data_type.cpp
@@ -104,4 +104,8 @@
{
return true;
}
+bool Bits::operator==(const Bits& other) const
+{
+ return this->m_allowedValues == other.m_allowedValues;
+}
}
diff --git a/src/leaf_data_type.hpp b/src/leaf_data_type.hpp
index 288f765..98cb12e 100644
--- a/src/leaf_data_type.hpp
+++ b/src/leaf_data_type.hpp
@@ -65,6 +65,10 @@
bool operator==(const IdentityRef& other) const;
std::set<identityRef_> m_allowedValues;
};
+struct Bits {
+ bool operator==(const Bits& other) const;
+ std::set<std::string> m_allowedValues;
+};
struct LeafRef;
struct Union;
using LeafDataType = std::variant<
@@ -83,6 +87,7 @@
yang::Binary,
yang::Empty,
yang::IdentityRef,
+ yang::Bits,
yang::LeafRef,
yang::Union
>;
diff --git a/src/utils.cpp b/src/utils.cpp
index 11a97b4..afb291e 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -135,6 +135,14 @@
});
return ss.str();
}
+ std::string operator()(const yang::Bits& type)
+ {
+ std::ostringstream ss;
+ ss << "bits {";
+ std::copy(type.m_allowedValues.begin(), type.m_allowedValues.end(), std::experimental::make_ostream_joiner(ss, ", "));
+ ss << "}";
+ return ss.str();
+ }
};
std::string leafDataTypeToString(const yang::LeafDataType& type)
diff --git a/src/yang_schema.cpp b/src/yang_schema.cpp
index 681861e..8793dd4 100644
--- a/src/yang_schema.cpp
+++ b/src/yang_schema.cpp
@@ -249,6 +249,15 @@
case LY_TYPE_LEAFREF:
resType.emplace<yang::LeafRef>(::leafrefPath(type), std::make_unique<yang::TypeInfo>(leafType(::leafrefPath(type))));
break;
+ case LY_TYPE_BITS:
+ {
+ auto resBits = yang::Bits{};
+ for (const auto& bit : type->info()->bits()->bit()) {
+ resBits.m_allowedValues.emplace(bit->name());
+ }
+ resType.emplace<yang::Bits>(std::move(resBits));
+ break;
+ }
case LY_TYPE_UNION:
{
auto resUnion = yang::Union{};