blob: 15655d71755376c69d90b3bd22e472290ee3c3b4 [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átabf52802020-05-19 01:31:17 +020017x3::rule<writable_leaf_path_class, dataPath_> const writableLeafPath = "writableLeafPath";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020018x3::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;
Václav Kubernátabf52802020-05-19 01:31:17 +020062
63 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
64
65 NodeParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction)
66 : m_filterFunction(filterFunction)
67 {
68 }
69
Václav Kubernát4a58ce62020-05-14 17:58:10 +020070 // GCC complains that `end` isn't used when doing completions only
71 // 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 +020072 template <typename It, typename Ctx, typename RCtx, typename Attr>
Václav Kubernát4a58ce62020-05-14 17:58:10 +020073 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 +020074 {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020075 std::string tableName;
Václav Kubernát4a58ce62020-05-14 17:58:10 +020076 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020077 tableName = "schemaNode";
78 } else {
79 tableName = "dataNode";
80 }
Václav Kubernát4a58ce62020-05-14 17:58:10 +020081 x3::symbols<attribute_type> table(tableName);
Václav Kubernát60a0ed52020-04-28 15:21:33 +020082
83 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
84 parserContext.m_suggestions.clear();
85 for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +020086 attribute_type out;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020087 std::string parseString;
88 if (child.first) {
89 out.m_prefix = module_{*child.first};
90 parseString = *child.first + ":";
91 }
92 parseString += child.second;
Václav Kubernátabf52802020-05-19 01:31:17 +020093
94 if (!m_filterFunction(parserContext.m_schema, joinPaths(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always), parseString))) {
95 continue;
96 }
97
Václav Kubernát60a0ed52020-04-28 15:21:33 +020098 switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) {
99 case yang::NodeTypes::Container:
100 case yang::NodeTypes::PresenceContainer:
101 out.m_suffix = container_{child.second};
102 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
103 break;
104 case yang::NodeTypes::Leaf:
105 out.m_suffix = leaf_{child.second};
106 parserContext.m_suggestions.emplace(Completion{parseString + " "});
107 break;
108 case yang::NodeTypes::List:
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200109 if constexpr (std::is_same<attribute_type, schemaNode_>()) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200110 out.m_suffix = list_{child.second};
111 } else {
112 out.m_suffix = listElement_{child.second, {}};
113 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200114 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
115 break;
116 case yang::NodeTypes::Action:
117 case yang::NodeTypes::AnyXml:
118 case yang::NodeTypes::LeafList:
119 case yang::NodeTypes::Notification:
120 case yang::NodeTypes::Rpc:
121 continue;
122 }
123 table.add(parseString, out);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200124 table.add("..", attribute_type{nodeup_{}});
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200125 if (!child.first) {
126 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
127 out.m_prefix = topLevelModule;
128 table.add(topLevelModule->m_name + ":" + parseString, out);
129 }
130 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200131 parserContext.m_completionIterator = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200132
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200133 if constexpr (PARSER_MODE == NodeParserMode::CompletionsOnly) {
134 return true;
135 } else {
136 It saveIter;
137 // GCC complains that I assign saveIter because I use it only if NodeType is dataNode_
138 // FIXME: GCC 10.1 doesn't emit a warning here. Make this unconditional when GCC 10 is available.
139 if constexpr (std::is_same<attribute_type, dataNode_>()) {
140 saveIter = begin;
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200141 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200142
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200143 auto res = table.parse(begin, end, ctx, rctx, attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200144
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200145 if (attr.m_prefix) {
146 parserContext.m_curModule = attr.m_prefix->m_name;
147 }
148
149 if (attr.m_suffix.type() == typeid(leaf_)) {
150 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
151 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
152 return boost::optional<std::string>{it.m_name};
153 }),
154 boost::get<leaf_>(attr.m_suffix).m_name};
155 parserContext.m_tmpListKeyLeafPath.m_node = node;
156 }
157
158 if constexpr (std::is_same<attribute_type, dataNode_>()) {
159 if (attr.m_suffix.type() == typeid(listElement_)) {
160 parserContext.m_tmpListName = boost::get<listElement_>(attr.m_suffix).m_name;
161 res = listSuffix.parse(begin, end, ctx, rctx, boost::get<listElement_>(attr.m_suffix).m_keys);
162
163 // FIXME: think of a better way to do this, that is, get rid of manual iterator reverting
164 if (!res) {
165 // If listSuffix didn't succeed, we check, if we allow incomplete nodes. If we do, then we replace listElement_ with list_.
166 // If we don't, we fail the whole symbol table.
167 if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) {
168 res = true;
169 attr.m_suffix = list_{boost::get<listElement_>(attr.m_suffix).m_name};
170 } else {
171 begin = saveIter;
172 }
173 }
174 }
175 }
176
177 if (res) {
178 parserContext.pushPathFragment(attr);
179 parserContext.m_topLevelModulePresent = true;
180 }
181
182 if (attr.m_prefix) {
183 parserContext.m_curModule = boost::none;
184 }
185 return res;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200186 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200187 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200188};
189
Václav Kubernátabf52802020-05-19 01:31:17 +0200190using schemaNode = NodeParser<NodeParserMode::SchemaNode>;
191using dataNode = NodeParser<NodeParserMode::CompleteDataNode>;
192using dataNodeAllowList = NodeParser<NodeParserMode::IncompleteDataNode>;
193using pathCompletions = NodeParser<NodeParserMode::CompletionsOnly>;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200194
Václav Kubernát4618fa42020-05-07 01:33:19 +0200195using AnyPath = boost::variant<schemaPath_, dataPath_>;
196
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200197enum class PathParserMode {
198 AnyPath,
199 DataPath,
200 DataPathListEnd
201};
202
203template <>
204struct ModeToAttribute<PathParserMode::AnyPath> {
205 using type = AnyPath;
206};
207
208template <>
209struct ModeToAttribute<PathParserMode::DataPath> {
210 using type = dataPath_;
211};
212
213template <>
214struct ModeToAttribute<PathParserMode::DataPathListEnd> {
215 using type = dataPath_;
216};
217
218template <PathParserMode PARSER_MODE>
219struct PathParser : x3::parser<PathParser<PARSER_MODE>> {
220 using attribute_type = ModeToAttribute<PARSER_MODE>;
Václav Kubernátabf52802020-05-19 01:31:17 +0200221 std::function<bool(const Schema&, const std::string& path)> m_filterFunction;
222
223 PathParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction = [] (const auto&, const auto&) {return true;})
224 : m_filterFunction(filterFunction)
225 {
226 }
227
Václav Kubernát4618fa42020-05-07 01:33:19 +0200228 template <typename It, typename Ctx, typename RCtx, typename Attr>
229 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
230 {
Václav Kubernát74d35a42020-05-15 15:29:16 +0200231 initializePath.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200232 dataPath_ attrData;
233
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200234 auto pathEnd = x3::rule<class PathEnd>{"pathEnd"} = &space_separator | x3::eoi;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200235 // absoluteStart has to be separate from the dataPath parser,
236 // otherwise, if the "dataNode % '/'" parser fails, the begin iterator
237 // gets reverted to before the starting slash.
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200238 auto res = (-absoluteStart).parse(begin, end, ctx, rctx, attrData.m_scope);
239 auto dataPath = x3::attr(attrData.m_scope)
Václav Kubernátabf52802020-05-19 01:31:17 +0200240 >> (dataNode{m_filterFunction} % '/' | pathEnd >> x3::attr(std::vector<dataNode_>{}))
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200241 >> -trailingSlash;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200242 res = dataPath.parse(begin, end, ctx, rctx, attrData);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200243
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200244 // If we allow data paths with a list at the end, we just try to parse that separately.
245 if constexpr (PARSER_MODE == PathParserMode::DataPathListEnd || PARSER_MODE == PathParserMode::AnyPath) {
Václav Kubernát4618fa42020-05-07 01:33:19 +0200246 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200247 dataNode_ attrNodeList;
Václav Kubernátabf52802020-05-19 01:31:17 +0200248 res = dataNodeAllowList{m_filterFunction}.parse(begin, end, ctx, rctx, attrNodeList);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200249 if (res) {
250 attrData.m_nodes.push_back(attrNodeList);
251 // If the trailing slash matches, no more nodes are parsed.
252 // That means no more completion. So, I generate them
253 // manually.
Václav Kubernátabf52802020-05-19 01:31:17 +0200254 res = (-(trailingSlash >> x3::omit[pathCompletions{m_filterFunction}])).parse(begin, end, ctx, rctx, attrData.m_trailingSlash);
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200255 }
256 }
257 }
258
259 attr = attrData;
260 if constexpr (PARSER_MODE == PathParserMode::AnyPath) {
261 // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path
262 auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(),
263 [] (const auto& node) { return node.m_suffix.type() == typeid(listElement_); });
264 // If parsing failed, or if there's more input we try parsing schema nodes.
265 if (!hasLists) {
266 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
267 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
268 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
Václav Kubernátabf52802020-05-19 01:31:17 +0200269 auto schemaPath = schemaNode{m_filterFunction} % '/';
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200270 // The schemaPath parser continues where the dataPath parser ended.
271 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
272 auto trailing = -trailingSlash >> pathEnd;
273 res = trailing.parse(begin, end, ctx, rctx, attrSchema.m_trailingSlash);
274 attr = attrSchema;
275 }
Václav Kubernát4618fa42020-05-07 01:33:19 +0200276 }
277 }
278 return res;
279 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200280};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200281
282// Need to use these wrappers so that my PathParser class gets the proper
283// attribute. Otherwise, Spirit injects the attribute of the outer parser that
284// uses my PathParser.
285// Example grammar: anyPath | module.
286// The PathParser class would get a boost::variant as the attribute, but I
287// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200288// attribute I want (and let Spirit deal with boost::variant).
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200289auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath>{};
290auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath>{};
291auto const dataPathListEnd = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200292
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200293#if __clang__
294#pragma GCC diagnostic push
295#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
296#endif
297
298auto const rest =
299 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
300
301auto const key_identifier_def =
302 x3::lexeme[
303 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
304 ];
305
306auto const createKeySuggestions_def =
307 x3::eps;
308
309auto const createValueSuggestions_def =
310 x3::eps;
311
312auto const suggestKeysEnd_def =
313 x3::eps;
314
315auto const keyValue_def =
316 key_identifier > '=' > createValueSuggestions > leaf_data;
317
318auto const keyValueWrapper =
319 x3::lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
320
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200321// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
322auto const listSuffix_def =
323 *keyValueWrapper;
324
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200325auto const list_def =
326 node_identifier >> !char_('[');
327
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200328auto const absoluteStart_def =
329 x3::omit['/'] >> x3::attr(Scope::Absolute);
330
331auto const trailingSlash_def =
332 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
333
Václav Kubernátabf52802020-05-19 01:31:17 +0200334auto const filterConfigFalse = [] (const Schema& schema, const std::string& path) {
335 return schema.isConfig(path);
336};
337
338auto const writableLeafPath_def =
339 PathParser<PathParserMode::DataPath>{filterConfigFalse};
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200340
341auto const presenceContainerPath_def =
342 dataPath;
343
344auto const listInstancePath_def =
345 dataPath;
346
347// A "nothing" parser, which is used to indicate we tried to parse a path
348auto const initializePath_def =
349 x3::eps;
350
351
352
353#if __clang__
354#pragma GCC diagnostic pop
355#endif
356
357BOOST_SPIRIT_DEFINE(keyValue)
358BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200359BOOST_SPIRIT_DEFINE(listSuffix)
Václav Kubernátabf52802020-05-19 01:31:17 +0200360BOOST_SPIRIT_DEFINE(writableLeafPath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200361BOOST_SPIRIT_DEFINE(presenceContainerPath)
362BOOST_SPIRIT_DEFINE(listInstancePath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200363BOOST_SPIRIT_DEFINE(initializePath)
364BOOST_SPIRIT_DEFINE(createKeySuggestions)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200365BOOST_SPIRIT_DEFINE(createValueSuggestions)
366BOOST_SPIRIT_DEFINE(suggestKeysEnd)
367BOOST_SPIRIT_DEFINE(absoluteStart)
368BOOST_SPIRIT_DEFINE(trailingSlash)