blob: 67345c7bed23aa0a9c0fcdddd4b0930f6d1481fc [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,
259 DataPathListEnd
260};
261
262template <>
263struct ModeToAttribute<PathParserMode::AnyPath> {
264 using type = AnyPath;
265};
266
267template <>
268struct ModeToAttribute<PathParserMode::DataPath> {
269 using type = dataPath_;
270};
271
272template <>
273struct ModeToAttribute<PathParserMode::DataPathListEnd> {
274 using type = dataPath_;
275};
276
Václav Kubernátf20f6f92022-04-27 12:13:13 +0200277auto const trailingSlash = x3::rule<struct trailingSlash_class, x3::unused_type>{"trailingSlash"} =
Václav Kubernát52b90222022-04-27 11:29:54 +0200278 x3::omit['/'];
279
280// A "nothing" parser, which is used to indicate we tried to parse a path
281auto const initializePath = x3::rule<initializePath_class, x3::unused_type>{"initializePath"} =
282 x3::eps;
283
284auto const absoluteStart = x3::rule<absoluteStart_class, Scope>{"absoluteStart"} =
285 x3::omit['/'] >> x3::attr(Scope::Absolute);
286
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200287template <PathParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
288struct PathParser : x3::parser<PathParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát28b44a82022-04-05 09:54:43 +0200289 using attribute_type = typename ModeToAttribute<PARSER_MODE>::type;
Václav Kubernátabf52802020-05-19 01:31:17 +0200290 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
291
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100292 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 +0200293 : m_filterFunction(filterFunction)
294 {
295 }
296
Václav Kubernát4618fa42020-05-07 01:33:19 +0200297 template <typename It, typename Ctx, typename RCtx, typename Attr>
298 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
299 {
Václav Kubernát74d35a42020-05-15 15:29:16 +0200300 initializePath.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200301 dataPath_ attrData;
302
303 // absoluteStart has to be separate from the dataPath parser,
304 // otherwise, if the "dataNode % '/'" parser fails, the begin iterator
305 // gets reverted to before the starting slash.
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200306 auto res = (-absoluteStart).parse(begin, end, ctx, rctx, attrData.m_scope);
307 auto dataPath = x3::attr(attrData.m_scope)
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200308 >> (dataNode<COMPLETION_MODE>{m_filterFunction} % '/' | pathEnd >> x3::attr(std::vector<dataNode_>{}))
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200309 >> -trailingSlash;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200310 res = dataPath.parse(begin, end, ctx, rctx, attrData);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200311
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200312 // If we allow data paths with a list at the end, we just try to parse that separately.
313 if constexpr (PARSER_MODE == PathParserMode::DataPathListEnd || PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát4618fa42020-05-07 01:33:19 +0200314 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200315 dataNode_ attrNodeList;
Václav Kubernát7eb47152020-11-12 16:57:03 +0100316 auto hasListEnd = incompleteDataNode<COMPLETION_MODE>{m_filterFunction}.parse(begin, end, ctx, rctx, attrNodeList);
317 if (hasListEnd) {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200318 attrData.m_nodes.emplace_back(attrNodeList);
Václav Kubernát85ba7382020-11-18 18:11:18 +0100319 // If the trailing slash matches, no more nodes are parsed. That means no more completion. So, I
320 // generate them manually, but only if we're in AnyPath mode, so, for example, inside an `ls`
321 // command. If we're in DataPathListEnd it doesn't make sense to parse put any more nodes after the
322 // final list.
323 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát39f83f52021-02-19 02:52:08 +0100324 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 +0100325 } else {
Václav Kubernát39f83f52021-02-19 02:52:08 +0100326 res = (-trailingSlash).parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát85ba7382020-11-18 18:11:18 +0100327 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200328 }
329 }
330 }
331
332 attr = attrData;
333 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
334 // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path
335 auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200336 [] (const auto& node) { return std::holds_alternative<listElement_>(node.m_suffix); });
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200337 // If parsing failed, or if there's more input we try parsing schema nodes.
338 if (!hasLists) {
339 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
340 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
341 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200342 auto schemaPath = schemaNode<COMPLETION_MODE>{m_filterFunction} % '/';
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200343 // The schemaPath parser continues where the dataPath parser ended.
344 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
345 auto trailing = -trailingSlash >> pathEnd;
Václav Kubernát39f83f52021-02-19 02:52:08 +0100346 res = trailing.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200347 attr = attrSchema;
348 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200349 }
350 }
351 return res;
352 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200353};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200354
355// Need to use these wrappers so that my PathParser class gets the proper
356// attribute. Otherwise, Spirit injects the attribute of the outer parser that
357// uses my PathParser.
358// Example grammar: anyPath | module.
359// The PathParser class would get a boost::variant as the attribute, but I
360// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200361// attribute I want (and let Spirit deal with boost::variant).
Václav Kubernát60b48462022-04-27 12:23:15 +0200362auto const anyPath = x3::rule<struct anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath, CompletionMode::Schema>{};
363auto const dataPath = x3::rule<struct dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath, CompletionMode::Data>{};
364auto const dataPathListEnd = x3::rule<struct dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200365
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200366#if __clang__
367#pragma GCC diagnostic push
368#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
369#endif
370
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100371auto const filterConfigFalse = [](const Schema& schema, const std::string& path) {
Václav Kubernátabf52802020-05-19 01:31:17 +0200372 return schema.isConfig(path);
373};
374
Václav Kubernát3ffd24a2020-10-29 08:25:56 +0100375// A WritableOps value is injected through the `x3::with` with this tag (see usage of the tag). It controls whether
376// `config: false` data can be set with the `set` command. This is used by yang-cli because that tool needs modeling of
377// the full datastore, including the "read-only" data.
Václav Kubernát28cf3362020-06-29 17:52:51 +0200378struct writableOps_tag;
379
380PathParser<PathParserMode::DataPath, CompletionMode::Data> const dataPathFilterConfigFalse{filterConfigFalse};
381
382struct WritableLeafPath : x3::parser<WritableLeafPath> {
383 using attribute_type = dataPath_;
384 template <typename It, typename Ctx, typename RCtx, typename Attr>
385 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
386 {
387 bool res;
388 if (x3::get<writableOps_tag>(ctx) == WritableOps::Yes) {
389 res = dataPath.parse(begin, end, ctx, rctx, attr);
390 } else {
391 res = dataPathFilterConfigFalse.parse(begin, end, ctx, rctx, attr);
392 }
393 if (!res) {
394 return false;
395 }
396
397 if (attr.m_nodes.empty() || !std::holds_alternative<leaf_>(attr.m_nodes.back().m_suffix)) {
398 auto& parserContext = x3::get<parser_context_tag>(ctx);
399 parserContext.m_errorMsg = "This is not a path to leaf.";
400 return false;
401 }
402
403 return true;
404 }
405
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100406} const writableLeafPath;
Václav Kubernát28cf3362020-06-29 17:52:51 +0200407
Václav Kubernátd8408e02020-12-02 05:13:27 +0100408enum class AllowInput {
409 Yes,
410 No
411};
412
413template <AllowInput ALLOW_INPUT>
414struct RpcActionPath : x3::parser<RpcActionPath<ALLOW_INPUT>> {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200415 using attribute_type = dataPath_;
416 template <typename It, typename Ctx, typename RCtx, typename Attr>
417 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
418 {
Václav Kubernátd8408e02020-12-02 05:13:27 +0100419 auto grammar = PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
420 if constexpr (ALLOW_INPUT == AllowInput::No) {
421 auto nodeType = schema.nodeType(path);
422 if (nodeType == yang::NodeTypes::Rpc || nodeType == yang::NodeTypes::Action) {
423 return !schema.hasInputNodes(path);
424 }
425 }
426
427 return true;
428 }};
429 bool res = grammar.parse(begin, end, ctx, rctx, attr);
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200430 if (!res) {
431 return false;
432 }
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200433
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200434 if (attr.m_nodes.empty()
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100435 || (!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 +0200436 auto& parserContext = x3::get<parser_context_tag>(ctx);
437 parserContext.m_errorMsg = "This is not a path to an RPC/action.";
438 return false;
439 }
440
441 return true;
442 }
Václav Kubernáte7248b22020-06-26 15:38:59 +0200443};
444
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100445auto const noRpcOrAction = [](const Schema& schema, const std::string& path) {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200446 auto nodeType = schema.nodeType(path);
447 return nodeType != yang::NodeTypes::Rpc && nodeType != yang::NodeTypes::Action;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200448};
449
Václav Kubernátf20f6f92022-04-27 12:13:13 +0200450auto const getPath = x3::rule<struct getPath_class, decltype(get_::m_path)::value_type>{"getPath"} =
Václav Kubernát31029682020-10-29 08:23:04 +0100451 PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{noRpcOrAction} |
Václav Kubernát31029682020-10-29 08:23:04 +0100452 (module >> "*");
453
Václav Kubernátf20f6f92022-04-27 12:13:13 +0200454auto const cdPath = x3::rule<struct cdPath_class, dataPath_>{"cdPath"} =
Václav Kubernát0d47e5e2020-11-17 12:27:05 +0100455 PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
456 return noRpcOrAction(schema, path) && schema.nodeType(path) != yang::NodeTypes::Leaf;
457 }};
Václav Kubernáte7248b22020-06-26 15:38:59 +0200458
Václav Kubernát52b90222022-04-27 11:29:54 +0200459auto const presenceContainerPath = x3::rule<presenceContainerPath_class, dataPath_>{"presenceContainerPath"} =
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200460 dataPath;
461
Václav Kubernát52b90222022-04-27 11:29:54 +0200462auto const listInstancePath = x3::rule<listInstancePath_class, dataPath_>{"listInstancePath"} =
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200463 dataPath;
464
Václav Kubernát52b90222022-04-27 11:29:54 +0200465auto const leafListElementPath = x3::rule<leafListElementPath_class, dataPath_>{"leafListElementPath"} =
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200466 dataPath;
467
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200468
469
470#if __clang__
471#pragma GCC diagnostic pop
472#endif