blob: 961db96966a2c349a3d20bd1f5af09c8b2cec54c [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átce108db2021-01-25 10:05:40 +010017auto pathEnd = x3::rule<class PathEnd>{"pathEnd"} = &space_separator | x3::eoi;
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020018
Václav Kubernát4a58ce62020-05-14 17:58:10 +020019enum class NodeParserMode {
20 CompleteDataNode,
21 IncompleteDataNode,
22 CompletionsOnly,
23 SchemaNode
24};
25
26template <auto>
27struct ModeToAttribute;
28template <>
29struct ModeToAttribute<NodeParserMode::CompleteDataNode> {
30 using type = dataNode_;
31};
32template <>
33struct ModeToAttribute<NodeParserMode::IncompleteDataNode> {
34 using type = dataNode_;
35};
36template <>
37struct ModeToAttribute<NodeParserMode::SchemaNode> {
38 using type = schemaNode_;
39};
40// The CompletionsOnly attribute is dataNode_ only because of convenience:
41// having the same return type means we can get by without a ton of `if constexpr` stanzas.
42// So the code will still "parse data into the target attr" for simplicity.
43template <>
44struct ModeToAttribute<NodeParserMode::CompletionsOnly> {
45 using type = dataNode_;
46};
47
Václav Kubernát743d9eb2020-05-18 13:42:36 +020048enum class CompletionMode {
49 Schema,
50 Data
51};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020052
Václav Kubernát52b90222022-04-27 11:29:54 +020053auto const createKeySuggestions = x3::rule<createKeySuggestions_class, x3::unused_type>{"createKeySuggestions"} =
54 x3::eps;
55
56auto const key_identifier = x3::rule<key_identifier_class, std::string>{"key_identifier"} =
57 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")));
58
59auto const createValueSuggestions = x3::rule<createValueSuggestions_class, x3::unused_type>{"createValueSuggestions"} =
60 x3::eps;
61
62auto const keyValue = x3::rule<keyValue_class, keyValue_>{"keyValue"} =
63 key_identifier > '=' > createValueSuggestions > leaf_data;
64
65auto const suggestKeysEnd = x3::rule<suggestKeysEnd_class, x3::unused_type>{"suggestKeysEnd"} =
66 x3::eps;
67
68auto const keyValueWrapper =
69 '[' > createKeySuggestions > keyValue > suggestKeysEnd > ']';
70
71// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
72auto const listSuffix = x3::rule<listSuffix_class, std::vector<keyValue_>>{"listSuffix"} =
73 *keyValueWrapper;
74
75struct SuggestLeafListEnd : x3::parser<SuggestLeafListEnd> {
76 using attribute_type = x3::unused_type;
77 template <typename It, typename Ctx, typename RCtx, typename Attr>
78 bool parse(It& begin, It, Ctx const& ctx, RCtx&, Attr&) const
79 {
80 auto& parserContext = x3::get<parser_context_tag>(ctx);
81 parserContext.m_completionIterator = begin;
82 parserContext.m_suggestions = {Completion{"]"}};
83
84 return true;
85 }
86} const suggestLeafListEnd;
87
88auto const leafListValue = x3::rule<class leafListValue_class, leaf_data_>{"leafListValue"} =
89 '[' >> leaf_data >> suggestLeafListEnd >> ']';
90
Václav Kubernát743d9eb2020-05-18 13:42:36 +020091template <NodeParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
92struct NodeParser : x3::parser<NodeParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020093 using attribute_type = typename ModeToAttribute<PARSER_MODE>::type;
Václav Kubernátabf52802020-05-19 01:31:17 +020094
95 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
96
97 NodeParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction)
98 : m_filterFunction(filterFunction)
99 {
100 }
101
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200102 template <typename It, typename Ctx, typename RCtx, typename Attr>
Václav Kubernát832bd7e2020-12-03 03:06:37 +0100103 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200104 {
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200105 std::string tableName;
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200106 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200107 tableName = "schemaNode";
108 } else {
109 tableName = "dataNode";
110 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200111 x3::symbols<attribute_type> table(tableName);
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200112
113 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
114 parserContext.m_suggestions.clear();
115 for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200116 attribute_type out;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200117 std::string parseString;
118 if (child.first) {
119 out.m_prefix = module_{*child.first};
120 parseString = *child.first + ":";
121 }
122 parseString += child.second;
Václav Kubernátabf52802020-05-19 01:31:17 +0200123
124 if (!m_filterFunction(parserContext.m_schema, joinPaths(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always), parseString))) {
125 continue;
126 }
127
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200128 switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) {
129 case yang::NodeTypes::Container:
130 case yang::NodeTypes::PresenceContainer:
131 out.m_suffix = container_{child.second};
132 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
133 break;
134 case yang::NodeTypes::Leaf:
135 out.m_suffix = leaf_{child.second};
136 parserContext.m_suggestions.emplace(Completion{parseString + " "});
137 break;
138 case yang::NodeTypes::List:
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200139 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200140 out.m_suffix = list_{child.second};
141 } else {
142 out.m_suffix = listElement_{child.second, {}};
143 }
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200144
145 if constexpr (COMPLETION_MODE == CompletionMode::Schema) {
146 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
147 } else {
148 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
149 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200150 break;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200151 case yang::NodeTypes::LeafList:
152 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
153 out.m_suffix = leafList_{child.second};
154 } else {
155 out.m_suffix = leafListElement_{child.second, {}};
156 }
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200157
158 if constexpr (COMPLETION_MODE == CompletionMode::Schema) {
159 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
160 } else {
161 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
162 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200163 break;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200164 case yang::NodeTypes::Rpc:
165 out.m_suffix = rpcNode_{child.second};
166 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
167 break;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200168 case yang::NodeTypes::Action:
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200169 out.m_suffix = actionNode_{child.second};
170 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
171 break;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200172 case yang::NodeTypes::AnyXml:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200173 case yang::NodeTypes::Notification:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200174 continue;
175 }
176 table.add(parseString, out);
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200177 if (!child.first) {
178 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
179 out.m_prefix = topLevelModule;
180 table.add(topLevelModule->m_name + ":" + parseString, out);
181 }
182 }
Václav Kubernátaed4bc72020-06-08 01:09:25 +0200183 table.add("..", attribute_type{nodeup_{}});
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200184 parserContext.m_completionIterator = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200185
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200186 if constexpr (PARSER_MODE == NodeParserMode::CompletionsOnly) {
187 return true;
188 } else {
Václav Kubernát832bd7e2020-12-03 03:06:37 +0100189 It saveIter = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200190
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200191 auto res = table.parse(begin, end, ctx, rctx, attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200192
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200193 if (std::holds_alternative<leaf_>(attr.m_suffix)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200194 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
195 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
196 return boost::optional<std::string>{it.m_name};
197 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200198 std::get<leaf_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200199 parserContext.m_tmpListKeyLeafPath.m_node = node;
200 }
201
202 if constexpr (std::is_same<attribute_type, dataNode_>()) {
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200203 if (std::holds_alternative<listElement_>(attr.m_suffix)) {
Václav Kubernát2db124c2020-05-28 21:58:36 +0200204 parserContext.m_tmpListPath = parserContext.currentDataPath();
205 auto tmpList = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200206 parserContext.m_tmpListPath.m_nodes.emplace_back(attr.m_prefix, tmpList);
Václav Kubernát2db124c2020-05-28 21:58:36 +0200207
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200208 res = listSuffix.parse(begin, end, ctx, rctx, std::get<listElement_>(attr.m_suffix).m_keys);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200209
210 // FIXME: think of a better way to do this, that is, get rid of manual iterator reverting
211 if (!res) {
212 // If listSuffix didn't succeed, we check, if we allow incomplete nodes. If we do, then we replace listElement_ with list_.
213 // If we don't, we fail the whole symbol table.
214 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
215 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200216 attr.m_suffix = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200217 } else {
218 begin = saveIter;
219 }
220 }
221 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200222
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200223 if (std::holds_alternative<leafListElement_>(attr.m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200224 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
225 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
226 return boost::optional<std::string>{it.m_name};
227 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200228 std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200229 parserContext.m_tmpListKeyLeafPath.m_node = node;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200230 res = leafListValue.parse(begin, end, ctx, rctx, std::get<leafListElement_>(attr.m_suffix).m_value);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200231
232 if (!res) {
233 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
234 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200235 attr.m_suffix = leafList_{std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200236 } else {
237 begin = saveIter;
238 }
239 }
240 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200241 }
242
243 if (res) {
Václav Kubernátce108db2021-01-25 10:05:40 +0100244 // After a path fragment, there can only be a slash or a "pathEnd". If this is not the case
245 // then that means there are other unparsed characters after the fragment. In that case the parsing
246 // needs to fail.
247 res = (pathEnd | &char_('/')).parse(begin, end, ctx, rctx, x3::unused);
248 if (!res) {
249 begin = saveIter;
250 } else {
251 parserContext.pushPathFragment(attr);
252 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200253 }
254
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200255 return res;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200256 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200257 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200258};
259
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200260template <CompletionMode COMPLETION_MODE> using schemaNode = NodeParser<NodeParserMode::SchemaNode, COMPLETION_MODE>;
261template <CompletionMode COMPLETION_MODE> using dataNode = NodeParser<NodeParserMode::CompleteDataNode, COMPLETION_MODE>;
262template <CompletionMode COMPLETION_MODE> using incompleteDataNode = NodeParser<NodeParserMode::IncompleteDataNode, COMPLETION_MODE>;
263template <CompletionMode COMPLETION_MODE> using pathCompletions = NodeParser<NodeParserMode::CompletionsOnly, COMPLETION_MODE>;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200264
Václav Kubernát4618fa42020-05-07 01:33:19 +0200265using AnyPath = boost::variant<schemaPath_, dataPath_>;
266
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200267enum class PathParserMode {
268 AnyPath,
269 DataPath,
270 DataPathListEnd
271};
272
273template <>
274struct ModeToAttribute<PathParserMode::AnyPath> {
275 using type = AnyPath;
276};
277
278template <>
279struct ModeToAttribute<PathParserMode::DataPath> {
280 using type = dataPath_;
281};
282
283template <>
284struct ModeToAttribute<PathParserMode::DataPathListEnd> {
285 using type = dataPath_;
286};
287
Václav Kubernát52b90222022-04-27 11:29:54 +0200288auto const trailingSlash = x3::rule<trailingSlash_class, x3::unused_type>{"trailingSlash"} =
289 x3::omit['/'];
290
291// A "nothing" parser, which is used to indicate we tried to parse a path
292auto const initializePath = x3::rule<initializePath_class, x3::unused_type>{"initializePath"} =
293 x3::eps;
294
295auto const absoluteStart = x3::rule<absoluteStart_class, Scope>{"absoluteStart"} =
296 x3::omit['/'] >> x3::attr(Scope::Absolute);
297
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200298template <PathParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
299struct PathParser : x3::parser<PathParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát28b44a82022-04-05 09:54:43 +0200300 using attribute_type = typename ModeToAttribute<PARSER_MODE>::type;
Václav Kubernátabf52802020-05-19 01:31:17 +0200301 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
302
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100303 PathParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction = [](const auto&, const auto&) { return true; })
Václav Kubernátabf52802020-05-19 01:31:17 +0200304 : m_filterFunction(filterFunction)
305 {
306 }
307
Václav Kubernát4618fa42020-05-07 01:33:19 +0200308 template <typename It, typename Ctx, typename RCtx, typename Attr>
309 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
310 {
Václav Kubernát74d35a42020-05-15 15:29:16 +0200311 initializePath.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200312 dataPath_ attrData;
313
314 // absoluteStart has to be separate from the dataPath parser,
315 // otherwise, if the "dataNode % '/'" parser fails, the begin iterator
316 // gets reverted to before the starting slash.
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200317 auto res = (-absoluteStart).parse(begin, end, ctx, rctx, attrData.m_scope);
318 auto dataPath = x3::attr(attrData.m_scope)
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200319 >> (dataNode<COMPLETION_MODE>{m_filterFunction} % '/' | pathEnd >> x3::attr(std::vector<dataNode_>{}))
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200320 >> -trailingSlash;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200321 res = dataPath.parse(begin, end, ctx, rctx, attrData);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200322
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200323 // If we allow data paths with a list at the end, we just try to parse that separately.
324 if constexpr (PARSER_MODE == PathParserMode::DataPathListEnd || PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát4618fa42020-05-07 01:33:19 +0200325 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200326 dataNode_ attrNodeList;
Václav Kubernát7eb47152020-11-12 16:57:03 +0100327 auto hasListEnd = incompleteDataNode<COMPLETION_MODE>{m_filterFunction}.parse(begin, end, ctx, rctx, attrNodeList);
328 if (hasListEnd) {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200329 attrData.m_nodes.emplace_back(attrNodeList);
Václav Kubernát85ba7382020-11-18 18:11:18 +0100330 // If the trailing slash matches, no more nodes are parsed. That means no more completion. So, I
331 // generate them manually, but only if we're in AnyPath mode, so, for example, inside an `ls`
332 // command. If we're in DataPathListEnd it doesn't make sense to parse put any more nodes after the
333 // final list.
334 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát39f83f52021-02-19 02:52:08 +0100335 res = (-(trailingSlash >> x3::omit[pathCompletions<COMPLETION_MODE>{m_filterFunction}])).parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát85ba7382020-11-18 18:11:18 +0100336 } else {
Václav Kubernát39f83f52021-02-19 02:52:08 +0100337 res = (-trailingSlash).parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát85ba7382020-11-18 18:11:18 +0100338 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200339 }
340 }
341 }
342
343 attr = attrData;
344 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
345 // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path
346 auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200347 [] (const auto& node) { return std::holds_alternative<listElement_>(node.m_suffix); });
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200348 // If parsing failed, or if there's more input we try parsing schema nodes.
349 if (!hasLists) {
350 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
351 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
352 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200353 auto schemaPath = schemaNode<COMPLETION_MODE>{m_filterFunction} % '/';
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200354 // The schemaPath parser continues where the dataPath parser ended.
355 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
356 auto trailing = -trailingSlash >> pathEnd;
Václav Kubernát39f83f52021-02-19 02:52:08 +0100357 res = trailing.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200358 attr = attrSchema;
359 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200360 }
361 }
362 return res;
363 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200364};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200365
366// Need to use these wrappers so that my PathParser class gets the proper
367// attribute. Otherwise, Spirit injects the attribute of the outer parser that
368// uses my PathParser.
369// Example grammar: anyPath | module.
370// The PathParser class would get a boost::variant as the attribute, but I
371// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200372// attribute I want (and let Spirit deal with boost::variant).
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200373auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath, CompletionMode::Schema>{};
374auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath, CompletionMode::Data>{};
375auto const dataPathListEnd = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200376
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200377#if __clang__
378#pragma GCC diagnostic push
379#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
380#endif
381
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100382auto const filterConfigFalse = [](const Schema& schema, const std::string& path) {
Václav Kubernátabf52802020-05-19 01:31:17 +0200383 return schema.isConfig(path);
384};
385
Václav Kubernát3ffd24a2020-10-29 08:25:56 +0100386// A WritableOps value is injected through the `x3::with` with this tag (see usage of the tag). It controls whether
387// `config: false` data can be set with the `set` command. This is used by yang-cli because that tool needs modeling of
388// the full datastore, including the "read-only" data.
Václav Kubernát28cf3362020-06-29 17:52:51 +0200389struct writableOps_tag;
390
391PathParser<PathParserMode::DataPath, CompletionMode::Data> const dataPathFilterConfigFalse{filterConfigFalse};
392
393struct WritableLeafPath : x3::parser<WritableLeafPath> {
394 using attribute_type = dataPath_;
395 template <typename It, typename Ctx, typename RCtx, typename Attr>
396 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
397 {
398 bool res;
399 if (x3::get<writableOps_tag>(ctx) == WritableOps::Yes) {
400 res = dataPath.parse(begin, end, ctx, rctx, attr);
401 } else {
402 res = dataPathFilterConfigFalse.parse(begin, end, ctx, rctx, attr);
403 }
404 if (!res) {
405 return false;
406 }
407
408 if (attr.m_nodes.empty() || !std::holds_alternative<leaf_>(attr.m_nodes.back().m_suffix)) {
409 auto& parserContext = x3::get<parser_context_tag>(ctx);
410 parserContext.m_errorMsg = "This is not a path to leaf.";
411 return false;
412 }
413
414 return true;
415 }
416
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100417} const writableLeafPath;
Václav Kubernát28cf3362020-06-29 17:52:51 +0200418
Václav Kubernátd8408e02020-12-02 05:13:27 +0100419enum class AllowInput {
420 Yes,
421 No
422};
423
424template <AllowInput ALLOW_INPUT>
425struct RpcActionPath : x3::parser<RpcActionPath<ALLOW_INPUT>> {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200426 using attribute_type = dataPath_;
427 template <typename It, typename Ctx, typename RCtx, typename Attr>
428 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
429 {
Václav Kubernátd8408e02020-12-02 05:13:27 +0100430 auto grammar = PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
431 if constexpr (ALLOW_INPUT == AllowInput::No) {
432 auto nodeType = schema.nodeType(path);
433 if (nodeType == yang::NodeTypes::Rpc || nodeType == yang::NodeTypes::Action) {
434 return !schema.hasInputNodes(path);
435 }
436 }
437
438 return true;
439 }};
440 bool res = grammar.parse(begin, end, ctx, rctx, attr);
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200441 if (!res) {
442 return false;
443 }
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200444
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200445 if (attr.m_nodes.empty()
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100446 || (!std::holds_alternative<rpcNode_>(attr.m_nodes.back().m_suffix) && !std::holds_alternative<actionNode_>(attr.m_nodes.back().m_suffix))) {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200447 auto& parserContext = x3::get<parser_context_tag>(ctx);
448 parserContext.m_errorMsg = "This is not a path to an RPC/action.";
449 return false;
450 }
451
452 return true;
453 }
Václav Kubernáte7248b22020-06-26 15:38:59 +0200454};
455
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100456auto const noRpcOrAction = [](const Schema& schema, const std::string& path) {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200457 auto nodeType = schema.nodeType(path);
458 return nodeType != yang::NodeTypes::Rpc && nodeType != yang::NodeTypes::Action;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200459};
460
Václav Kubernát52b90222022-04-27 11:29:54 +0200461auto const getPath = x3::rule<getPath_class, decltype(get_::m_path)::value_type>{"getPath"} =
Václav Kubernát31029682020-10-29 08:23:04 +0100462 PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{noRpcOrAction} |
Václav Kubernát31029682020-10-29 08:23:04 +0100463 (module >> "*");
464
Václav Kubernát52b90222022-04-27 11:29:54 +0200465auto const cdPath = x3::rule<cdPath_class, dataPath_>{"cdPath"} =
Václav Kubernát0d47e5e2020-11-17 12:27:05 +0100466 PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
467 return noRpcOrAction(schema, path) && schema.nodeType(path) != yang::NodeTypes::Leaf;
468 }};
Václav Kubernáte7248b22020-06-26 15:38:59 +0200469
Václav Kubernát52b90222022-04-27 11:29:54 +0200470auto const presenceContainerPath = x3::rule<presenceContainerPath_class, dataPath_>{"presenceContainerPath"} =
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200471 dataPath;
472
Václav Kubernát52b90222022-04-27 11:29:54 +0200473auto const listInstancePath = x3::rule<listInstancePath_class, dataPath_>{"listInstancePath"} =
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200474 dataPath;
475
Václav Kubernát52b90222022-04-27 11:29:54 +0200476auto const leafListElementPath = x3::rule<leafListElementPath_class, dataPath_>{"leafListElementPath"} =
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200477 dataPath;
478
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200479
480
481#if __clang__
482#pragma GCC diagnostic pop
483#endif