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 | } |
| 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()), |
| 114 | [](auto it) { return Completion{it.m_value}; }); |
| 115 | parserContext.m_completionIterator = first; |
| 116 | } |
| 117 | bool operator()(const yang::Enum& type) const |
| 118 | { |
| 119 | createSetSuggestions(type); |
| 120 | // leaf_data_enum will advance the iterator if it succeeds, so I have |
| 121 | // to save the iterator here, to roll it back in case the enum is |
| 122 | // invalid. |
| 123 | auto saveIter = first; |
| 124 | auto pass = leaf_data_enum.parse(first, last, ctx, rctx, attr); |
| 125 | if (!pass) { |
| 126 | return false; |
| 127 | } |
| 128 | auto isValidEnum = type.m_allowedValues.count(boost::get<enum_>(attr)) != 0; |
| 129 | if (!isValidEnum) { |
| 130 | first = saveIter; |
| 131 | parserContext.m_errorMsg = "leaf data type mismatch: Expected an enum here. Allowed values:"; |
| 132 | for (const auto& it : type.m_allowedValues) { |
| 133 | parserContext.m_errorMsg += " " + it.m_value; |
| 134 | } |
| 135 | } |
| 136 | return isValidEnum; |
| 137 | } |
| 138 | bool operator()(const yang::IdentityRef& type) const |
| 139 | { |
| 140 | createSetSuggestions(type); |
| 141 | // leaf_data_identityRef will advance the iterator if it succeeds, so I have |
| 142 | // to save the iterator here, to roll it back in case the enum is |
| 143 | // invalid. |
| 144 | auto saveIter = first; |
| 145 | auto pass = leaf_data_identityRef.parse(first, last, ctx, rctx, attr); |
| 146 | if (!pass) { |
| 147 | return false; |
| 148 | } |
| 149 | identityRef_ pair{boost::get<identityRef_>(attr)}; |
| 150 | if (!pair.m_prefix) { |
| 151 | pair.m_prefix = module_{parserContext.currentSchemaPath().m_nodes.front().m_prefix.get().m_name}; |
| 152 | } |
| 153 | auto isValidIdentity = type.m_allowedValues.count(pair) != 0; |
| 154 | if (!isValidIdentity) { |
| 155 | first = saveIter; |
| 156 | } |
| 157 | return isValidIdentity; |
| 158 | } |
| 159 | bool operator()(const yang::LeafRef& leafRef) const |
| 160 | { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 161 | return std::visit(*this, leafRef.m_targetType->m_type); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 162 | } |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 163 | bool operator()(const yang::Union& unionInfo) const |
| 164 | { |
| 165 | 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] | 166 | return std::visit(*this, type.m_type); |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 167 | }); |
| 168 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 169 | }; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 170 | |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 171 | struct LeafData : x3::parser<LeafData> { |
| 172 | using attribute_type = leaf_data_; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 173 | |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 174 | // TODO: Can this be placed in a .cpp file? |
| 175 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 176 | bool parse(It& first, It last, Ctx const& ctx, RCtx& rctx, Attr& attr) const |
| 177 | { |
| 178 | ParserContext& parserContext = x3::get<parser_context_tag>(ctx); |
| 179 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 180 | 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] | 181 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 182 | 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] | 183 | |
| 184 | if (!pass) { |
| 185 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 186 | 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] | 187 | } |
| 188 | } |
| 189 | return pass; |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | auto const leaf_data = x3::no_skip[std::move(LeafData())]; |
| 194 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 195 | BOOST_SPIRIT_DEFINE(leaf_data_enum) |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 196 | BOOST_SPIRIT_DEFINE(leaf_data_string) |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 197 | BOOST_SPIRIT_DEFINE(leaf_data_binary) |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 198 | BOOST_SPIRIT_DEFINE(leaf_data_identityRef) |