blob: 9fa3db1d87e3e5f2c73321896277f055afe00395 [file] [log] [blame]
Václav Kubernátd0ea9b22020-04-24 00:44:15 +02001/*
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>
11#include "ast_handlers.hpp"
12#include "common_parsers.hpp"
13#include "leaf_data.hpp"
14
15namespace x3 = boost::spirit::x3;
16
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020017x3::rule<presenceContainerPath_class, dataPath_> const presenceContainerPath = "presenceContainerPath";
18x3::rule<listInstancePath_class, dataPath_> const listInstancePath = "listInstancePath";
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020019x3::rule<leafListElementPath_class, dataPath_> const leafListElementPath = "leafListElementPath";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020020x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020021x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020022x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart";
23x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue";
24x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020025x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020026x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions";
27x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions";
28x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd";
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020029x3::rule<class leafListValue_class, leaf_data_> const leafListValue = "leafListValue";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020030
Václav Kubernát4a58ce62020-05-14 17:58:10 +020031enum class NodeParserMode {
32 CompleteDataNode,
33 IncompleteDataNode,
34 CompletionsOnly,
35 SchemaNode
36};
37
38template <auto>
39struct ModeToAttribute;
40template <>
41struct ModeToAttribute<NodeParserMode::CompleteDataNode> {
42 using type = dataNode_;
43};
44template <>
45struct ModeToAttribute<NodeParserMode::IncompleteDataNode> {
46 using type = dataNode_;
47};
48template <>
49struct ModeToAttribute<NodeParserMode::SchemaNode> {
50 using type = schemaNode_;
51};
52// The CompletionsOnly attribute is dataNode_ only because of convenience:
53// having the same return type means we can get by without a ton of `if constexpr` stanzas.
54// So the code will still "parse data into the target attr" for simplicity.
55template <>
56struct ModeToAttribute<NodeParserMode::CompletionsOnly> {
57 using type = dataNode_;
58};
59
Václav Kubernát743d9eb2020-05-18 13:42:36 +020060enum class CompletionMode {
61 Schema,
62 Data
63};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020064
Václav Kubernát743d9eb2020-05-18 13:42:36 +020065template <NodeParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
66struct NodeParser : x3::parser<NodeParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020067 using attribute_type = typename ModeToAttribute<PARSER_MODE>::type;
Václav Kubernátabf52802020-05-19 01:31:17 +020068
69 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
70
71 NodeParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction)
72 : m_filterFunction(filterFunction)
73 {
74 }
75
Václav Kubernát4a58ce62020-05-14 17:58:10 +020076 // GCC complains that `end` isn't used when doing completions only
77 // FIXME: GCC 10.1 doesn't emit a warning here. Remove [[maybe_unused]] when GCC 10 is available
Václav Kubernát60a0ed52020-04-28 15:21:33 +020078 template <typename It, typename Ctx, typename RCtx, typename Attr>
Václav Kubernát4a58ce62020-05-14 17:58:10 +020079 bool parse(It& begin, [[maybe_unused]] It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
Václav Kubernát60a0ed52020-04-28 15:21:33 +020080 {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020081 std::string tableName;
Václav Kubernát4a58ce62020-05-14 17:58:10 +020082 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020083 tableName = "schemaNode";
84 } else {
85 tableName = "dataNode";
86 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +020087 x3::symbols<attribute_type> table(tableName);
Václav Kubernát60a0ed52020-04-28 15:21:33 +020088
89 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
90 parserContext.m_suggestions.clear();
91 for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020092 attribute_type out;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020093 std::string parseString;
94 if (child.first) {
95 out.m_prefix = module_{*child.first};
96 parseString = *child.first + ":";
97 }
98 parseString += child.second;
Václav Kubernátabf52802020-05-19 01:31:17 +020099
100 if (!m_filterFunction(parserContext.m_schema, joinPaths(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always), parseString))) {
101 continue;
102 }
103
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200104 switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) {
105 case yang::NodeTypes::Container:
106 case yang::NodeTypes::PresenceContainer:
107 out.m_suffix = container_{child.second};
108 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
109 break;
110 case yang::NodeTypes::Leaf:
111 out.m_suffix = leaf_{child.second};
112 parserContext.m_suggestions.emplace(Completion{parseString + " "});
113 break;
114 case yang::NodeTypes::List:
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200115 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200116 out.m_suffix = list_{child.second};
117 } else {
118 out.m_suffix = listElement_{child.second, {}};
119 }
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200120
121 if constexpr (COMPLETION_MODE == CompletionMode::Schema) {
122 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
123 } else {
124 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
125 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200126 break;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200127 case yang::NodeTypes::LeafList:
128 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
129 out.m_suffix = leafList_{child.second};
130 } else {
131 out.m_suffix = leafListElement_{child.second, {}};
132 }
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200133
134 if constexpr (COMPLETION_MODE == CompletionMode::Schema) {
135 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
136 } else {
137 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
138 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200139 break;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200140 case yang::NodeTypes::Action:
141 case yang::NodeTypes::AnyXml:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200142 case yang::NodeTypes::Notification:
143 case yang::NodeTypes::Rpc:
144 continue;
145 }
146 table.add(parseString, out);
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200147 if (!child.first) {
148 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
149 out.m_prefix = topLevelModule;
150 table.add(topLevelModule->m_name + ":" + parseString, out);
151 }
152 }
Václav Kubernátaed4bc72020-06-08 01:09:25 +0200153 table.add("..", attribute_type{nodeup_{}});
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200154 parserContext.m_completionIterator = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200155
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200156 if constexpr (PARSER_MODE == NodeParserMode::CompletionsOnly) {
157 return true;
158 } else {
159 It saveIter;
160 // GCC complains that I assign saveIter because I use it only if NodeType is dataNode_
161 // FIXME: GCC 10.1 doesn't emit a warning here. Make this unconditional when GCC 10 is available.
162 if constexpr (std::is_same<attribute_type, dataNode_>()) {
163 saveIter = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200164 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200165
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200166 auto res = table.parse(begin, end, ctx, rctx, attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200167
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200168 if (std::holds_alternative<leaf_>(attr.m_suffix)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200169 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
170 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
171 return boost::optional<std::string>{it.m_name};
172 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200173 std::get<leaf_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200174 parserContext.m_tmpListKeyLeafPath.m_node = node;
175 }
176
177 if constexpr (std::is_same<attribute_type, dataNode_>()) {
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200178 if (std::holds_alternative<listElement_>(attr.m_suffix)) {
Václav Kubernát2db124c2020-05-28 21:58:36 +0200179 parserContext.m_tmpListPath = parserContext.currentDataPath();
180 auto tmpList = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200181 parserContext.m_tmpListPath.m_nodes.emplace_back(attr.m_prefix, tmpList);
Václav Kubernát2db124c2020-05-28 21:58:36 +0200182
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200183 res = listSuffix.parse(begin, end, ctx, rctx, std::get<listElement_>(attr.m_suffix).m_keys);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200184
185 // FIXME: think of a better way to do this, that is, get rid of manual iterator reverting
186 if (!res) {
187 // If listSuffix didn't succeed, we check, if we allow incomplete nodes. If we do, then we replace listElement_ with list_.
188 // If we don't, we fail the whole symbol table.
189 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
190 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200191 attr.m_suffix = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200192 } else {
193 begin = saveIter;
194 }
195 }
196 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200197
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200198 if (std::holds_alternative<leafListElement_>(attr.m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200199 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
200 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
201 return boost::optional<std::string>{it.m_name};
202 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200203 std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200204 parserContext.m_tmpListKeyLeafPath.m_node = node;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200205 res = leafListValue.parse(begin, end, ctx, rctx, std::get<leafListElement_>(attr.m_suffix).m_value);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200206
207 if (!res) {
208 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
209 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200210 attr.m_suffix = leafList_{std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200211 } else {
212 begin = saveIter;
213 }
214 }
215 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200216 }
217
218 if (res) {
219 parserContext.pushPathFragment(attr);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200220 }
221
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200222 return res;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200223 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200224 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200225};
226
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200227template <CompletionMode COMPLETION_MODE> using schemaNode = NodeParser<NodeParserMode::SchemaNode, COMPLETION_MODE>;
228template <CompletionMode COMPLETION_MODE> using dataNode = NodeParser<NodeParserMode::CompleteDataNode, COMPLETION_MODE>;
229template <CompletionMode COMPLETION_MODE> using incompleteDataNode = NodeParser<NodeParserMode::IncompleteDataNode, COMPLETION_MODE>;
230template <CompletionMode COMPLETION_MODE> using pathCompletions = NodeParser<NodeParserMode::CompletionsOnly, COMPLETION_MODE>;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200231
Václav Kubernát4618fa42020-05-07 01:33:19 +0200232using AnyPath = boost::variant<schemaPath_, dataPath_>;
233
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200234enum class PathParserMode {
235 AnyPath,
236 DataPath,
237 DataPathListEnd
238};
239
240template <>
241struct ModeToAttribute<PathParserMode::AnyPath> {
242 using type = AnyPath;
243};
244
245template <>
246struct ModeToAttribute<PathParserMode::DataPath> {
247 using type = dataPath_;
248};
249
250template <>
251struct ModeToAttribute<PathParserMode::DataPathListEnd> {
252 using type = dataPath_;
253};
254
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200255template <PathParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
256struct PathParser : x3::parser<PathParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200257 using attribute_type = ModeToAttribute<PARSER_MODE>;
Václav Kubernátabf52802020-05-19 01:31:17 +0200258 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
259
260 PathParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction = [] (const auto&, const auto&) {return true;})
261 : m_filterFunction(filterFunction)
262 {
263 }
264
Václav Kubernát4618fa42020-05-07 01:33:19 +0200265 template <typename It, typename Ctx, typename RCtx, typename Attr>
266 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
267 {
Václav Kubernát74d35a42020-05-15 15:29:16 +0200268 initializePath.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200269 dataPath_ attrData;
270
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200271 auto pathEnd = x3::rule<class PathEnd>{"pathEnd"} = &space_separator | x3::eoi;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200272 // absoluteStart has to be separate from the dataPath parser,
273 // otherwise, if the "dataNode % '/'" parser fails, the begin iterator
274 // gets reverted to before the starting slash.
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200275 auto res = (-absoluteStart).parse(begin, end, ctx, rctx, attrData.m_scope);
276 auto dataPath = x3::attr(attrData.m_scope)
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200277 >> (dataNode<COMPLETION_MODE>{m_filterFunction} % '/' | pathEnd >> x3::attr(std::vector<dataNode_>{}))
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200278 >> -trailingSlash;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200279 res = dataPath.parse(begin, end, ctx, rctx, attrData);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200280
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200281 // If we allow data paths with a list at the end, we just try to parse that separately.
282 if constexpr (PARSER_MODE == PathParserMode::DataPathListEnd || PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát4618fa42020-05-07 01:33:19 +0200283 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200284 dataNode_ attrNodeList;
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200285 res = incompleteDataNode<COMPLETION_MODE>{m_filterFunction}.parse(begin, end, ctx, rctx, attrNodeList);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200286 if (res) {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200287 attrData.m_nodes.emplace_back(attrNodeList);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200288 // If the trailing slash matches, no more nodes are parsed.
289 // That means no more completion. So, I generate them
290 // manually.
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200291 res = (-(trailingSlash >> x3::omit[pathCompletions<COMPLETION_MODE>{m_filterFunction}])).parse(begin, end, ctx, rctx, attrData.m_trailingSlash);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200292 }
293 }
294 }
295
296 attr = attrData;
297 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
298 // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path
299 auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200300 [] (const auto& node) { return std::holds_alternative<listElement_>(node.m_suffix); });
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200301 // If parsing failed, or if there's more input we try parsing schema nodes.
302 if (!hasLists) {
303 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
304 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
305 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200306 auto schemaPath = schemaNode<COMPLETION_MODE>{m_filterFunction} % '/';
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200307 // The schemaPath parser continues where the dataPath parser ended.
308 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
309 auto trailing = -trailingSlash >> pathEnd;
310 res = trailing.parse(begin, end, ctx, rctx, attrSchema.m_trailingSlash);
311 attr = attrSchema;
312 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200313 }
314 }
315 return res;
316 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200317};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200318
319// Need to use these wrappers so that my PathParser class gets the proper
320// attribute. Otherwise, Spirit injects the attribute of the outer parser that
321// uses my PathParser.
322// Example grammar: anyPath | module.
323// The PathParser class would get a boost::variant as the attribute, but I
324// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200325// attribute I want (and let Spirit deal with boost::variant).
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200326auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath, CompletionMode::Schema>{};
327auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath, CompletionMode::Data>{};
328auto const dataPathListEnd = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200329
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200330#if __clang__
331#pragma GCC diagnostic push
332#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
333#endif
334
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200335struct SuggestLeafListEnd : x3::parser<SuggestLeafListEnd> {
336 using attribute_type = x3::unused_type;
337 template <typename It, typename Ctx, typename RCtx, typename Attr>
338 bool parse(It& begin, It, Ctx const& ctx, RCtx&, Attr&) const
339 {
340 auto& parserContext = x3::get<parser_context_tag>(ctx);
341 parserContext.m_completionIterator = begin;
342 parserContext.m_suggestions = {Completion{"]"}};
343
344 return true;
345 }
346} const suggestLeafListEnd;
347
348auto const leafListValue_def =
349 '[' >> leaf_data >> suggestLeafListEnd >> ']';
350
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200351auto const rest =
352 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
353
354auto const key_identifier_def =
355 x3::lexeme[
356 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
357 ];
358
359auto const createKeySuggestions_def =
360 x3::eps;
361
362auto const createValueSuggestions_def =
363 x3::eps;
364
365auto const suggestKeysEnd_def =
366 x3::eps;
367
368auto const keyValue_def =
369 key_identifier > '=' > createValueSuggestions > leaf_data;
370
371auto const keyValueWrapper =
Václav Kubernát9fa5dca2020-06-01 03:56:41 +0200372 x3::no_skip['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200373
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200374// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
375auto const listSuffix_def =
376 *keyValueWrapper;
377
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200378auto const list_def =
379 node_identifier >> !char_('[');
380
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200381auto const absoluteStart_def =
382 x3::omit['/'] >> x3::attr(Scope::Absolute);
383
384auto const trailingSlash_def =
385 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
386
Václav Kubernátabf52802020-05-19 01:31:17 +0200387auto const filterConfigFalse = [] (const Schema& schema, const std::string& path) {
388 return schema.isConfig(path);
389};
390
Václav Kubernát28cf3362020-06-29 17:52:51 +0200391struct writableOps_tag;
392
393PathParser<PathParserMode::DataPath, CompletionMode::Data> const dataPathFilterConfigFalse{filterConfigFalse};
394
395struct WritableLeafPath : x3::parser<WritableLeafPath> {
396 using attribute_type = dataPath_;
397 template <typename It, typename Ctx, typename RCtx, typename Attr>
398 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
399 {
400 bool res;
401 if (x3::get<writableOps_tag>(ctx) == WritableOps::Yes) {
402 res = dataPath.parse(begin, end, ctx, rctx, attr);
403 } else {
404 res = dataPathFilterConfigFalse.parse(begin, end, ctx, rctx, attr);
405 }
406 if (!res) {
407 return false;
408 }
409
410 if (attr.m_nodes.empty() || !std::holds_alternative<leaf_>(attr.m_nodes.back().m_suffix)) {
411 auto& parserContext = x3::get<parser_context_tag>(ctx);
412 parserContext.m_errorMsg = "This is not a path to leaf.";
413 return false;
414 }
415
416 return true;
417 }
418
419} writableLeafPath;
420
Václav Kubernátabf52802020-05-19 01:31:17 +0200421auto const writableLeafPath_def =
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200422 PathParser<PathParserMode::DataPath, CompletionMode::Data>{filterConfigFalse};
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200423
424auto const presenceContainerPath_def =
425 dataPath;
426
427auto const listInstancePath_def =
428 dataPath;
429
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200430auto const leafListElementPath_def =
431 dataPath;
432
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200433// A "nothing" parser, which is used to indicate we tried to parse a path
434auto const initializePath_def =
435 x3::eps;
436
437
438
439#if __clang__
440#pragma GCC diagnostic pop
441#endif
442
443BOOST_SPIRIT_DEFINE(keyValue)
444BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200445BOOST_SPIRIT_DEFINE(listSuffix)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200446BOOST_SPIRIT_DEFINE(presenceContainerPath)
447BOOST_SPIRIT_DEFINE(listInstancePath)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200448BOOST_SPIRIT_DEFINE(leafListElementPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200449BOOST_SPIRIT_DEFINE(initializePath)
450BOOST_SPIRIT_DEFINE(createKeySuggestions)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200451BOOST_SPIRIT_DEFINE(createValueSuggestions)
452BOOST_SPIRIT_DEFINE(suggestKeysEnd)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200453BOOST_SPIRIT_DEFINE(leafListValue)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200454BOOST_SPIRIT_DEFINE(absoluteStart)
455BOOST_SPIRIT_DEFINE(trailingSlash)