aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/library/yql/minikql/jsonpath/ast_nodes.h
blob: 7f6adb0904ed434f65be45b88fae450fd15ec77f (plain) (blame)
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#pragma once

#include <ydb/library/yql/public/issue/yql_issue.h>

#include <library/cpp/json/json_value.h> 
#include <library/cpp/regex/hyperscan/hyperscan.h>

namespace NYql::NJsonPath {

class TRootNode;
class TContextObjectNode;
class TVariableNode;
class TLastArrayIndexNode;
class TNumberLiteralNode;
class TAccessorExprNode;
class TMemberAccessNode;
class TWildcardMemberAccessNode;
class TArrayAccessNode;
class TWildcardArrayAccessNode;
class TUnaryOperationNode;
class TBinaryOperationNode;
class TBooleanLiteralNode;
class TNullLiteralNode;
class TStringLiteralNode;
class TFilterObjectNode;
class TFilterPredicateNode;
class TMethodCallNode;
class TStartsWithPredicateNode;
class TExistsPredicateNode;
class TIsUnknownPredicateNode;
class TLikeRegexPredicateNode;

enum class EJsonPathMode {
    Lax = 0,
    Strict = 1,
};

class IAstNodeVisitor {
public:
    virtual void VisitRoot(const TRootNode& node) = 0;
    virtual void VisitContextObject(const TContextObjectNode& node) = 0;
    virtual void VisitVariable(const TVariableNode& node) = 0;
    virtual void VisitLastArrayIndex(const TLastArrayIndexNode& node) = 0;
    virtual void VisitNumberLiteral(const TNumberLiteralNode& node) = 0;
    virtual void VisitMemberAccess(const TMemberAccessNode& node) = 0;
    virtual void VisitWildcardMemberAccess(const TWildcardMemberAccessNode& node) = 0;
    virtual void VisitArrayAccess(const TArrayAccessNode& node) = 0;
    virtual void VisitWildcardArrayAccess(const TWildcardArrayAccessNode& node) = 0;
    virtual void VisitUnaryOperation(const TUnaryOperationNode& node) = 0;
    virtual void VisitBinaryOperation(const TBinaryOperationNode& node) = 0;
    virtual void VisitBooleanLiteral(const TBooleanLiteralNode& node) = 0;
    virtual void VisitNullLiteral(const TNullLiteralNode& node) = 0;
    virtual void VisitStringLiteral(const TStringLiteralNode& node) = 0;
    virtual void VisitFilterObject(const TFilterObjectNode& node) = 0;
    virtual void VisitFilterPredicate(const TFilterPredicateNode& node) = 0;
    virtual void VisitMethodCall(const TMethodCallNode& node) = 0;
    virtual void VisitStartsWithPredicate(const TStartsWithPredicateNode& node) = 0;
    virtual void VisitExistsPredicate(const TExistsPredicateNode& node) = 0;
    virtual void VisitIsUnknownPredicate(const TIsUnknownPredicateNode& node) = 0;
    virtual void VisitLikeRegexPredicate(const TLikeRegexPredicateNode& node) = 0;

    virtual ~IAstNodeVisitor() = default;
};

enum class EReturnType {
    Any = 0,
    Bool = 1,
};

class TAstNode : public TSimpleRefCount<TAstNode> {
public:
    explicit TAstNode(TPosition pos);

    TPosition GetPos() const;

    virtual void Accept(IAstNodeVisitor& visitor) const = 0;

    virtual EReturnType GetReturnType() const;

    virtual ~TAstNode() = default;

private:
    TPosition Pos;
};

using TAstNodePtr = TIntrusivePtr<TAstNode>;

class TRootNode : public TAstNode {
public:
    TRootNode(TPosition pos, TAstNodePtr expr, EJsonPathMode mode);

    const TAstNodePtr GetExpr() const;

    EJsonPathMode GetMode() const;

    void Accept(IAstNodeVisitor& visitor) const override;

    EReturnType GetReturnType() const override;

private:
    TAstNodePtr Expr;
    EJsonPathMode Mode;
};

class TContextObjectNode : public TAstNode {
public:
    explicit TContextObjectNode(TPosition pos);

    void Accept(IAstNodeVisitor& visitor) const override;
};

class TVariableNode : public TAstNode {
public:
    TVariableNode(TPosition pos, const TString& name);

    const TString& GetName() const;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    TString Name;
};

class TLastArrayIndexNode : public TAstNode {
public:
    explicit TLastArrayIndexNode(TPosition pos);

    void Accept(IAstNodeVisitor& visitor) const override;
};

class TNumberLiteralNode : public TAstNode {
public:
    TNumberLiteralNode(TPosition pos, double value);

    double GetValue() const;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    double Value;
};

class TMemberAccessNode : public TAstNode {
public:
    TMemberAccessNode(TPosition pos, const TString& member, TAstNodePtr input);

    const TStringBuf GetMember() const;

    const TAstNodePtr GetInput() const;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    TString Member;
    TAstNodePtr Input;
};

class TWildcardMemberAccessNode : public TAstNode {
public:
    TWildcardMemberAccessNode(TPosition pos, TAstNodePtr input);

