blob: 00629bb1d8cd20a30179b69ed8f2ab0a090a495a [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áte7248b22020-06-26 15:38:59 +020017x3::rule<cdPath_class, dataPath_> const cdPath = "cdPath";
18x3::rule<rpcPath_class, dataPath_> const rpcPath = "rpcPath";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020019x3::rule<presenceContainerPath_class, dataPath_> const presenceContainerPath = "presenceContainerPath";
20x3::rule<listInstancePath_class, dataPath_> const listInstancePath = "listInstancePath";
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020021x3::rule<leafListElementPath_class, dataPath_> const leafListElementPath = "leafListElementPath";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020022x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020023x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020024x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart";
25x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue";
26x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020027x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020028x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions";
29x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions";
30x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd";
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020031x3::rule<class leafListValue_class, leaf_data_> const leafListValue = "leafListValue";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020032
Václav Kubernát4a58ce62020-05-14 17:58:10 +020033enum class NodeParserMode {
34 CompleteDataNode,
35 IncompleteDataNode,
36 CompletionsOnly,
37 SchemaNode
38};
39
40template <auto>
41struct ModeToAttribute;
42template <>
43struct ModeToAttribute<NodeParserMode::CompleteDataNode> {
44 using type = dataNode_;
45};
46template <>
47struct ModeToAttribute<NodeParserMode::IncompleteDataNode> {
48 using type = dataNode_;
49};
50template <>
51struct ModeToAttribute<NodeParserMode::SchemaNode> {
52 using type = schemaNode_;
53};
54// The CompletionsOnly attribute is dataNode_ only because of convenience:
55// having the same return type means we can get by without a ton of `if constexpr` stanzas.
56// So the code will still "parse data into the target attr" for simplicity.
57template <>
58struct ModeToAttribute<NodeParserMode::CompletionsOnly> {
59 using type = dataNode_;
60};
61
Václav Kubernát743d9eb2020-05-18 13:42:36 +020062enum class CompletionMode {
63 Schema,
64 Data
65};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020066
Václav Kubernát743d9eb2020-05-18 13:42:36 +020067template <NodeParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
68struct NodeParser : x3::parser<NodeParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020069 using attribute_type = typename ModeToAttribute<PARSER_MODE>::type;
Václav Kubernátabf52802020-05-19 01:31:17 +020070
71 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
72
73 NodeParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction)
74 : m_filterFunction(filterFunction)
75 {
76 }
77
Václav Kubernát4a58ce62020-05-14 17:58:10 +020078 // GCC complains that `end` isn't used when doing completions only
79 // 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 +020080 template <typename It, typename Ctx, typename RCtx, typename Attr>
Václav Kubernát4a58ce62020-05-14 17:58:10 +020081 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 +020082 {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020083 std::string tableName;
Václav Kubernát4a58ce62020-05-14 17:58:10 +020084 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020085 tableName = "schemaNode";
86 } else {
87 tableName = "dataNode";
88 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +020089 x3::symbols<attribute_type> table(tableName);
Václav Kubernát60a0ed52020-04-28 15:21:33 +020090
91 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
92 parserContext.m_suggestions.clear();
93 for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020094 attribute_type out;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020095 std::string parseString;
96 if (child.first) {
97 out.m_prefix = module_{*child.first};
98 parseString = *child.first + ":";
99 }
100 parseString += child.second;
Václav Kubernátabf52802020-05-19 01:31:17 +0200101
102 if (!m_filterFunction(parserContext.m_schema, joinPaths(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always), parseString))) {
103 continue;
104 }
105
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200106 switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) {
107 case yang::NodeTypes::Container:
108 case yang::NodeTypes::PresenceContainer:
109 out.m_suffix = container_{child.second};
110 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
111 break;
112 case yang::NodeTypes::Leaf:
113 out.m_suffix = leaf_{child.second};
114 parserContext.m_suggestions.emplace(Completion{parseString + " "});
115 break;
116 case yang::NodeTypes::List:
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200117 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200118 out.m_suffix = list_{child.second};
119 } else {
120 out.m_suffix = listElement_{child.second, {}};
121 }
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200122
123 if constexpr (COMPLETION_MODE == CompletionMode::Schema) {
124 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
125 } else {
126 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
127 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200128 break;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200129 case yang::NodeTypes::LeafList:
130 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
131 out.m_suffix = leafList_{child.second};
132 } else {
133 out.m_suffix = leafListElement_{child.second, {}};
134 }
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200135
136 if constexpr (COMPLETION_MODE == CompletionMode::Schema) {
137 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
138 } else {
139 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
140 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200141 break;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200142 case yang::NodeTypes::Rpc:
143 out.m_suffix = rpcNode_{child.second};
144 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
145 break;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200146 case yang::NodeTypes::Action:
147 case yang::NodeTypes::AnyXml:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200148 case yang::NodeTypes::Notification:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200149 continue;
150 }
151 table.add(parseString, out);
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200152 if (!child.first) {
153 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
154 out.m_prefix = topLevelModule;
155 table.add(topLevelModule->m_name + ":" + parseString, out);
156 }
157 }
Václav Kubernátaed4bc72020-06-08 01:09:25 +0200158 table.add("..", attribute_type{nodeup_{}});
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200159 parserContext.m_completionIterator = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200160
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200161 if constexpr (PARSER_MODE == NodeParserMode::CompletionsOnly) {
162 return true;
163 } else {
164 It saveIter;
165 // GCC complains that I assign saveIter because I use it only if NodeType is dataNode_
166 // FIXME: GCC 10.1 doesn't emit a warning here. Make this unconditional when GCC 10 is available.
167 if constexpr (std::is_same<attribute_type, dataNode_>()) {
168 saveIter = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200169 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200170
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200171 auto res = table.parse(begin, end, ctx, rctx, attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200172
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200173 if (std::holds_alternative<leaf_>(attr.m_suffix)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200174 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
175 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
176 return boost::optional<std::string>{it.m_name};
177 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200178 std::get<leaf_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200179 parserContext.m_tmpListKeyLeafPath.m_node = node;
180 }
181
182 if constexpr (std::is_same<attribute_type, dataNode_>()) {
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200183 if (std::holds_alternative<listElement_>(attr.m_suffix)) {
Václav Kubernát2db124c2020-05-28 21:58:36 +0200184 parserContext.m_tmpListPath = parserContext.currentDataPath();
185 auto tmpList = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200186 parserContext.m_tmpListPath.m_nodes.emplace_back(attr.m_prefix, tmpList);
Václav Kubernát2db124c2020-05-28 21:58:36 +0200187
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200188 res = listSuffix.parse(begin, end, ctx, rctx, std::get<listElement_>(attr.m_suffix).m_keys);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200189
190 // FIXME: think of a better way to do this, that is, get rid of manual iterator reverting
191 if (!res) {
192 // If listSuffix didn't succeed, we check, if we allow incomplete nodes. If we do, then we replace listElement_ with list_.
193 // If we don't, we fail the whole symbol table.
194 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
195 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200196 attr.m_suffix = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200197 } else {
198 begin = saveIter;
199 }
200 }
201 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200202
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200203 if (std::holds_alternative<leafListElement_>(attr.m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200204 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
205 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
206 return boost::optional<std::string>{it.m_name};
207 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200208 std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200209 parserContext.m_tmpListKeyLeafPath.m_node = node;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200210 res = leafListValue.parse(begin, end, ctx, rctx, std::get<leafListElement_>(attr.m_suffix).m_value);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200211
212 if (!res) {
213 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
214 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200215 attr.m_suffix = leafList_{std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200216 } else {
217 begin = saveIter;
218 }
219 }
220 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200221 }
222
223 if (res) {
224 parserContext.pushPathFragment(attr);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200225 }
226
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200227 return res;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200228 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200229 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200230};
231
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200232template <CompletionMode COMPLETION_MODE> using schemaNode = NodeParser<NodeParserMode::SchemaNode, COMPLETION_MODE>;
233template <CompletionMode COMPLETION_MODE> using dataNode = NodeParser<NodeParserMode::CompleteDataNode, COMPLETION_MODE>;
234template <CompletionMode COMPLETION_MODE> using incompleteDataNode = NodeParser<NodeParserMode::IncompleteDataNode, COMPLETION_MODE>;
235template <CompletionMode COMPLETION_MODE> using pathCompletions = NodeParser<NodeParserMode::CompletionsOnly, COMPLETION_MODE>;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200236
Václav Kubernát4618fa42020-05-07 01:33:19 +0200237using AnyPath = boost::variant<schemaPath_, dataPath_>;
238
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200239enum class PathParserMode {
240 AnyPath,
241 DataPath,
242 DataPathListEnd
243};
244
245template <>
246struct ModeToAttribute<PathParserMode::AnyPath> {
247 using type = AnyPath;
248};
249
250template <>
251struct ModeToAttribute<PathParserMode::DataPath> {
252 using type = dataPath_;
253};
254
255template <>
256struct ModeToAttribute<PathParserMode::DataPathListEnd> {
257 using type = dataPath_;
258};
259
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200260template <PathParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
261struct PathParser : x3::parser<PathParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200262 using attribute_type = ModeToAttribute<PARSER_MODE>;
Václav Kubernátabf52802020-05-19 01:31:17 +0200263 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
264
265 PathParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction = [] (const auto&, const auto&) {return true;})
266 : m_filterFunction(filterFunction)
267 {
268 }
269
Václav Kubernát4618fa42020-05-07 01:33:19 +0200270 template <typename It, typename Ctx, typename RCtx, typename Attr>
271 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
272 {
Václav Kubernát74d35a42020-05-15 15:29:16 +0200273 initializePath.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200274 dataPath_ attrData;
275
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200276 auto pathEnd = x3::rule<class PathEnd>{"pathEnd"} = &space_separator | x3::eoi;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200277 // absoluteStart has to be separate from the dataPath parser,
278 // otherwise, if the "dataNode % '/'" parser fails, the begin iterator
279 // gets reverted to before the starting slash.
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200280 auto res = (-absoluteStart).parse(begin, end, ctx, rctx, attrData.m_scope);
281 auto dataPath = x3::attr(attrData.m_scope)
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200282 >> (dataNode<COMPLETION_MODE>{m_filterFunction} % '/' | pathEnd >> x3::attr(std::vector<dataNode_>{}))
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200283 >> -trailingSlash;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200284 res = dataPath.parse(begin, end, ctx, rctx, attrData);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200285
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200286 // If we allow data paths with a list at the end, we just try to parse that separately.
287 if constexpr (PARSER_MODE == PathParserMode::DataPathListEnd || PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát4618fa42020-05-07 01:33:19 +0200288 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200289 dataNode_ attrNodeList;
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200290 res = incompleteDataNode<COMPLETION_MODE>{m_filterFunction}.parse(begin, end, ctx, rctx, attrNodeList);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200291 if (res) {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200292 attrData.m_nodes.emplace_back(attrNodeList);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200293 // If the trailing slash matches, no more nodes are parsed.
294 // That means no more completion. So, I generate them
295 // manually.
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200296 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 +0200297 }
298 }
299 }
300
301 attr = attrData;
302 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
303 // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path
304 auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200305 [] (const auto& node) { return std::holds_alternative<listElement_>(node.m_suffix); });
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200306 // If parsing failed, or if there's more input we try parsing schema nodes.
307 if (!hasLists) {
308 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
309 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
310 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200311 auto schemaPath = schemaNode<COMPLETION_MODE>{m_filterFunction} % '/';
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200312 // The schemaPath parser continues where the dataPath parser ended.
313 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
314 auto trailing = -trailingSlash >> pathEnd;
315 res = trailing.parse(begin, end, ctx, rctx, attrSchema.m_trailingSlash);
316 attr = attrSchema;
317 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200318 }
319 }
320 return res;
321 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200322};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200323
324// Need to use these wrappers so that my PathParser class gets the proper
325// attribute. Otherwise, Spirit injects the attribute of the outer parser that
326// uses my PathParser.
327// Example grammar: anyPath | module.
328// The PathParser class would get a boost::variant as the attribute, but I
329// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200330// attribute I want (and let Spirit deal with boost::variant).
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200331auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath, CompletionMode::Schema>{};
332auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath, CompletionMode::Data>{};
333auto const dataPathListEnd = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200334
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200335#if __clang__
336#pragma GCC diagnostic push
337#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
338#endif
339
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200340struct SuggestLeafListEnd : x3::parser<SuggestLeafListEnd> {
341 using attribute_type = x3::unused_type;
342 template <typename It, typename Ctx, typename RCtx, typename Attr>
343 bool parse(It& begin, It, Ctx const& ctx, RCtx&, Attr&) const
344 {
345 auto& parserContext = x3::get<parser_context_tag>(ctx);
346 parserContext.m_completionIterator = begin;
347 parserContext.m_suggestions = {Completion{"]"}};
348
349 return true;
350 }
351} const suggestLeafListEnd;
352
353auto const leafListValue_def =
354 '[' >> leaf_data >> suggestLeafListEnd >> ']';
355
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200356auto const rest =
357 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
358
359auto const key_identifier_def =
360 x3::lexeme[
361 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
362 ];
363
364auto const createKeySuggestions_def =
365 x3::eps;
366
367auto const createValueSuggestions_def =
368 x3::eps;
369
370auto const suggestKeysEnd_def =
371 x3::eps;
372
373auto const keyValue_def =
374 key_identifier > '=' > createValueSuggestions > leaf_data;
375
376auto const keyValueWrapper =
Václav Kubernát9fa5dca2020-06-01 03:56:41 +0200377 x3::no_skip['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200378
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200379// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
380auto const listSuffix_def =
381 *keyValueWrapper;
382
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200383auto const list_def =
384 node_identifier >> !char_('[');
385
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200386auto const absoluteStart_def =
387 x3::omit['/'] >> x3::attr(Scope::Absolute);
388
389auto const trailingSlash_def =
390 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
391
Václav Kubernátabf52802020-05-19 01:31:17 +0200392auto const filterConfigFalse = [] (const Schema& schema, const std::string& path) {
393 return schema.isConfig(path);
394};
395
Václav Kubernát28cf3362020-06-29 17:52:51 +0200396struct writableOps_tag;
397
398PathParser<PathParserMode::DataPath, CompletionMode::Data> const dataPathFilterConfigFalse{filterConfigFalse};
399
400struct WritableLeafPath : x3::parser<WritableLeafPath> {
401 using attribute_type = dataPath_;
402 template <typename It, typename Ctx, typename RCtx, typename Attr>
403 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
404 {
405 bool res;
406 if (x3::get<writableOps_tag>(ctx) == WritableOps::Yes) {
407 res = dataPath.parse(begin, end, ctx, rctx, attr);
408 } else {
409 res = dataPathFilterConfigFalse.parse(begin, end, ctx, rctx, attr);
410 }
411 if (!res) {
412 return false;
413 }
414
415 if (attr.m_nodes.empty() || !std::holds_alternative<leaf_>(attr.m_nodes.back().m_suffix)) {
416 auto& parserContext = x3::get<parser_context_tag>(ctx);
417 parserContext.m_errorMsg = "This is not a path to leaf.";
418 return false;
419 }
420
421 return true;
422 }
423
424} writableLeafPath;
425
Václav Kubernátabf52802020-05-19 01:31:17 +0200426auto const writableLeafPath_def =
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200427 PathParser<PathParserMode::DataPath, CompletionMode::Data>{filterConfigFalse};
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200428
Václav Kubernáte7248b22020-06-26 15:38:59 +0200429auto const onlyRpc = [] (const Schema& schema, const std::string& path) {
430 return schema.nodeType(path) == yang::NodeTypes::Rpc;
431};
432
433auto const rpcPath_def =
434 PathParser<PathParserMode::DataPath, CompletionMode::Data>{onlyRpc};
435
436auto const noRpc = [] (const Schema& schema, const std::string& path) {
437 return schema.nodeType(path) != yang::NodeTypes::Rpc;
438};
439
440auto const cdPath_def =
441 PathParser<PathParserMode::DataPath, CompletionMode::Data>{noRpc};
442
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200443auto const presenceContainerPath_def =
444 dataPath;
445
446auto const listInstancePath_def =
447 dataPath;
448
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200449auto const leafListElementPath_def =
450 dataPath;
451
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200452// A "nothing" parser, which is used to indicate we tried to parse a path
453auto const initializePath_def =
454 x3::eps;
455
456
457
458#if __clang__
459#pragma GCC diagnostic pop
460#endif
461
462BOOST_SPIRIT_DEFINE(keyValue)
463BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200464BOOST_SPIRIT_DEFINE(listSuffix)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200465BOOST_SPIRIT_DEFINE(rpcPath)
466BOOST_SPIRIT_DEFINE(cdPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200467BOOST_SPIRIT_DEFINE(presenceContainerPath)
468BOOST_SPIRIT_DEFINE(listInstancePath)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200469BOOST_SPIRIT_DEFINE(leafListElementPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200470BOOST_SPIRIT_DEFINE(initializePath)
471BOOST_SPIRIT_DEFINE(createKeySuggestions)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200472BOOST_SPIRIT_DEFINE(createValueSuggestions)
473BOOST_SPIRIT_DEFINE(suggestKeysEnd)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200474BOOST_SPIRIT_DEFINE(leafListValue)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200475BOOST_SPIRIT_DEFINE(absoluteStart)
476BOOST_SPIRIT_DEFINE(trailingSlash)