blob: 68705d0a47ba9141d2da14ebc3c4936c068967c3 [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_class_binary, std::string> const leaf_data_binary_data = "leaf_data_binary_data";
26x3::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 +010027
Václav Kubernát882174d2020-03-25 21:31:46 +010028using x3::char_;
29
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010030auto const leaf_data_enum_def =
Václav Kubernát3a99f002020-03-31 02:27:41 +020031 +char_;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010032
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010033struct bool_symbol_table : x3::symbols<bool> {
34 bool_symbol_table()
35 {
Václav Kubernát3a99f002020-03-31 02:27:41 +020036 add
37 ("true", true)
38 ("false", false);
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010039 }
Václav Kubernát882174d2020-03-25 21:31:46 +010040} const bool_symbols;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010041
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010042auto const leaf_data_string_def =
43 '\'' >> *(char_-'\'') >> '\'' |
44 '\"' >> *(char_-'\"') >> '\"';
45
46// This intermediate rule is neccessary for coercing to std::string.
Václav Kubernát882174d2020-03-25 21:31:46 +010047// 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át9ae8cc42020-03-25 19:17:41 +010050auto const leaf_data_binary_data_def =
51 +(x3::alnum | char_('+') | char_('/')) >> -char_('=') >> -char_('=');
52
53auto const leaf_data_binary_def =
54 leaf_data_binary_data;
55
56auto const leaf_data_identityRef_data_def =
57 -module >> node_identifier;
58
Václav Kubernát3a99f002020-03-31 02:27:41 +020059// TODO: get rid of this and use leaf_data_identityRef_data directly
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010060auto const leaf_data_identityRef_def =
Václav Kubernát3a99f002020-03-31 02:27:41 +020061 leaf_data_identityRef_data;
62
63template <typename It, typename Ctx, typename RCtx, typename Attr>
64struct 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át13b23d72020-04-16 21:49:51 +0200174 return std::visit(*this, leafRef.m_targetType->m_type);
Václav Kubernát3a99f002020-03-31 02:27:41 +0200175 }
Václav Kubernát2984f442020-02-20 17:43:35 +0100176 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át13b23d72020-04-16 21:49:51 +0200179 return std::visit(*this, type.m_type);
Václav Kubernát2984f442020-02-20 17:43:35 +0100180 });
181 }
Václav Kubernát3a99f002020-03-31 02:27:41 +0200182};
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100183
Václav Kubernát882174d2020-03-25 21:31:46 +0100184struct LeafData : x3::parser<LeafData> {
185 using attribute_type = leaf_data_;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100186
Václav Kubernát882174d2020-03-25 21:31:46 +0100187 // 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át13b23d72020-04-16 21:49:51 +0200193 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 +0100194
Václav Kubernát3a99f002020-03-31 02:27:41 +0200195 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 +0100196
197 if (!pass) {
198 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200199 parserContext.m_errorMsg = "leaf data type mismatch: Expected " + leafDataTypeToString(type) + " here:";
Václav Kubernát882174d2020-03-25 21:31:46 +0100200 }
201 }
202 return pass;
203 }
204};
205
206auto const leaf_data = x3::no_skip[std::move(LeafData())];
207
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100208BOOST_SPIRIT_DEFINE(leaf_data_enum)
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100209BOOST_SPIRIT_DEFINE(leaf_data_string)
210BOOST_SPIRIT_DEFINE(leaf_data_binary_data)
211BOOST_SPIRIT_DEFINE(leaf_data_binary)
212BOOST_SPIRIT_DEFINE(leaf_data_identityRef_data)
213BOOST_SPIRIT_DEFINE(leaf_data_identityRef)