blob: e2af35e2f682d5d8761d27c0690e2045dfcc9ed9 [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";
28x3::rule<dataNode_class, dataNode_> const dataNode = "dataNode";
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020029x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart";
30x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue";
31x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier";
32x3::rule<listPrefix_class, std::string> const listPrefix = "listPrefix";
33x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix";
34x3::rule<listElement_class, listElement_> const listElement = "listElement";
35x3::rule<list_class, list_> const list = "list";
36x3::rule<nodeup_class, nodeup_> const nodeup = "nodeup";
37x3::rule<container_class, container_> const container = "container";
38x3::rule<leaf_class, leaf_> const leaf = "leaf";
39x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions";
40x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions";
41x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd";
42
43
Václav Kubernát60a0ed52020-04-28 15:21:33 +020044struct schemaNode : x3::parser<schemaNode> {
45 using attribute_type = schemaNode_;
46 template <typename It, typename Ctx, typename RCtx, typename Attr>
47 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const
48 {
49 x3::symbols<schemaNode_> table(std::string{"schemaNode"}); // The constructor doesn't work with just the string literal
50
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)) {
54 schemaNode_ out;
55 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:
72 out.m_suffix = list_{child.second};
73 parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch});
74 break;
75 case yang::NodeTypes::Action:
76 case yang::NodeTypes::AnyXml:
77 case yang::NodeTypes::LeafList:
78 case yang::NodeTypes::Notification:
79 case yang::NodeTypes::Rpc:
80 continue;
81 }
82 table.add(parseString, out);
83 table.add("..", attribute_type{nodeup_{}});
84 if (!child.first) {
85 auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix;
86 out.m_prefix = topLevelModule;
87 table.add(topLevelModule->m_name + ":" + parseString, out);
88 }
89 }
90 auto res = table.parse(begin, end, ctx, rctx, attr);
91 if (res) {
92 parserContext.pushPathFragment(attr);
93 }
94 return res;
95 }
96} schemaNode;
97
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020098#if __clang__
99#pragma GCC diagnostic push
100#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
101#endif
102
103auto const rest =
104 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
105
106auto const key_identifier_def =
107 x3::lexeme[
108 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
109 ];
110
111auto const createKeySuggestions_def =
112 x3::eps;
113
114auto const createValueSuggestions_def =
115 x3::eps;
116
117auto const suggestKeysEnd_def =
118 x3::eps;
119
120auto const keyValue_def =
121 key_identifier > '=' > createValueSuggestions > leaf_data;
122
123auto const keyValueWrapper =
124 x3::lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
125
126auto const listPrefix_def =
127 node_identifier;
128
129// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
130auto const listSuffix_def =
131 *keyValueWrapper;
132
133auto const listElement_def =
134 listPrefix >> &char_('[') > listSuffix;
135
136auto const list_def =
137 node_identifier >> !char_('[');
138
139auto const nodeup_def =
140 x3::lit("..") > x3::attr(nodeup_());
141
142auto const container_def =
143 node_identifier;
144
145auto const leaf_def =
146 node_identifier;
147
148auto const absoluteStart_def =
149 x3::omit['/'] >> x3::attr(Scope::Absolute);
150
151auto const trailingSlash_def =
152 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
153
154auto const createPathSuggestions_def =
155 x3::eps;
156
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200157auto const dataNode_def =
158 createPathSuggestions >> -(module) >> (container | listElement | nodeup | leaf);
159
160// I have to insert an empty vector to the first alternative, otherwise they won't have the same attribute
161auto 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));
162
163auto const dataNodeList_def =
164 createPathSuggestions >> -(module) >> list;
165
166// This intermediate rule is mandatory, because we need the first alternative
167// to be collapsed to a vector. If we didn't use the intermediate rule,
168// Spirit wouldn't know we want it to collapse.
169// https://github.com/boostorg/spirit/issues/408
170auto const dataNodesListEnd_def =
171 dataNode % '/' >> '/' >> dataNodeList >> -(&char_('/') >> createPathSuggestions) |
172 x3::attr(decltype(dataPath_::m_nodes)()) >> dataNodeList;
173
174auto 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));
175
176auto 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));
177
178auto const leafPath_def =
179 dataPath;
180
181auto const presenceContainerPath_def =
182 dataPath;
183
184auto const listInstancePath_def =
185 dataPath;
186
187// A "nothing" parser, which is used to indicate we tried to parse a path
188auto const initializePath_def =
189 x3::eps;
190
191
192
193#if __clang__
194#pragma GCC diagnostic pop
195#endif
196
197BOOST_SPIRIT_DEFINE(keyValue)
198BOOST_SPIRIT_DEFINE(key_identifier)
199BOOST_SPIRIT_DEFINE(listPrefix)
200BOOST_SPIRIT_DEFINE(listSuffix)
201BOOST_SPIRIT_DEFINE(listElement)
202BOOST_SPIRIT_DEFINE(list)
203BOOST_SPIRIT_DEFINE(nodeup)
Václav Kubernátd0ea9b22020-04-24 00:44:15 +0200204BOOST_SPIRIT_DEFINE(dataNode)
205BOOST_SPIRIT_DEFINE(container)
206BOOST_SPIRIT_DEFINE(leaf)
207BOOST_SPIRIT_DEFINE(dataNodeList)
208BOOST_SPIRIT_DEFINE(dataNodesListEnd)
209BOOST_SPIRIT_DEFINE(leafPath)
210BOOST_SPIRIT_DEFINE(presenceContainerPath)
211BOOST_SPIRIT_DEFINE(listInstancePath)
212BOOST_SPIRIT_DEFINE(schemaPath)
213BOOST_SPIRIT_DEFINE(dataPath)
214BOOST_SPIRIT_DEFINE(dataPathListEnd)
215BOOST_SPIRIT_DEFINE(initializePath)
216BOOST_SPIRIT_DEFINE(createKeySuggestions)
217BOOST_SPIRIT_DEFINE(createPathSuggestions)
218BOOST_SPIRIT_DEFINE(createValueSuggestions)
219BOOST_SPIRIT_DEFINE(suggestKeysEnd)
220BOOST_SPIRIT_DEFINE(absoluteStart)
221BOOST_SPIRIT_DEFINE(trailingSlash)