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::Enum>, enum_> const leaf_data_enum = "leaf_data_enum"; |
| 21 | x3::rule<struct leaf_data_class<yang::IdentityRef>, identityRef_> const leaf_data_identityRef = "leaf_data_identityRef"; |
| 22 | x3::rule<struct leaf_data_class<yang::Binary>, binary_> const leaf_data_binary = "leaf_data_binary"; |
| 23 | x3::rule<struct leaf_data_class<yang::Decimal>, double> const leaf_data_decimal = "leaf_data_decimal"; |
| 24 | 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] | 25 | |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 26 | using x3::char_; |
| 27 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 28 | auto const leaf_data_enum_def = |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 29 | +char_; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 30 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 31 | struct bool_symbol_table : x3::symbols<bool> { |
| 32 | bool_symbol_table() |
| 33 | { |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 34 | add |
| 35 | ("true", true) |
| 36 | ("false", false); |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 37 | } |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 38 | } const bool_symbols; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 39 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 40 | auto const leaf_data_string_def = |
| 41 | '\'' >> *(char_-'\'') >> '\'' | |
| 42 | '\"' >> *(char_-'\"') >> '\"'; |
| 43 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 44 | auto const leaf_data_binary_def = |
Václav Kubernát | e118f00 | 2020-05-14 22:54:13 +0200 | [diff] [blame] | 45 | as<std::string>[+(x3::alnum | char_('+') | char_('/')) >> -char_('=') >> -char_('=')]; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 46 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 47 | auto const leaf_data_identityRef_def = |
Václav Kubernát | e7f62ae | 2020-05-14 22:57:53 +0200 | [diff] [blame] | 48 | -module >> node_identifier; |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 49 | |
| 50 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 51 | struct impl_LeafData { |
| 52 | It& first; |
| 53 | It last; |
| 54 | Ctx const& ctx; |
| 55 | RCtx& rctx; |
| 56 | Attr& attr; |
| 57 | ParserContext& parserContext; |
| 58 | |
| 59 | bool operator()(const yang::Binary&) const |
| 60 | { |
| 61 | return leaf_data_binary.parse(first, last, ctx, rctx, attr); |
| 62 | } |
| 63 | bool operator()(const yang::Bool&) const |
| 64 | { |
| 65 | return bool_symbols.parse(first, last, ctx, rctx, attr); |
| 66 | } |
| 67 | bool operator()(const yang::Decimal&) const |
| 68 | { |
| 69 | return x3::double_.parse(first, last, ctx, rctx, attr); |
| 70 | } |
| 71 | bool operator()(const yang::Uint8&) const |
| 72 | { |
| 73 | return x3::uint8.parse(first, last, ctx, rctx, attr); |
| 74 | } |
| 75 | bool operator()(const yang::Uint16&) const |
| 76 | { |
| 77 | return x3::uint16.parse(first, last, ctx, rctx, attr); |
| 78 | } |
| 79 | bool operator()(const yang::Uint32&) const |
| 80 | { |
| 81 | return x3::uint32.parse(first, last, ctx, rctx, attr); |
| 82 | } |
| 83 | bool operator()(const yang::Uint64&) const |
| 84 | { |
| 85 | return x3::uint64.parse(first, last, ctx, rctx, attr); |
| 86 | } |
| 87 | bool operator()(const yang::Int8&) const |
| 88 | { |
| 89 | return x3::int8.parse(first, last, ctx, rctx, attr); |
| 90 | } |
| 91 | bool operator()(const yang::Int16&) const |
| 92 | { |
| 93 | return x3::int16.parse(first, last, ctx, rctx, attr); |
| 94 | } |
| 95 | bool operator()(const yang::Int32&) const |
| 96 | { |
| 97 | return x3::int32.parse(first, last, ctx, rctx, attr); |
| 98 | } |
| 99 | bool operator()(const yang::Int64&) const |
| 100 | { |
| 101 | return x3::int64.parse(first, last, ctx, rctx, attr); |
| 102 | } |
| 103 | bool operator()(const yang::String&) const |
| 104 | { |
| 105 | return leaf_data_string.parse(first, last, ctx, rctx, attr); |
| 106 | } |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 107 | bool operator()(const yang::Empty) const |
| 108 | { |
| 109 | return x3::attr(empty_{}).parse(first, last, ctx, rctx, attr); |
| 110 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 111 | template <typename Type> |
| 112 | void createSetSuggestions(const Type& type) const |
| 113 | { |
| 114 | parserContext.m_suggestions.clear(); |
| 115 | std::transform(type.m_allowedValues.begin(), |
| 116 | type.m_allowedValues.end(), |
| 117 | std::inserter(parserContext.m_suggestions, parserContext.m_suggestions.end()), |
Václav Kubernát | 549b08f | 2020-05-14 22:19:36 +0200 | [diff] [blame] | 118 | [](auto it) { |
| 119 | std::string res; |
| 120 | if constexpr (std::is_same<Type, yang::IdentityRef>()) { |
| 121 | res = it.m_prefix ? it.m_prefix->m_name + ":" : ""; |
| 122 | } |
| 123 | res += it.m_value; |
| 124 | return Completion{res}; |
| 125 | }); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 126 | parserContext.m_completionIterator = first; |
| 127 | } |
| 128 | bool operator()(const yang::Enum& type) const |
| 129 | { |
| 130 | createSetSuggestions(type); |
Václav Kubernát | a68c0ef | 2020-05-07 10:32:56 +0200 | [diff] [blame] | 131 | auto checkValidEnum = [this, type] (auto& ctx) { |
| 132 | if (type.m_allowedValues.count(boost::get<enum_>(attr)) == 0) { |
| 133 | _pass(ctx) = false; |
| 134 | parserContext.m_errorMsg = "leaf data type mismatch: Expected an enum here. Allowed values:"; |
| 135 | for (const auto& it : type.m_allowedValues) { |
| 136 | parserContext.m_errorMsg += " " + it.m_value; |
| 137 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 138 | } |
Václav Kubernát | a68c0ef | 2020-05-07 10:32:56 +0200 | [diff] [blame] | 139 | }; |
| 140 | return leaf_data_enum[checkValidEnum].parse(first, last, ctx, rctx, attr); |
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 | a68c0ef | 2020-05-07 10:32:56 +0200 | [diff] [blame] | 145 | auto checkValidIdentity = [this, type] (auto& ctx) { |
| 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 | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 159 | bool operator()(const yang::Union& unionInfo) const |
| 160 | { |
| 161 | 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] | 162 | return std::visit(*this, type.m_type); |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 163 | }); |
| 164 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 165 | }; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 166 | |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 167 | struct LeafData : x3::parser<LeafData> { |
| 168 | using attribute_type = leaf_data_; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 169 | |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 170 | // TODO: Can this be placed in a .cpp file? |
| 171 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 172 | bool parse(It& first, It last, Ctx const& ctx, RCtx& rctx, Attr& attr) const |
| 173 | { |
| 174 | ParserContext& parserContext = x3::get<parser_context_tag>(ctx); |
| 175 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 176 | 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] | 177 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 178 | 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] | 179 | |
| 180 | if (!pass) { |
| 181 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 182 | 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] | 183 | } |
| 184 | } |
| 185 | return pass; |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | auto const leaf_data = x3::no_skip[std::move(LeafData())]; |
| 190 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 191 | BOOST_SPIRIT_DEFINE(leaf_data_enum) |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 192 | BOOST_SPIRIT_DEFINE(leaf_data_string) |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 193 | BOOST_SPIRIT_DEFINE(leaf_data_binary) |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 194 | BOOST_SPIRIT_DEFINE(leaf_data_identityRef) |