    const TAstNodePtr GetInput() const;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    TAstNodePtr Input;
};

class TArrayAccessNode : public TAstNode {
public:
    struct TSubscript {
        TAstNodePtr From;
        TAstNodePtr To;
    };

    TArrayAccessNode(TPosition pos, TVector<TSubscript> subscripts, TAstNodePtr input);

    const TVector<TSubscript>& GetSubscripts() const;

    const TAstNodePtr GetInput() const;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    TVector<TSubscript> Subscripts;
    TAstNodePtr Input;
};

class TWildcardArrayAccessNode : public TAstNode {
public:
    TWildcardArrayAccessNode(TPosition pos, TAstNodePtr input);

    const TAstNodePtr GetInput() const;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    TAstNodePtr Input;
};

enum class EUnaryOperation {
    Plus = 0,
    Minus = 1,
    Not = 2,
};

class TUnaryOperationNode : public TAstNode {
public:
    TUnaryOperationNode(TPosition pos, EUnaryOperation op, TAstNodePtr expr);

    EUnaryOperation GetOp() const;

    const TAstNodePtr GetExpr() const;

    void Accept(IAstNodeVisitor& visitor) const override;

    EReturnType GetReturnType() const override;

private:
    EUnaryOperation Operation;
    TAstNodePtr Expr;
};

enum class EBinaryOperation {
    Add = 0,
    Substract = 1,
    Multiply = 2,
    Divide = 3,
    Modulo = 4,
    Less = 5,
    LessEqual = 6,
    Greater = 7,
    GreaterEqual = 8,
    Equal = 9,
    NotEqual = 10,
    And = 11,
    Or = 12,
};

class TBinaryOperationNode : public TAstNode {
public:
    TBinaryOperationNode(TPosition pos, EBinaryOperation op, TAstNodePtr leftExpr, TAstNodePtr rightExpr);

    EBinaryOperation GetOp() const;

    const TAstNodePtr GetLeftExpr() const;

    const TAstNodePtr GetRightExpr() const;

    void Accept(IAstNodeVisitor& visitor) const override;

    EReturnType GetReturnType() const override;

private:
    EBinaryOperation Operation;
    TAstNodePtr LeftExpr;
    TAstNodePtr RightExpr;
};

class TBooleanLiteralNode : public TAstNode {
public:
    TBooleanLiteralNode(TPosition pos, bool value);

    bool GetValue() const;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    bool Value;
};

class TNullLiteralNode : public TAstNode {
public:
    explicit TNullLiteralNode(TPosition pos);

    void Accept(IAstNodeVisitor& visitor) const override;
};

class TStringLiteralNode : public TAstNode {
public:
    TStringLiteralNode(TPosition pos, const TString& value);

    const TString& GetValue() const;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    TString Value;
};

class TFilterObjectNode : public TAstNode {
public:
    explicit TFilterObjectNode(TPosition pos);

    void Accept(IAstNodeVisitor& visitor) const override;
};

class TFilterPredicateNode : public TAstNode {
public:
    TFilterPredicateNode(TPosition pos, TAstNodePtr predicate, TAstNodePtr input);

    const TAstNodePtr GetPredicate() const;

    const TAstNodePtr GetInput() const;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    TAstNodePtr Predicate;
    TAstNodePtr Input;
};

enum class EMethodType {
    Abs = 0,
    Floor = 1,
    Ceiling = 2,
    Double = 3,
    Type = 4,
    Size = 5,
    KeyValue = 6,
};

class TMethodCallNode : public TAstNode {
public:
    TMethodCallNode(TPosition pos, EMethodType type, TAstNodePtr input);

    EMethodType GetType() const;

    const TAstNodePtr GetInput() const;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    EMethodType Type;
    TAstNodePtr Input;
};

class TStartsWithPredicateNode : public TAstNode {
public:
    TStartsWithPredicateNode(TPosition pos, TAstNodePtr input, TAstNodePtr prefix);

    const TAstNodePtr GetInput() const;

    const TAstNodePtr GetPrefix() const;

    EReturnType GetReturnType() const override;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    TAstNodePtr Input;
    TAstNodePtr Prefix;
};

class TExistsPredicateNode : public TAstNode {
public:
    TExistsPredicateNode(TPosition pos, TAstNodePtr input);

    const TAstNodePtr GetInput() const;

    EReturnType GetReturnType() const override;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    TAstNodePtr Input;
};

class TIsUnknownPredicateNode : public TAstNode {
public:
    TIsUnknownPredicateNode(TPosition pos, TAstNodePtr input);

    const TAstNodePtr GetInput() const;

    EReturnType GetReturnType() const override;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    TAstNodePtr Input;
};

class TLikeRegexPredicateNode : public TAstNode {
public:
    TLikeRegexPredicateNode(TPosition pos, TAstNodePtr input, NHyperscan::TDatabase&& regex);

    const TAstNodePtr GetInput() const;

    const NHyperscan::TDatabase& GetRegex() const;

    EReturnType GetReturnType() const override;

    void Accept(IAstNodeVisitor& visitor) const override;

private:
    TAstNodePtr Input;
    NHyperscan::TDatabase Regex;
};

}