blob: 5a4cf9eac87254b41e7ebd48b4fe5856d76b011f [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át60b48462022-04-27 12:23:15 +020017auto pathEnd = x3::rule<struct 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
Václav Kubernát92f5ec52022-04-27 12:01:56 +020075auto const suggestLeafListEnd = staticSuggestions({"]"});
Václav Kubernát52b90222022-04-27 11:29:54 +020076
Václav Kubernát60b48462022-04-27 12:23:15 +020077auto const leafListValue = x3::rule<struct leafListValue_class, leaf_data_>{"leafListValue"} =
Václav Kubernát52b90222022-04-27 11:29:54 +020078 '[' >> leaf_data >> suggestLeafListEnd >> ']';
79
Václav Kubernát743d9eb2020-05-18 13:42:36 +020080template <NodeParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
81struct NodeParser : x3::parser<NodeParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020082 using attribute_type = typename ModeToAttribute<PARSER_MODE>::type;
Václav Kubernátabf52802020-05-19 01:31:17 +020083
84 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
85
86 NodeParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction)
87 : m_filterFunction(filterFunction)
88 {
89 }
90
Václav Kubernát60a0ed52020-04-28 15:21:33 +020091 template <typename It, typename Ctx, typename RCtx, typename Attr>
Václav Kubernát832bd7e2020-12-03 03:06:37 +010092 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
Václav Kubernát60a0ed52020-04-28 15:21:33 +020093 {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020094 std::string tableName;
Václav Kubernát4a58ce62020-05-14 17:58:10 +020095 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020096 tableName = "schemaNode";
97 } else {
98 tableName = "dataNode";
99 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200100 x3::symbols<attribute_type> table(tableName);
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200101
102 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
103 parserContext.m_suggestions.clear();
104 for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200105 attribute_type out;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200106 std::string parseString;
107 if (child.first) {
108 out.m_prefix = module_{*child.first};
109 parseString = *child.first + ":";
110 }
111 parseString += child.second;
Václav Kubernátabf52802020-05-19 01:31:17 +0200112
113 if (!m_filterFunction(parserContext.m_schema, joinPaths(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always), parseString))) {
114 continue;
115 }
116
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200117 switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) {
118 case yang::NodeTypes::Container:
119 case yang::NodeTypes::PresenceContainer:
120 out.m_suffix = container_{child.second};
121 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
122 break;
123 case yang::NodeTypes::Leaf:
124 out.m_suffix = leaf_{child.second};
125 parserContext.m_suggestions.emplace(Completion{parseString + " "});
126 break;
127 case yang::NodeTypes::List:
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200128 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200129 out.m_suffix = list_{child.second};
130 } else {
131 out.m_suffix = listElement_{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át60a0ed52020-04-28 15:21:33 +0200139 break;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200140 case yang::NodeTypes::LeafList:
141 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
142 out.m_suffix = leafList_{child.second};
143 } else {
144 out.m_suffix = leafListElement_{child.second, {}};
145 }
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200146
147 if constexpr (COMPLETION_MODE == CompletionMode::Schema) {
148 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
149 } else {
150 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
151 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200152 break;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200153 case yang::NodeTypes::Rpc:
154 out.m_suffix = rpcNode_{child.second};
155 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
156 break;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200157 case yang::NodeTypes::Action:
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200158 out.m_suffix = actionNode_{child.second};
159 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
160 break;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200161 case yang::NodeTypes::AnyXml:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200162 case yang::NodeTypes::Notification:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200163 continue;
164 }
165 table.add(parseString, out);
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200166 if (!child.first) {
167 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
168 out.m_prefix = topLevelModule;
169 table.add(topLevelModule->m_name + ":" + parseString, out);
170 }
171 }
Václav Kubernátaed4bc72020-06-08 01:09:25 +0200172 table.add("..", attribute_type{nodeup_{}});
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200173 parserContext.m_completionIterator = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200174
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200175 if constexpr (PARSER_MODE == NodeParserMode::CompletionsOnly) {
176 return true;
177 } else {
Václav Kubernát832bd7e2020-12-03 03:06:37 +0100178 It saveIter = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200179
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200180 auto res = table.parse(begin, end, ctx, rctx, attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200181
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200182 if (std::holds_alternative<leaf_>(attr.m_suffix)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200183 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
184 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
185 return boost::optional<std::string>{it.m_name};
186 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200187 std::get<leaf_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200188 parserContext.m_tmpListKeyLeafPath.m_node = node;
189 }
190
191 if constexpr (std::is_same<attribute_type, dataNode_>()) {
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200192 if (std::holds_alternative<listElement_>(attr.m_suffix)) {
Václav Kubernát2db124c2020-05-28 21:58:36 +0200193 parserContext.m_tmpListPath = parserContext.currentDataPath();
194 auto tmpList = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200195 parserContext.m_tmpListPath.m_nodes.emplace_back(attr.m_prefix, tmpList);
Václav Kubernát2db124c2020-05-28 21:58:36 +0200196
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200197 res = listSuffix.parse(begin, end, ctx, rctx, std::get<listElement_>(attr.m_suffix).m_keys);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200198
199 // FIXME: think of a better way to do this, that is, get rid of manual iterator reverting
200 if (!res) {
201 // If listSuffix didn't succeed, we check, if we allow incomplete nodes. If we do, then we replace listElement_ with list_.
202 // If we don't, we fail the whole symbol table.
203 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
204 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200205 attr.m_suffix = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200206 } else {
207 begin = saveIter;
208 }
209 }
210 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200211
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200212 if (std::holds_alternative<leafListElement_>(attr.m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200213 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
214 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
215 return boost::optional<std::string>{it.m_name};
216 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200217 std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200218 parserContext.m_tmpListKeyLeafPath.m_node = node;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200219 res = leafListValue.parse(begin, end, ctx, rctx, std::get<leafListElement_>(attr.m_suffix).m_value);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200220
221 if (!res) {
222 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
223 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200224 attr.m_suffix = leafList_{std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200225 } else {
226 begin = saveIter;
227 }
228 }
229 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200230 }
231
232 if (res) {
Václav Kubernátce108db2021-01-25 10:05:40 +0100233 // After a path fragment, there can only be a slash or a "pathEnd". If this is not the case
234 // then that means there are other unparsed characters after the fragment. In that case the parsing
235 // needs to fail.
236 res = (pathEnd | &char_('/')).parse(begin, end, ctx, rctx, x3::unused);
237 if (!res) {
238 begin = saveIter;
239 } else {
240 parserContext.pushPathFragment(attr);
241 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200242 }
243
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200244 return res;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200245 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200246 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200247};
248
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200249template <CompletionMode COMPLETION_MODE> using schemaNode = NodeParser<NodeParserMode::SchemaNode, COMPLETION_MODE>;
250template <CompletionMode COMPLETION_MODE> using dataNode = NodeParser<NodeParserMode::CompleteDataNode, COMPLETION_MODE>;
251template <CompletionMode COMPLETION_MODE> using incompleteDataNode = NodeParser<NodeParserMode::IncompleteDataNode, COMPLETION_MODE>;
252template <CompletionMode COMPLETION_MODE> using pathCompletions = NodeParser<NodeParserMode::CompletionsOnly, COMPLETION_MODE>;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200253
Václav Kubernát4618fa42020-05-07 01:33:19 +0200254using AnyPath = boost::variant<schemaPath_, dataPath_>;
255
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200256enum class PathParserMode {
257 AnyPath,
258 DataPath,
Jan Kundrátbb7aa852023-08-30 11:51:43 +0200259 DataPathListEnd,
260 DataPathAbsolute
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200261};
262
263template <>
264struct ModeToAttribute<PathParserMode::AnyPath> {
265 using type = AnyPath;
266};
267
268template <>
269struct ModeToAttribute<PathParserMode::DataPath> {
270 using type = dataPath_;
271};
272
273template <>
274struct ModeToAttribute<PathParserMode::DataPathListEnd> {
275 using type = dataPath_;
276};
277
Jan Kundrátbb7aa852023-08-30 11:51:43 +0200278template <>
279struct ModeToAttribute<PathParserMode::DataPathAbsolute> {
280 using type = dataPath_;
281};
282
Václav Kubernátf20f6f92022-04-27 12:13:13 +0200283auto const trailingSlash = x3::rule<struct trailingSlash_class, x3::unused_type>{"trailingSlash"} =
Václav Kubernát52b90222022-04-27 11:29:54 +0200284 x3::omit['/'];
285
286// A "nothing" parser, which is used to indicate we tried to parse a path
287auto const initializePath = x3::rule<initializePath_class, x3::unused_type>{"initializePath"} =
288 x3::eps;
289
290auto const absoluteStart = x3::rule<absoluteStart_class, Scope>{"absoluteStart"} =
291 x3::omit['/'] >> x3::attr(Scope::Absolute);
292
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200293template <PathParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
294struct PathParser : x3::parser<PathParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát28b44a82022-04-05 09:54:43 +0200295 using attribute_type = typename ModeToAttribute<PARSER_MODE>::type;
Václav Kubernátabf52802020-05-19 01:31:17 +0200296 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
297
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100298 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 +0200299 : m_filterFunction(filterFunction)
300 {
301 }
302
Václav Kubernát4618fa42020-05-07 01:33:19 +0200303 template <typename It, typename Ctx, typename RCtx, typename Attr>
304 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
305 {
Václav Kubernát74d35a42020-05-15 15:29:16 +0200306 initializePath.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200307 dataPath_ attrData;
308
Jan Kundrátbb7aa852023-08-30 11:51:43 +0200309 auto res = [](){
310 if constexpr (PARSER_MODE == PathParserMode::DataPathAbsolute) {
311 return absoluteStart;
312 } else {
313 return -absoluteStart;
314 }
315 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200316 // absoluteStart has to be separate from the dataPath parser,
317 // otherwise, if the "dataNode % '/'" parser fails, the begin iterator
318 // gets reverted to before the starting slash.
Jan Kundrátbb7aa852023-08-30 11:51:43 +0200319 ().parse(begin, end, ctx, rctx, attrData.m_scope);
320 if (!res) {
321 x3::get<parser_context_tag>(ctx).m_suggestions.emplace(Completion{"/"});
322 return res;
323 }
324
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200325 auto dataPath = x3::attr(attrData.m_scope)
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200326 >> (dataNode<COMPLETION_MODE>{m_filterFunction} % '/' | pathEnd >> x3::attr(std::vector<dataNode_>{}))
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200327 >> -trailingSlash;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200328 res = dataPath.parse(begin, end, ctx, rctx, attrData);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200329
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200330 // If we allow data paths with a list at the end, we just try to parse that separately.
331 if constexpr (PARSER_MODE == PathParserMode::DataPathListEnd || PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát4618fa42020-05-07 01:33:19 +0200332 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200333 dataNode_ attrNodeList;
Václav Kubernát7eb47152020-11-12 16:57:03 +0100334 auto hasListEnd = incompleteDataNode<COMPLETION_MODE>{m_filterFunction}.parse(begin, end, ctx, rctx, attrNodeList);
335 if (hasListEnd) {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200336 attrData.m_nodes.emplace_back(attrNodeList);
Václav Kubernát85ba7382020-11-18 18:11:18 +0100337 // If the trailing slash matches, no more nodes are parsed. That means no more completion. So, I
338 // generate them manually, but only if we're in AnyPath mode, so, for example, inside an `ls`
339 // command. If we're in DataPathListEnd it doesn't make sense to parse put any more nodes after the
340 // final list.
341 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát39f83f52021-02-19 02:52:08 +0100342 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 +0100343 } else {
Václav Kubernát39f83f52021-02-19 02:52:08 +0100344 res = (-trailingSlash).parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát85ba7382020-11-18 18:11:18 +0100345 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200346 }
347 }
348 }
349
350 attr = attrData;
351 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
352 // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path
353 auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200354 [] (const auto& node) { return std::holds_alternative<listElement_>(node.m_suffix); });
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200355 // If parsing failed, or if there's more input we try parsing schema nodes.
356 if (!hasLists) {
357 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
358 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
359 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200360 auto schemaPath = schemaNode<COMPLETION_MODE>{m_filterFunction} % '/';
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200361 // The schemaPath parser continues where the dataPath parser ended.
362 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
363 auto trailing = -trailingSlash >> pathEnd;
Václav Kubernát39f83f52021-02-19 02:52:08 +0100364 res = trailing.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200365 attr = attrSchema;
366 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200367 }
368 }
369 return res;
370 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200371};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200372
373// Need to use these wrappers so that my PathParser class gets the proper
374// attribute. Otherwise, Spirit injects the attribute of the outer parser that
375// uses my PathParser.
376// Example grammar: anyPath | module.
377// The PathParser class would get a boost::variant as the attribute, but I
378// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200379// attribute I want (and let Spirit deal with boost::variant).
Václav Kubernát60b48462022-04-27 12:23:15 +0200380auto const anyPath = x3::rule<struct anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath, CompletionMode::Schema>{};
381auto const dataPath = x3::rule<struct dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath, CompletionMode::Data>{};
382auto const dataPathListEnd = x3::rule<struct dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{};
Jan Kundrátbb7aa852023-08-30 11:51:43 +0200383auto const dataPathAbsolute = x3::rule<struct dataPath_class, dataPath_>{"dataPathAbsolute"} = PathParser<PathParserMode::DataPathAbsolute, CompletionMode::Data>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200384
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200385#if __clang__
386#pragma GCC diagnostic push
387#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
388#endif
389
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100390auto const filterConfigFalse = [](const Schema& schema, const std::string& path) {
Václav Kubernátabf52802020-05-19 01:31:17 +0200391 return schema.isConfig(path);
392};
393
Václav Kubernát3ffd24a2020-10-29 08:25:56 +0100394// A WritableOps value is injected through the `x3::with` with this tag (see usage of the tag). It controls whether
395// `config: false` data can be set with the `set` command. This is used by yang-cli because that tool needs modeling of
396// the full datastore, including the "read-only" data.
Václav Kubernát28cf3362020-06-29 17:52:51 +0200397struct writableOps_tag;
398
399PathParser<PathParserMode::DataPath, CompletionMode::Data> const dataPathFilterConfigFalse{filterConfigFalse};
400
401struct WritableLeafPath : x3::parser<WritableLeafPath> {
402 using attribute_type = dataPath_;
403 template <typename It, typename Ctx, typename RCtx, typename Attr>
404 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
405 {
406 bool res;
407 if (x3::get<writableOps_tag>(ctx) == WritableOps::Yes) {
408 res = dataPath.parse(begin, end, ctx, rctx, attr);
409 } else {
410 res = dataPathFilterConfigFalse.parse(begin, end, ctx, rctx, attr);
411 }
412 if (!res) {
413 return false;
414 }
415
416 if (attr.m_nodes.empty() || !std::holds_alternative<leaf_>(attr.m_nodes.back().m_suffix)) {
417 auto& parserContext = x3::get<parser_context_tag>(ctx);
418 parserContext.m_errorMsg = "This is not a path to leaf.";
419 return false;
420 }
421
422 return true;
423 }
424
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100425} const writableLeafPath;
Václav Kubernát28cf3362020-06-29 17:52:51 +0200426
Václav Kubernátd8408e02020-12-02 05:13:27 +0100427enum class AllowInput {
428 Yes,
429 No
430};
431
432template <AllowInput ALLOW_INPUT>
433struct RpcActionPath : x3::parser<RpcActionPath<ALLOW_INPUT>> {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200434 using attribute_type = dataPath_;
435 template <typename It, typename Ctx, typename RCtx, typename Attr>
436 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
437 {
Václav Kubernátd8408e02020-12-02 05:13:27 +0100438 auto grammar = PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
439 if constexpr (ALLOW_INPUT == AllowInput::No) {
440 auto nodeType = schema.nodeType(path);
441 if (nodeType == yang::NodeTypes::Rpc || nodeType == yang::NodeTypes::Action) {
442 return !schema.hasInputNodes(path);
443 }
444 }
445
446 return true;
447 }};
448 bool res = grammar.parse(begin, end, ctx, rctx, attr);
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200449 if (!res) {
450 return false;
451 }
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200452
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200453 if (attr.m_nodes.empty()
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100454 || (!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 +0200455 auto& parserContext = x3::get<parser_context_tag>(ctx);
456 parserContext.m_errorMsg = "This is not a path to an RPC/action.";
457 return false;
458 }
459
460 return true;
461 }
Václav Kubernáte7248b22020-06-26 15:38:59 +0200462};
463
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100464auto const noRpcOrAction = [](const Schema& schema, const std::string& path) {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200465 auto nodeType = schema.nodeType(path);
466 return nodeType != yang::NodeTypes::Rpc && nodeType != yang::NodeTypes::Action;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200467};
468
Václav Kubernátf20f6f92022-04-27 12:13:13 +0200469auto const getPath = x3::rule<struct getPath_class, decltype(get_::m_path)::value_type>{"getPath"} =
Václav Kubernát31029682020-10-29 08:23:04 +0100470 PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{noRpcOrAction} |
Václav Kubernát31029682020-10-29 08:23:04 +0100471 (module >> "*");
472
Václav Kubernátf20f6f92022-04-27 12:13:13 +0200473auto const cdPath = x3::rule<struct cdPath_class, dataPath_>{"cdPath"} =
Václav Kubernát0d47e5e2020-11-17 12:27:05 +0100474 PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
475 return noRpcOrAction(schema, path) && schema.nodeType(path) != yang::NodeTypes::Leaf;
476 }};
Václav Kubernáte7248b22020-06-26 15:38:59 +0200477
Václav Kubernát52b90222022-04-27 11:29:54 +0200478auto const presenceContainerPath = x3::rule<presenceContainerPath_class, dataPath_>{"presenceContainerPath"} =
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200479 dataPath;
480
Václav Kubernát52b90222022-04-27 11:29:54 +0200481auto const listInstancePath = x3::rule<listInstancePath_class, dataPath_>{"listInstancePath"} =
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200482 dataPath;
483
Václav Kubernát52b90222022-04-27 11:29:54 +0200484auto const leafListElementPath = x3::rule<leafListElementPath_class, dataPath_>{"leafListElementPath"} =
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200485 dataPath;
486
Jan Kundrátbb7aa852023-08-30 11:51:43 +0200487template <typename It, typename Ctx, typename RCtx, typename Attr>
488bool leaf_data_parse_data_path(It& first, It last, Ctx const& ctx, RCtx& rctx, Attr& attr)
489{
490 dataPath_ path;
491 auto res = dataPathAbsolute.parse(first, last, ctx, rctx, path);
492 if (res) {
493 // FIXME: list key escaping in XPath is different from netconf-cli.
494 // If the key(s) are not all strings, this will produce netconf-cli-style
495 // string like /foo:xxx[k1=blah][k2=666] instead of standard XPaths like
496 // /foo:xxx[k1='blah'][k2='666'], and these will cause troubles when passed
497 // to libyang later on.
498 attr = instanceIdentifier_{pathToDataString(path, Prefixes::WhenNeeded)};
499 }
500 return res;
501}
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200502
503#if __clang__
504#pragma GCC diagnostic pop
505#endif