Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Václav Kubernát <kubernat@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <boost/spirit/home/x3.hpp> |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 11 | #include "ast_handlers.hpp" |
| 12 | #include "common_parsers.hpp" |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 13 | #include "leaf_data_type.hpp" |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 14 | #include "schema.hpp" |
| 15 | namespace x3 = boost::spirit::x3; |
| 16 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 17 | template <typename TYPE> |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 18 | struct leaf_data_class; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 19 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 20 | x3::rule<struct leaf_data_class<yang::IdentityRef>, identityRef_> const leaf_data_identityRef = "leaf_data_identityRef"; |
| 21 | x3::rule<struct leaf_data_class<yang::Binary>, binary_> const leaf_data_binary = "leaf_data_binary"; |
| 22 | x3::rule<struct leaf_data_class<yang::Decimal>, double> const leaf_data_decimal = "leaf_data_decimal"; |
| 23 | x3::rule<struct leaf_data_class<yang::String>, std::string> const leaf_data_string = "leaf_data_string"; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 24 | |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 25 | using x3::char_; |
| 26 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 27 | struct bool_symbol_table : x3::symbols<bool> { |
| 28 | bool_symbol_table() |
| 29 | { |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 30 | add |
| 31 | ("true", true) |
| 32 | ("false", false); |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 33 | } |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 34 | } const bool_symbols; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 35 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 36 | auto const leaf_data_string_def = |
| 37 | '\'' >> *(char_-'\'') >> '\'' | |
| 38 | '\"' >> *(char_-'\"') >> '\"'; |
| 39 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 40 | auto const leaf_data_binary_def = |
Václav Kubernát | e118f00 | 2020-05-14 22:54:13 +0200 | [diff] [blame] | 41 | as<std::string>[+(x3::alnum | char_('+') | char_('/')) >> -char_('=') >> -char_('=')]; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 42 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 43 | auto const leaf_data_identityRef_def = |
Václav Kubernát | e7f62ae | 2020-05-14 22:57:53 +0200 | [diff] [blame] | 44 | -module >> node_identifier; |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 45 | |
| 46 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 47 | struct impl_LeafData { |
| 48 | It& first; |
| 49 | It last; |
| 50 | Ctx const& ctx; |
| 51 | RCtx& rctx; |
| 52 | Attr& attr; |
| 53 | ParserContext& parserContext; |
| 54 | |
| 55 | bool operator()(const yang::Binary&) const |
| 56 | { |
| 57 | return leaf_data_binary.parse(first, last, ctx, rctx, attr); |
| 58 | } |
| 59 | bool operator()(const yang::Bool&) const |
| 60 | { |
| 61 | return bool_symbols.parse(first, last, ctx, rctx, attr); |
| 62 | } |
| 63 | bool operator()(const yang::Decimal&) const |
| 64 | { |
| 65 | return x3::double_.parse(first, last, ctx, rctx, attr); |
| 66 | } |
| 67 | bool operator()(const yang::Uint8&) const |
| 68 | { |
| 69 | return x3::uint8.parse(first, last, ctx, rctx, attr); |
| 70 | } |
| 71 | bool operator()(const yang::Uint16&) const |
| 72 | { |
| 73 | return x3::uint16.parse(first, last, ctx, rctx, attr); |
| 74 | } |
| 75 | bool operator()(const yang::Uint32&) const |
| 76 | { |
| 77 | return x3::uint32.parse(first, last, ctx, rctx, attr); |
| 78 | } |
| 79 | bool operator()(const yang::Uint64&) const |
| 80 | { |
| 81 | return x3::uint64.parse(first, last, ctx, rctx, attr); |
| 82 | } |
| 83 | bool operator()(const yang::Int8&) const |
| 84 | { |
| 85 | return x3::int8.parse(first, last, ctx, rctx, attr); |
| 86 | } |
| 87 | bool operator()(const yang::Int16&) const |
| 88 | { |
| 89 | return x3::int16.parse(first, last, ctx, rctx, attr); |
| 90 | } |
| 91 | bool operator()(const yang::Int32&) const |
| 92 | { |
| 93 | return x3::int32.parse(first, last, ctx, rctx, attr); |
| 94 | } |
| 95 | bool operator()(const yang::Int64&) const |
| 96 | { |
| 97 | return x3::int64.parse(first, last, ctx, rctx, attr); |
| 98 | } |
| 99 | bool operator()(const yang::String&) const |
| 100 | { |
| 101 | return leaf_data_string.parse(first, last, ctx, rctx, attr); |
| 102 | } |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 103 | bool operator()(const yang::Empty) const |
| 104 | { |
| 105 | return x3::attr(empty_{}).parse(first, last, ctx, rctx, attr); |
| 106 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 107 | template <typename Type> |
| 108 | void createSetSuggestions(const Type& type) const |
| 109 | { |
| 110 | parserContext.m_suggestions.clear(); |
| 111 | std::transform(type.m_allowedValues.begin(), |
| 112 | type.m_allowedValues.end(), |
| 113 | std::inserter(parserContext.m_suggestions, parserContext.m_suggestions.end()), |
Václav Kubernát | 549b08f | 2020-05-14 22:19:36 +0200 | [diff] [blame] | 114 | [](auto it) { |
| 115 | std::string res; |
| 116 | if constexpr (std::is_same<Type, yang::IdentityRef>()) { |
| 117 | res = it.m_prefix ? it.m_prefix->m_name + ":" : ""; |
| 118 | } |
| 119 | res += it.m_value; |
| 120 | return Completion{res}; |
| 121 | }); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 122 | parserContext.m_completionIterator = first; |
| 123 | } |
| 124 | bool operator()(const yang::Enum& type) const |
| 125 | { |
| 126 | createSetSuggestions(type); |
Václav Kubernát | 0af7257 | 2020-07-22 15:15:59 +0200 | [diff] [blame] | 127 | x3::symbols<enum_> parser; |
| 128 | for (const auto& value : type.m_allowedValues) { |
| 129 | parser.add(value.m_value, value); |
| 130 | } |
| 131 | auto res = parser.parse(first, last, ctx, rctx, attr); |
| 132 | if (!res) { |
| 133 | parserContext.m_errorMsg = "leaf data type mismatch: Expected an enum here. Allowed values:"; |
| 134 | for (const auto& it : type.m_allowedValues) { |
| 135 | parserContext.m_errorMsg += " " + it.m_value; |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 136 | } |
Václav Kubernát | 0af7257 | 2020-07-22 15:15:59 +0200 | [diff] [blame] | 137 | } |
| 138 | return res; |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 139 | } |
| 140 | bool operator()(const yang::IdentityRef& type) const |
| 141 | { |
| 142 | createSetSuggestions(type); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame^] | 143 | auto checkValidIdentity = [this, type](auto& ctx) { |
Václav Kubernát | a68c0ef | 2020-05-07 10:32:56 +0200 | [diff] [blame] | 144 | identityRef_ pair{boost::get<identityRef_>(_attr(ctx))}; |
| 145 | if (!pair.m_prefix) { |
| 146 | pair.m_prefix = module_{parserContext.currentSchemaPath().m_nodes.front().m_prefix.get().m_name}; |
| 147 | } |
| 148 | _pass(ctx) = type.m_allowedValues.count(pair) != 0; |
| 149 | }; |
| 150 | |
| 151 | return leaf_data_identityRef[checkValidIdentity].parse(first, last, ctx, rctx, attr); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 152 | } |
| 153 | bool operator()(const yang::LeafRef& leafRef) const |
| 154 | { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 155 | return std::visit(*this, leafRef.m_targetType->m_type); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 156 | } |
Václav Kubernát | dab73ca | 2020-10-26 23:44:43 +0100 | [diff] [blame] | 157 | bool operator()(const yang::Bits& bits) const |
| 158 | { |
| 159 | parserContext.m_suggestions.clear(); |
| 160 | x3::symbols<std::string> parser; |
| 161 | for (const auto& bit : bits.m_allowedValues) { |
| 162 | parser.add(bit, bit); |
| 163 | parserContext.m_suggestions.insert(Completion{bit}); |
| 164 | } |
| 165 | |
| 166 | std::vector<std::string> bitsRes; |
| 167 | |
| 168 | do { |
| 169 | std::string bit; |
| 170 | auto pass = parser.parse(first, last, ctx, rctx, bit); |
| 171 | if (pass) { |
| 172 | bitsRes.push_back(bit); |
| 173 | parser.remove(bit); |
| 174 | parserContext.m_suggestions.erase(Completion{bit}); |
| 175 | } |
| 176 | } while (space_separator.parse(first, last, ctx, rctx, x3::unused)); |
| 177 | |
| 178 | attr = bits_{bitsRes}; |
| 179 | |
| 180 | return true; |
| 181 | } |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 182 | bool operator()(const yang::Union& unionInfo) const |
| 183 | { |
| 184 | return std::any_of(unionInfo.m_unionTypes.begin(), unionInfo.m_unionTypes.end(), [this](const auto& type) { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 185 | return std::visit(*this, type.m_type); |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 186 | }); |
| 187 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 188 | }; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 189 | |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 190 | struct LeafData : x3::parser<LeafData> { |
| 191 | using attribute_type = leaf_data_; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 192 | |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 193 | // TODO: Can this be placed in a .cpp file? |
| 194 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 195 | bool parse(It& first, It last, Ctx const& ctx, RCtx& rctx, Attr& attr) const |
| 196 | { |
| 197 | ParserContext& parserContext = x3::get<parser_context_tag>(ctx); |
| 198 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 199 | auto type = schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node).m_type; |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 200 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 201 | auto pass = std::visit(impl_LeafData<It, Ctx, RCtx, Attr>{first, last, ctx, rctx, attr, parserContext}, type); |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 202 | |
| 203 | if (!pass) { |
| 204 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 205 | parserContext.m_errorMsg = "leaf data type mismatch: Expected " + leafDataTypeToString(type) + " here:"; |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | return pass; |
| 209 | } |
| 210 | }; |
| 211 | |
Václav Kubernát | f92724b | 2020-07-08 17:58:22 +0200 | [diff] [blame] | 212 | auto const leaf_data = x3::no_skip[LeafData()]; |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 213 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 214 | BOOST_SPIRIT_DEFINE(leaf_data_string) |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 215 | BOOST_SPIRIT_DEFINE(leaf_data_binary) |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 216 | BOOST_SPIRIT_DEFINE(leaf_data_identityRef) |