-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathast_path.cpp
More file actions
375 lines (317 loc) · 9.49 KB
/
ast_path.cpp
File metadata and controls
375 lines (317 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
* Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
*
* Written by Václav Kubernát <[email protected]>
*
*/
#include <algorithm>
#include <experimental/iterator>
#include <sstream>
#include "ast_path.hpp"
#include "utils.hpp"
container_::container_(const std::string& name)
: m_name(name)
{
}
bool container_::operator==(const container_& b) const
{
return this->m_name == b.m_name;
}
leaf_::leaf_(const std::string& name)
: m_name(name)
{
}
bool leafListElement_::operator==(const leafListElement_& b) const
{
return this->m_name == b.m_name && this->m_value == b.m_value;
}
leafList_::leafList_(const std::string& name)
: m_name(name)
{
}
bool leafList_::operator==(const leafList_& b) const
{
return this->m_name == b.m_name;
}
bool module_::operator==(const module_& b) const
{
return this->m_name == b.m_name;
}
dataNode_::dataNode_() = default;
dataNode_::dataNode_(decltype(m_suffix) node)
: m_suffix(node)
{
}
dataNode_::dataNode_(module_ module, decltype(m_suffix) node)
: m_prefix(module)
, m_suffix(node)
{
}
dataNode_::dataNode_(boost::optional<module_> module, decltype(m_suffix) node)
: m_prefix(module)
, m_suffix(node)
{
}
schemaNode_::schemaNode_(decltype(m_suffix) node)
: m_suffix(node)
{
}
schemaNode_::schemaNode_(module_ module, decltype(m_suffix) node)
: m_prefix(module)
, m_suffix(node)
{
}
schemaNode_::schemaNode_() = default;
bool schemaNode_::operator==(const schemaNode_& b) const
{
return this->m_suffix == b.m_suffix && this->m_prefix == b.m_prefix;
}
bool dataNode_::operator==(const dataNode_& b) const
{
return this->m_suffix == b.m_suffix && this->m_prefix == b.m_prefix;
}
bool leaf_::operator==(const leaf_& b) const
{
return this->m_name == b.m_name;
}
listElement_::listElement_(const std::string& listName, const ListInstance& keys)
: m_name(listName)
, m_keys(keys)
{
}
bool listElement_::operator==(const listElement_& b) const
{
return (this->m_name == b.m_name && this->m_keys == b.m_keys);
}
bool list_::operator==(const list_& b) const
{
return (this->m_name == b.m_name);
}
bool rpcNode_::operator==(const rpcNode_& other) const
{
return this->m_name == other.m_name;
}
bool actionNode_::operator==(const actionNode_& other) const
{
return this->m_name == other.m_name;
}
list_::list_(const std::string& listName)
: m_name(listName)
{
}
namespace {
template <typename T, typename U>
auto findFirstOf(const std::vector<U>& nodes)
{
return std::find_if(nodes.begin(), nodes.end(), [](const auto& e) {
return std::holds_alternative<T>(e.m_suffix);
});
}
template <typename T>
void validatePathNodes(const std::vector<T>& nodes)
{
static_assert(std::is_same<T, dataNode_>() || std::is_same<T, schemaNode_>());
if (nodes.empty()) {
// there are default ctors, so it makes sense to specify the same thing via explicit args and not fail
return;
}
if (auto firstLeaf = findFirstOf<leaf_>(nodes);
firstLeaf != nodes.end() && firstLeaf != nodes.end() - 1) {
throw std::logic_error{"Cannot put any extra nodes after a leaf"};
}
if (auto firstLeafList = findFirstOf<leafList_>(nodes);
firstLeafList != nodes.end() && firstLeafList != nodes.end() - 1) {
throw std::logic_error{"Cannot put any extra nodes after a leaf-list"};
}
if constexpr (std::is_same<T, dataNode_>()) {
if (auto firstLeafListElements = findFirstOf<leafListElement_>(nodes);
firstLeafListElements != nodes.end() && firstLeafListElements != nodes.end() - 1) {
throw std::logic_error{"Cannot put any extra nodes after a leaf-list with element specification"};
}
if (auto firstList = findFirstOf<list_>(nodes);
firstList != nodes.end() && firstList != nodes.end() - 1) {
throw std::logic_error{
"A list with no key specification can be present only as a last item in a dataPath. Did you mean to use a schemaPath?"
};
}
}
}
}
schemaPath_::schemaPath_() = default;
schemaPath_::schemaPath_(const Scope scope, const std::vector<schemaNode_>& nodes)
: m_scope(scope)
, m_nodes(nodes)
{
validatePathNodes(m_nodes);
}
bool schemaPath_::operator==(const schemaPath_& b) const
{
if (this->m_nodes.size() != b.m_nodes.size()) {
return false;
}
return this->m_nodes == b.m_nodes;
}
dataPath_::dataPath_() = default;
dataPath_::dataPath_(const Scope scope, const std::vector<dataNode_>& nodes)
: m_scope(scope)
, m_nodes(nodes)
{
validatePathNodes(m_nodes);
}
bool dataPath_::operator==(const dataPath_& b) const
{
if (this->m_nodes.size() != b.m_nodes.size()) {
return false;
}
return this->m_nodes == b.m_nodes;
}
struct nodeToSchemaStringVisitor {
std::string operator()(const nodeup_&) const
{
return "..";
}
template <class T>
std::string operator()(const T& node) const
{
return node.m_name;
}
};
std::string escapeListKeyString(const std::string& what)
{
// If we have both single and double quote, then we're screwed, but that "shouldn't happen"
// in <= YANG 1.1 due to limitations in XPath 1.0.
if (what.find('\'') != std::string::npos) {
return '\"' + what + '\"';
} else {
return '\'' + what + '\'';
}
}
struct nodeToDataStringVisitor {
std::string operator()(const listElement_& node) const
{
std::ostringstream res;
res << node.m_name + "[";
std::transform(node.m_keys.begin(), node.m_keys.end(),
std::experimental::make_ostream_joiner(res, "]["),
[] (const auto& it) { return it.first + "=" + escapeListKeyString(leafDataToString(it.second)); });
res << "]";
return res.str();
}
std::string operator()(const leafListElement_& node) const
{
return node.m_name + "[.=" + escapeListKeyString(leafDataToString(node.m_value)) + "]";
}
std::string operator()(const nodeup_&) const
{
return "..";
}
template <class T>
std::string operator()(const T& node) const
{
return node.m_name;
}
};
std::string nodeToSchemaString(decltype(dataPath_::m_nodes)::value_type node)
{
return std::visit(nodeToSchemaStringVisitor(), node.m_suffix);
}
std::string pathToDataString(const dataPath_& path, Prefixes prefixes)
{
std::string res;
if (path.m_scope == Scope::Absolute) {
res = "/";
}
for (const auto& it : path.m_nodes) {
if (it.m_prefix) {
res = joinPaths(res, it.m_prefix.value().m_name + ":" + std::visit(nodeToDataStringVisitor(), it.m_suffix));
} else {
res = joinPaths(res, (prefixes == Prefixes::Always && path.m_nodes.at(0).m_prefix ? path.m_nodes.at(0).m_prefix.value().m_name + ":" : "") + std::visit(nodeToDataStringVisitor(), it.m_suffix));
}
}
return res;
}
std::string pathToSchemaString(const schemaPath_& path, Prefixes prefixes)
{
std::string res;
if (path.m_scope == Scope::Absolute) {
res = "/";
}
for (const auto& it : path.m_nodes) {
if (it.m_prefix) {
res = joinPaths(res, it.m_prefix.value().m_name + ":" + std::visit(nodeToSchemaStringVisitor(), it.m_suffix));
} else {
res = joinPaths(res, (prefixes == Prefixes::Always && path.m_nodes.at(0).m_prefix ? path.m_nodes.at(0).m_prefix.value().m_name + ":" : "") + std::visit(nodeToSchemaStringVisitor(), it.m_suffix));
}
}
return res;
}
std::string pathToSchemaString(const dataPath_& path, Prefixes prefixes)
{
return pathToSchemaString(dataPathToSchemaPath(path), prefixes);
}
struct dataSuffixToSchemaSuffix {
using ReturnType = decltype(schemaNode_::m_suffix);
ReturnType operator()(const listElement_& listElement) const
{
return list_{listElement.m_name};
}
ReturnType operator()(const leafListElement_& leafListElement) const
{
return leafList_{leafListElement.m_name};
}
template <typename T>
ReturnType operator()(const T& suffix) const
{
return suffix;
}
};
schemaNode_ dataNodeToSchemaNode(const dataNode_& node)
{
schemaNode_ res;
res.m_prefix = node.m_prefix;
res.m_suffix = std::visit(dataSuffixToSchemaSuffix(), node.m_suffix);
return res;
}
schemaPath_ dataPathToSchemaPath(const dataPath_& path)
{
schemaPath_ res{path.m_scope, {}};
std::transform(path.m_nodes.begin(), path.m_nodes.end(),
std::back_inserter(res.m_nodes),
[](const dataNode_& node) { return dataNodeToSchemaNode(node); });
return res;
}
namespace {
template <typename NodeType>
void impl_pushFragment(std::vector<NodeType>& where, const NodeType& what)
{
if (std::holds_alternative<nodeup_>(what.m_suffix)) {
if (!where.empty()) { // Allow going up, when already at root
where.pop_back();
}
} else {
where.emplace_back(what);
}
}
}
void schemaPath_::pushFragment(const schemaNode_& fragment)
{
impl_pushFragment(m_nodes, fragment);
validatePathNodes(m_nodes);
}
void dataPath_::pushFragment(const dataNode_& fragment)
{
impl_pushFragment(m_nodes, fragment);
validatePathNodes(m_nodes);
}
dataPath_ realPath(const dataPath_& cwd, const dataPath_& newPath)
{
if (newPath.m_scope == Scope::Absolute) {
return newPath;
}
dataPath_ res = cwd;
for (const auto& it : newPath.m_nodes) {
res.pushFragment(it);
}
return res;
}