blob: 0e0e499729f6e3077e36d4f6e9db2430010ccb7e [file] [log] [blame]
Václav Kubernát9ae8cc42020-03-25 19:17:41 +01001/*
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át9ae8cc42020-03-25 19:17:41 +010011#include "ast_handlers.hpp"
12#include "common_parsers.hpp"
Václav Kubernát3a99f002020-03-31 02:27:41 +020013#include "leaf_data_type.hpp"
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010014#include "schema.hpp"
15namespace x3 = boost::spirit::x3;
16
Václav Kubernát3a99f002020-03-31 02:27:41 +020017template <typename TYPE>
Václav Kubernát882174d2020-03-25 21:31:46 +010018struct leaf_data_class;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010019
Václav Kubernát882174d2020-03-25 21:31:46 +010020using x3::char_;
21
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010022struct bool_symbol_table : x3::symbols<bool> {
23 bool_symbol_table()
24 {
Václav Kubernát3a99f002020-03-31 02:27:41 +020025 add
26 ("true", true)
27 ("false", false);
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010028 }
Václav Kubernát882174d2020-03-25 21:31:46 +010029} const bool_symbols;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010030
Václav Kubernát52b90222022-04-27 11:29:54 +020031auto const leaf_data_string = x3::rule<struct leaf_data_class<yang::String>, std::string>{"leaf_data_string"} =
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010032 '\'' >> *(char_-'\'') >> '\'' |
33 '\"' >> *(char_-'\"') >> '\"';
34
Václav Kubernát52b90222022-04-27 11:29:54 +020035auto const leaf_data_binary = x3::rule<struct leaf_data_class<yang::Binary>, binary_>{"leaf_data_binary"} =
Václav Kubernáte118f002020-05-14 22:54:13 +020036 as<std::string>[+(x3::alnum | char_('+') | char_('/')) >> -char_('=') >> -char_('=')];
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010037
Václav Kubernát52b90222022-04-27 11:29:54 +020038auto const leaf_data_identityRef = x3::rule<struct leaf_data_class<yang::IdentityRef>, identityRef_>{"leaf_data_identityRef"} =
Václav Kubernáte7f62ae2020-05-14 22:57:53 +020039 -module >> node_identifier;
Václav Kubernát3a99f002020-03-31 02:27:41 +020040
41template <typename It, typename Ctx, typename RCtx, typename Attr>
42struct impl_LeafData {
43 It& first;
44 It last;
45 Ctx const& ctx;
46 RCtx& rctx;
47 Attr& attr;
48 ParserContext& parserContext;
49
50 bool operator()(const yang::Binary&) const
51 {
52 return leaf_data_binary.parse(first, last, ctx, rctx, attr);
53 }
54 bool operator()(const yang::Bool&) const
55 {
56 return bool_symbols.parse(first, last, ctx, rctx, attr);
57 }
58 bool operator()(const yang::Decimal&) const
59 {
60 return x3::double_.parse(first, last, ctx, rctx, attr);
61 }
62 bool operator()(const yang::Uint8&) const
63 {
64 return x3::uint8.parse(first, last, ctx, rctx, attr);
65 }
66 bool operator()(const yang::Uint16&) const
67 {
68 return x3::uint16.parse(first, last, ctx, rctx, attr);
69 }
70 bool operator()(const yang::Uint32&) const
71 {
72 return x3::uint32.parse(first, last, ctx, rctx, attr);
73 }
74 bool operator()(const yang::Uint64&) const
75 {
76 return x3::uint64.parse(first, last, ctx, rctx, attr);
77 }
78 bool operator()(const yang::Int8&) const
79 {
80 return x3::int8.parse(first, last, ctx, rctx, attr);
81 }
82 bool operator()(const yang::Int16&) const
83 {
84 return x3::int16.parse(first, last, ctx, rctx, attr);
85 }
86 bool operator()(const yang::Int32&) const
87 {
88 return x3::int32.parse(first, last, ctx, rctx, attr);
89 }
90 bool operator()(const yang::Int64&) const
91 {
92 return x3::int64.parse(first, last, ctx, rctx, attr);
93 }
94 bool operator()(const yang::String&) const
95 {
96 return leaf_data_string.parse(first, last, ctx, rctx, attr);
97 }
Jan Kundrát379bb572020-05-07 03:23:13 +020098 bool operator()(const yang::Empty) const
99 {
100 return x3::attr(empty_{}).parse(first, last, ctx, rctx, attr);
101 }
Václav Kubernát3a99f002020-03-31 02:27:41 +0200102 template <typename Type>
103 void createSetSuggestions(const Type& type) const
104 {
105 parserContext.m_suggestions.clear();
106 std::transform(type.m_allowedValues.begin(),
107 type.m_allowedValues.end(),
108 std::inserter(parserContext.m_suggestions, parserContext.m_suggestions.end()),
Václav Kubernát549b08f2020-05-14 22:19:36 +0200109 [](auto it) {
110 std::string res;
111 if constexpr (std::is_same<Type, yang::IdentityRef>()) {
112 res = it.m_prefix ? it.m_prefix->m_name + ":" : "";
113 }
114 res += it.m_value;
115 return Completion{res};
116 });
Václav Kubernát3a99f002020-03-31 02:27:41 +0200117 parserContext.m_completionIterator = first;
118 }
119 bool operator()(const yang::Enum& type) const
120 {
121 createSetSuggestions(type);
Václav Kubernát0af72572020-07-22 15:15:59 +0200122 x3::symbols<enum_> parser;
123 for (const auto& value : type.m_allowedValues) {
124 parser.add(value.m_value, value);
125 }
126 auto res = parser.parse(first, last, ctx, rctx, attr);
127 if (!res) {
128 parserContext.m_errorMsg = "leaf data type mismatch: Expected an enum here. Allowed values:";
129 for (const auto& it : type.m_allowedValues) {
130 parserContext.m_errorMsg += " " + it.m_value;
Václav Kubernát3a99f002020-03-31 02:27:41 +0200131 }
Václav Kubernát0af72572020-07-22 15:15:59 +0200132 }
133 return res;
Václav Kubernát3a99f002020-03-31 02:27:41 +0200134 }
135 bool operator()(const yang::IdentityRef& type) const
136 {
137 createSetSuggestions(type);
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100138 auto checkValidIdentity = [this, type](auto& ctx) {
Václav Kubernáta68c0ef2020-05-07 10:32:56 +0200139 identityRef_ pair{boost::get<identityRef_>(_attr(ctx))};
140 if (!pair.m_prefix) {
141 pair.m_prefix = module_{parserContext.currentSchemaPath().m_nodes.front().m_prefix.get().m_name};
142 }
143 _pass(ctx) = type.m_allowedValues.count(pair) != 0;
144 };
145
146 return leaf_data_identityRef[checkValidIdentity].parse(first, last, ctx, rctx, attr);
Václav Kubernát3a99f002020-03-31 02:27:41 +0200147 }
148 bool operator()(const yang::LeafRef& leafRef) const
149 {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200150 return std::visit(*this, leafRef.m_targetType->m_type);
Václav Kubernát3a99f002020-03-31 02:27:41 +0200151 }
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100152 bool operator()(const yang::Bits& bits) const
153 {
154 parserContext.m_suggestions.clear();
155 x3::symbols<std::string> parser;
156 for (const auto& bit : bits.m_allowedValues) {
157 parser.add(bit, bit);
158 parserContext.m_suggestions.insert(Completion{bit});
159 }
Václav Kubernát467c22c2020-12-03 12:04:04 +0100160 parserContext.m_completionIterator = first;
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100161
162 std::vector<std::string> bitsRes;
163
164 do {
165 std::string bit;
166 auto pass = parser.parse(first, last, ctx, rctx, bit);
167 if (pass) {
168 bitsRes.push_back(bit);
169 parser.remove(bit);
170 parserContext.m_suggestions.erase(Completion{bit});
171 }
172 } while (space_separator.parse(first, last, ctx, rctx, x3::unused));
173
174 attr = bits_{bitsRes};
175
176 return true;
177 }
Václav Kubernát2984f442020-02-20 17:43:35 +0100178 bool operator()(const yang::Union& unionInfo) const
179 {
180 return std::any_of(unionInfo.m_unionTypes.begin(), unionInfo.m_unionTypes.end(), [this](const auto& type) {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200181 return std::visit(*this, type.m_type);
Václav Kubernát2984f442020-02-20 17:43:35 +0100182 });
183 }
Václav Kubernát3a99f002020-03-31 02:27:41 +0200184};
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100185
Václav Kubernát882174d2020-03-25 21:31:46 +0100186struct LeafData : x3::parser<LeafData> {
187 using attribute_type = leaf_data_;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100188
Václav Kubernát882174d2020-03-25 21:31:46 +0100189 // TODO: Can this be placed in a .cpp file?
190 template <typename It, typename Ctx, typename RCtx, typename Attr>
191 bool parse(It& first, It last, Ctx const& ctx, RCtx& rctx, Attr& attr) const
192 {
193 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
194 const Schema& schema = parserContext.m_schema;
Václav Kubernát13b23d72020-04-16 21:49:51 +0200195 auto type = schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node).m_type;
Václav Kubernát882174d2020-03-25 21:31:46 +0100196
Václav Kubernát3a99f002020-03-31 02:27:41 +0200197 auto pass = std::visit(impl_LeafData<It, Ctx, RCtx, Attr>{first, last, ctx, rctx, attr, parserContext}, type);
Václav Kubernát882174d2020-03-25 21:31:46 +0100198
199 if (!pass) {
200 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200201 parserContext.m_errorMsg = "leaf data type mismatch: Expected " + leafDataTypeToString(type) + " here:";
Václav Kubernát882174d2020-03-25 21:31:46 +0100202 }
203 }
204 return pass;
205 }
206};
207
Václav Kubernát76bb8c22022-01-19 01:32:31 +0100208auto const leaf_data = LeafData();