blob: 29dbd04aadbcd0336827d358e2236fc90fb710ac [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";
Václav Kubernát31029682020-10-29 08:23:04 +010018x3::rule<getPath_class, decltype(get_::m_path)> const getPath = "getPath";
Václav Kubernáte7248b22020-06-26 15:38:59 +020019x3::rule<rpcPath_class, dataPath_> const rpcPath = "rpcPath";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020020x3::rule<presenceContainerPath_class, dataPath_> const presenceContainerPath = "presenceContainerPath";
21x3::rule<listInstancePath_class, dataPath_> const listInstancePath = "listInstancePath";
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020022x3::rule<leafListElementPath_class, dataPath_> const leafListElementPath = "leafListElementPath";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020023x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020024x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020025x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart";
26x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue";
27x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020028x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020029x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions";
30x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions";
31x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd";
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020032x3::rule<class leafListValue_class, leaf_data_> const leafListValue = "leafListValue";
Václav Kubernátce108db2021-01-25 10:05:40 +010033auto pathEnd = x3::rule<class PathEnd>{"pathEnd"} = &space_separator | x3::eoi;
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020034
Václav Kubernát4a58ce62020-05-14 17:58:10 +020035enum class NodeParserMode {
36 CompleteDataNode,
37 IncompleteDataNode,
38 CompletionsOnly,
39 SchemaNode
40};
41
42template <auto>
43struct ModeToAttribute;
44template <>
45struct ModeToAttribute<NodeParserMode::CompleteDataNode> {
46 using type = dataNode_;
47};
48template <>
49struct ModeToAttribute<NodeParserMode::IncompleteDataNode> {
50 using type = dataNode_;
51};
52template <>
53struct ModeToAttribute<NodeParserMode::SchemaNode> {
54 using type = schemaNode_;
55};
56// The CompletionsOnly attribute is dataNode_ only because of convenience:
57// having the same return type means we can get by without a ton of `if constexpr` stanzas.
58// So the code will still "parse data into the target attr" for simplicity.
59template <>
60struct ModeToAttribute<NodeParserMode::CompletionsOnly> {
61 using type = dataNode_;
62};
63
Václav Kubernát743d9eb2020-05-18 13:42:36 +020064enum class CompletionMode {
65 Schema,
66 Data
67};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020068
Václav Kubernát743d9eb2020-05-18 13:42:36 +020069template <NodeParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
70struct NodeParser : x3::parser<NodeParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020071 using attribute_type = typename ModeToAttribute<PARSER_MODE>::type;
Václav Kubernátabf52802020-05-19 01:31:17 +020072
73 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
74
75 NodeParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction)
76 : m_filterFunction(filterFunction)
77 {
78 }
79
Václav Kubernát60a0ed52020-04-28 15:21:33 +020080 template <typename It, typename Ctx, typename RCtx, typename Attr>
Václav Kubernát832bd7e2020-12-03 03:06:37 +010081 bool parse(It& begin, 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:
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200147 out.m_suffix = actionNode_{child.second};
148 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
149 break;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200150 case yang::NodeTypes::AnyXml:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200151 case yang::NodeTypes::Notification:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200152 continue;
153 }
154 table.add(parseString, out);
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200155 if (!child.first) {
156 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
157 out.m_prefix = topLevelModule;
158 table.add(topLevelModule->m_name + ":" + parseString, out);
159 }
160 }
Václav Kubernátaed4bc72020-06-08 01:09:25 +0200161 table.add("..", attribute_type{nodeup_{}});
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200162 parserContext.m_completionIterator = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200163
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200164 if constexpr (PARSER_MODE == NodeParserMode::CompletionsOnly) {
165 return true;
166 } else {
Václav Kubernát832bd7e2020-12-03 03:06:37 +0100167 It saveIter = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200168
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200169 auto res = table.parse(begin, end, ctx, rctx, attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200170
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200171 if (std::holds_alternative<leaf_>(attr.m_suffix)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200172 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
173 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
174 return boost::optional<std::string>{it.m_name};
175 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200176 std::get<leaf_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200177 parserContext.m_tmpListKeyLeafPath.m_node = node;
178 }
179
180 if constexpr (std::is_same<attribute_type, dataNode_>()) {
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200181 if (std::holds_alternative<listElement_>(attr.m_suffix)) {
Václav Kubernát2db124c2020-05-28 21:58:36 +0200182 parserContext.m_tmpListPath = parserContext.currentDataPath();
183 auto tmpList = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200184 parserContext.m_tmpListPath.m_nodes.emplace_back(attr.m_prefix, tmpList);
Václav Kubernát2db124c2020-05-28 21:58:36 +0200185
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200186 res = listSuffix.parse(begin, end, ctx, rctx, std::get<listElement_>(attr.m_suffix).m_keys);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200187
188 // FIXME: think of a better way to do this, that is, get rid of manual iterator reverting
189 if (!res) {
190 // If listSuffix didn't succeed, we check, if we allow incomplete nodes. If we do, then we replace listElement_ with list_.
191 // If we don't, we fail the whole symbol table.
192 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
193 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200194 attr.m_suffix = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200195 } else {
196 begin = saveIter;
197 }
198 }
199 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200200
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200201 if (std::holds_alternative<leafListElement_>(attr.m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200202 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
203 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
204 return boost::optional<std::string>{it.m_name};
205 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200206 std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200207 parserContext.m_tmpListKeyLeafPath.m_node = node;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200208 res = leafListValue.parse(begin, end, ctx, rctx, std::get<leafListElement_>(attr.m_suffix).m_value);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200209
210 if (!res) {
211 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
212 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200213 attr.m_suffix = leafList_{std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200214 } else {
215 begin = saveIter;
216 }
217 }
218 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200219 }
220
221 if (res) {
Václav Kubernátce108db2021-01-25 10:05:40 +0100222 // After a path fragment, there can only be a slash or a "pathEnd". If this is not the case
223 // then that means there are other unparsed characters after the fragment. In that case the parsing
224 // needs to fail.
225 res = (pathEnd | &char_('/')).parse(begin, end, ctx, rctx, x3::unused);
226 if (!res) {
227 begin = saveIter;
228 } else {
229 parserContext.pushPathFragment(attr);
230 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200231 }
232
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200233 return res;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200234 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200235 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200236};
237
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200238template <CompletionMode COMPLETION_MODE> using schemaNode = NodeParser<NodeParserMode::SchemaNode, COMPLETION_MODE>;
239template <CompletionMode COMPLETION_MODE> using dataNode = NodeParser<NodeParserMode::CompleteDataNode, COMPLETION_MODE>;
240template <CompletionMode COMPLETION_MODE> using incompleteDataNode = NodeParser<NodeParserMode::IncompleteDataNode, COMPLETION_MODE>;
241template <CompletionMode COMPLETION_MODE> using pathCompletions = NodeParser<NodeParserMode::CompletionsOnly, COMPLETION_MODE>;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200242
Václav Kubernát4618fa42020-05-07 01:33:19 +0200243using AnyPath = boost::variant<schemaPath_, dataPath_>;
244
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200245enum class PathParserMode {
246 AnyPath,
247 DataPath,
248 DataPathListEnd
249};
250
251template <>
252struct ModeToAttribute<PathParserMode::AnyPath> {
253 using type = AnyPath;
254};
255
256template <>
257struct ModeToAttribute<PathParserMode::DataPath> {
258 using type = dataPath_;
259};
260
261template <>
262struct ModeToAttribute<PathParserMode::DataPathListEnd> {
263 using type = dataPath_;
264};
265
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200266template <PathParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
267struct PathParser : x3::parser<PathParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200268 using attribute_type = ModeToAttribute<PARSER_MODE>;
Václav Kubernátabf52802020-05-19 01:31:17 +0200269 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
270
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100271 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 +0200272 : m_filterFunction(filterFunction)
273 {
274 }
275
Václav Kubernát4618fa42020-05-07 01:33:19 +0200276 template <typename It, typename Ctx, typename RCtx, typename Attr>
277 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
278 {
Václav Kubernát74d35a42020-05-15 15:29:16 +0200279 initializePath.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200280 dataPath_ attrData;
281
282 // absoluteStart has to be separate from the dataPath parser,
283 // otherwise, if the "dataNode % '/'" parser fails, the begin iterator
284 // gets reverted to before the starting slash.
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200285 auto res = (-absoluteStart).parse(begin, end, ctx, rctx, attrData.m_scope);
286 auto dataPath = x3::attr(attrData.m_scope)
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200287 >> (dataNode<COMPLETION_MODE>{m_filterFunction} % '/' | pathEnd >> x3::attr(std::vector<dataNode_>{}))
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200288 >> -trailingSlash;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200289 res = dataPath.parse(begin, end, ctx, rctx, attrData);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200290
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200291 // If we allow data paths with a list at the end, we just try to parse that separately.
292 if constexpr (PARSER_MODE == PathParserMode::DataPathListEnd || PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát4618fa42020-05-07 01:33:19 +0200293 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200294 dataNode_ attrNodeList;
Václav Kubernát7eb47152020-11-12 16:57:03 +0100295 auto hasListEnd = incompleteDataNode<COMPLETION_MODE>{m_filterFunction}.parse(begin, end, ctx, rctx, attrNodeList);
296 if (hasListEnd) {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200297 attrData.m_nodes.emplace_back(attrNodeList);
Václav Kubernát85ba7382020-11-18 18:11:18 +0100298 // If the trailing slash matches, no more nodes are parsed. That means no more completion. So, I
299 // generate them manually, but only if we're in AnyPath mode, so, for example, inside an `ls`
300 // command. If we're in DataPathListEnd it doesn't make sense to parse put any more nodes after the
301 // final list.
302 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
303 res = (-(trailingSlash >> x3::omit[pathCompletions<COMPLETION_MODE>{m_filterFunction}])).parse(begin, end, ctx, rctx, attrData.m_trailingSlash);
304 } else {
305 res = (-trailingSlash).parse(begin, end, ctx, rctx, attrData.m_trailingSlash);
306 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200307 }
308 }
309 }
310
311 attr = attrData;
312 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
313 // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path
314 auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200315 [] (const auto& node) { return std::holds_alternative<listElement_>(node.m_suffix); });
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200316 // If parsing failed, or if there's more input we try parsing schema nodes.
317 if (!hasLists) {
318 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
319 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
320 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200321 auto schemaPath = schemaNode<COMPLETION_MODE>{m_filterFunction} % '/';
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200322 // The schemaPath parser continues where the dataPath parser ended.
323 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
324 auto trailing = -trailingSlash >> pathEnd;
325 res = trailing.parse(begin, end, ctx, rctx, attrSchema.m_trailingSlash);
326 attr = attrSchema;
327 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200328 }
329 }
330 return res;
331 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200332};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200333
334// Need to use these wrappers so that my PathParser class gets the proper
335// attribute. Otherwise, Spirit injects the attribute of the outer parser that
336// uses my PathParser.
337// Example grammar: anyPath | module.
338// The PathParser class would get a boost::variant as the attribute, but I
339// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200340// attribute I want (and let Spirit deal with boost::variant).
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200341auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath, CompletionMode::Schema>{};
342auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath, CompletionMode::Data>{};
343auto const dataPathListEnd = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200344
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200345#if __clang__
346#pragma GCC diagnostic push
347#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
348#endif
349
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200350struct SuggestLeafListEnd : x3::parser<SuggestLeafListEnd> {
351 using attribute_type = x3::unused_type;
352 template <typename It, typename Ctx, typename RCtx, typename Attr>
353 bool parse(It& begin, It, Ctx const& ctx, RCtx&, Attr&) const
354 {
355 auto& parserContext = x3::get<parser_context_tag>(ctx);
356 parserContext.m_completionIterator = begin;
357 parserContext.m_suggestions = {Completion{"]"}};
358
359 return true;
360 }
361} const suggestLeafListEnd;
362
363auto const leafListValue_def =
364 '[' >> leaf_data >> suggestLeafListEnd >> ']';
365
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200366auto const rest =
367 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
368
369auto const key_identifier_def =
370 x3::lexeme[
371 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
372 ];
373
374auto const createKeySuggestions_def =
375 x3::eps;
376
377auto const createValueSuggestions_def =
378 x3::eps;
379
380auto const suggestKeysEnd_def =
381 x3::eps;
382
383auto const keyValue_def =
384 key_identifier > '=' > createValueSuggestions > leaf_data;
385
386auto const keyValueWrapper =
Václav Kubernát9fa5dca2020-06-01 03:56:41 +0200387 x3::no_skip['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200388
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200389// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
390auto const listSuffix_def =
391 *keyValueWrapper;
392
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200393auto const list_def =
394 node_identifier >> !char_('[');
395
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200396auto const absoluteStart_def =
397 x3::omit['/'] >> x3::attr(Scope::Absolute);
398
399auto const trailingSlash_def =
400 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
401
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100402auto const filterConfigFalse = [](const Schema& schema, const std::string& path) {
Václav Kubernátabf52802020-05-19 01:31:17 +0200403 return schema.isConfig(path);
404};
405
Václav Kubernát3ffd24a2020-10-29 08:25:56 +0100406// A WritableOps value is injected through the `x3::with` with this tag (see usage of the tag). It controls whether
407// `config: false` data can be set with the `set` command. This is used by yang-cli because that tool needs modeling of
408// the full datastore, including the "read-only" data.
Václav Kubernát28cf3362020-06-29 17:52:51 +0200409struct writableOps_tag;
410
411PathParser<PathParserMode::DataPath, CompletionMode::Data> const dataPathFilterConfigFalse{filterConfigFalse};
412
413struct WritableLeafPath : x3::parser<WritableLeafPath> {
414 using attribute_type = dataPath_;
415 template <typename It, typename Ctx, typename RCtx, typename Attr>
416 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
417 {
418 bool res;
419 if (x3::get<writableOps_tag>(ctx) == WritableOps::Yes) {
420 res = dataPath.parse(begin, end, ctx, rctx, attr);
421 } else {
422 res = dataPathFilterConfigFalse.parse(begin, end, ctx, rctx, attr);
423 }
424 if (!res) {
425 return false;
426 }
427
428 if (attr.m_nodes.empty() || !std::holds_alternative<leaf_>(attr.m_nodes.back().m_suffix)) {
429 auto& parserContext = x3::get<parser_context_tag>(ctx);
430 parserContext.m_errorMsg = "This is not a path to leaf.";
431 return false;
432 }
433
434 return true;
435 }
436
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100437} const writableLeafPath;
Václav Kubernát28cf3362020-06-29 17:52:51 +0200438
Václav Kubernátd8408e02020-12-02 05:13:27 +0100439enum class AllowInput {
440 Yes,
441 No
442};
443
444template <AllowInput ALLOW_INPUT>
445struct RpcActionPath : x3::parser<RpcActionPath<ALLOW_INPUT>> {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200446 using attribute_type = dataPath_;
447 template <typename It, typename Ctx, typename RCtx, typename Attr>
448 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
449 {
Václav Kubernátd8408e02020-12-02 05:13:27 +0100450 auto grammar = PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
451 if constexpr (ALLOW_INPUT == AllowInput::No) {
452 auto nodeType = schema.nodeType(path);
453 if (nodeType == yang::NodeTypes::Rpc || nodeType == yang::NodeTypes::Action) {
454 return !schema.hasInputNodes(path);
455 }
456 }
457
458 return true;
459 }};
460 bool res = grammar.parse(begin, end, ctx, rctx, attr);
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200461 if (!res) {
462 return false;
463 }
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200464
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200465 if (attr.m_nodes.empty()
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100466 || (!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 +0200467 auto& parserContext = x3::get<parser_context_tag>(ctx);
468 parserContext.m_errorMsg = "This is not a path to an RPC/action.";
469 return false;
470 }
471
472 return true;
473 }
Václav Kubernáte7248b22020-06-26 15:38:59 +0200474};
475
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100476auto const noRpcOrAction = [](const Schema& schema, const std::string& path) {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200477 auto nodeType = schema.nodeType(path);
478 return nodeType != yang::NodeTypes::Rpc && nodeType != yang::NodeTypes::Action;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200479};
480
Václav Kubernát31029682020-10-29 08:23:04 +0100481auto const getPath_def =
482 PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{noRpcOrAction} |
Václav Kubernát31029682020-10-29 08:23:04 +0100483 (module >> "*");
484
Václav Kubernáte7248b22020-06-26 15:38:59 +0200485auto const cdPath_def =
Václav Kubernát0d47e5e2020-11-17 12:27:05 +0100486 PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
487 return noRpcOrAction(schema, path) && schema.nodeType(path) != yang::NodeTypes::Leaf;
488 }};
Václav Kubernáte7248b22020-06-26 15:38:59 +0200489
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200490auto const presenceContainerPath_def =
491 dataPath;
492
493auto const listInstancePath_def =
494 dataPath;
495
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200496auto const leafListElementPath_def =
497 dataPath;
498
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200499// A "nothing" parser, which is used to indicate we tried to parse a path
500auto const initializePath_def =
501 x3::eps;
502
503
504
505#if __clang__
506#pragma GCC diagnostic pop
507#endif
508
509BOOST_SPIRIT_DEFINE(keyValue)
510BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200511BOOST_SPIRIT_DEFINE(listSuffix)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200512BOOST_SPIRIT_DEFINE(cdPath)
Václav Kubernát31029682020-10-29 08:23:04 +0100513BOOST_SPIRIT_DEFINE(getPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200514BOOST_SPIRIT_DEFINE(presenceContainerPath)
515BOOST_SPIRIT_DEFINE(listInstancePath)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200516BOOST_SPIRIT_DEFINE(leafListElementPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200517BOOST_SPIRIT_DEFINE(initializePath)
518BOOST_SPIRIT_DEFINE(createKeySuggestions)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200519BOOST_SPIRIT_DEFINE(createValueSuggestions)
520BOOST_SPIRIT_DEFINE(suggestKeysEnd)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200521BOOST_SPIRIT_DEFINE(leafListValue)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200522BOOST_SPIRIT_DEFINE(absoluteStart)
523BOOST_SPIRIT_DEFINE(trailingSlash)