blob: 04a2dba1a75fb5318ec2c48e26a0491c41213f8a [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át743d9eb2020-05-18 13:42:36 +0200294 res = incompleteDataNode<COMPLETION_MODE>{m_filterFunction}.parse(begin, end, ctx, rctx, attrNodeList);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200295 if (res) {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200296 attrData.m_nodes.emplace_back(attrNodeList);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200297 // If the trailing slash matches, no more nodes are parsed.
298 // That means no more completion. So, I generate them
299 // manually.
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200300 res = (-(trailingSlash >> x3::omit[pathCompletions<COMPLETION_MODE>{m_filterFunction}])).parse(begin, end, ctx, rctx, attrData.m_trailingSlash);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200301 }
302 }
303 }
304
305 attr = attrData;
306 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
307 // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path
308 auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(),
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200309 [] (const auto& node) { return std::holds_alternative<listElement_>(node.m_suffix); });
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200310 // If parsing failed, or if there's more input we try parsing schema nodes.
311 if (!hasLists) {
312 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
313 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
314 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200315 auto schemaPath = schemaNode<COMPLETION_MODE>{m_filterFunction} % '/';
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200316 // The schemaPath parser continues where the dataPath parser ended.
317 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
318 auto trailing = -trailingSlash >> pathEnd;
319 res = trailing.parse(begin, end, ctx, rctx, attrSchema.m_trailingSlash);
320 attr = attrSchema;
321 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200322 }
323 }
324 return res;
325 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200326};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200327
328// Need to use these wrappers so that my PathParser class gets the proper
329// attribute. Otherwise, Spirit injects the attribute of the outer parser that
330// uses my PathParser.
331// Example grammar: anyPath | module.
332// The PathParser class would get a boost::variant as the attribute, but I
333// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200334// attribute I want (and let Spirit deal with boost::variant).
Václav Kubernát743d9eb2020-05-18 13:42:36 +0200335auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath, CompletionMode::Schema>{};
336auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath, CompletionMode::Data>{};
337auto const dataPathListEnd = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200338
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200339#if __clang__
340#pragma GCC diagnostic push
341#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
342#endif
343
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200344struct SuggestLeafListEnd : x3::parser<SuggestLeafListEnd> {
345 using attribute_type = x3::unused_type;
346 template <typename It, typename Ctx, typename RCtx, typename Attr>
347 bool parse(It& begin, It, Ctx const& ctx, RCtx&, Attr&) const
348 {
349 auto& parserContext = x3::get<parser_context_tag>(ctx);
350 parserContext.m_completionIterator = begin;
351 parserContext.m_suggestions = {Completion{"]"}};
352
353 return true;
354 }
355} const suggestLeafListEnd;
356
357auto const leafListValue_def =
358 '[' >> leaf_data >> suggestLeafListEnd >> ']';
359
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200360auto const rest =
361 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
362
363auto const key_identifier_def =
364 x3::lexeme[
365 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
366 ];
367
368auto const createKeySuggestions_def =
369 x3::eps;
370
371auto const createValueSuggestions_def =
372 x3::eps;
373
374auto const suggestKeysEnd_def =
375 x3::eps;
376
377auto const keyValue_def =
378 key_identifier > '=' > createValueSuggestions > leaf_data;
379
380auto const keyValueWrapper =
Václav Kubernát9fa5dca2020-06-01 03:56:41 +0200381 x3::no_skip['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200382
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200383// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
384auto const listSuffix_def =
385 *keyValueWrapper;
386
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200387auto const list_def =
388 node_identifier >> !char_('[');
389
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200390auto const absoluteStart_def =
391 x3::omit['/'] >> x3::attr(Scope::Absolute);
392
393auto const trailingSlash_def =
394 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
395
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100396auto const filterConfigFalse = [](const Schema& schema, const std::string& path) {
Václav Kubernátabf52802020-05-19 01:31:17 +0200397 return schema.isConfig(path);
398};
399
Václav Kubernát3ffd24a2020-10-29 08:25:56 +0100400// A WritableOps value is injected through the `x3::with` with this tag (see usage of the tag). It controls whether
401// `config: false` data can be set with the `set` command. This is used by yang-cli because that tool needs modeling of
402// the full datastore, including the "read-only" data.
Václav Kubernát28cf3362020-06-29 17:52:51 +0200403struct writableOps_tag;
404
405PathParser<PathParserMode::DataPath, CompletionMode::Data> const dataPathFilterConfigFalse{filterConfigFalse};
406
407struct WritableLeafPath : x3::parser<WritableLeafPath> {
408 using attribute_type = dataPath_;
409 template <typename It, typename Ctx, typename RCtx, typename Attr>
410 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
411 {
412 bool res;
413 if (x3::get<writableOps_tag>(ctx) == WritableOps::Yes) {
414 res = dataPath.parse(begin, end, ctx, rctx, attr);
415 } else {
416 res = dataPathFilterConfigFalse.parse(begin, end, ctx, rctx, attr);
417 }
418 if (!res) {
419 return false;
420 }
421
422 if (attr.m_nodes.empty() || !std::holds_alternative<leaf_>(attr.m_nodes.back().m_suffix)) {
423 auto& parserContext = x3::get<parser_context_tag>(ctx);
424 parserContext.m_errorMsg = "This is not a path to leaf.";
425 return false;
426 }
427
428 return true;
429 }
430
431} writableLeafPath;
432
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200433struct RpcActionPath : x3::parser<RpcActionPath> {
434 using attribute_type = dataPath_;
435 template <typename It, typename Ctx, typename RCtx, typename Attr>
436 static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr)
437 {
438 bool res = dataPath.parse(begin, end, ctx, rctx, attr);
439 if (!res) {
440 return false;
441 }
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200442
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200443 if (attr.m_nodes.empty()
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100444 || (!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 +0200445 auto& parserContext = x3::get<parser_context_tag>(ctx);
446 parserContext.m_errorMsg = "This is not a path to an RPC/action.";
447 return false;
448 }
449
450 return true;
451 }
Václav Kubernáte7248b22020-06-26 15:38:59 +0200452};
453
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200454auto const rpcActionPath = as<dataPath_>[RpcActionPath()];
Václav Kubernáte7248b22020-06-26 15:38:59 +0200455
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100456auto const noRpcOrAction = [](const Schema& schema, const std::string& path) {
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200457 auto nodeType = schema.nodeType(path);
458 return nodeType != yang::NodeTypes::Rpc && nodeType != yang::NodeTypes::Action;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200459};
460
Václav Kubernát31029682020-10-29 08:23:04 +0100461auto const getPath_def =
462 PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{noRpcOrAction} |
463 PathParser<PathParserMode::DataPath, CompletionMode::Data>{noRpcOrAction} |
464 (module >> "*");
465
Václav Kubernáte7248b22020-06-26 15:38:59 +0200466auto const cdPath_def =
Václav Kubernát0d47e5e2020-11-17 12:27:05 +0100467 PathParser<PathParserMode::DataPath, CompletionMode::Data>{[] (const Schema& schema, const std::string& path) {
468 return noRpcOrAction(schema, path) && schema.nodeType(path) != yang::NodeTypes::Leaf;
469 }};
Václav Kubernáte7248b22020-06-26 15:38:59 +0200470
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200471auto const presenceContainerPath_def =
472 dataPath;
473
474auto const listInstancePath_def =
475 dataPath;
476
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200477auto const leafListElementPath_def =
478 dataPath;
479
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200480// A "nothing" parser, which is used to indicate we tried to parse a path
481auto const initializePath_def =
482 x3::eps;
483
484
485
486#if __clang__
487#pragma GCC diagnostic pop
488#endif
489
490BOOST_SPIRIT_DEFINE(keyValue)
491BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200492BOOST_SPIRIT_DEFINE(listSuffix)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200493BOOST_SPIRIT_DEFINE(cdPath)
Václav Kubernát31029682020-10-29 08:23:04 +0100494BOOST_SPIRIT_DEFINE(getPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200495BOOST_SPIRIT_DEFINE(presenceContainerPath)
496BOOST_SPIRIT_DEFINE(listInstancePath)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200497BOOST_SPIRIT_DEFINE(leafListElementPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200498BOOST_SPIRIT_DEFINE(initializePath)
499BOOST_SPIRIT_DEFINE(createKeySuggestions)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200500BOOST_SPIRIT_DEFINE(createValueSuggestions)
501BOOST_SPIRIT_DEFINE(suggestKeysEnd)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200502BOOST_SPIRIT_DEFINE(leafListValue)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200503BOOST_SPIRIT_DEFINE(absoluteStart)
504BOOST_SPIRIT_DEFINE(trailingSlash)