blob: fca760d8fcab5e6211c0a1c2b6ef5cafb74a8f07 [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át4a58ce62020-05-14 17:58:10 +020079 // GCC complains that `end` isn't used when doing completions only
80 // FIXME: GCC 10.1 doesn't emit a warning here. Remove [[maybe_unused]] when GCC 10 is available
Václav Kubernát60a0ed52020-04-28 15:21:33 +020081 template <typename It, typename Ctx, typename RCtx, typename Attr>
Václav Kubernát4a58ce62020-05-14 17:58:10 +020082 bool parse(It& begin, [[maybe_unused]] It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
Václav Kubernát60a0ed52020-04-28 15:21:33 +020083 {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020084 std::string tableName;
Václav Kubernát4a58ce62020-05-14 17:58:10 +020085 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020086 tableName = "schemaNode";
87 } else {
88 tableName = "dataNode";
89 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +020090 x3::symbols<attribute_type> table(tableName);
Václav Kubernát60a0ed52020-04-28 15:21:33 +020091
92 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
93 parserContext.m_suggestions.clear();
94 for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020095 attribute_type out;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020096 std::string parseString;
97 if (child.first) {
98 out.m_prefix = module_{*child.first};
99 parseString = *child.first + ":";
100 }
101 parseString += child.second;
Václav Kubernátabf52802020-05-19 01:31:17 +0200102
103 if (!m_filterFunction(parserContext.m_schema, joinPaths(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always), parseString))) {
104 continue;
105 }
106
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200107 switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) {
108 case yang::NodeTypes::Container:
109 case yang::NodeTypes::PresenceContainer:
110 out.m_suffix = container_{child.second};
111 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
112 break;
113 case yang::NodeTypes::Leaf:
114 out.m_suffix = leaf_{child.second};
115 parserContext.m_suggestions.emplace(Completion{parseString + " "});
116 break;
117 case yang::NodeTypes::List:
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200118 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200119 out.m_suffix = list_{child.second};
120 } else {
121 out.m_suffix = listElement_{child.second, {}};
122 }
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200123
124 if constexpr (COMPLETION_MODE == CompletionMode::Schema) {
125 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
126 } else {
127 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
128 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200129 break;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200130 case yang::NodeTypes::LeafList:
131 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
132 out.m_suffix = leafList_{child.second};
133 } else {
134 out.m_suffix = leafListElement_{child.second, {}};
135 }
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200136
137 if constexpr (COMPLETION_MODE == CompletionMode::Schema) {
138 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
139 } else {
140 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
141 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200142 break;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200143 case yang::NodeTypes::Rpc:
144 out.m_suffix = rpcNode_{child.second};
145 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
146 break;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200147 case yang::NodeTypes::Action:
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200148 out.m_suffix = actionNode_{child.second};
149 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
150 break;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200151 case yang::NodeTypes::AnyXml:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200152 case yang::NodeTypes::Notification:
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200153 continue;
154 }
155 table.add(parseString, out);
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200156 if (!child.first) {
157 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
158 out.m_prefix = topLevelModule;
159 table.add(topLevelModule->m_name + ":" + parseString, out);
160 }
161 }
Václav Kubernátaed4bc72020-06-08 01:09:25 +0200162 table.add("..", attribute_type{nodeup_{}});
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200163 parserContext.m_completionIterator = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200164
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200165 if constexpr (PARSER_MODE == NodeParserMode::CompletionsOnly) {
166 return true;
167 } else {
168 It saveIter;
169 // GCC complains that I assign saveIter because I use it only if NodeType is dataNode_
170 // FIXME: GCC 10.1 doesn't emit a warning here. Make this unconditional when GCC 10 is available.
171 if constexpr (std::is_same<attribute_type, dataNode_>()) {
172 saveIter = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200173 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200174
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200175 auto res = table.parse(begin, end, ctx, rctx, attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200176
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200177 if (std::holds_alternative<leaf_>(attr.m_suffix)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200178 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
179 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
180 return boost::optional<std::string>{it.m_name};
181 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200182 std::get<leaf_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200183 parserContext.m_tmpListKeyLeafPath.m_node = node;
184 }
185
186 if constexpr (std::is_same<attribute_type, dataNode_>()) {
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200187 if (std::holds_alternative<listElement_>(attr.m_suffix)) {
Václav Kubernát2db124c2020-05-28 21:58:36 +0200188 parserContext.m_tmpListPath = parserContext.currentDataPath();
189 auto tmpList = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200190 parserContext.m_tmpListPath.m_nodes.emplace_back(attr.m_prefix, tmpList);
Václav Kubernát2db124c2020-05-28 21:58:36 +0200191
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200192 res = listSuffix.parse(begin, end, ctx, rctx, std::get<listElement_>(attr.m_suffix).m_keys);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200193
194 // FIXME: think of a better way to do this, that is, get rid of manual iterator reverting
195 if (!res) {
196 // If listSuffix didn't succeed, we check, if we allow incomplete nodes. If we do, then we replace listElement_ with list_.
197 // If we don't, we fail the whole symbol table.
198 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
199 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200200 attr.m_suffix = list_{std::get<listElement_>(attr.m_suffix).m_name};
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200201 } else {
202 begin = saveIter;
203 }
204 }
205 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200206
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200207 if (std::holds_alternative<leafListElement_>(attr.m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200208 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
209 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
210 return boost::optional<std::string>{it.m_name};
211 }),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200212 std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200213 parserContext.m_tmpListKeyLeafPath.m_node = node;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200214 res = leafListValue.parse(begin, end, ctx, rctx, std::get<leafListElement_>(attr.m_suffix).m_value);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200215
216 if (!res) {
217 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
218 res = true;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200219 attr.m_suffix = leafList_{std::get<leafListElement_>(attr.m_suffix).m_name};
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200220 } else {
221 begin = saveIter;
222 }
223 }
224 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200225 }
226
227 if (res) {
228 parserContext.pushPathFragment(attr);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200229 }
230
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200231 return res;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200232 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200233 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200234};
235
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200236template <CompletionMode COMPLETION_MODE> using schemaNode = NodeParser<NodeParserMode::SchemaNode, COMPLETION_MODE>;
237template <CompletionMode COMPLETION_MODE> using dataNode = NodeParser<NodeParserMode::CompleteDataNode, COMPLETION_MODE>;
238template <CompletionMode COMPLETION_MODE> using incompleteDataNode = NodeParser<NodeParserMode::IncompleteDataNode, COMPLETION_MODE>;
239template <CompletionMode COMPLETION_MODE> using pathCompletions = NodeParser<NodeParserMode::CompletionsOnly, COMPLETION_MODE>;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200240
Václav Kubernát4618fa42020-05-07 01:33:19 +0200241using AnyPath = boost::variant<schemaPath_, dataPath_>;
242
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200243enum class PathParserMode {
244 AnyPath,
245 DataPath,
246 DataPathListEnd
247};
248
249template <>
250struct ModeToAttribute<PathParserMode::AnyPath> {
251 using type = AnyPath;
252};
253
254template <>
255struct ModeToAttribute<PathParserMode::DataPath> {
256 using type = dataPath_;
257};
258
259template <>
260struct ModeToAttribute<PathParserMode::DataPathListEnd> {
261 using type = dataPath_;
262};
263
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200264template <PathParserMode PARSER_MODE, CompletionMode COMPLETION_MODE>
265struct PathParser : x3::parser<PathParser<PARSER_MODE, COMPLETION_MODE>> {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200266 using attribute_type = ModeToAttribute<PARSER_MODE>;
Václav Kubernátabf52802020-05-19 01:31:17 +0200267 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
268
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100269 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 +0200270 : m_filterFunction(filterFunction)
271 {
272 }
273
Václav Kubernát4618fa42020-05-07 01:33:19 +0200274 template <typename It, typename Ctx, typename RCtx, typename Attr>
275 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
276 {
Václav Kubernát74d35a42020-05-15 15:29:16 +0200277 initializePath.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200278 dataPath_ attrData;
279
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200280 auto pathEnd = x3::rule<class PathEnd>{"pathEnd"} = &space_separator | x3::eoi;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200281 // absoluteStart has to be separate from the dataPath parser,
282 // otherwise, if the "dataNode % '/'" parser fails, the begin iterator
283 // gets reverted to before the starting slash.
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200284 auto res = (-absoluteStart).parse(begin, end, ctx, rctx, attrData.m_scope);
285 auto dataPath = x3::attr(attrData.m_scope)
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200286 >> (dataNode<COMPLETION_MODE>{m_filterFunction} % '/' | pathEnd >> x3::attr(std::vector<dataNode_>{}))
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200287 >> -trailingSlash;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200288 res = dataPath.parse(begin, end, ctx, rctx, attrData);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200289
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200290 // If we allow data paths with a list at the end, we just try to parse that separately.
291 if constexpr (PARSER_MODE == PathParserMode::DataPathListEnd || PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát4618fa42020-05-07 01:33:19 +0200292 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200293 dataNode_ attrNodeList;
Václav Kubernát7eb47152020-11-12 16:57:03 +0100294 auto hasListEnd = incompleteDataNode<COMPLETION_MODE>{m_filterFunction}.parse(begin, end, ctx, rctx, attrNodeList);
295 if (hasListEnd) {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200296 attrData.m_nodes.emplace_back(attrNodeList);
Václav Kubernát85ba7382020-11-18 18:11:18 +0100297 // If the trailing slash matches, no more nodes are parsed. That means no more completion. So, I
298 // generate them manually, but only if we're in AnyPath mode, so, for example, inside an `ls`
299 // command. If we're in DataPathListEnd it doesn't make sense to parse put any more nodes after the
300 // final list.
301 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
302 res = (-(trailingSlash >> x3::omit[pathCompletions<COMPLETION_MODE>{m_filterFunction}])).parse(begin, end, ctx, rctx, attrData.m_trailingSlash);
303 } else {
304 res = (-trailingSlash).parse(begin, end, ctx, rctx, attrData.m_trailingSlash);
305 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200306 }
307 }
308 }
309
310 attr = attrData;
311 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
312 // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path
313 auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200314 [] (const auto& node) { return std::holds_alternative<listElement_>(node.m_suffix); });
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200315 // If parsing failed, or if there's more input we try parsing schema nodes.
316 if (!hasLists) {
317 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
318 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
319 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200320 auto schemaPath = schemaNode<COMPLETION_MODE>{m_filterFunction} % '/';
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200321 // The schemaPath parser continues where the dataPath parser ended.
322 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
323 auto trailing = -trailingSlash >> pathEnd;
324 res = trailing.parse(begin, end, ctx, rctx, attrSchema.m_trailingSlash);
325 attr = attrSchema;
326 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200327 }
328 }
329 return res;
330 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200331};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200332
333// Need to use these wrappers so that my PathParser class gets the proper
334// attribute. Otherwise, Spirit injects the attribute of the outer parser that
335// uses my PathParser.
336// Example grammar: anyPath | module.
337// The PathParser class would get a boost::variant as the attribute, but I
338// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200339// attribute I want (and let Spirit deal with boost::variant).
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200340auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath, CompletionMode::Schema>{};
341auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath, CompletionMode::Data>{};
342auto const dataPathListEnd = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200343
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200344#if __clang__
345#pragma GCC diagnostic push
346#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
347#endif
348
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200349struct SuggestLeafListEnd : x3::parser<SuggestLeafListEnd> {
350 using attribute_type = x3::unused_type;
351 template <typename It, typename Ctx, typename RCtx, typename Attr>
352 bool parse(It& begin, It, Ctx const& ctx, RCtx&, Attr&) const
353 {
354 auto& parserContext = x3::get<parser_context_tag>(ctx);
355 parserContext.m_completionIterator = begin;
356 parserContext.m_suggestions = {Completion{"]"}};
357
358 return true;
359 }
360} const suggestLeafListEnd;
361
362auto const leafListValue_def =
363 '[' >> leaf_data >> suggestLeafListEnd >> ']';
364
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200365auto const rest =
366 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
367
368auto const key_identifier_def =
369 x3::lexeme[
370 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
371 ];
372
373auto const createKeySuggestions_def =
374 x3::eps;
375
376auto const createValueSuggestions_def =
377 x3::eps;
378
379auto const suggestKeysEnd_def =
380 x3::eps;
381
382auto const keyValue_def =
383 key_identifier > '=' > createValueSuggestions > leaf_data;
384
385auto const keyValueWrapper =
Václav Kubernát9fa5dca2020-06-01 03:56:41 +0200386 x3::no_skip['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200387
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200388// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
389auto const listSuffix_def =
390 *keyValueWrapper;
391
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200392auto const list_def =
393 node_identifier >> !char_('[');
394
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200395auto const absoluteStart_def =
396 x3::omit['/'] >> x3::attr(Scope::Absolute);
397
398auto const trailingSlash_def =
399 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
400
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100401auto const filterConfigFalse = [](const Schema& schema, const std::string& path) {
Václav Kubernátabf52802020-05-19 01:31:17 +0200402 return schema.isConfig(path);
403};
404
Václav Kubernát3ffd24a2020-10-29 08:25:56 +0100405// A WritableOps value is injected through the `x3::with` with this tag (see usage of the tag). It controls whether
406// `config: false` data can be set with the `set` command. This is used by yang-cli because that tool needs modeling of
407// the full datastore, including the "read-only" data.
Václav Kubernát28cf3362020-06-29 17:52:51 +0200408struct writableOps_tag;
409
410PathParser<PathParserMode::DataPath, CompletionMode::Data> const dataPathFilterConfigFalse{filterConfigFalse};
411
412struct WritableLeafPath : x3::parser<WritableLeafPath> {
413 using attribute_type = dataPath_;
414 template <typename It, typename Ctx, typename RCtx, typename Attr>
415 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
416 {
417 bool res;
418 if (x3::get<writableOps_tag>(ctx) == WritableOps::Yes) {
419 res = dataPath.parse(begin, end, ctx, rctx, attr);
420 } else {
421 res = dataPathFilterConfigFalse.parse(begin, end, ctx, rctx, attr);
422 }
423 if (!res) {
424 return false;
425 }
426
427 if (attr.m_nodes.empty() || !std::holds_alternative<leaf_>(attr.m_nodes.back().m_suffix)) {
428 auto& parserContext = x3::get<parser_context_tag>(ctx);
429 parserContext.m_errorMsg = "This is not a path to leaf.";
430 return false;
431 }
432
433 return true;
434 }
435
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100436} const writableLeafPath;
Václav Kubernát28cf3362020-06-29 17:52:51 +0200437
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200438struct RpcActionPath : x3::parser<RpcActionPath> {
439 using attribute_type = dataPath_;
440 template <typename It, typename Ctx, typename RCtx, typename Attr>
441 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
442 {
443 bool res = dataPath.parse(begin, end, ctx, rctx, attr);
444 if (!res) {
445 return false;
446 }
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200447
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200448 if (attr.m_nodes.empty()
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100449 || (!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 +0200450 auto& parserContext = x3::get<parser_context_tag>(ctx);
451 parserContext.m_errorMsg = "This is not a path to an RPC/action.";
452 return false;
453 }
454
455 return true;
456 }
Václav Kubernáte7248b22020-06-26 15:38:59 +0200457};
458
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200459auto const rpcActionPath = as<dataPath_>[RpcActionPath()];
Václav Kubernáte7248b22020-06-26 15:38:59 +0200460
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100461auto const noRpcOrAction = [](const Schema& schema, const std::string& path) {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200462 auto nodeType = schema.nodeType(path);
463 return nodeType != yang::NodeTypes::Rpc && nodeType != yang::NodeTypes::Action;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200464};
465
Václav Kubernát31029682020-10-29 08:23:04 +0100466auto const getPath_def =
467 PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{noRpcOrAction} |
Václav Kubernát31029682020-10-29 08:23:04 +0100468 (module >> "*");
469
Václav Kubernáte7248b22020-06-26 15:38:59 +0200470auto const cdPath_def =
Václav Kubernát0d47e5e2020-11-17 12:27:05 +0100471 PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
472 return noRpcOrAction(schema, path) && schema.nodeType(path) != yang::NodeTypes::Leaf;
473 }};
Václav Kubernáte7248b22020-06-26 15:38:59 +0200474
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200475auto const presenceContainerPath_def =
476 dataPath;
477
478auto const listInstancePath_def =
479 dataPath;
480
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200481auto const leafListElementPath_def =
482 dataPath;
483
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200484// A "nothing" parser, which is used to indicate we tried to parse a path
485auto const initializePath_def =
486 x3::eps;
487
488
489
490#if __clang__
491#pragma GCC diagnostic pop
492#endif
493
494BOOST_SPIRIT_DEFINE(keyValue)
495BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200496BOOST_SPIRIT_DEFINE(listSuffix)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200497BOOST_SPIRIT_DEFINE(cdPath)
Václav Kubernát31029682020-10-29 08:23:04 +0100498BOOST_SPIRIT_DEFINE(getPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200499BOOST_SPIRIT_DEFINE(presenceContainerPath)
500BOOST_SPIRIT_DEFINE(listInstancePath)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200501BOOST_SPIRIT_DEFINE(leafListElementPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200502BOOST_SPIRIT_DEFINE(initializePath)
503BOOST_SPIRIT_DEFINE(createKeySuggestions)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200504BOOST_SPIRIT_DEFINE(createValueSuggestions)
505BOOST_SPIRIT_DEFINE(suggestKeysEnd)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200506BOOST_SPIRIT_DEFINE(leafListValue)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200507BOOST_SPIRIT_DEFINE(absoluteStart)
508BOOST_SPIRIT_DEFINE(trailingSlash)