blob: c3df931488aed37b9ab7ea49fb00158311a5f033 [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átd0ea9b22020-04-24 00:44:15 +020033
Václav Kubernát4a58ce62020-05-14 17:58:10 +020034enum class NodeParserMode {
35 CompleteDataNode,
36 IncompleteDataNode,
37 CompletionsOnly,
38 SchemaNode
39};
40
41template <auto>
42struct ModeToAttribute;
43template <>
44struct ModeToAttribute<NodeParserMode::CompleteDataNode> {
45 using type = dataNode_;
46};
47template <>
48struct ModeToAttribute<NodeParserMode::IncompleteDataNode> {
49 using type = dataNode_;
50};
51template <>
52struct ModeToAttribute<NodeParserMode::SchemaNode> {
53 using type = schemaNode_;
54};
55// The CompletionsOnly attribute is dataNode_ only because of convenience:
56// having the same return type means we can get by without a ton of `if constexpr` stanzas.
57// So the code will still "parse data into the target attr" for simplicity.
58template <>
59struct ModeToAttribute<NodeParserMode::CompletionsOnly> {
60 using type = dataNode_;
61};
62
Václav Kubernát743d9eb2020-05-18 13:42:36 +020063enum class CompletionMode {
64 Schema,
65 Data
66};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020067
Václav Kubernát743d9eb2020-05-18 13:42:36 +020068template <NodeParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
69struct NodeParser : x3::parser<NodeParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020070 using attribute_type = typename ModeToAttribute<PARSER_MODE>::type;
Václav Kubernátabf52802020-05-19 01:31:17 +020071
72 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
73
74 NodeParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction)
75 : m_filterFunction(filterFunction)
76 {
77 }
78
Václav Kubernát60a0ed52020-04-28 15:21:33 +020079 template <typename It, typename Ctx, typename RCtx, typename Attr>
Václav Kubernát832bd7e2020-12-03 03:06:37 +010080 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
Václav Kubernát60a0ed52020-04-28 15:21:33 +020081 {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020082 std::string tableName;
Václav Kubernát4a58ce62020-05-14 17:58:10 +020083 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020084 tableName = "schemaNode";
85 } else {
86 tableName = "dataNode";
87 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +020088 x3::symbols<attribute_type> table(tableName);
Václav Kubernát60a0ed52020-04-28 15:21:33 +020089
90 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
91 parserContext.m_suggestions.clear();
92 for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020093 attribute_type out;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020094 std::string parseString;
95 if (child.first) {
96 out.m_prefix = module_{*child.first};
97 parseString = *child.first + ":";
98 }
99 parseString += child.second;
Václav Kubernátabf52802020-05-19 01:31:17 +0200100
101 if (!m_filterFunction(parserContext.m_schema, joinPaths(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always), parseString))) {
102 continue;
103 }
104
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200105 switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) {
106 case yang::NodeTypes::Container:
107 case yang::NodeTypes::PresenceContainer:
108 out.m_suffix = container_{child.second};
109 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
110 break;
111 case yang::NodeTypes::Leaf:
112 out.m_suffix = leaf_{child.second};
113 parserContext.m_suggestions.emplace(Completion{parseString + " "});
114 break;
115 case yang::NodeTypes::List:
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200116 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200117 out.m_suffix = list_{child.second};
118 } else {
119 out.m_suffix = listElement_{child.second, {}};
120 }
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200121
122 if constexpr (COMPLETION_MODE == CompletionMode::Schema) {
123 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
124 } else {
125 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
126 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200127 break;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200128 case yang::NodeTypes::LeafList:
129 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
130 out.m_suffix = leafList_{child.second};
131 } else {
132 out.m_suffix = leafListElement_{child.second, {}};
133 }
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200134
135 if constexpr (COMPLETION_MODE == CompletionMode::Schema) {
136 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
137 } else {
138 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
139 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200140 break;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200141 case yang::NodeTypes::Rpc:
142 out.m_suffix = rpcNode_{child.second};
143 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
144 break;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200145 case yang::NodeTypes::Action:
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200146 out.m_suffix = actionNode_{child.second};
147 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
148 break;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200149 case yang::NodeTypes::AnyXml:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200150 case yang::NodeTypes::Notification:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200151 continue;
152 }
153 table.add(parseString, out);
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200154 if (!child.first) {
155 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
156 out.m_prefix = topLevelModule;
157 table.add(topLevelModule->m_name + ":" + parseString, out);
158 }
159 }
Václav Kubernátaed4bc72020-06-08 01:09:25 +0200160 table.add("..", attribute_type{nodeup_{}});
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200161 parserContext.m_completionIterator = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200162
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200163 if constexpr (PARSER_MODE == NodeParserMode::CompletionsOnly) {
164 return true;
165 } else {
Václav Kubernát832bd7e2020-12-03 03:06:37 +0100166 It saveIter = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200167
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200168 auto res = table.parse(begin, end, ctx, rctx, attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200169
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200170 if (std::holds_alternative<leaf_>(attr.m_suffix)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200171 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
172 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
173 return boost::optional<std::string>{it.m_name};
174 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200175 std::get<leaf_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200176 parserContext.m_tmpListKeyLeafPath.m_node = node;
177 }
178
179 if constexpr (std::is_same<attribute_type, dataNode_>()) {
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200180 if (std::holds_alternative<listElement_>(attr.m_suffix)) {
Václav Kubernát2db124c2020-05-28 21:58:36 +0200181 parserContext.m_tmpListPath = parserContext.currentDataPath();
182 auto tmpList = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200183 parserContext.m_tmpListPath.m_nodes.emplace_back(attr.m_prefix, tmpList);
Václav Kubernát2db124c2020-05-28 21:58:36 +0200184
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200185 res = listSuffix.parse(begin, end, ctx, rctx, std::get<listElement_>(attr.m_suffix).m_keys);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200186
187 // FIXME: think of a better way to do this, that is, get rid of manual iterator reverting
188 if (!res) {
189 // If listSuffix didn't succeed, we check, if we allow incomplete nodes. If we do, then we replace listElement_ with list_.
190 // If we don't, we fail the whole symbol table.
191 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
192 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200193 attr.m_suffix = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200194 } else {
195 begin = saveIter;
196 }
197 }
198 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200199
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200200 if (std::holds_alternative<leafListElement_>(attr.m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200201 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
202 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
203 return boost::optional<std::string>{it.m_name};
204 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200205 std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200206 parserContext.m_tmpListKeyLeafPath.m_node = node;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200207 res = leafListValue.parse(begin, end, ctx, rctx, std::get<leafListElement_>(attr.m_suffix).m_value);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200208
209 if (!res) {
210 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
211 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200212 attr.m_suffix = leafList_{std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200213 } else {
214 begin = saveIter;
215 }
216 }
217 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200218 }
219
220 if (res) {
221 parserContext.pushPathFragment(attr);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200222 }
223
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200224 return res;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200225 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200226 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200227};
228
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200229template <CompletionMode COMPLETION_MODE> using schemaNode = NodeParser<NodeParserMode::SchemaNode, COMPLETION_MODE>;
230template <CompletionMode COMPLETION_MODE> using dataNode = NodeParser<NodeParserMode::CompleteDataNode, COMPLETION_MODE>;
231template <CompletionMode COMPLETION_MODE> using incompleteDataNode = NodeParser<NodeParserMode::IncompleteDataNode, COMPLETION_MODE>;
232template <CompletionMode COMPLETION_MODE> using pathCompletions = NodeParser<NodeParserMode::CompletionsOnly, COMPLETION_MODE>;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200233
Václav Kubernát4618fa42020-05-07 01:33:19 +0200234using AnyPath = boost::variant<schemaPath_, dataPath_>;
235
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200236enum class PathParserMode {
237 AnyPath,
238 DataPath,
239 DataPathListEnd
240};
241
242template <>
243struct ModeToAttribute<PathParserMode::AnyPath> {
244 using type = AnyPath;
245};
246
247template <>
248struct ModeToAttribute<PathParserMode::DataPath> {
249 using type = dataPath_;
250};
251
252template <>
253struct ModeToAttribute<PathParserMode::DataPathListEnd> {
254 using type = dataPath_;
255};
256
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200257template <PathParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
258struct PathParser : x3::parser<PathParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200259 using attribute_type = ModeToAttribute<PARSER_MODE>;
Václav Kubernátabf52802020-05-19 01:31:17 +0200260 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
261
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100262 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 +0200263 : m_filterFunction(filterFunction)
264 {
265 }
266
Václav Kubernát4618fa42020-05-07 01:33:19 +0200267 template <typename It, typename Ctx, typename RCtx, typename Attr>
268 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
269 {
Václav Kubernát74d35a42020-05-15 15:29:16 +0200270 initializePath.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200271 dataPath_ attrData;
272
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200273 auto pathEnd = x3::rule<class PathEnd>{"pathEnd"} = &space_separator | x3::eoi;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200274 // absoluteStart has to be separate from the dataPath parser,
275 // otherwise, if the "dataNode % '/'" parser fails, the begin iterator
276 // gets reverted to before the starting slash.
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200277 auto res = (-absoluteStart).parse(begin, end, ctx, rctx, attrData.m_scope);
278 auto dataPath = x3::attr(attrData.m_scope)
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200279 >> (dataNode<COMPLETION_MODE>{m_filterFunction} % '/' | pathEnd >> x3::attr(std::vector<dataNode_>{}))
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200280 >> -trailingSlash;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200281 res = dataPath.parse(begin, end, ctx, rctx, attrData);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200282
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200283 // If we allow data paths with a list at the end, we just try to parse that separately.
284 if constexpr (PARSER_MODE == PathParserMode::DataPathListEnd || PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát4618fa42020-05-07 01:33:19 +0200285 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200286 dataNode_ attrNodeList;
Václav Kubernát7eb47152020-11-12 16:57:03 +0100287 auto hasListEnd = incompleteDataNode<COMPLETION_MODE>{m_filterFunction}.parse(begin, end, ctx, rctx, attrNodeList);
288 if (hasListEnd) {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200289 attrData.m_nodes.emplace_back(attrNodeList);
Václav Kubernát85ba7382020-11-18 18:11:18 +0100290 // If the trailing slash matches, no more nodes are parsed. That means no more completion. So, I
291 // generate them manually, but only if we're in AnyPath mode, so, for example, inside an `ls`
292 // command. If we're in DataPathListEnd it doesn't make sense to parse put any more nodes after the
293 // final list.
294 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
295 res = (-(trailingSlash >> x3::omit[pathCompletions<COMPLETION_MODE>{m_filterFunction}])).parse(begin, end, ctx, rctx, attrData.m_trailingSlash);
296 } else {
297 res = (-trailingSlash).parse(begin, end, ctx, rctx, attrData.m_trailingSlash);
298 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200299 }
300 }
301 }
302
303 attr = attrData;
304 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
305 // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path
306 auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200307 [] (const auto& node) { return std::holds_alternative<listElement_>(node.m_suffix); });
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200308 // If parsing failed, or if there's more input we try parsing schema nodes.
309 if (!hasLists) {
310 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
311 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
312 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200313 auto schemaPath = schemaNode<COMPLETION_MODE>{m_filterFunction} % '/';
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200314 // The schemaPath parser continues where the dataPath parser ended.
315 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
316 auto trailing = -trailingSlash >> pathEnd;
317 res = trailing.parse(begin, end, ctx, rctx, attrSchema.m_trailingSlash);
318 attr = attrSchema;
319 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200320 }
321 }
322 return res;
323 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200324};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200325
326// Need to use these wrappers so that my PathParser class gets the proper
327// attribute. Otherwise, Spirit injects the attribute of the outer parser that
328// uses my PathParser.
329// Example grammar: anyPath | module.
330// The PathParser class would get a boost::variant as the attribute, but I
331// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200332// attribute I want (and let Spirit deal with boost::variant).
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200333auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath, CompletionMode::Schema>{};
334auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath, CompletionMode::Data>{};
335auto const dataPathListEnd = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200336
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200337#if __clang__
338#pragma GCC diagnostic push
339#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
340#endif
341
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200342struct SuggestLeafListEnd : x3::parser<SuggestLeafListEnd> {
343 using attribute_type = x3::unused_type;
344 template <typename It, typename Ctx, typename RCtx, typename Attr>
345 bool parse(It& begin, It, Ctx const& ctx, RCtx&, Attr&) const
346 {
347 auto& parserContext = x3::get<parser_context_tag>(ctx);
348 parserContext.m_completionIterator = begin;
349 parserContext.m_suggestions = {Completion{"]"}};
350
351 return true;
352 }
353} const suggestLeafListEnd;
354
355auto const leafListValue_def =
356 '[' >> leaf_data >> suggestLeafListEnd >> ']';
357
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200358auto const rest =
359 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
360
361auto const key_identifier_def =
362 x3::lexeme[
363 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
364 ];
365
366auto const createKeySuggestions_def =
367 x3::eps;
368
369auto const createValueSuggestions_def =
370 x3::eps;
371
372auto const suggestKeysEnd_def =
373 x3::eps;
374
375auto const keyValue_def =
376 key_identifier > '=' > createValueSuggestions > leaf_data;
377
378auto const keyValueWrapper =
Václav Kubernát9fa5dca2020-06-01 03:56:41 +0200379 x3::no_skip['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200380
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200381// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
382auto const listSuffix_def =
383 *keyValueWrapper;
384
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200385auto const list_def =
386 node_identifier >> !char_('[');
387
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200388auto const absoluteStart_def =
389 x3::omit['/'] >> x3::attr(Scope::Absolute);
390
391auto const trailingSlash_def =
392 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
393
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100394auto const filterConfigFalse = [](const Schema& schema, const std::string& path) {
Václav Kubernátabf52802020-05-19 01:31:17 +0200395 return schema.isConfig(path);
396};
397
Václav Kubernát3ffd24a2020-10-29 08:25:56 +0100398// A WritableOps value is injected through the `x3::with` with this tag (see usage of the tag). It controls whether
399// `config: false` data can be set with the `set` command. This is used by yang-cli because that tool needs modeling of
400// the full datastore, including the "read-only" data.
Václav Kubernát28cf3362020-06-29 17:52:51 +0200401struct writableOps_tag;
402
403PathParser<PathParserMode::DataPath, CompletionMode::Data> const dataPathFilterConfigFalse{filterConfigFalse};
404
405struct WritableLeafPath : x3::parser<WritableLeafPath> {
406 using attribute_type = dataPath_;
407 template <typename It, typename Ctx, typename RCtx, typename Attr>
408 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
409 {
410 bool res;
411 if (x3::get<writableOps_tag>(ctx) == WritableOps::Yes) {
412 res = dataPath.parse(begin, end, ctx, rctx, attr);
413 } else {
414 res = dataPathFilterConfigFalse.parse(begin, end, ctx, rctx, attr);
415 }
416 if (!res) {
417 return false;
418 }
419
420 if (attr.m_nodes.empty() || !std::holds_alternative<leaf_>(attr.m_nodes.back().m_suffix)) {
421 auto& parserContext = x3::get<parser_context_tag>(ctx);
422 parserContext.m_errorMsg = "This is not a path to leaf.";
423 return false;
424 }
425
426 return true;
427 }
428
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100429} const writableLeafPath;
Václav Kubernát28cf3362020-06-29 17:52:51 +0200430
Václav Kubernátd8408e02020-12-02 05:13:27 +0100431enum class AllowInput {
432 Yes,
433 No
434};
435
436template <AllowInput ALLOW_INPUT>
437struct RpcActionPath : x3::parser<RpcActionPath<ALLOW_INPUT>> {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200438 using attribute_type = dataPath_;
439 template <typename It, typename Ctx, typename RCtx, typename Attr>
440 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
441 {
Václav Kubernátd8408e02020-12-02 05:13:27 +0100442 auto grammar = PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
443 if constexpr (ALLOW_INPUT == AllowInput::No) {
444 auto nodeType = schema.nodeType(path);
445 if (nodeType == yang::NodeTypes::Rpc || nodeType == yang::NodeTypes::Action) {
446 return !schema.hasInputNodes(path);
447 }
448 }
449
450 return true;
451 }};
452 bool res = grammar.parse(begin, end, ctx, rctx, attr);
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200453 if (!res) {
454 return false;
455 }
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200456
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200457 if (attr.m_nodes.empty()
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100458 || (!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 +0200459 auto& parserContext = x3::get<parser_context_tag>(ctx);
460 parserContext.m_errorMsg = "This is not a path to an RPC/action.";
461 return false;
462 }
463
464 return true;
465 }
Václav Kubernáte7248b22020-06-26 15:38:59 +0200466};
467
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100468auto const noRpcOrAction = [](const Schema& schema, const std::string& path) {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200469 auto nodeType = schema.nodeType(path);
470 return nodeType != yang::NodeTypes::Rpc && nodeType != yang::NodeTypes::Action;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200471};
472
Václav Kubernát31029682020-10-29 08:23:04 +0100473auto const getPath_def =
474 PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{noRpcOrAction} |
Václav Kubernát31029682020-10-29 08:23:04 +0100475 (module >> "*");
476
Václav Kubernáte7248b22020-06-26 15:38:59 +0200477auto const cdPath_def =
Václav Kubernát0d47e5e2020-11-17 12:27:05 +0100478 PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
479 return noRpcOrAction(schema, path) && schema.nodeType(path) != yang::NodeTypes::Leaf;
480 }};
Václav Kubernáte7248b22020-06-26 15:38:59 +0200481
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200482auto const presenceContainerPath_def =
483 dataPath;
484
485auto const listInstancePath_def =
486 dataPath;
487
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200488auto const leafListElementPath_def =
489 dataPath;
490
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200491// A "nothing" parser, which is used to indicate we tried to parse a path
492auto const initializePath_def =
493 x3::eps;
494
495
496
497#if __clang__
498#pragma GCC diagnostic pop
499#endif
500
501BOOST_SPIRIT_DEFINE(keyValue)
502BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200503BOOST_SPIRIT_DEFINE(listSuffix)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200504BOOST_SPIRIT_DEFINE(cdPath)
Václav Kubernát31029682020-10-29 08:23:04 +0100505BOOST_SPIRIT_DEFINE(getPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200506BOOST_SPIRIT_DEFINE(presenceContainerPath)
507BOOST_SPIRIT_DEFINE(listInstancePath)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200508BOOST_SPIRIT_DEFINE(leafListElementPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200509BOOST_SPIRIT_DEFINE(initializePath)
510BOOST_SPIRIT_DEFINE(createKeySuggestions)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200511BOOST_SPIRIT_DEFINE(createValueSuggestions)
512BOOST_SPIRIT_DEFINE(suggestKeysEnd)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200513BOOST_SPIRIT_DEFINE(leafListValue)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200514BOOST_SPIRIT_DEFINE(absoluteStart)
515BOOST_SPIRIT_DEFINE(trailingSlash)