blob: 6b894d8706a178c8e3460d66d308fef7e44e9666 [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átd0ea9b22020-04-24 00:44:15 +020017x3::rule<leaf_path_class, dataPath_> const leafPath = "leafPath";
18x3::rule<presenceContainerPath_class, dataPath_> const presenceContainerPath = "presenceContainerPath";
19x3::rule<listInstancePath_class, dataPath_> const listInstancePath = "listInstancePath";
20x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020021x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020022x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart";
23x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue";
24x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020025x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020026x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions";
27x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions";
28x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd";
29
Václav Kubernát4a58ce62020-05-14 17:58:10 +020030enum class NodeParserMode {
31 CompleteDataNode,
32 IncompleteDataNode,
33 CompletionsOnly,
34 SchemaNode
35};
36
37template <auto>
38struct ModeToAttribute;
39template <>
40struct ModeToAttribute<NodeParserMode::CompleteDataNode> {
41 using type = dataNode_;
42};
43template <>
44struct ModeToAttribute<NodeParserMode::IncompleteDataNode> {
45 using type = dataNode_;
46};
47template <>
48struct ModeToAttribute<NodeParserMode::SchemaNode> {
49 using type = schemaNode_;
50};
51// The CompletionsOnly attribute is dataNode_ only because of convenience:
52// having the same return type means we can get by without a ton of `if constexpr` stanzas.
53// So the code will still "parse data into the target attr" for simplicity.
54template <>
55struct ModeToAttribute<NodeParserMode::CompletionsOnly> {
56 using type = dataNode_;
57};
58
59template <NodeParserMode PARSER_MODE>
60struct NodeParser : x3::parser<NodeParser<PARSER_MODE>> {
61 using attribute_type = typename ModeToAttribute<PARSER_MODE>::type;
62 // GCC complains that `end` isn't used when doing completions only
63 // 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 +020064 template <typename It, typename Ctx, typename RCtx, typename Attr>
Václav Kubernát4a58ce62020-05-14 17:58:10 +020065 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 +020066 {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020067 std::string tableName;
Václav Kubernát4a58ce62020-05-14 17:58:10 +020068 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020069 tableName = "schemaNode";
70 } else {
71 tableName = "dataNode";
72 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +020073 x3::symbols<attribute_type> table(tableName);
Václav Kubernát60a0ed52020-04-28 15:21:33 +020074
75 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
76 parserContext.m_suggestions.clear();
77 for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020078 attribute_type out;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020079 std::string parseString;
80 if (child.first) {
81 out.m_prefix = module_{*child.first};
82 parseString = *child.first + ":";
83 }
84 parseString += child.second;
85 switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) {
86 case yang::NodeTypes::Container:
87 case yang::NodeTypes::PresenceContainer:
88 out.m_suffix = container_{child.second};
89 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
90 break;
91 case yang::NodeTypes::Leaf:
92 out.m_suffix = leaf_{child.second};
93 parserContext.m_suggestions.emplace(Completion{parseString + " "});
94 break;
95 case yang::NodeTypes::List:
Václav Kubernát4a58ce62020-05-14 17:58:10 +020096 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020097 out.m_suffix = list_{child.second};
98 } else {
99 out.m_suffix = listElement_{child.second, {}};
100 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200101 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
102 break;
103 case yang::NodeTypes::Action:
104 case yang::NodeTypes::AnyXml:
105 case yang::NodeTypes::LeafList:
106 case yang::NodeTypes::Notification:
107 case yang::NodeTypes::Rpc:
108 continue;
109 }
110 table.add(parseString, out);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200111 table.add("..", attribute_type{nodeup_{}});
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200112 if (!child.first) {
113 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
114 out.m_prefix = topLevelModule;
115 table.add(topLevelModule->m_name + ":" + parseString, out);
116 }
117 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200118 parserContext.m_completionIterator = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200119
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200120 if constexpr (PARSER_MODE == NodeParserMode::CompletionsOnly) {
121 return true;
122 } else {
123 It saveIter;
124 // GCC complains that I assign saveIter because I use it only if NodeType is dataNode_
125 // FIXME: GCC 10.1 doesn't emit a warning here. Make this unconditional when GCC 10 is available.
126 if constexpr (std::is_same<attribute_type, dataNode_>()) {
127 saveIter = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200128 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200129
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200130 auto res = table.parse(begin, end, ctx, rctx, attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200131
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200132 if (attr.m_prefix) {
133 parserContext.m_curModule = attr.m_prefix->m_name;
134 }
135
136 if (attr.m_suffix.type() == typeid(leaf_)) {
137 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
138 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
139 return boost::optional<std::string>{it.m_name};
140 }),
141 boost::get<leaf_>(attr.m_suffix).m_name};
142 parserContext.m_tmpListKeyLeafPath.m_node = node;
143 }
144
145 if constexpr (std::is_same<attribute_type, dataNode_>()) {
146 if (attr.m_suffix.type() == typeid(listElement_)) {
147 parserContext.m_tmpListName = boost::get<listElement_>(attr.m_suffix).m_name;
148 res = listSuffix.parse(begin, end, ctx, rctx, boost::get<listElement_>(attr.m_suffix).m_keys);
149
150 // FIXME: think of a better way to do this, that is, get rid of manual iterator reverting
151 if (!res) {
152 // If listSuffix didn't succeed, we check, if we allow incomplete nodes. If we do, then we replace listElement_ with list_.
153 // If we don't, we fail the whole symbol table.
154 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
155 res = true;
156 attr.m_suffix = list_{boost::get<listElement_>(attr.m_suffix).m_name};
157 } else {
158 begin = saveIter;
159 }
160 }
161 }
162 }
163
164 if (res) {
165 parserContext.pushPathFragment(attr);
166 parserContext.m_topLevelModulePresent = true;
167 }
168
169 if (attr.m_prefix) {
170 parserContext.m_curModule = boost::none;
171 }
172 return res;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200173 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200174 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200175};
176
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200177NodeParser<NodeParserMode::SchemaNode> schemaNode;
178NodeParser<NodeParserMode::CompleteDataNode> dataNode;
179NodeParser<NodeParserMode::IncompleteDataNode> dataNodeAllowList;
180NodeParser<NodeParserMode::CompletionsOnly> pathCompletions;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200181
Václav Kubernát4618fa42020-05-07 01:33:19 +0200182using AnyPath = boost::variant<schemaPath_, dataPath_>;
183
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200184enum class PathParserMode {
185 AnyPath,
186 DataPath,
187 DataPathListEnd
188};
189
190template <>
191struct ModeToAttribute<PathParserMode::AnyPath> {
192 using type = AnyPath;
193};
194
195template <>
196struct ModeToAttribute<PathParserMode::DataPath> {
197 using type = dataPath_;
198};
199
200template <>
201struct ModeToAttribute<PathParserMode::DataPathListEnd> {
202 using type = dataPath_;
203};
204
205template <PathParserMode PARSER_MODE>
206struct PathParser : x3::parser<PathParser<PARSER_MODE>> {
207 using attribute_type = ModeToAttribute<PARSER_MODE>;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200208 template <typename It, typename Ctx, typename RCtx, typename Attr>
209 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
210 {
Václav Kubernát74d35a42020-05-15 15:29:16 +0200211 initializePath.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200212 dataPath_ attrData;
213
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200214 auto pathEnd = x3::rule<class PathEnd>{"pathEnd"} = &space_separator | x3::eoi;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200215 // absoluteStart has to be separate from the dataPath parser,
216 // otherwise, if the "dataNode % '/'" parser fails, the begin iterator
217 // gets reverted to before the starting slash.
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200218 auto res = (-absoluteStart).parse(begin, end, ctx, rctx, attrData.m_scope);
219 auto dataPath = x3::attr(attrData.m_scope)
220 >> (dataNode % '/' | pathEnd >> x3::attr(std::vector<dataNode_>{}))
221 >> -trailingSlash;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200222 res = dataPath.parse(begin, end, ctx, rctx, attrData);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200223
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200224 // If we allow data paths with a list at the end, we just try to parse that separately.
225 if constexpr (PARSER_MODE == PathParserMode::DataPathListEnd || PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát4618fa42020-05-07 01:33:19 +0200226 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200227 dataNode_ attrNodeList;
228 res = dataNodeAllowList.parse(begin, end, ctx, rctx, attrNodeList);
229 if (res) {
230 attrData.m_nodes.push_back(attrNodeList);
231 // If the trailing slash matches, no more nodes are parsed.
232 // That means no more completion. So, I generate them
233 // manually.
234 res = (-(trailingSlash >> x3::omit[pathCompletions])).parse(begin, end, ctx, rctx, attrData.m_trailingSlash);
235 }
236 }
237 }
238
239 attr = attrData;
240 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
241 // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path
242 auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(),
243 [] (const auto& node) { return node.m_suffix.type() == typeid(listElement_); });
244 // If parsing failed, or if there's more input we try parsing schema nodes.
245 if (!hasLists) {
246 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
247 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
248 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
249 auto schemaPath = schemaNode % '/';
250 // The schemaPath parser continues where the dataPath parser ended.
251 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
252 auto trailing = -trailingSlash >> pathEnd;
253 res = trailing.parse(begin, end, ctx, rctx, attrSchema.m_trailingSlash);
254 attr = attrSchema;
255 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200256 }
257 }
258 return res;
259 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200260};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200261
262// Need to use these wrappers so that my PathParser class gets the proper
263// attribute. Otherwise, Spirit injects the attribute of the outer parser that
264// uses my PathParser.
265// Example grammar: anyPath | module.
266// The PathParser class would get a boost::variant as the attribute, but I
267// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200268// attribute I want (and let Spirit deal with boost::variant).
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200269auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath>{};
270auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath>{};
271auto const dataPathListEnd = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200272
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200273#if __clang__
274#pragma GCC diagnostic push
275#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
276#endif
277
278auto const rest =
279 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
280
281auto const key_identifier_def =
282 x3::lexeme[
283 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
284 ];
285
286auto const createKeySuggestions_def =
287 x3::eps;
288
289auto const createValueSuggestions_def =
290 x3::eps;
291
292auto const suggestKeysEnd_def =
293 x3::eps;
294
295auto const keyValue_def =
296 key_identifier > '=' > createValueSuggestions > leaf_data;
297
298auto const keyValueWrapper =
299 x3::lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
300
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200301// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
302auto const listSuffix_def =
303 *keyValueWrapper;
304
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200305auto const list_def =
306 node_identifier >> !char_('[');
307
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200308auto const absoluteStart_def =
309 x3::omit['/'] >> x3::attr(Scope::Absolute);
310
311auto const trailingSlash_def =
312 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
313
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200314auto const leafPath_def =
315 dataPath;
316
317auto const presenceContainerPath_def =
318 dataPath;
319
320auto const listInstancePath_def =
321 dataPath;
322
323// A "nothing" parser, which is used to indicate we tried to parse a path
324auto const initializePath_def =
325 x3::eps;
326
327
328
329#if __clang__
330#pragma GCC diagnostic pop
331#endif
332
333BOOST_SPIRIT_DEFINE(keyValue)
334BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200335BOOST_SPIRIT_DEFINE(listSuffix)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200336BOOST_SPIRIT_DEFINE(leafPath)
337BOOST_SPIRIT_DEFINE(presenceContainerPath)
338BOOST_SPIRIT_DEFINE(listInstancePath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200339BOOST_SPIRIT_DEFINE(initializePath)
340BOOST_SPIRIT_DEFINE(createKeySuggestions)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200341BOOST_SPIRIT_DEFINE(createValueSuggestions)
342BOOST_SPIRIT_DEFINE(suggestKeysEnd)
343BOOST_SPIRIT_DEFINE(absoluteStart)
344BOOST_SPIRIT_DEFINE(trailingSlash)