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