- Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathexpr_parser.h
1623 lines (1244 loc) · 36.3 KB
/
expr_parser.h
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0, as
* published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms, as
* designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* Without limiting anything contained in the foregoing, this file,
* which is part of Connector/C++, is also subject to the
* Universal FOSS Exception, version 1.0, a copy of which can be found at
* https://oss.oracle.com/licenses/universal-foss-exception.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _EXPR_PARSER_H_
#define_EXPR_PARSER_H_
#include<mysql/cdk/common.h>
#include"parser.h"
PUSH_SYS_WARNINGS_CDK
#include<vector>
#include<map>
#include<locale>
#include<algorithm>// for_each()
POP_SYS_WARNINGS_CDK
/*
Parsing strings containing expressions as used by X DevAPI.
*/
/*
List of reserved words used in DevAPI expressions.
The list is given by KEYWORD_LIST() macro where each X(KKK,SSS) entry
describes a keyword SSS with name KKK. Below enum value Keyword::KKK is
defined for each keyword declared here.
*/
#undef IN
#defineKEYWORD_LIST(X) \
X(NOT, "not") \
X(AND, "and") \
X(OR, "or") \
X(XOR, "xor") \
X(IS, "is") \
X(BETWEEN, "between") \
X(L_TRUE, "true") \
X(L_FALSE, "false") \
X(L_NULL, "null") \
X(LIKE, "like") \
X(RLIKE, "rlike") \
X(INTERVAL, "interval") \
X(REGEXP, "regexp") \
X(OVERLAPS, "overlaps")\
X(ESCAPE, "escape") \
X(HEX, "hex") \
X(BIN, "bin") \
X(MOD, "mod") \
X(AS, "as") \
X(USING, "using") \
X(ASC, "asc") \
X(DESC, "desc") \
X(CAST, "cast") \
X(CHARACTER, "character") \
X(SET, "set") \
X(CHARSET, "charset") \
X(ASCII, "ascii") \
X(UNICODE, "unicode") \
X(BYTE, "byte") \
X(BINARY, "binary") \
X(CHAR, "char") \
X(NCHAR, "nchar") \
X(DATE, "date") \
X(DATETIME, "datetime") \
X(TIME, "time") \
X(DECIMAL, "decimal") \
X(SIGNED, "signed") \
X(UNSIGNED, "unsigned") \
X(INTEGER, "integer") \
X(INT, "int") \
X(JSON, "json") \
X(IN, "in") \
X(SOUNDS, "sounds") \
X(LEADING, "leading") \
X(TRAILING, "trailing") \
X(BOTH, "both") \
X(FROM, "from") \
UNITS_LIST(X)
#defineUNITS_LIST(X) \
X(MICROSECOND, "microsecond") \
X(SECOND, "second") \
X(MINUTE, "minute") \
X(HOUR, "hour") \
X(DAY, "day") \
X(WEEK, "week") \
X(MONTH, "month") \
X(QUARTER, "quarter") \
X(YEAR, "year") \
/*
List of operators that can appear in X DevAPI expressions.
Each entry X(OOO,SSS,TTT,KKK) declares operator with name OOO, which is
reported to CDK as string SSS. TTT is a set of tokens that map to this
operator (usually just one token). KKK is a set of keywords that map to
the token. Below an enum constant Op::OOO is defined for each operator
declared here.
TODO: Find good reference for operator names recognized by xprotocol
*/
#defineOPERATOR_LIST(X) \
UNARY_OP(X) \
BINARY_OP(X)
#defineUNARY_OP(X) \
X(STAR, "*", {Token::STAR}, {}) \
X(PLUS, "+", {Token::PLUS}, {}) \
X(MINUS, "-", {Token::MINUS}, {}) \
X(NEG, "!", {Token::BANG}, {}) \
X(BITNEG, "~", {Token::TILDE}, {}) \
X(NOT, "not", {}, {Keyword::NOT}) \
#defineBINARY_OP(X) \
X(ADD, "+", {Token::PLUS}, {}) \
X(SUB, "-", {Token::MINUS}, {}) \
X(MUL, "*", {Token::STAR}, {}) \
X(DIV, "/", {Token::SLASH}, {}) \
X(MOD, "%", {Token::PERCENT}, {Keyword::MOD}) \
X(OR, "||", {Token::BAR2}, {Keyword::OR}) \
X(AND, "&&", {Token::AMPERSTAND2}, {Keyword::AND}) \
X(BITOR, "|", {Token::BAR}, {}) \
X(BITAND, "&", {Token::AMPERSTAND}, {}) \
X(BITXOR, "^", {Token::HAT}, {}) \
X(LSHIFT, "<<", {Token::LSHIFT}, {}) \
X(RSHIFT, ">>", {Token::RSHIFT}, {}) \
X(EQ, "==", ({Token::EQ, Token::EQ2}), {}) \
X(NE, "!=", ({Token::NE, Token::DF}), {}) \
X(GT, ">", {Token::GT}, {}) \
X(GE, ">=", {Token::GE}, {}) \
X(LT, "<", {Token::LT}, {}) \
X(LE, "<=", {Token::LE}, {}) \
X(IS, "is", {}, {Keyword::IS}) \
X(IS_NOT, "is_not", {}, {}) \
X(IN, "in", {}, {Keyword::IN}) \
X(NOT_IN, "not_in", {}, {}) \
X(CONT_IN, "cont_in", {}, {}) \
X(NOT_CONT_IN, "not_cont_in", {}, {}) \
X(LIKE, "like", {}, {Keyword::LIKE}) \
X(NOT_LIKE, "not_like", {}, {}) \
X(RLIKE, "regexp", {}, {Keyword::RLIKE}) \
X(NOT_RLIKE, "not_regexp", {}, {}) \
X(BETWEEN, "between", {}, {Keyword::BETWEEN}) \
X(NOT_BETWEEN, "not_between", {}, {}) \
X(REGEXP, "regexp", {}, {Keyword::REGEXP}) \
X(NOT_REGEXP, "not_regexp", {}, {}) \
X(CAST, "cast", {}, {Keyword::CAST}) \
X(SOUNDS_LIKE, "sounds like", {}, {Keyword::SOUNDS})\
X(OVERLAPS, "overlaps", {}, {Keyword::OVERLAPS}) \
X(NOT_OVERLAPS, "not_overlaps", {}, {}) \
namespace parser {
using string = std::string;
/*
Class which manages reserved words.
For a given token, it can recognize if it is a reserved word and return
the enum value identifying this keyword.
*/
classKeyword
{
public:
/*
For each reserved word with name NNN from RESERVED_LIST(),
declare enum constant Keyword::NNN.
*/
#definekw_enum(A,B) A,
enum Type {
NONE,
KEYWORD_LIST(kw_enum)
};
typedef std::set<Type> Set;
/*
Check if given token is a keyword, and if yes, return enum constant of
this keyword. If the token is not a keyword it returns NONE.
*/
static Type get(const Token &tok);
// Return canonical name of a keyword.
staticconstchar* name(Type kk)
{
#definekw_name(A,B) case Keyword::A: return #A;
switch (kk)
{
KEYWORD_LIST(kw_name)
default: returnNULL;
}
}
/*
Case insensitive string comparison function which is used to match
keywords.
TODO: First argument can be a cdk string - avoid utf8 conversion.
TODO: Simpler implementation that is not sensitive to locale settings.
*/
staticboolequal(const string &a, const string &b)
{
static kw_cmp cmp;
return (!cmp(a, b) && !cmp(b, a));
}
private:
structkw_cmp
{
structchar_cmp
{
booloperator()(char, char) const;
};
booloperator()(const std::string &a, const std::string &b) const
{
static char_cmp cmp;
returnstd::lexicographical_compare(
a.begin(), a.end(),
b.begin(), b.end(), cmp
);
}
};
/*
Map which maps words to keyword ids. Case insensitive comparison is
used to locate words in this map.
*/
typedef std::map<std::string, Type, kw_cmp> map_t;
staticmap_t kw_map;
/*
Default ctor builds the keyword map based on keyword declarations given
by KEYWORD_LIST() macro.
*/
Keyword()
{
#definekw_add(A,B) kw_map[B] = A;
KEYWORD_LIST(kw_add);
}
// This initializer instance makes sure that the keyword map is built.
static Keyword init;
};
inline
Keyword::Type Keyword::get(const Token &t)
{
// Only WORD tokens can be a keyword.
if (Token::WORD != t.get_type())
return NONE;
// Locate WORD in the keyword map.
cdk::bytes data = t.get_bytes();
auto x = kw_map.find(std::string((constchar*)data.begin(), data.size()));
return (x == kw_map.end() ? NONE : x->second);
}
inline
booloperator==(Keyword::Type type, const Token &tok)
{
return type == Keyword::get(tok);
}
#if1
inline
boolKeyword::kw_cmp::char_cmp::operator()(char a, char b) const
{
/*
Note: Explicitly using "C" locale's ctype facet to not depend on
default locale settings. This comparison needs to work correctly only
for ASCII characters. (only these characters are used in keywords).
*/
static std::locale c_loc("C");
staticconstauto &ctf
= std::use_facet<std::ctype<char>>(c_loc);
return ctf.tolower(a) < ctf.tolower(b);
}
#endif
// --------------------------------------------------------------------------
/*
Class managing operators that can appear in X DevAPI expressions.
This class can recognize if given token names a valid operator. Note that
the same token can name a unary and a binary operator.
*/
classOp
{
public:
/*
Define enum constant for each operator declared by UNARY/BINARY_OP()
macros.
*/
#defineop_enum(A,B,T,K) A,
enum Type {
NONE,
UNARY_OP(op_enum)
BINARY_START,
BINARY_OP(op_enum)
};
typedef std::set<Type> Set;
/*
Check if given token names a unary operator and if yes return this operator
id. If the token is not a unary operator returns NONE.
*/
static Type get_unary(const Token &tok);
/*
Check if given token names a binary operator and if yes return this operator
id. If the token is not a binary operator returns NONE.
*/
static Type get_binary(const Token &tok);
staticconstchar* name(Type tt)
{
#defineop_name(A,B,T,K) case Op::A: return B;
switch (tt)
{
OPERATOR_LIST(op_name)
case NONE:
case BINARY_START:
default:
returnNULL;
}
}
private:
/*
Maps used to recognize operators.
Operator can be a keyword or other token. For each kind of operator (unary
or binary) we have two maps. One map maps keyword ids to operators. The
other map maps other token types to operators. These maps are filled based
on the information given by UNARY/BINARY_OP() macros that declare
operators.
*/
typedef std::map<Token::Type, Type> tok_map_t;
typedef std::map<Keyword::Type, Type> kw_map_t;
statictok_map_t unary_tok_map;
statickw_map_t unary_kw_map;
statictok_map_t binary_tok_map;
statickw_map_t binary_kw_map;
Op()
{
#defineop_add(X, A,B,T,K) \
for(Token::Type tt : Token::Set T) \
X##_tok_map[tt] = Op::A; \
for(Keyword::Type kk : Keyword::Set K) \
X##_kw_map[kk] = Op::A;
#defineop_add_unary(A,B,T,K) op_add(unary,A,B,T,K)
#defineop_add_binary(A,B,T,K) op_add(binary,A,B,T,K)
UNARY_OP(op_add_unary)
BINARY_OP(op_add_binary)
}
static Op init;
};
inline
Op::Type Op::get_unary(const Token &tok)
{
// First check the token map.
auto find = unary_tok_map.find(tok.get_type());
if (find != unary_tok_map.end())
return find->second;
// If operator not found, try keyword map.
Keyword::Type kw = Keyword::get(tok);
if (Keyword::NONE == kw)
return NONE;
auto find1 = unary_kw_map.find(kw);
return find1 == unary_kw_map.end() ? NONE : find1->second;
}
inline
Op::Type Op::get_binary(const Token &tok)
{
auto find = binary_tok_map.find(tok.get_type());
if (find != binary_tok_map.end())
return find->second;
Keyword::Type kw = Keyword::get(tok);
if (Keyword::NONE == kw)
return NONE;
auto find1 = binary_kw_map.find(kw);
return find1 == binary_kw_map.end() ? NONE : find1->second;
}
inline
booloperator==(Op::Type tt, const Token &tok)
{
if (tt > Op::BINARY_START)
return tt == Op::get_binary(tok);
else
return tt == Op::get_unary(tok);
}
// --------------------------------------------------------------------------
/*
Specialization of Token_base which adds methods that recognize keywords,
operators and sets of these.
*/
classExpr_token_base
: public Token_base
{
public:
const Token* consume_token(Keyword::Type kk)
{
if (!cur_token_type_is(kk))
returnNULL;
returnconsume_token();
}
const Token* consume_token(const Keyword::Set &kws)
{
if (!cur_token_type_in(kws))
returnNULL;
returnconsume_token();
}
const Token* consume_token(Op::Type op)
{
if (!cur_token_type_is(op))
returnNULL;
returnconsume_token();
}
const Token* consume_token(const Op::Set &ops)
{
if (!cur_token_type_in(ops))
returnNULL;
returnconsume_token();
}
using Token_base::consume_token;
const Token& consume_token_throw(Keyword::Type kk, const string &msg)
{
const Token *t = consume_token(kk);
if (!t)
parse_error(msg);
return *t;
}
using Token_base::consume_token_throw;
boolcur_token_type_is(Keyword::Type kk)
{
const Token *t = peek_token();
return (NULL != t) && (kk == *t);
}
boolcur_token_type_is(Op::Type op)
{
const Token *t = peek_token();
return (NULL != t) && (op == *t);
}
using Token_base::cur_token_type_is;
boolcur_token_type_in(const Keyword::Set &kws)
{
const Token *t = peek_token();
if (!t)
returnfalse;
return kws.find(Keyword::get(*t)) != kws.end();
}
boolcur_token_type_in(const Op::Set &ops)
{
const Token *t = peek_token();
if (!t)
returnfalse;
Op::Type op = Op::get_binary(*t);
if (ops.find(op) != ops.end())
returntrue;
return ops.find(Op::get_unary(*t)) != ops.end();
}
using Token_base::cur_token_type_in;
friendclassExpression_parser;
};
} // parser
namespaceparser {
using cdk::scoped_ptr;
using cdk::Expression;
// ------------------------------------------------------------------------------
/*
Helper classes that are used to store column references and document
paths within the parser.
*/
structTable_ref : publiccdk::api::Table_ref
{
struct : publiccdk::api::Schema_ref
{
cdk::string m_name;
virtualconst cdk::string name() const { return m_name; }
} m_schema_ref;
cdk::string m_name;
virtualconst cdk::string name() const { return m_name; }
virtualconst cdk::api::Schema_ref* schema() const
{ return m_schema_ref.m_name.empty() ? NULL : &m_schema_ref; }
voidset(const cdk::string &name)
{ m_name = name; }
voidset(const cdk::string &name, const cdk::string &schema)
{
m_name = name;
m_schema_ref.m_name = schema;
}
voidclear()
{
m_name.clear();
m_schema_ref.m_name.clear();
}
};
structColumn_ref : publiccdk::api::Column_ref
{
Table_ref m_table_ref;
cdk::string m_col_name;
virtualconst cdk::string name() const
{ return m_col_name; }
virtualconst cdk::api::Table_ref *table() const
{
return m_table_ref.m_name.empty() ? NULL : &m_table_ref;
}
voidset_name(const cdk::string &name)
{ m_col_name = name; }
voidset(const cdk::string &name)
{
m_table_ref.clear();
set_name(name);
}
voidset(const cdk::string &name, const cdk::string &table)
{
set(name);
m_table_ref.set(table);
}
voidset(const cdk::string &name,
const cdk::string &table, const cdk::string &schema)
{
set(name);
m_table_ref.set(table, schema);
}
Column_ref& operator=(const cdk::api::Column_ref &other)
{
m_col_name = other.name();
if (!other.table())
return *this;
if (other.table()->schema())
m_table_ref.set(other.table()->name(), other.table()->schema()->name());
else
m_table_ref.set(other.table()->name());
return *this;
}
voidclear()
{
m_col_name.clear();
m_table_ref.clear();
}
};
/*
Trivial Format_info class that is used to report opaque blob values.
*/
structFormat_info : publiccdk::Format_info
{
boolfor_type(cdk::Type_info ti) const { return cdk::TYPE_BYTES == ti; }
voidget_info(cdk::Format<cdk::TYPE_BYTES>&) const {}
// bring in the rest of overloads that would be hidden otherwise
// (avoid compiler warning)
using cdk::Format_info::get_info;
};
// ------------------------------------------------------------------------------
/*
Main parser class containing parsing logic. An instance acts
as Expression, that is, parsed expression can be visited
by expression processor with process() method.
There are 2 parsing modes which determine what kind of value references
are allowed within expression. In TABLE mode expression can refer
to columns of a table, in DOCUMENT mode it can refer to document elements
specified by a document path.
*/
structParser_mode
{
enum value { DOCUMENT, TABLE};
};
classExpr_parser_base
: public Expr_parser<Expression::Processor, Expr_token_base>
{
public:
typedef Expression::Processor Processor;
typedef Expression::Scalar::Processor Scalar_prc;
typedef cdk::api::Doc_path::Processor Path_prc;
static Expression::Processor *get_base_prc(Processor *prc)
{ return prc; }
protected:
/*
TODO: Temporary hack to meet current specs that inside expression
"x IN ('a', 'b', 'c')" the strings in the list should be reported as
octets, not as strings. This will probably change but in the meantime,
to not fail the tests, the m_strings_as_blobs flag tells to report
strings as octets. This happens only in case of IN expression
(see parse_iliri() method).
*/
Parser_mode::value m_parser_mode;
bool m_strings_as_blobs;
Expr_parser_base(It &first, const It &last,
Parser_mode::value parser_mode,
bool strings_as_blobs = false)
: Expr_parser<Expression::Processor, Expr_token_base>(first, last)
, m_parser_mode(parser_mode)
, m_strings_as_blobs(strings_as_blobs)
{
return;
}
booldo_parse(Processor *prc);
enum Start { FULL, ATOMIC, MUL, ADD, SHIFT, BIT, COMP, ILRI, AND, OR,
CAST_TYPE, COLID_DOCPATH, DOC, ARR};
/*
Parse tokens using given starting point of the expression grammar.
If processor is not NULL, the expression is reported directly to the
processor and this method returns NULL.
Otherwise, if processor is NULL, the result of parsing is stored
and returned from the method. Caller of this method takes ownership
of the returned Expression object.
*/
Expression* parse(Start, Processor*);
// methods for parsing different kinds of expressions.
Expression* parse_atomic(Processor*);
Expression* parse_mul(Processor*);
Expression* parse_add(Processor*);
Expression* parse_shift(Processor*);
Expression* parse_bit(Processor*);
Expression* parse_comp(Processor*);
Expression* parse_ilri(Processor*);
Expression* parse_and(Processor*);
Expression* parse_or(Processor*);
// Additional helper parsing methods.
Expression* left_assoc_binary_op(const Op::Set&, Start, Start, Processor*);
boolparse_function_call(const cdk::api::Table_ref&, Scalar_prc*);
voidparse_argslist(Expression::List::Processor*,
bool strings_as_blobs = false);
voidparse_special_args(
const cdk::api::Table_ref&,
Expression::List::Processor*
);
boolparse_schema_ident(Token::Type (*types)[2] = NULL);
voidparse_column_ident(Processor *);
voidparse_column_ident1(Processor*);
boolget_ident(string&);
voidparse_document_field(Path_prc*, bool prefix = false);
voidparse_document_field(const string&, Path_prc*);
voidparse_document_field(const string&, const string&, Path_prc*);
boolparse_document_path(Path_prc*, bool require_dot=false);
boolparse_document_path1(Path_prc*);
boolparse_docpath_member(Path_prc*);
boolparse_docpath_member_dot(Path_prc*);
boolparse_docpath_array(Path_prc*);
boolparse_cast(Scalar_prc*);
std::string parse_cast_type();
std::string cast_data_type_dimension(bool double_dimension = false);
voidparse_doc(Processor::Doc_prc*);
voidparse_arr(Processor::List_prc*);
private:
Column_ref m_col_ref;
friendclassExpression_parser;
friendclassOrder_parser;
friendclassProjection_parser;
friendclassTable_field_parser;
friendclassDoc_field_parser;
};
classExpression_parser
: public Expression
{
Tokenizer m_tokenizer;
Parser_mode::value m_mode;
public:
Expression_parser(Parser_mode::value parser_mode, bytes expr)
: m_tokenizer(expr), m_mode(parser_mode)
{}
voidprocess(Processor &prc) const
{
It first = m_tokenizer.begin();
It last = m_tokenizer.end();
if (m_tokenizer.empty())
throwExpr_token_base::Error(first, "Expected an expression");
/*
note: passing m_toks.end() directly as constructor argument results
in "incompatible iterators" exception when comparing iterators (at
least on win, vs2010). problem with passing temporary object?
*/
Expr_parser_base parser(first, last, m_mode);
parser.process(prc);
if (first != last)
throwExpr_token_base::Error(
first,
"Unexpected characters after expression"
);
}
};
/**
@brief The Order_parser class parses "<expr> [ASC|DESC]" using
Order_expr_processor
This parser can process api::Order_expr<Expression>.
Usage:
When processing api::Order_by::Processor user needs to call first
Processor::list_begin() and then pass the processor returned by
Processor::list_el() to Order_parser for each projection.
In the end, Processor::list_end() has to be called.
*/
classOrder_parser
: public cdk::api::Order_expr<Expression>
, Token_base
{
Tokenizer m_tokenizer;
Parser_mode::value m_mode;
public:
Order_parser(Parser_mode::value parser_mode, bytes expr)
: m_tokenizer(expr), m_mode(parser_mode)
{}
voidparse(Processor &prc);
voidprocess(Processor &prc) const
{
const_cast<Order_parser*>(this)->parse(prc);
}
};
/**
@brief The Projection_parser class parses "<expr> AS <alias>"
specifications. When used in table mode the "AS <alias>" part
is optional, otherwise error is thrown if it is not present.
This parser has can process 2 processor types:
api::Projection_expr<Expression>::Processor
Expression::Document::Processor
Usage:
When processing api::Projection::Processor user needs to call first
Processor::list_begin() and then pass the processor returned by
Processor::list_el() to Projection_parser for each projection.
In the end, Processor::list_end() has to be called.
When processing Expression::Document::Processor user will call
Processor::doc_begin() and then pass the processor to
Projection_parser for each projection.
In the end user will call Processor::doc_end()
*/
classProjection_parser
: public cdk::api::Projection_expr<Expression>
, public cdk::Expression::Document
, Expr_token_base
{
typedef cdk::api::Projection_expr<Expression>::Processor Projection_processor;
typedef cdk::Expression::Document::Processor Document_processor;
Tokenizer m_tokenizer;
Parser_mode::value m_mode;
It m_it;
public:
Projection_parser(Parser_mode::value parser_mode, bytes expr)
: m_tokenizer(expr), m_mode(parser_mode)
{
m_it = m_tokenizer.begin();
set_tokens(m_it, m_tokenizer.end());
}
voidparse_tbl_mode(Projection_processor &prc);
voidparse_doc_mode(Document_processor &prc);
voidprocess(Projection_processor &prc) const
{
const_cast<Projection_parser*>(this)->parse_tbl_mode(prc);
}
voidprocess(Document_processor &prc) const
{
const_cast<Projection_parser*>(this)->parse_doc_mode(prc);
}
};
/*
This class acts as cdk::Doc_path object taking path data from a string
containing document field specification (documentField grammar)
*/
classDoc_field_parser
: public cdk::Doc_path
{
Tokenizer m_tokenizer;
cdk::scoped_ptr<Expr_parser_base> m_parser;
It m_it;
public:
Doc_field_parser(bytes doc_path)
: m_tokenizer(doc_path)
{
m_it = m_tokenizer.begin();
const It end = m_tokenizer.end();
m_parser.reset(newExpr_parser_base(m_it, end, Parser_mode::DOCUMENT));
}
voidprocess(Processor &prc) const
{
const_cast<Expr_parser_base*>(m_parser.get())->parse_document_field(&prc);