-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathyaml-path-internals.h
More file actions
239 lines (197 loc) · 9.16 KB
/
yaml-path-internals.h
File metadata and controls
239 lines (197 loc) · 9.16 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
/*
MIT License
Copyright(c) 2019 Peter Hauptmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
/* This exposes yaml-path details to make them accessible to tests.
*/
#include "yaml-path.h"
#include <optional>
#include <sstream>
#include <variant>
#include <vector>
namespace YAML
{
namespace YamlPathDetail
{
/// \internal basic parser helper: removes \c offset chars from \c path, and returns the removed chars
PathArg SplitAt(PathArg & path, size_t offset);
/// \internal basic parser helper: removes chars from \c path until \c pred is false, and returns the removed chars
template <typename TPred>
PathArg Split(PathArg & path, TPred pred);
/// \internal returns an integer in which the bit positions specified by \c values are set, \sa BitsContain
template <typename TBits = uint64_t, typename T>
constexpr uint64_t BitsOf(std::initializer_list<T> values);
/// \internal checks if the bit at position \v is set in \c bits
template<typename TBits, typename TValue>
bool constexpr BitsContain(TBits bits, TValue v);
/// \internal Tokens supported by the token level parser of \ref PathScanner
enum class EToken
{
Invalid = -1,
None = 0,
QuotedIdentifier,
UnquotedIdentifier,
OpenBracket,
CloseBracket,
Period,
Equal,
Index, // translated from UnquotedIdentifier in NextSelectorToken
FetchArg,
OpenBrace,
CloseBrace,
Exclamation,
Caret,
Asterisk,
Tilde,
Comma,
};
/* when adding a new token, also add to:
- MapETokenName
- ValidTokensAtStart, if applicable
- TokenData, if a new data type is required
- PathScanner::NextToken, to recognize it
- PathScanner::NextSelector, to process it
*/
/// \internal data for one token, see \ref PathScanner
struct TokenData
{
EToken id = EToken::None;
PathArg value;
size_t index = 0;
};
/// \internal Selectors supported by the selector level parser of \ref PathScanner
enum class ESelector
{
Invalid = -1,
None = 0,
Key,
Index,
MapFilter,
};
// Data for different selector types
struct ArgNull {};
struct ArgKey { PathArg key; };
struct ArgIndex { size_t index; };
struct ArgKVPair { KVToken key; KVToken value; EKVOp op = EKVOp::Equal; };
using ArgMapFilter = std::vector<ArgKVPair>;
/** \internal progressive scanner/parser for a YAML path as specified by YAML::Select
This class implements two layers of the scan:
The <i>token level scanner</i>, retrieves \ref EToken "tokens" from the path,until nothing is left.
\ref NextToken retrieves the next token, \ref Token gives access to the last retrieved one.
The <i>selector level scanner</i> uses the token scanner to retrieve \ref ESelector "selectors" the same way.
\ref NextSelector calls \c NextToken until it has retrieved a complete selector to return.
\ref Selector returns the type of the last retrieved one, and \ref SelectorData retrieves a union that contains
the data for the last retrieved selector.
(The separation wasn't as clear when I started hacking at this, separating them now would be rather easy, but with marginal benefit,
especially since this is not a public interface.)
*/
class PathScanner
{
public:
using tSelectorData = std::variant<ArgNull, ArgKey, ArgIndex, ArgMapFilter>; ///< union of the selector data for all selector types
private:
PathArg m_rpath; // remainder of path to be scanned
PathBoundArgs m_args; // list of arguments that should be used as tokens
size_t m_argIdx; // next argument index to fetch
TokenData m_curToken;
ESelector m_selector = ESelector::None;
tSelectorData m_selectorData;
bool m_tokenPending = false; ///< m_curToken should be processed before reading a new one
bool m_periodAllowed = false; ///< next token may be a period
bool m_selectorRequired = false; ///< another selector is required for a well-formed path (e.g. after "abc.")
EPathError m_error = EPathError::OK;
PathArg m_fullPath;
PathException * m_diags = nullptr;
TokenData const & SetToken(EToken id, PathArg p);
TokenData const & SetToken(EToken id, size_t index);
template<typename TArg>
ESelector SetSelector(ESelector selector, TArg arg)
{
m_selector = selector;
m_selectorData = std::move(arg);
return m_selector;
}
void SkipWS();
bool NextSelectorToken(uint64_t validTokens, EPathError error = EPathError::InvalidToken);
bool PeekSelectorToken(uint64_t validTokens);
bool ReadKVToken(KVToken & result, uint64_t endTokens);
public:
PathScanner(PathArg p, PathBoundArgs args = {}, PathException * diags = nullptr);
explicit operator bool() const { return !m_rpath.empty() && m_error == EPathError::OK; }
auto Error() const { return m_error; }
auto const & Right() const { return m_rpath; } ///< remainder (unscanned part)
size_t ScanOffset() const { return m_fullPath.length() - m_rpath.length(); } ///< token scanner position
// -----token-level scanner
TokenData const & NextToken();
TokenData const & Token() const { return m_curToken; }
// -----selector-level scanner
ESelector NextSelector();
ESelector Selector() const { return m_selector; }
auto const & SelectorDataV() const { return m_selectorData; }
template <typename T>
T const & SelectorData() const { return std::get<T>(m_selectorData); }
// for access by utility functions to record an error
EPathError SetError(EPathError error, uint64_t validTypes = 0);
inline static const uint64_t ValidTokensAtStart = BitsOf({ EToken::FetchArg, EToken::None, EToken::OpenBracket, EToken::OpenBrace, EToken::QuotedIdentifier, EToken::UnquotedIdentifier });
};
template <typename T2, typename TEnum>
T2 MapValue(TEnum value, std::initializer_list<std::pair<TEnum, T2>> values, T2 dflt = T2());
extern std::initializer_list<std::pair<EToken, char const *>> MapETokenName;
extern std::initializer_list<std::pair<NodeType::value, char const *>> MapNodeTypeName;
extern std::initializer_list<std::pair<ESelector, char const *>> MapESelectorName;
extern std::initializer_list<std::pair<EPathError, char const *>> MapEPathErrorName;
}
}
// ----- Implementation
namespace YAML
{
namespace YamlPathDetail
{
template <typename TPred>
PathArg Split(PathArg & path, TPred pred)
{
size_t offset = 0;
while (offset < path.size() && pred(path[offset]))
++offset;
return SplitAt(path, offset);
}
template <typename TBits, typename T>
constexpr uint64_t BitsOf(std::initializer_list<T> values)
{
int64_t bits = 0;
for (auto v : values)
bits |= TBits(1) << TBits(v);
return bits;
}
template<typename TBits, typename TValue>
bool constexpr BitsContain(TBits bits, TValue v)
{
return ((TBits(1) << TBits(v)) & bits) != 0;
}
/// \internal helper to map enum values to names, used for diagnostics
template <typename T2, typename TEnum>
T2 MapValue(TEnum value, std::initializer_list<std::pair<TEnum, T2>> values, T2 dflt)
{
for (auto && p : values)
if (p.first == value)
return p.second;
return dflt;
}
}
}