blob: 404a9b28a49ee20b05a184b15f4250cb641ed66a [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át9ae8cc42020-03-25 19:17:41 +010025
Václav Kubernát882174d2020-03-25 21:31:46 +010026using x3::char_;
27
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010028auto const leaf_data_enum_def =
Václav Kubernát3a99f002020-03-31 02:27:41 +020029 +char_;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010030
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010031struct bool_symbol_table : x3::symbols<bool> {
32 bool_symbol_table()
33 {
Václav Kubernát3a99f002020-03-31 02:27:41 +020034 add
35 ("true", true)
36 ("false", false);
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010037 }
Václav Kubernát882174d2020-03-25 21:31:46 +010038} const bool_symbols;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010039
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010040auto const leaf_data_string_def =
41 '\'' >> *(char_-'\'') >> '\'' |
42 '\"' >> *(char_-'\"') >> '\"';
43
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010044auto const leaf_data_binary_def =
Václav Kubernáte118f002020-05-14 22:54:13 +020045 as<std::string>[+(x3::alnum | char_('+') | char_('/')) >> -char_('=') >> -char_('=')];
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010046
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010047auto const leaf_data_identityRef_def =
Václav Kubernáte7f62ae2020-05-14 22:57:53 +020048 -module >> node_identifier;
Václav Kubernát3a99f002020-03-31 02:27:41 +020049
50template <typename It, typename Ctx, typename RCtx, typename Attr>
51struct 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át13b23d72020-04-16 21:49:51 +0200161 return std::visit(*this, leafRef.m_targetType->m_type);
Václav Kubernát3a99f002020-03-31 02:27:41 +0200162 }
Václav Kubernát2984f442020-02-20 17:43:35 +0100163 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át13b23d72020-04-16 21:49:51 +0200166 return std::visit(*this, type.m_type);
Václav Kubernát2984f442020-02-20 17:43:35 +0100167 });
168 }
Václav Kubernát3a99f002020-03-31 02:27:41 +0200169};
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100170
Václav Kubernát882174d2020-03-25 21:31:46 +0100171struct LeafData : x3::parser<LeafData> {
172 using attribute_type = leaf_data_;
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100173
Václav Kubernát882174d2020-03-25 21:31:46 +0100174 // 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át13b23d72020-04-16 21:49:51 +0200180 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 +0100181
Václav Kubernát3a99f002020-03-31 02:27:41 +0200182 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 +0100183
184 if (!pass) {
185 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200186 parserContext.m_errorMsg = "leaf data type mismatch: Expected " + leafDataTypeToString(type) + " here:";
Václav Kubernát882174d2020-03-25 21:31:46 +0100187 }
188 }
189 return pass;
190 }
191};
192
193auto const leaf_data = x3::no_skip[std::move(LeafData())];
194
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100195BOOST_SPIRIT_DEFINE(leaf_data_enum)
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100196BOOST_SPIRIT_DEFINE(leaf_data_string)
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100197BOOST_SPIRIT_DEFINE(leaf_data_binary)
Václav Kubernát9ae8cc42020-03-25 19:17:41 +0100198BOOST_SPIRIT_DEFINE(leaf_data_identityRef)