blob: 6360912ad258c8ec0b277b87772e7d7bc76ee2aa [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<dataNodeList_class, decltype(dataPath_::m_nodes)::value_type> const dataNodeList = "dataNodeList";
18x3::rule<dataNodesListEnd_class, decltype(dataPath_::m_nodes)> const dataNodesListEnd = "dataNodesListEnd";
19x3::rule<dataPathListEnd_class, dataPath_> const dataPathListEnd = "dataPathListEnd";
20x3::rule<leaf_path_class, dataPath_> const leafPath = "leafPath";
21x3::rule<presenceContainerPath_class, dataPath_> const presenceContainerPath = "presenceContainerPath";
22x3::rule<listInstancePath_class, dataPath_> const listInstancePath = "listInstancePath";
23x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath";
24x3::rule<createPathSuggestions_class, x3::unused_type> const createPathSuggestions = "createPathSuggestions";
25x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020026x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart";
27x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue";
28x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020029x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020030x3::rule<list_class, list_> const list = "list";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020031x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions";
32x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions";
33x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd";
34
Václav Kubernátdd5945a2020-05-05 01:24:23 +020035template <typename NodeType>
36struct NodeParser : x3::parser<NodeParser<NodeType>> {
37 using attribute_type = NodeType;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020038 template <typename It, typename Ctx, typename RCtx, typename Attr>
39 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
40 {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020041 std::string tableName;
42 if constexpr (std::is_same<NodeType, schemaNode_>()) {
43 tableName = "schemaNode";
44 } else {
45 tableName = "dataNode";
46 }
47 x3::symbols<NodeType> table(tableName);
Václav Kubernát60a0ed52020-04-28 15:21:33 +020048
49 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
50 parserContext.m_suggestions.clear();
51 for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) {
Václav Kubernátdd5945a2020-05-05 01:24:23 +020052 NodeType out;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020053 std::string parseString;
54 if (child.first) {
55 out.m_prefix = module_{*child.first};
56 parseString = *child.first + ":";
57 }
58 parseString += child.second;
59 switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) {
60 case yang::NodeTypes::Container:
61 case yang::NodeTypes::PresenceContainer:
62 out.m_suffix = container_{child.second};
63 parserContext.m_suggestions.emplace(Completion{parseString + "/"});
64 break;
65 case yang::NodeTypes::Leaf:
66 out.m_suffix = leaf_{child.second};
67 parserContext.m_suggestions.emplace(Completion{parseString + " "});
68 break;
69 case yang::NodeTypes::List:
Václav Kubernátdd5945a2020-05-05 01:24:23 +020070 if constexpr (std::is_same<NodeType, schemaNode_>()) {
71 out.m_suffix = list_{child.second};
72 } else {
73 out.m_suffix = listElement_{child.second, {}};
74 }
Václav Kubernát60a0ed52020-04-28 15:21:33 +020075 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
76 break;
77 case yang::NodeTypes::Action:
78 case yang::NodeTypes::AnyXml:
79 case yang::NodeTypes::LeafList:
80 case yang::NodeTypes::Notification:
81 case yang::NodeTypes::Rpc:
82 continue;
83 }
84 table.add(parseString, out);
Václav Kubernátdd5945a2020-05-05 01:24:23 +020085 table.add("..", NodeType{nodeup_{}});
Václav Kubernát60a0ed52020-04-28 15:21:33 +020086 if (!child.first) {
87 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
88 out.m_prefix = topLevelModule;
89 table.add(topLevelModule->m_name + ":" + parseString, out);
90 }
91 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +020092 parserContext.m_completionIterator = begin;
Václav Kubernát60a0ed52020-04-28 15:21:33 +020093 auto res = table.parse(begin, end, ctx, rctx, attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +020094
95 if (attr.m_prefix) {
96 parserContext.m_curModule = attr.m_prefix->m_name;
97 }
98
99 if (attr.m_suffix.type() == typeid(leaf_)) {
100 parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath();
101 ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) {
102 return boost::optional<std::string>{it.m_name};
103 }), boost::get<leaf_>(attr.m_suffix).m_name};
104 parserContext.m_tmpListKeyLeafPath.m_node = node;
105 }
106
107 if constexpr (std::is_same<NodeType, dataNode_>()) {
108 if (attr.m_suffix.type() == typeid(listElement_)) {
109 parserContext.m_tmpListName = boost::get<listElement_>(attr.m_suffix).m_name;
110 res = listSuffix.parse(begin, end, ctx, rctx, boost::get<listElement_>(attr.m_suffix).m_keys);
111 }
112 }
113
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200114 if (res) {
115 parserContext.pushPathFragment(attr);
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200116 parserContext.m_topLevelModulePresent = true;
117 }
118
119 if (attr.m_prefix) {
120 parserContext.m_curModule = boost::none;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200121 }
122 return res;
123 }
Václav Kubernátdd5945a2020-05-05 01:24:23 +0200124};
125
126NodeParser<schemaNode_> schemaNode;
127NodeParser<dataNode_> dataNode;
Václav Kubernát60a0ed52020-04-28 15:21:33 +0200128
Václav Kubernát4618fa42020-05-07 01:33:19 +0200129using AnyPath = boost::variant<schemaPath_, dataPath_>;
130
Václav Kubernát74d35a42020-05-15 15:29:16 +0200131template <typename PathType>
132struct PathParser : x3::parser<PathParser<PathType>> {
133 using attribute_type = PathType;
Václav Kubernát4618fa42020-05-07 01:33:19 +0200134 template <typename It, typename Ctx, typename RCtx, typename Attr>
135 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
136 {
Václav Kubernát74d35a42020-05-15 15:29:16 +0200137 initializePath.parse(begin, end, ctx, rctx, x3::unused);
Václav Kubernát4618fa42020-05-07 01:33:19 +0200138 dataPath_ attrData;
139
140 // absoluteStart has to be separate from the dataPath parser,
141 // otherwise, if the "dataNode % '/'" parser fails, the begin iterator
142 // gets reverted to before the starting slash.
143 auto res = -absoluteStart.parse(begin, end, ctx, rctx, attrData.m_scope);
144 auto dataPath = x3::attr(attrData.m_scope) >> dataNode % '/' >> -trailingSlash;
145 res = dataPath.parse(begin, end, ctx, rctx, attrData);
146 attr = attrData;
147
148 if constexpr (std::is_same<Attr, AnyPath>()) {
149 auto pathEnd = x3::rule<class PathEnd>{"pathEnd"} = &space_separator | x3::eoi;
150 // If parsing failed, or if there's more input we try parsing schema nodes
151 if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) {
152 // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these.
153 schemaPath_ attrSchema = dataPathToSchemaPath(attrData);
154 auto schemaPath = schemaNode % '/';
155 // The schemaPath parser continues where the dataPath parser ended.
156 res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes);
157 auto trailing = -trailingSlash >> pathEnd;
158 res = trailing.parse(begin, end, ctx, rctx, attrSchema.m_trailingSlash);
159 attr = attrSchema;
160 }
161 }
162 return res;
163 }
Václav Kubernát74d35a42020-05-15 15:29:16 +0200164};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200165
166// Need to use these wrappers so that my PathParser class gets the proper
167// attribute. Otherwise, Spirit injects the attribute of the outer parser that
168// uses my PathParser.
169// Example grammar: anyPath | module.
170// The PathParser class would get a boost::variant as the attribute, but I
171// don't want to deal with that, so I use these wrappers to ensure the
Václav Kubernát74d35a42020-05-15 15:29:16 +0200172// attribute I want (and let Spirit deal with boost::variant).
173auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<AnyPath>{};
174auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<dataPath_>{};
Václav Kubernát4618fa42020-05-07 01:33:19 +0200175
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200176#if __clang__
177#pragma GCC diagnostic push
178#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
179#endif
180
181auto const rest =
182 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
183
184auto const key_identifier_def =
185 x3::lexeme[
186 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
187 ];
188
189auto const createKeySuggestions_def =
190 x3::eps;
191
192auto const createValueSuggestions_def =
193 x3::eps;
194
195auto const suggestKeysEnd_def =
196 x3::eps;
197
198auto const keyValue_def =
199 key_identifier > '=' > createValueSuggestions > leaf_data;
200
201auto const keyValueWrapper =
202 x3::lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
203
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200204// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
205auto const listSuffix_def =
206 *keyValueWrapper;
207
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200208auto const list_def =
209 node_identifier >> !char_('[');
210
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200211auto const absoluteStart_def =
212 x3::omit['/'] >> x3::attr(Scope::Absolute);
213
214auto const trailingSlash_def =
215 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
216
217auto const createPathSuggestions_def =
218 x3::eps;
219
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200220auto const dataNodeList_def =
221 createPathSuggestions >> -(module) >> list;
222
223// This intermediate rule is mandatory, because we need the first alternative
224// to be collapsed to a vector. If we didn't use the intermediate rule,
225// Spirit wouldn't know we want it to collapse.
226// https://github.com/boostorg/spirit/issues/408
227auto const dataNodesListEnd_def =
228 dataNode % '/' >> '/' >> dataNodeList >> -(&char_('/') >> createPathSuggestions) |
229 x3::attr(decltype(dataPath_::m_nodes)()) >> dataNodeList;
230
231auto 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));
232
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200233auto const leafPath_def =
234 dataPath;
235
236auto const presenceContainerPath_def =
237 dataPath;
238
239auto const listInstancePath_def =
240 dataPath;
241
242// A "nothing" parser, which is used to indicate we tried to parse a path
243auto const initializePath_def =
244 x3::eps;
245
246
247
248#if __clang__
249#pragma GCC diagnostic pop
250#endif
251
252BOOST_SPIRIT_DEFINE(keyValue)
253BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200254BOOST_SPIRIT_DEFINE(listSuffix)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200255BOOST_SPIRIT_DEFINE(list)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200256BOOST_SPIRIT_DEFINE(dataNodeList)
257BOOST_SPIRIT_DEFINE(dataNodesListEnd)
258BOOST_SPIRIT_DEFINE(leafPath)
259BOOST_SPIRIT_DEFINE(presenceContainerPath)
260BOOST_SPIRIT_DEFINE(listInstancePath)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200261BOOST_SPIRIT_DEFINE(dataPathListEnd)
262BOOST_SPIRIT_DEFINE(initializePath)
263BOOST_SPIRIT_DEFINE(createKeySuggestions)
264BOOST_SPIRIT_DEFINE(createPathSuggestions)
265BOOST_SPIRIT_DEFINE(createValueSuggestions)
266BOOST_SPIRIT_DEFINE(suggestKeysEnd)
267BOOST_SPIRIT_DEFINE(absoluteStart)
268BOOST_SPIRIT_DEFINE(trailingSlash)