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 | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 25 | x3::rule<struct leaf_data_class_binary, std::string> const leaf_data_binary_data = "leaf_data_binary_data"; |
| 26 | x3::rule<struct leaf_data_identityRef_data_class, identityRef_> const leaf_data_identityRef_data = "leaf_data_identityRef_data"; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 27 | |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 28 | using x3::char_; |
| 29 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 30 | auto const leaf_data_enum_def = |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 31 | +char_; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 32 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 33 | struct bool_symbol_table : x3::symbols<bool> { |
| 34 | bool_symbol_table() |
| 35 | { |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 36 | add |
| 37 | ("true", true) |
| 38 | ("false", false); |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 39 | } |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 40 | } const bool_symbols; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 41 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 42 | auto const leaf_data_string_def = |
| 43 | '\'' >> *(char_-'\'') >> '\'' | |
| 44 | '\"' >> *(char_-'\"') >> '\"'; |
| 45 | |
| 46 | // This intermediate rule is neccessary for coercing to std::string. |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 47 | // TODO: check if I can do the coercing right in the grammar with `as{}` from |
| 48 | // https://github.com/boostorg/spirit/issues/530#issuecomment-584836532 |
| 49 | // This would shave off some more lines. |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 50 | auto const leaf_data_binary_data_def = |
| 51 | +(x3::alnum | char_('+') | char_('/')) >> -char_('=') >> -char_('='); |
| 52 | |
| 53 | auto const leaf_data_binary_def = |
| 54 | leaf_data_binary_data; |
| 55 | |
| 56 | auto const leaf_data_identityRef_data_def = |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 57 | -module >> node_identifier; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 58 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 59 | // TODO: get rid of this and use leaf_data_identityRef_data directly |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 60 | auto const leaf_data_identityRef_def = |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 61 | leaf_data_identityRef_data; |
| 62 | |
| 63 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 64 | struct impl_LeafData { |
| 65 | It& first; |
| 66 | It last; |
| 67 | Ctx const& ctx; |
| 68 | RCtx& rctx; |
| 69 | Attr& attr; |
| 70 | ParserContext& parserContext; |
| 71 | |
| 72 | bool operator()(const yang::Binary&) const |
| 73 | { |
| 74 | return leaf_data_binary.parse(first, last, ctx, rctx, attr); |
| 75 | } |
| 76 | bool operator()(const yang::Bool&) const |
| 77 | { |
| 78 | return bool_symbols.parse(first, last, ctx, rctx, attr); |
| 79 | } |
| 80 | bool operator()(const yang::Decimal&) const |
| 81 | { |
| 82 | return x3::double_.parse(first, last, ctx, rctx, attr); |
| 83 | } |
| 84 | bool operator()(const yang::Uint8&) const |
| 85 | { |
| 86 | return x3::uint8.parse(first, last, ctx, rctx, attr); |
| 87 | } |
| 88 | bool operator()(const yang::Uint16&) const |
| 89 | { |
| 90 | return x3::uint16.parse(first, last, ctx, rctx, attr); |
| 91 | } |
| 92 | bool operator()(const yang::Uint32&) const |
| 93 | { |
| 94 | return x3::uint32.parse(first, last, ctx, rctx, attr); |
| 95 | } |
| 96 | bool operator()(const yang::Uint64&) const |
| 97 | { |
| 98 | return x3::uint64.parse(first, last, ctx, rctx, attr); |
| 99 | } |
| 100 | bool operator()(const yang::Int8&) const |
| 101 | { |
| 102 | return x3::int8.parse(first, last, ctx, rctx, attr); |
| 103 | } |
| 104 | bool operator()(const yang::Int16&) const |
| 105 | { |
| 106 | return x3::int16.parse(first, last, ctx, rctx, attr); |
| 107 | } |
| 108 | bool operator()(const yang::Int32&) const |
| 109 | { |
| 110 | return x3::int32.parse(first, last, ctx, rctx, attr); |
| 111 | } |
| 112 | bool operator()(const yang::Int64&) const |
| 113 | { |
| 114 | return x3::int64.parse(first, last, ctx, rctx, attr); |
| 115 | } |
| 116 | bool operator()(const yang::String&) const |
| 117 | { |
| 118 | return leaf_data_string.parse(first, last, ctx, rctx, attr); |
| 119 | } |
| 120 | template <typename Type> |
| 121 | void createSetSuggestions(const Type& type) const |
| 122 | { |
| 123 | parserContext.m_suggestions.clear(); |
| 124 | std::transform(type.m_allowedValues.begin(), |
| 125 | type.m_allowedValues.end(), |
| 126 | std::inserter(parserContext.m_suggestions, parserContext.m_suggestions.end()), |
| 127 | [](auto it) { return Completion{it.m_value}; }); |
| 128 | parserContext.m_completionIterator = first; |
| 129 | } |
| 130 | bool operator()(const yang::Enum& type) const |
| 131 | { |
| 132 | createSetSuggestions(type); |
| 133 | // leaf_data_enum will advance the iterator if it succeeds, so I have |
| 134 | // to save the iterator here, to roll it back in case the enum is |
| 135 | // invalid. |
| 136 | auto saveIter = first; |
| 137 | auto pass = leaf_data_enum.parse(first, last, ctx, rctx, attr); |
| 138 | if (!pass) { |
| 139 | return false; |
| 140 | } |
| 141 | auto isValidEnum = type.m_allowedValues.count(boost::get<enum_>(attr)) != 0; |
| 142 | if (!isValidEnum) { |
| 143 | first = saveIter; |
| 144 | parserContext.m_errorMsg = "leaf data type mismatch: Expected an enum here. Allowed values:"; |
| 145 | for (const auto& it : type.m_allowedValues) { |
| 146 | parserContext.m_errorMsg += " " + it.m_value; |
| 147 | } |
| 148 | } |
| 149 | return isValidEnum; |
| 150 | } |
| 151 | bool operator()(const yang::IdentityRef& type) const |
| 152 | { |
| 153 | createSetSuggestions(type); |
| 154 | // leaf_data_identityRef will advance the iterator if it succeeds, so I have |
| 155 | // to save the iterator here, to roll it back in case the enum is |
| 156 | // invalid. |
| 157 | auto saveIter = first; |
| 158 | auto pass = leaf_data_identityRef.parse(first, last, ctx, rctx, attr); |
| 159 | if (!pass) { |
| 160 | return false; |
| 161 | } |
| 162 | identityRef_ pair{boost::get<identityRef_>(attr)}; |
| 163 | if (!pair.m_prefix) { |
| 164 | pair.m_prefix = module_{parserContext.currentSchemaPath().m_nodes.front().m_prefix.get().m_name}; |
| 165 | } |
| 166 | auto isValidIdentity = type.m_allowedValues.count(pair) != 0; |
| 167 | if (!isValidIdentity) { |
| 168 | first = saveIter; |
| 169 | } |
| 170 | return isValidIdentity; |
| 171 | } |
| 172 | bool operator()(const yang::LeafRef& leafRef) const |
| 173 | { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 174 | return std::visit(*this, leafRef.m_targetType->m_type); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 175 | } |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 176 | bool operator()(const yang::Union& unionInfo) const |
| 177 | { |
| 178 | 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] | 179 | return std::visit(*this, type.m_type); |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 180 | }); |
| 181 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 182 | }; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 183 | |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 184 | struct LeafData : x3::parser<LeafData> { |
| 185 | using attribute_type = leaf_data_; |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 186 | |
Václav Kubernát | 882174d | 2020-03-25 21:31:46 +0100 | [diff] [blame] | 187 | // TODO: Can this be placed in a .cpp file? |
| 188 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 189 | bool parse(It& first, It last, Ctx const& ctx, RCtx& rctx, Attr& attr) const |
| 190 | { |
| 191 | ParserContext& parserContext = x3::get<parser_context_tag>(ctx); |
| 192 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 193 | 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] | 194 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 195 | 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] | 196 | |
| 197 | if (!pass) { |
| 198 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 199 | 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] | 200 | } |
| 201 | } |
| 202 | return pass; |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | auto const leaf_data = x3::no_skip[std::move(LeafData())]; |
| 207 | |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 208 | BOOST_SPIRIT_DEFINE(leaf_data_enum) |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 209 | BOOST_SPIRIT_DEFINE(leaf_data_string) |
| 210 | BOOST_SPIRIT_DEFINE(leaf_data_binary_data) |
| 211 | BOOST_SPIRIT_DEFINE(leaf_data_binary) |
| 212 | BOOST_SPIRIT_DEFINE(leaf_data_identityRef_data) |
| 213 | BOOST_SPIRIT_DEFINE(leaf_data_identityRef) |