blob: 19ab2a284847f7b8afc071fbbb3bb3617491a515 [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
17x3::rule<dataPath_class, dataPath_> const dataPath = "dataPath";
18x3::rule<schemaPath_class, schemaPath_> const schemaPath = "schemaPath";
19x3::rule<dataNodeList_class, decltype(dataPath_::m_nodes)::value_type> const dataNodeList = "dataNodeList";
20x3::rule<dataNodesListEnd_class, decltype(dataPath_::m_nodes)> const dataNodesListEnd = "dataNodesListEnd";
21x3::rule<dataPathListEnd_class, dataPath_> const dataPathListEnd = "dataPathListEnd";
22x3::rule<leaf_path_class, dataPath_> const leafPath = "leafPath";
23x3::rule<presenceContainerPath_class, dataPath_> const presenceContainerPath = "presenceContainerPath";
24x3::rule<listInstancePath_class, dataPath_> const listInstancePath = "listInstancePath";
25x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath";
26x3::rule<createPathSuggestions_class, x3::unused_type> const createPathSuggestions = "createPathSuggestions";
27x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020028x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart";
29x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue";
30x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020031x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020032x3::rule<list_class, list_> const list = "list";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020033x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions";
34x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions";
35x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd";
36
Václav Kubernátdd5945a2020-05-05 01:24:23 +020037template <typename NodeType>
38struct NodeParser : x3::parser<NodeParser<NodeType>> {
39 using attribute_type = NodeType;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020040 template <typename It, typename Ctx, typename RCtx, typename Attr>
41 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
42 {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020043 std::string tableName;
44 if constexpr (std::is_same<NodeType, schemaNode_>()) {
45 tableName = "schemaNode";
46 } else {
47 tableName = "dataNode";
48 }
49 x3::symbols<NodeType> table(tableName);
Václav Kubernát60a0ed52020-04-28 15:21:33 +020050
51 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
52 parserContext.m_suggestions.clear();
53 for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020054 NodeType out;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020055 std::string parseString;
56 if (child.first) {
57 out.m_prefix = module_{*child.first};
58 parseString = *child.first + ":";
59 }
60 parseString += child.second;
61 switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) {
62 case yang::NodeTypes::Container:
63 case yang::NodeTypes::PresenceContainer:
64 out.m_suffix = container_{child.second};
65 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
66 break;
67 case yang::NodeTypes::Leaf:
68 out.m_suffix = leaf_{child.second};
69 parserContext.m_suggestions.emplace(Completion{parseString + " "});
70 break;
71 case yang::NodeTypes::List:
Václav Kubernátdd5945a2020-05-05 01:24:23 +020072 if constexpr (std::is_same<NodeType, schemaNode_>()) {
73 out.m_suffix = list_{child.second};
74 } else {
75 out.m_suffix = listElement_{child.second, {}};
76 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +020077 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
78 break;
79 case yang::NodeTypes::Action:
80 case yang::NodeTypes::AnyXml:
81 case yang::NodeTypes::LeafList:
82 case yang::NodeTypes::Notification:
83 case yang::NodeTypes::Rpc:
84 continue;
85 }
86 table.add(parseString, out);
Václav Kubernátdd5945a2020-05-05 01:24:23 +020087 table.add("..", NodeType{nodeup_{}});
Václav Kubernát60a0ed52020-04-28 15:21:33 +020088 if (!child.first) {
89 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
90 out.m_prefix = topLevelModule;
91 table.add(topLevelModule->m_name + ":" + parseString, out);
92 }
93 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +020094 parserContext.m_completionIterator = begin;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020095 auto res = table.parse(begin, end, ctx, rctx, attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +020096
97 if (attr.m_prefix) {
98 parserContext.m_curModule = attr.m_prefix->m_name;
99 }
100
101 if (attr.m_suffix.type() == typeid(leaf_)) {
102 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
103 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
104 return boost::optional<std::string>{it.m_name};
105 }), boost::get<leaf_>(attr.m_suffix).m_name};
106 parserContext.m_tmpListKeyLeafPath.m_node = node;
107 }
108
109 if constexpr (std::is_same<NodeType, dataNode_>()) {
110 if (attr.m_suffix.type() == typeid(listElement_)) {
111 parserContext.m_tmpListName = boost::get<listElement_>(attr.m_suffix).m_name;
112 res = listSuffix.parse(begin, end, ctx, rctx, boost::get<listElement_>(attr.m_suffix).m_keys);
113 }
114 }
115
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200116 if (res) {
117 parserContext.pushPathFragment(attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200118 parserContext.m_topLevelModulePresent = true;
119 }
120
121 if (attr.m_prefix) {
122 parserContext.m_curModule = boost::none;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200123 }
124 return res;
125 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200126};
127
128NodeParser<schemaNode_> schemaNode;
129NodeParser<dataNode_> dataNode;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200130
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200131#if __clang__
132#pragma GCC diagnostic push
133#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
134#endif
135
136auto const rest =
137 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
138
139auto const key_identifier_def =
140 x3::lexeme[
141 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
142 ];
143
144auto const createKeySuggestions_def =
145 x3::eps;
146
147auto const createValueSuggestions_def =
148 x3::eps;
149
150auto const suggestKeysEnd_def =
151 x3::eps;
152
153auto const keyValue_def =
154 key_identifier > '=' > createValueSuggestions > leaf_data;
155
156auto const keyValueWrapper =
157 x3::lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
158
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200159// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
160auto const listSuffix_def =
161 *keyValueWrapper;
162
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200163auto const list_def =
164 node_identifier >> !char_('[');
165
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200166auto const absoluteStart_def =
167 x3::omit['/'] >> x3::attr(Scope::Absolute);
168
169auto const trailingSlash_def =
170 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
171
172auto const createPathSuggestions_def =
173 x3::eps;
174
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200175// I have to insert an empty vector to the first alternative, otherwise they won't have the same attribute
176auto const dataPath_def = initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(dataPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi | initializePath >> -(absoluteStart >> createPathSuggestions) >> dataNode % '/' >> (-(trailingSlash >> createPathSuggestions) >> -(completing >> rest) >> (&space_separator | x3::eoi));
177
178auto const dataNodeList_def =
179 createPathSuggestions >> -(module) >> list;
180
181// This intermediate rule is mandatory, because we need the first alternative
182// to be collapsed to a vector. If we didn't use the intermediate rule,
183// Spirit wouldn't know we want it to collapse.
184// https://github.com/boostorg/spirit/issues/408
185auto const dataNodesListEnd_def =
186 dataNode % '/' >> '/' >> dataNodeList >> -(&char_('/') >> createPathSuggestions) |
187 x3::attr(decltype(dataPath_::m_nodes)()) >> dataNodeList;
188
189auto const dataPathListEnd_def = initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(dataPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi | initializePath >> -(absoluteStart >> createPathSuggestions) >> dataNodesListEnd >> (-(trailingSlash >> createPathSuggestions) >> -(completing >> rest) >> (&space_separator | x3::eoi));
190
191auto const schemaPath_def = initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(schemaPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi | initializePath >> -(absoluteStart >> createPathSuggestions) >> schemaNode % '/' >> (-(trailingSlash >> createPathSuggestions) >> -(completing >> rest) >> (&space_separator | x3::eoi));
192
193auto const leafPath_def =
194 dataPath;
195
196auto const presenceContainerPath_def =
197 dataPath;
198
199auto const listInstancePath_def =
200 dataPath;
201
202// A "nothing" parser, which is used to indicate we tried to parse a path
203auto const initializePath_def =
204 x3::eps;
205
206
207
208#if __clang__
209#pragma GCC diagnostic pop
210#endif
211
212BOOST_SPIRIT_DEFINE(keyValue)
213BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200214BOOST_SPIRIT_DEFINE(listSuffix)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200215BOOST_SPIRIT_DEFINE(list)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200216BOOST_SPIRIT_DEFINE(dataNodeList)
217BOOST_SPIRIT_DEFINE(dataNodesListEnd)
218BOOST_SPIRIT_DEFINE(leafPath)
219BOOST_SPIRIT_DEFINE(presenceContainerPath)
220BOOST_SPIRIT_DEFINE(listInstancePath)
221BOOST_SPIRIT_DEFINE(schemaPath)
222BOOST_SPIRIT_DEFINE(dataPath)
223BOOST_SPIRIT_DEFINE(dataPathListEnd)
224BOOST_SPIRIT_DEFINE(initializePath)
225BOOST_SPIRIT_DEFINE(createKeySuggestions)
226BOOST_SPIRIT_DEFINE(createPathSuggestions)
227BOOST_SPIRIT_DEFINE(createValueSuggestions)
228BOOST_SPIRIT_DEFINE(suggestKeysEnd)
229BOOST_SPIRIT_DEFINE(absoluteStart)
230BOOST_SPIRIT_DEFINE(trailingSlash)