blob: 0bac9627ad412860f3bb26d43afbe4edf308a39c [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át3a99f002020-03-31 02:27:41 +020020x3::rule<struct leaf_data_class<yang::Enum>, enum_> const leaf_data_enum = "leaf_data_enum";
21x3::rule<struct leaf_data_class<yang::IdentityRef>, identityRef_> const leaf_data_identityRef = "leaf_data_identityRef";
22x3::rule<struct leaf_data_class<yang::Binary>, binary_> const leaf_data_binary = "leaf_data_binary";
23x3::rule<struct leaf_data_class<yang::Decimal>, double> const leaf_data_decimal = "leaf_data_decimal";
24x3::rule<struct leaf_data_class<yang::String>, std::string> const leaf_data_string = "leaf_data_string";
Václav Kubernát882174d2020-03-25 21:31:46 +010025x3::rule<struct leaf_data_identityRef_data_class, identityRef_> const leaf_data_identityRef_data = "leaf_data_identityRef_data";
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010026
Václav Kubernát882174d2020-03-25 21:31:46 +010027using x3::char_;
28
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010029auto const leaf_data_enum_def =
Václav Kubernát3a99f002020-03-31 02:27:41 +020030 +char_;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010031
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010032struct bool_symbol_table : x3::symbols<bool> {
33 bool_symbol_table()
34 {
Václav Kubernát3a99f002020-03-31 02:27:41 +020035 add
36 ("true", true)
37 ("false", false);
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010038 }
Václav Kubernát882174d2020-03-25 21:31:46 +010039} const bool_symbols;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010040
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010041auto const leaf_data_string_def =
42 '\'' >> *(char_-'\'') >> '\'' |
43 '\"' >> *(char_-'\"') >> '\"';
44
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010045auto const leaf_data_binary_def =
Václav Kubernáte118f002020-05-14 22:54:13 +020046 as<std::string>[+(x3::alnum | char_('+') | char_('/')) >> -char_('=') >> -char_('=')];
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010047
48auto const leaf_data_identityRef_data_def =
Jan Kundrát0d8abd12020-05-07 02:00:14 +020049 -module >> node_identifier;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010050
Václav Kubernát3a99f002020-03-31 02:27:41 +020051// TODO: get rid of this and use leaf_data_identityRef_data directly
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010052auto const leaf_data_identityRef_def =
Václav Kubernát3a99f002020-03-31 02:27:41 +020053 leaf_data_identityRef_data;
54
55template <typename It, typename Ctx, typename RCtx, typename Attr>
56struct impl_LeafData {
57 It& first;
58 It last;
59 Ctx const& ctx;
60 RCtx& rctx;
61 Attr& attr;
62 ParserContext& parserContext;
63
64 bool operator()(const yang::Binary&) const
65 {
66 return leaf_data_binary.parse(first, last, ctx, rctx, attr);
67 }
68 bool operator()(const yang::Bool&) const
69 {
70 return bool_symbols.parse(first, last, ctx, rctx, attr);
71 }
72 bool operator()(const yang::Decimal&) const
73 {
74 return x3::double_.parse(first, last, ctx, rctx, attr);
75 }
76 bool operator()(const yang::Uint8&) const
77 {
78 return x3::uint8.parse(first, last, ctx, rctx, attr);
79 }
80 bool operator()(const yang::Uint16&) const
81 {
82 return x3::uint16.parse(first, last, ctx, rctx, attr);
83 }
84 bool operator()(const yang::Uint32&) const
85 {
86 return x3::uint32.parse(first, last, ctx, rctx, attr);
87 }
88 bool operator()(const yang::Uint64&) const
89 {
90 return x3::uint64.parse(first, last, ctx, rctx, attr);
91 }
92 bool operator()(const yang::Int8&) const
93 {
94 return x3::int8.parse(first, last, ctx, rctx, attr);
95 }
96 bool operator()(const yang::Int16&) const
97 {
98 return x3::int16.parse(first, last, ctx, rctx, attr);
99 }
100 bool operator()(const yang::Int32&) const
101 {
102 return x3::int32.parse(first, last, ctx, rctx, attr);
103 }
104 bool operator()(const yang::Int64&) const
105 {
106 return x3::int64.parse(first, last, ctx, rctx, attr);
107 }
108 bool operator()(const yang::String&) const
109 {
110 return leaf_data_string.parse(first, last, ctx, rctx, attr);
111 }
112 template <typename Type>
113 void createSetSuggestions(const Type& type) const
114 {
115 parserContext.m_suggestions.clear();
116 std::transform(type.m_allowedValues.begin(),
117 type.m_allowedValues.end(),
118 std::inserter(parserContext.m_suggestions, parserContext.m_suggestions.end()),
119 [](auto it) { return Completion{it.m_value}; });
120 parserContext.m_completionIterator = first;
121 }
122 bool operator()(const yang::Enum& type) const
123 {
124 createSetSuggestions(type);
125 // leaf_data_enum will advance the iterator if it succeeds, so I have
126 // to save the iterator here, to roll it back in case the enum is
127 // invalid.
128 auto saveIter = first;
129 auto pass = leaf_data_enum.parse(first, last, ctx, rctx, attr);
130 if (!pass) {
131 return false;
132 }
133 auto isValidEnum = type.m_allowedValues.count(boost::get<enum_>(attr)) != 0;
134 if (!isValidEnum) {
135 first = saveIter;
136 parserContext.m_errorMsg = "leaf data type mismatch: Expected an enum here. Allowed values:";
137 for (const auto& it : type.m_allowedValues) {
138 parserContext.m_errorMsg += " " + it.m_value;
139 }
140 }
141 return isValidEnum;
142 }
143 bool operator()(const yang::IdentityRef& type) const
144 {
145 createSetSuggestions(type);
146 // leaf_data_identityRef will advance the iterator if it succeeds, so I have
147 // to save the iterator here, to roll it back in case the enum is
148 // invalid.
149 auto saveIter = first;
150 auto pass = leaf_data_identityRef.parse(first, last, ctx, rctx, attr);
151 if (!pass) {
152 return false;
153 }
154 identityRef_ pair{boost::get<identityRef_>(attr)};
155 if (!pair.m_prefix) {
156 pair.m_prefix = module_{parserContext.currentSchemaPath().m_nodes.front().m_prefix.get().m_name};
157 }
158 auto isValidIdentity = type.m_allowedValues.count(pair) != 0;
159 if (!isValidIdentity) {
160 first = saveIter;
161 }
162 return isValidIdentity;
163 }
164 bool operator()(const yang::LeafRef& leafRef) const
165 {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200166 return std::visit(*this, leafRef.m_targetType->m_type);
Václav Kubernát3a99f002020-03-31 02:27:41 +0200167 }
Václav Kubernát2984f442020-02-20 17:43:35 +0100168 bool operator()(const yang::Union& unionInfo) const
169 {
170 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 +0200171 return std::visit(*this, type.m_type);
Václav Kubernát2984f442020-02-20 17:43:35 +0100172 });
173 }
Václav Kubernát3a99f002020-03-31 02:27:41 +0200174};
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100175
Václav Kubernát882174d2020-03-25 21:31:46 +0100176struct LeafData : x3::parser<LeafData> {
177 using attribute_type = leaf_data_;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100178
Václav Kubernát882174d2020-03-25 21:31:46 +0100179 // TODO: Can this be placed in a .cpp file?
180 template <typename It, typename Ctx, typename RCtx, typename Attr>
181 bool parse(It& first, It last, Ctx const& ctx, RCtx& rctx, Attr& attr) const
182 {
183 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
184 const Schema& schema = parserContext.m_schema;
Václav Kubernát13b23d72020-04-16 21:49:51 +0200185 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 +0100186
Václav Kubernát3a99f002020-03-31 02:27:41 +0200187 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 +0100188
189 if (!pass) {
190 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200191 parserContext.m_errorMsg = "leaf data type mismatch: Expected " + leafDataTypeToString(type) + " here:";
Václav Kubernát882174d2020-03-25 21:31:46 +0100192 }
193 }
194 return pass;
195 }
196};
197
198auto const leaf_data = x3::no_skip[std::move(LeafData())];
199
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100200BOOST_SPIRIT_DEFINE(leaf_data_enum)
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100201BOOST_SPIRIT_DEFINE(leaf_data_string)
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100202BOOST_SPIRIT_DEFINE(leaf_data_binary)
203BOOST_SPIRIT_DEFINE(leaf_data_identityRef_data)
204BOOST_SPIRIT_DEFINE(leaf_data_identityRef)