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
|
#pragma once
#include "yql_errors.h"
#include <library/cpp/deprecated/enum_codegen/enum_codegen.h>
#include <util/generic/ptr.h>
#include <util/generic/strbuf.h>
#include <util/generic/string.h>
#include <util/generic/vector.h>
#include <util/stream/output.h>
#include <util/stream/str.h>
#include <util/memory/pool.h>
#include <util/generic/array_ref.h>
namespace NYql {
struct TNodeFlags {
enum : ui16 {
Default = 0,
ArbitraryContent = 0x01,
BinaryContent = 0x02,
MultilineContent = 0x04,
};
static constexpr ui32 FlagsMask = 0x07; // all flags should fit here
};
struct TAstNode {
#define YQL_AST_NODE_TYPE_MAP(xx) \
xx(List, 0) \
xx(Atom, 1) \
enum EType : ui32 {
YQL_AST_NODE_TYPE_MAP(ENUM_VALUE_GEN)
};
static const ui32 SmallListCount = 2;
void PrintTo(IOutputStream& out) const;
void PrettyPrintTo(IOutputStream& out, ui32 prettyFlags) const;
inline TString ToString() const {
TStringStream str;
PrintTo(str);
return str.Str();
}
inline TString ToString(ui32 prettyFlags) const {
TStringStream str;
PrettyPrintTo(str, prettyFlags);
return str.Str();
}
inline EType GetType() const {
return Type;
}
inline bool IsAtom() const {
return Type == Atom;
}
inline bool IsList() const {
return Type == List;
}
inline bool IsListOfSize(ui32 len) const {
return Type == List && ListCount == len;
}
inline TPosition GetPosition() const {
return Position;
}
inline void SetPosition(TPosition position) {
Position = position;
}
inline TStringBuf GetContent() const {
Y_ABORT_UNLESS(IsAtom());
return TStringBuf(Data.A.Content, Data.A.Size);
}
inline void SetContent(TStringBuf newContent, TMemoryPool& pool) {
Y_ABORT_UNLESS(IsAtom());
auto poolContent = pool.AppendString(newContent);
Data.A.Content = poolContent.data();
Data.A.Size = poolContent.size();
}
inline void SetLiteralContent(TStringBuf newContent) {
Y_ABORT_UNLESS(IsAtom());
Data.A.Content = newContent.data();
Data.A.Size = newContent.size();
}
inline ui32 GetFlags() const {
Y_ABORT_UNLESS(IsAtom());
return Data.A.Flags;
}
inline void SetFlags(ui32 flags) {
Y_ABORT_UNLESS(IsAtom());
Data.A.Flags = flags;
}
inline ui32 GetChildrenCount() const {
Y_ABORT_UNLESS(IsList());
return ListCount;
}
inline const TAstNode* GetChild(ui32 index) const {
Y_ABORT_UNLESS(IsList());
Y_ABORT_UNLESS(index < ListCount);
if (ListCount <= SmallListCount) {
return Data.S.Children[index];
} else {
return Data.L.Children[index];
}
}
inline TAstNode* GetChild(ui32 index) {
Y_ABORT_UNLESS(IsList());
Y_ABORT_UNLESS(index < ListCount);
if (ListCount <= SmallListCount) {
return Data.S.Children[index];
} else {
return Data.L.Children[index];
}
}
inline TArrayRef<TAstNode* const> GetChildren() const {
Y_ABORT_UNLESS(IsList());
return {ListCount <= SmallListCount ? Data.S.Children : Data.L.Children, ListCount};
}
static inline TAstNode* NewAtom(TPosition position, TStringBuf content, TMemoryPool& pool, ui32 flags = TNodeFlags::Default) {
auto poolContent = pool.AppendString(content);
auto ret = pool.Allocate<TAstNode>();
::new(ret) TAstNode(position, poolContent, flags);
return ret;
}
// atom with non-owning content, useful for literal strings
static inline TAstNode* NewLiteralAtom(TPosition position, TStringBuf content, TMemoryPool& pool, ui32 flags = TNodeFlags::Default) {
auto ret = pool.Allocate<TAstNode>();
::new(ret) TAstNode(position, content, flags);
return ret;
}
static inline TAstNode* NewList(TPosition position, TAstNode** children, ui32 childrenCount, TMemoryPool& pool) {
TAstNode** poolChildren = nullptr;
if (childrenCount) {
if (childrenCount > SmallListCount) {
poolChildren = pool.AllocateArray<TAstNode*>(childrenCount);
memcpy(poolChildren, children, sizeof(void*) * childrenCount);
} else {
poolChildren = children;
}
for (ui32 index = 0; index < childrenCount; ++index) {
Y_ABORT_UNLESS(poolChildren[index]);
}
}
auto ret = pool.Allocate<TAstNode>();
::new(ret) TAstNode(position, poolChildren, childrenCount);
return ret;
}
template <typename... TNodes>
static inline TAstNode* NewList(TPosition position, TMemoryPool& pool, TNodes... nodes) {
TAstNode* children[] = { nodes... };
return NewList(position, children, sizeof...(nodes), pool);
}
static inline TAstNode* NewList(TPosition position, TMemoryPool& pool) {
return NewList(position, nullptr, 0, pool);
}
static TAstNode QuoteAtom;
static inline TAstNode* Quote(TPosition position, TMemoryPool& pool, TAstNode* node) {
return NewList(position, pool, &QuoteAtom, node);
}
inline ~TAstNode() {}
void Destroy() {
TString().swap(Position.File);
}
private:
inline TAstNode(TPosition position, TStringBuf content, ui32 flags)
: Position(position)
, Type(Atom)
, ListCount(0)
{
Data.A.Content = content.data();
Data.A.Size = content.size();
Data.A.Flags = flags;
}
inline TAstNode(TPosition position, TAstNode** children, ui32 childrenCount)
: Position(position)
, Type(List)
, ListCount(childrenCount)
{
if (childrenCount <= SmallListCount) {
for (ui32 index = 0; index < childrenCount; ++index) {
Data.S.Children[index] = children[index];
}
} else {
Data.L.Children = children;
}
}
TPosition Position;
const EType Type;
const ui32 ListCount;
struct TAtom {
const char* Content;
ui32 Size;
ui32 Flags;
};
struct TListType {
TAstNode** Children;
};
struct TSmallList {
TAstNode* Children[SmallListCount];
};
union {
TAtom A;
TListType L;
TSmallList S;
} Data;
};
enum class ESyntaxType {
YQLv0,
YQLv1,
Pg,
};
class IAutoParamBuilder;
class IAutoParamDataBuilder;
class IAutoParamTypeBuilder {
public:
virtual ~IAutoParamTypeBuilder() = default;
virtual void Pg(const TString& name) = 0;
virtual void BeginList() = 0;
virtual void EndList() = 0;
virtual void BeginTuple() = 0;
virtual void EndTuple() = 0;
virtual void BeforeItem() = 0;
virtual void AfterItem() = 0;
virtual IAutoParamDataBuilder& FinishType() = 0;
};
class IAutoParamDataBuilder {
public:
virtual ~IAutoParamDataBuilder() = default;
virtual void Pg(const TMaybe<TString>& value) = 0;
virtual void BeginList() = 0;
virtual void EndList() = 0;
virtual void BeginTuple() = 0;
virtual void EndTuple() = 0;
virtual void BeforeItem() = 0;
virtual void AfterItem() = 0;
virtual IAutoParamBuilder& FinishData() = 0;
};
class IAutoParamBuilder : public TThrRefBase {
public:
virtual ~IAutoParamBuilder() = default;
virtual ui32 Size() const = 0;
virtual bool Contains(const TString& name) const = 0;
virtual IAutoParamTypeBuilder& Add(const TString& name) = 0;
};
using IAutoParamBuilderPtr = TIntrusivePtr<IAutoParamBuilder>;
class IAutoParamBuilderFactory {
public:
virtual ~IAutoParamBuilderFactory() = default;
virtual IAutoParamBuilderPtr MakeBuilder() = 0;
};
struct TAstParseResult {
std::unique_ptr<TMemoryPool> Pool;
TAstNode* Root = nullptr;
TIssues Issues;
IAutoParamBuilderPtr PgAutoParamValues;
ESyntaxType ActualSyntaxType = ESyntaxType::YQLv1;
inline bool IsOk() const {
return !!Root;
}
TAstParseResult() = default;
~TAstParseResult();
TAstParseResult(const TAstParseResult&) = delete;
TAstParseResult& operator=(const TAstParseResult&) = delete;
TAstParseResult(TAstParseResult&&);
TAstParseResult& operator=(TAstParseResult&&);
void Destroy();
};
struct TStmtParseInfo {
bool KeepInCache = true;
TMaybe<TString> CommandTagName = {};
};
struct TAstPrintFlags {
enum {
Default = 0,
PerLine = 0x01,
ShortQuote = 0x02,
AdaptArbitraryContent = 0x04,
};
};
TAstParseResult ParseAst(const TStringBuf& str, TMemoryPool* externalPool = nullptr, const TString& file = {});
} // namespace NYql
template<>
void Out<NYql::TAstNode::EType>(class IOutputStream &o, NYql::TAstNode::EType x);
|