Repository navigation
Expand file tree
/
Copy pathclass-wp-mysql-lexer.php
More file actions
1548 lines (1443 loc) · 68.1 KB
/
Copy pathclass-wp-mysql-lexer.php
File metadata and controls
1548 lines (1443 loc) · 68.1 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
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
<?php
/**
* MySQL lexer.
*
* An exhaustive lexer for the MySQL SQL dialect. It scans the provided SQL
* input payload and generates MySQL token objects in the vocabulary of the
* official MySQL grammar: token ids are the grammar's own token numbers, and
* the keyword table is generated from MySQL's keyword list (sql/lex.h) — see
* the generated token data at the top of this class.
*
* The lexer is implemented with zero dependencies, it doesn't require any PHP
* extensions, and it doesn't use PCRE or any other regular expression engines.
*
* The token stream matches the one MySQL's own server lexer feeds to its Bison
* grammar: the "@" sign is a standalone terminal followed by its name, "WITH
* ROLLUP" is contracted into the internal WITH_ROLLUP_SYM terminal, and the
* input is terminated with END_OF_INPUT and Bison's end marker. These are
* produced directly during scanning, as MySQL's lexer does (sql/sql_lex.cc),
* not by a post-processing pass.
*
* The byte-level scanning structure (identifiers, numbers, quoted text,
* operators) follows the MySQL Workbench lexer. See:
* https://github.com/mysql/mysql-workbench/blob/8.0.38/library/parsers/grammars/MySQLLexer.g4
* https://github.com/mysql/mysql-workbench/blob/8.0.38/library/parsers/mysql/MySQLBaseLexer.cpp
*/
class WP_MySQL_Lexer {
/**
* Auto-generated MySQL grammar tokens, keywords, and functions.
*
* The auto-generated block below defines MySQL grammar symbols extracted
* from the official MySQL grammar by the "composer run build-grammar" command.
* It is important that these symbols are included in the lexer class itself,
* so that PHP can inline the token constants for optimal performance.
*/
// BEGIN: AUTO-GENERATED MYSQL GRAMMAR SYMBOLS
// This block is auto-generated from MySQL mysql-8.4.10. DO NOT EDIT!
// phpcs:disable
const LEFT_PAREN = 40;
const RIGHT_PAREN = 41;
const LEFT_BRACE = 123;
const RIGHT_BRACE = 125;
const COMMA = 44;
const DOT = 46;
const COLON = 58;
const SEMICOLON = 59;
const AT_SIGN = 64;
const PLUS = 43;
const MINUS = 45;
const ASTERISK = 42;
const SLASH = 47;
const PERCENT = 37;
const AMPERSAND = 38;
const PIPE = 124;
const CARET = 94;
const TILDE = 126;
const EXCLAMATION = 33;
const EQ = 415;
const EQUAL_SYM = 416;
const NE = 614;
const GE = 456;
const GT_SYM = 469;
const LE = 522;
const LT = 549;
const SET_VAR = 757;
const AND_AND_SYM = 273;
const OR2_SYM = 642;
const OR_OR_SYM = 644;
const SHIFT_LEFT = 759;
const SHIFT_RIGHT = 760;
const JSON_SEPARATOR_SYM = 514;
const JSON_UNQUOTED_SEPARATOR_SYM = 907;
const PARAM_MARKER = 652;
const IDENT = 482;
const IDENT_QUOTED = 484;
const TEXT_STRING = 827;
const NCHAR_STRING = 611;
const DOLLAR_QUOTED_STRING_SYM = 1204;
const UNDERSCORE_CHARSET = 852;
const BIN_NUM = 292;
const HEX_NUM = 474;
const NUM = 628;
const LONG_NUM = 545;
const ULONGLONG_NUM = 849;
const DECIMAL_NUM = 377;
const FLOAT_NUM = 443;
const NULL_SYM = 627;
const NOT_SYM = 622;
const NOT2_SYM = 621;
const WITH = 892;
const ROLLUP_SYM = 734;
const WITH_ROLLUP_SYM = 894;
const END_OF_INPUT = 411;
const YYEOF = 0;
const KEYWORDS = array( '!=' => 614, '&&' => 273, '<' => 549, '<<' => 759, '<=' => 522, '<=>' => 416, '<>' => 614, '=' => 415, '>' => 469, '>=' => 456, '>>' => 760, 'ACCESSIBLE' => 259, 'ACCOUNT' => 260, 'ACTION' => 261, 'ACTIVE' => 973, 'ADD' => 262, 'ADDDATE' => 263, 'ADMIN' => 910, 'AFTER' => 264, 'AGAINST' => 265, 'AGGREGATE' => 266, 'ALGORITHM' => 267, 'ALL' => 268, 'ALTER' => 269, 'ALWAYS' => 270, 'ANALYZE' => 272, 'AND' => 274, 'ANY' => 275, 'ARRAY' => 976, 'AS' => 276, 'ASC' => 277, 'ASCII' => 278, 'ASENSITIVE' => 279, 'ASSIGN_GTIDS_TO_ANONYMOUS_TRANSACTIONS' => 1161, 'AT' => 280, 'ATTRIBUTE' => 1153, 'AUTHENTICATION' => 1191, 'AUTO' => 1211, 'AUTOEXTEND_SIZE' => 281, 'AUTO_INCREMENT' => 282, 'AVG' => 284, 'AVG_ROW_LENGTH' => 283, 'BACKUP' => 285, 'BEFORE' => 286, 'BEGIN' => 287, 'BERNOULLI' => 1213, 'BETWEEN' => 288, 'BIGINT' => 289, 'BINARY' => 290, 'BINLOG' => 291, 'BIT' => 295, 'BIT_AND' => 293, 'BIT_OR' => 294, 'BIT_XOR' => 296, 'BLOB' => 297, 'BLOCK' => 298, 'BOOL' => 300, 'BOOLEAN' => 299, 'BOTH' => 301, 'BTREE' => 302, 'BUCKETS' => 929, 'BULK' => 1201, 'BY' => 303, 'BYTE' => 304, 'CACHE' => 305, 'CALL' => 306, 'CASCADE' => 307, 'CASCADED' => 308, 'CASE' => 309, 'CAST' => 310, 'CATALOG_NAME' => 311, 'CHAIN' => 312, 'CHALLENGE_RESPONSE' => 1198, 'CHANGE' => 313, 'CHANGED' => 314, 'CHANNEL' => 315, 'CHAR' => 317, 'CHARACTER' => 317, 'CHARSET' => 316, 'CHECK' => 319, 'CHECKSUM' => 318, 'CIPHER' => 320, 'CLASS_ORIGIN' => 321, 'CLIENT' => 322, 'CLONE' => 931, 'CLOSE' => 323, 'COALESCE' => 324, 'CODE' => 325, 'COLLATE' => 326, 'COLLATION' => 327, 'COLUMN' => 329, 'COLUMNS' => 328, 'COLUMN_FORMAT' => 330, 'COLUMN_NAME' => 331, 'COMMENT' => 332, 'COMMIT' => 334, 'COMMITTED' => 333, 'COMPACT' => 335, 'COMPLETION' => 336, 'COMPONENT' => 914, 'COMPRESSED' => 337, 'COMPRESSION' => 338, 'CONCURRENT' => 340, 'CONDITION' => 341, 'CONNECTION' => 342, 'CONSISTENT' => 343, 'CONSTRAINT' => 344, 'CONSTRAINT_CATALOG' => 345, 'CONSTRAINT_NAME' => 346, 'CONSTRAINT_SCHEMA' => 347, 'CONTAINS' => 348, 'CONTEXT' => 349, 'CONTINUE' => 350, 'CONVERT' => 351, 'COUNT' => 352, 'CPU' => 353, 'CREATE' => 354, 'CROSS' => 355, 'CUBE' => 356, 'CUME_DIST' => 932, 'CURDATE' => 357, 'CURRENT' => 358, 'CURRENT_DATE' => 357, 'CURRENT_TIME' => 362, 'CURRENT_TIMESTAMP' => 623, 'CURRENT_USER' => 359, 'CURSOR' => 360, 'CURSOR_NAME' => 361, 'CURTIME' => 362, 'DATA' => 366, 'DATABASE' => 363, 'DATABASES' => 364, 'DATAFILE' => 365, 'DATE' => 370, 'DATETIME' => 367, 'DATE_ADD' => 368, 'DATE_SUB' => 369, 'DAY' => 375, 'DAY_HOUR' => 371, 'DAY_MICROSECOND' => 372, 'DAY_MINUTE' => 373, 'DAY_SECOND' => 374, 'DEALLOCATE' => 376, 'DEC' => 378, 'DECIMAL' => 378, 'DECLARE' => 379, 'DEFAULT' => 380, 'DEFAULT_AUTH' => 381, 'DEFINER' => 382, 'DEFINITION' => 969, 'DELAYED' => 383, 'DELAY_KEY_WRITE' => 384, 'DELETE' => 385, 'DENSE_RANK' => 933, 'DESC' => 386, 'DESCRIBE' => 387, 'DESCRIPTION' => 970, 'DETERMINISTIC' => 389, 'DIAGNOSTICS' => 390, 'DIRECTORY' => 391, 'DISABLE' => 392, 'DISCARD' => 393, 'DISK' => 394, 'DISTINCT' => 395, 'DISTINCTROW' => 395, 'DIV' => 396, 'DO' => 398, 'DOUBLE' => 397, 'DROP' => 399, 'DUAL' => 400, 'DUMPFILE' => 401, 'DUPLICATE' => 402, 'DYNAMIC' => 403, 'EACH' => 404, 'ELSE' => 405, 'ELSEIF' => 406, 'EMPTY' => 954, 'ENABLE' => 407, 'ENCLOSED' => 408, 'ENCRYPTION' => 339, 'END' => 409, 'ENDS' => 410, 'ENFORCED' => 985, 'ENGINE' => 413, 'ENGINES' => 412, 'ENGINE_ATTRIBUTE' => 1154, 'ENUM' => 414, 'ERROR' => 417, 'ERRORS' => 418, 'ESCAPE' => 420, 'ESCAPED' => 419, 'EVENT' => 422, 'EVENTS' => 421, 'EVERY' => 423, 'EXCEPT' => 913, 'EXCHANGE' => 424, 'EXCLUDE' => 934, 'EXECUTE' => 425, 'EXISTS' => 426, 'EXIT' => 427, 'EXPANSION' => 428, 'EXPIRE' => 429, 'EXPLAIN' => 387, 'EXPORT' => 430, 'EXTENDED' => 431, 'EXTENT_SIZE' => 432, 'EXTRACT' => 433, 'FACTOR' => 1192, 'FAILED_LOGIN_ATTEMPTS' => 995, 'FALSE' => 434, 'FAST' => 435, 'FAULTS' => 436, 'FETCH' => 437, 'FIELDS' => 328, 'FILE' => 438, 'FILE_BLOCK_SIZE' => 439, 'FILTER' => 440, 'FINISH' => 1193, 'FIRST' => 441, 'FIRST_VALUE' => 935, 'FIXED' => 442, 'FLOAT' => 444, 'FLOAT4' => 444, 'FLOAT8' => 397, 'FLUSH' => 445, 'FOLLOWING' => 936, 'FOLLOWS' => 446, 'FOR' => 449, 'FORCE' => 447, 'FOREIGN' => 448, 'FORMAT' => 450, 'FOUND' => 451, 'FROM' => 452, 'FULL' => 453, 'FULLTEXT' => 454, 'FUNCTION' => 455, 'GENERAL' => 457, 'GENERATE' => 1203, 'GENERATED' => 458, 'GEOMCOLLECTION' => 460, 'GEOMETRY' => 461, 'GEOMETRYCOLLECTION' => 460, 'GET' => 463, 'GET_FORMAT' => 462, 'GET_SOURCE_PUBLIC_KEY' => 1162, 'GLOBAL' => 464, 'GRANT' => 465, 'GRANTS' => 466, 'GROUP' => 467, 'GROUPING' => 926, 'GROUPS' => 937, 'GROUP_CONCAT' => 468, 'GROUP_REPLICATION' => 459, 'GTIDS' => 1207, 'GTID_ONLY' => 1199, 'HANDLER' => 470, 'HASH' => 471, 'HAVING' => 472, 'HELP' => 473, 'HIGH_PRIORITY' => 475, 'HISTOGRAM' => 928, 'HISTORY' => 959, 'HOST' => 476, 'HOSTS' => 477, 'HOUR' => 481, 'HOUR_MICROSECOND' => 478, 'HOUR_MINUTE' => 479, 'HOUR_SECOND' => 480, 'IDENTIFIED' => 483, 'IF' => 485, 'IGNORE' => 486, 'IGNORE_SERVER_IDS' => 487, 'IMPORT' => 488, 'IN' => 504, 'INACTIVE' => 974, 'INDEX' => 490, 'INDEXES' => 489, 'INFILE' => 491, 'INITIAL' => 1197, 'INITIAL_SIZE' => 492, 'INITIATE' => 1194, 'INNER' => 493, 'INOUT' => 494, 'INSENSITIVE' => 495, 'INSERT' => 496, 'INSERT_METHOD' => 497, 'INSTALL' => 499, 'INSTANCE' => 498, 'INT' => 502, 'INT1' => 836, 'INT2' => 768, 'INT3' => 584, 'INT4' => 502, 'INT8' => 289, 'INTEGER' => 502, 'INTERSECT' => 1200, 'INTERVAL' => 500, 'INTO' => 501, 'INVISIBLE' => 911, 'INVOKER' => 503, 'IO' => 507, 'IO_AFTER_GTIDS' => 505, 'IO_BEFORE_GTIDS' => 506, 'IO_THREAD' => 702, 'IPC' => 508, 'IS' => 509, 'ISOLATION' => 510, 'ISSUER' => 511, 'ITERATE' => 512, 'JOIN' => 513, 'JSON' => 515, 'JSON_ARRAYAGG' => 921, 'JSON_OBJECTAGG' => 920, 'JSON_TABLE' => 955, 'JSON_VALUE' => 1151, 'KEY' => 518, 'KEYRING' => 1190, 'KEYS' => 516, 'KEY_BLOCK_SIZE' => 517, 'KILL' => 519, 'LAG' => 938, 'LANGUAGE' => 520, 'LAST' => 521, 'LAST_VALUE' => 939, 'LATERAL' => 975, 'LEAD' => 940, 'LEADING' => 523, 'LEAVE' => 525, 'LEAVES' => 524, 'LEFT' => 526, 'LESS' => 527, 'LEVEL' => 528, 'LIKE' => 530, 'LIMIT' => 531, 'LINEAR' => 532, 'LINES' => 533, 'LINESTRING' => 534, 'LIST' => 535, 'LOAD' => 536, 'LOCAL' => 537, 'LOCALTIME' => 623, 'LOCALTIMESTAMP' => 623, 'LOCK' => 540, 'LOCKED' => 924, 'LOCKS' => 539, 'LOG' => 1206, 'LOGFILE' => 541, 'LOGS' => 542, 'LONG' => 546, 'LONGBLOB' => 543, 'LONGTEXT' => 544, 'LOOP' => 547, 'LOW_PRIORITY' => 548, 'MANUAL' => 1212, 'MASTER' => 571, 'MATCH' => 574, 'MAX' => 579, 'MAXVALUE' => 582, 'MAX_CONNECTIONS_PER_HOUR' => 575, 'MAX_QUERIES_PER_HOUR' => 576, 'MAX_ROWS' => 577, 'MAX_SIZE' => 578, 'MAX_UPDATES_PER_HOUR' => 580, 'MAX_USER_CONNECTIONS' => 581, 'MEDIUM' => 586, 'MEDIUMBLOB' => 583, 'MEDIUMINT' => 584, 'MEDIUMTEXT' => 585, 'MEMBER' => 977, 'MEMORY' => 587, 'MERGE' => 588, 'MESSAGE_TEXT' => 589, 'MICROSECOND' => 590, 'MID' => 811, 'MIDDLEINT' => 584, 'MIGRATE' => 591, 'MIN' => 596, 'MINUTE' => 594, 'MINUTE_MICROSECOND' => 592, 'MINUTE_SECOND' => 593, 'MIN_ROWS' => 595, 'MOD' => 600, 'MODE' => 597, 'MODIFIES' => 598, 'MODIFY' => 599, 'MONTH' => 601, 'MULTILINESTRING' => 602, 'MULTIPOINT' => 603, 'MULTIPOLYGON' => 604, 'MUTEX' => 605, 'MYSQL_ERRNO' => 606, 'NAME' => 608, 'NAMES' => 607, 'NATIONAL' => 609, 'NATURAL' => 610, 'NCHAR' => 612, 'NDB' => 613, 'NDBCLUSTER' => 613, 'NESTED' => 956, 'NETWORK_NAMESPACE' => 987, 'NEVER' => 616, 'NEW' => 617, 'NEXT' => 618, 'NO' => 624, 'NODEGROUP' => 619, 'NONE' => 620, 'NOT' => 622, 'NOW' => 623, 'NOWAIT' => 925, 'NO_WAIT' => 625, 'NO_WRITE_TO_BINLOG' => 626, 'NTH_VALUE' => 941, 'NTILE' => 942, 'NULL' => 627, 'NULLS' => 943, 'NUMBER' => 629, 'NUMERIC' => 630, 'NVARCHAR' => 631, 'OF' => 922, 'OFF' => 998, 'OFFSET' => 632, 'OJ' => 986, 'OLD' => 984, 'ON' => 633, 'ONE' => 634, 'ONLY' => 635, 'OPEN' => 636, 'OPTIMIZE' => 637, 'OPTIMIZER_COSTS' => 638, 'OPTION' => 640, 'OPTIONAL' => 978, 'OPTIONALLY' => 641, 'OPTIONS' => 639, 'OR' => 645, 'ORDER' => 643, 'ORDINALITY' => 957, 'ORGANIZATION' => 971, 'OTHERS' => 944, 'OUT' => 648, 'OUTER' => 646, 'OUTFILE' => 647, 'OVER' => 945, 'OWNER' => 649, 'PACK_KEYS' => 650, 'PAGE' => 651, 'PARALLEL' => 1208, 'PARSER' => 653, 'PARSE_TREE' => 1205, 'PARTIAL' => 655, 'PARTITION' => 656, 'PARTITIONING' => 658, 'PARTITIONS' => 657, 'PASSWORD' => 659, 'PASSWORD_LOCK_TIME' => 994, 'PATH' => 958, 'PERCENT_RANK' => 946, 'PERSIST' => 908, 'PERSIST_ONLY' => 927, 'PHASE' => 660, 'PLUGIN' => 662, 'PLUGINS' => 663, 'PLUGIN_DIR' => 661, 'POINT' => 664, 'POLYGON' => 665, 'PORT' => 666, 'POSITION' => 667, 'PRECEDES' => 668, 'PRECEDING' => 947, 'PRECISION' => 669, 'PREPARE' => 670, 'PRESERVE' => 671, 'PREV' => 672, 'PRIMARY' => 673, 'PRIVILEGES' => 674, 'PRIVILEGE_CHECKS_USER' => 991, 'PROCEDURE' => 675, 'PROCESS' => 676, 'PROCESSLIST' => 677, 'PROFILE' => 678, 'PROFILES' => 679, 'PROXY' => 680, 'PURGE' => 681, 'QUALIFY' => 1210, 'QUARTER' => 682, 'QUERY' => 683, 'QUICK' => 684, 'RANDOM' => 988, 'RANGE' => 685, 'RANK' => 948, 'READ' => 688, 'READS' => 686, 'READ_ONLY' => 687, 'READ_WRITE' => 689, 'REAL' => 690, 'REBUILD' => 691, 'RECOVER' => 692, 'RECURSIVE' => 915, 'REDO_BUFFER_SIZE' => 694, 'REDUNDANT' => 695, 'REFERENCE' => 972, 'REFERENCES' => 696, 'REGEXP' => 697, 'REGISTRATION' => 1195, 'RELAY' => 698, 'RELAYLOG' => 699, 'RELAY_LOG_FILE' => 700, 'RELAY_LOG_POS' => 701, 'RELAY_THREAD' => 702, 'RELEASE' => 703, 'RELOAD' => 704, 'REMOVE' => 705, 'RENAME' => 706, 'REORGANIZE' => 707, 'REPAIR' => 708, 'REPEAT' => 710, 'REPEATABLE' => 709, 'REPLACE' => 711, 'REPLICA' => 1159, 'REPLICAS' => 1160, 'REPLICATE_DO_DB' => 713, 'REPLICATE_DO_TABLE' => 715, 'REPLICATE_IGNORE_DB' => 714, 'REPLICATE_IGNORE_TABLE' => 716, 'REPLICATE_REWRITE_DB' => 719, 'REPLICATE_WILD_DO_TABLE' => 717, 'REPLICATE_WILD_IGNORE_TABLE' => 718, 'REPLICATION' => 712, 'REQUIRE' => 720, 'REQUIRE_ROW_FORMAT' => 993, 'REQUIRE_TABLE_PRIMARY_KEY_CHECK' => 996, 'RESET' => 721, 'RESIGNAL' => 722, 'RESOURCE' => 963, 'RESPECT' => 949, 'RESTART' => 968, 'RESTORE' => 724, 'RESTRICT' => 725, 'RESUME' => 726, 'RETAIN' => 983, 'RETURN' => 729, 'RETURNED_SQLSTATE' => 727, 'RETURNING' => 999, 'RETURNS' => 728, 'REUSE' => 960, 'REVERSE' => 730, 'REVOKE' => 731, 'RIGHT' => 732, 'RLIKE' => 697, 'ROLE' => 909, 'ROLLBACK' => 733, 'ROLLUP' => 734, 'ROTATE' => 735, 'ROUTINE' => 736, 'ROW' => 739, 'ROWS' => 737, 'ROW_COUNT' => 740, 'ROW_FORMAT' => 738, 'ROW_NUMBER' => 950, 'RTREE' => 741, 'S3' => 1209, 'SAVEPOINT' => 742, 'SCHEDULE' => 743, 'SCHEMA' => 363, 'SCHEMAS' => 364, 'SCHEMA_NAME' => 744, 'SECOND' => 746, 'SECONDARY' => 979, 'SECONDARY_ENGINE' => 980, 'SECONDARY_ENGINE_ATTRIBUTE' => 1155, 'SECONDARY_LOAD' => 981, 'SECONDARY_UNLOAD' => 982, 'SECOND_MICROSECOND' => 745, 'SECURITY' => 747, 'SELECT' => 748, 'SENSITIVE' => 749, 'SEPARATOR' => 750, 'SERIAL' => 752, 'SERIALIZABLE' => 751, 'SERVER' => 754, 'SESSION' => 753, 'SESSION_USER' => 867, 'SET' => 756, 'SHARE' => 758, 'SHOW' => 761, 'SHUTDOWN' => 762, 'SIGNAL' => 763, 'SIGNED' => 764, 'SIMPLE' => 765, 'SKIP' => 923, 'SLAVE' => 766, 'SLOW' => 767, 'SMALLINT' => 768, 'SNAPSHOT' => 769, 'SOCKET' => 770, 'SOME' => 275, 'SONAME' => 771, 'SOUNDS' => 772, 'SOURCE' => 773, 'SOURCE_AUTO_POSITION' => 1163, 'SOURCE_BIND' => 1164, 'SOURCE_COMPRESSION_ALGORITHMS' => 1165, 'SOURCE_CONNECTION_AUTO_FAILOVER' => 1156, 'SOURCE_CONNECT_RETRY' => 1166, 'SOURCE_DELAY' => 1167, 'SOURCE_HEARTBEAT_PERIOD' => 1168, 'SOURCE_HOST' => 1169, 'SOURCE_LOG_FILE' => 1170, 'SOURCE_LOG_POS' => 1171, 'SOURCE_PASSWORD' => 1172, 'SOURCE_PORT' => 1173, 'SOURCE_PUBLIC_KEY_PATH' => 1174, 'SOURCE_RETRY_COUNT' => 1175, 'SOURCE_SSL' => 1176, 'SOURCE_SSL_CA' => 1177, 'SOURCE_SSL_CAPATH' => 1178, 'SOURCE_SSL_CERT' => 1179, 'SOURCE_SSL_CIPHER' => 1180, 'SOURCE_SSL_CRL' => 1181, 'SOURCE_SSL_CRLPATH' => 1182, 'SOURCE_SSL_KEY' => 1183, 'SOURCE_SSL_VERIFY_SERVER_CERT' => 1184, 'SOURCE_TLS_CIPHERSUITES' => 1185, 'SOURCE_TLS_VERSION' => 1186, 'SOURCE_USER' => 1187, 'SOURCE_ZSTD_COMPRESSION_LEVEL' => 1188, 'SPATIAL' => 774, 'SPECIFIC' => 775, 'SQL' => 788, 'SQLEXCEPTION' => 776, 'SQLSTATE' => 777, 'SQLWARNING' => 778, 'SQL_AFTER_GTIDS' => 779, 'SQL_AFTER_MTS_GAPS' => 780, 'SQL_BEFORE_GTIDS' => 781, 'SQL_BIG_RESULT' => 782, 'SQL_BUFFER_RESULT' => 783, 'SQL_CALC_FOUND_ROWS' => 785, 'SQL_NO_CACHE' => 786, 'SQL_SMALL_RESULT' => 787, 'SQL_THREAD' => 789, 'SQL_TSI_DAY' => 375, 'SQL_TSI_HOUR' => 481, 'SQL_TSI_MINUTE' => 594, 'SQL_TSI_MONTH' => 601, 'SQL_TSI_QUARTER' => 682, 'SQL_TSI_SECOND' => 746, 'SQL_TSI_WEEK' => 887, 'SQL_TSI_YEAR' => 905, 'SRID' => 961, 'SSL' => 790, 'STACKED' => 791, 'START' => 794, 'STARTING' => 792, 'STARTS' => 793, 'STATS_AUTO_RECALC' => 795, 'STATS_PERSISTENT' => 796, 'STATS_SAMPLE_PAGES' => 797, 'STATUS' => 798, 'STD' => 800, 'STDDEV' => 800, 'STDDEV_POP' => 800, 'STDDEV_SAMP' => 799, 'STOP' => 801, 'STORAGE' => 802, 'STORED' => 803, 'STRAIGHT_JOIN' => 804, 'STREAM' => 997, 'STRING' => 805, 'ST_COLLECT' => 1189, 'SUBCLASS_ORIGIN' => 806, 'SUBDATE' => 807, 'SUBJECT' => 808, 'SUBPARTITION' => 810, 'SUBPARTITIONS' => 809, 'SUBSTR' => 811, 'SUBSTRING' => 811, 'SUM' => 812, 'SUPER' => 813, 'SUSPEND' => 814, 'SWAPS' => 815, 'SWITCHES' => 816, 'SYSDATE' => 817, 'SYSTEM' => 964, 'SYSTEM_USER' => 867, 'TABLE' => 821, 'TABLES' => 818, 'TABLESAMPLE' => 1214, 'TABLESPACE' => 819, 'TABLE_CHECKSUM' => 822, 'TABLE_NAME' => 823, 'TEMPORARY' => 824, 'TEMPTABLE' => 825, 'TERMINATED' => 826, 'TEXT' => 828, 'THAN' => 829, 'THEN' => 830, 'THREAD_PRIORITY' => 962, 'TIES' => 951, 'TIME' => 834, 'TIMESTAMP' => 831, 'TIMESTAMPADD' => 832, 'TIMESTAMPDIFF' => 833, 'TINYBLOB' => 835, 'TINYINT' => 836, 'TINYTEXT' => 837, 'TLS' => 1152, 'TO' => 838, 'TRAILING' => 839, 'TRANSACTION' => 840, 'TRIGGER' => 842, 'TRIGGERS' => 841, 'TRIM' => 843, 'TRUE' => 844, 'TRUNCATE' => 845, 'TYPE' => 847, 'TYPES' => 846, 'UNBOUNDED' => 952, 'UNCOMMITTED' => 850, 'UNDEFINED' => 851, 'UNDO' => 855, 'UNDOFILE' => 853, 'UNDO_BUFFER_SIZE' => 854, 'UNICODE' => 856, 'UNINSTALL' => 857, 'UNION' => 858, 'UNIQUE' => 859, 'UNKNOWN' => 860, 'UNLOCK' => 861, 'UNREGISTER' => 1196, 'UNSIGNED' => 862, 'UNTIL' => 863, 'UPDATE' => 864, 'UPGRADE' => 865, 'URL' => 1202, 'USAGE' => 866, 'USE' => 869, 'USER' => 867, 'USER_RESOURCES' => 723, 'USE_FRM' => 868, 'USING' => 870, 'UTC_DATE' => 871, 'UTC_TIME' => 873, 'UTC_TIMESTAMP' => 872, 'VALIDATION' => 874, 'VALUE' => 876, 'VALUES' => 875, 'VARBINARY' => 877, 'VARCHAR' => 878, 'VARCHARACTER' => 878, 'VARIABLES' => 879, 'VARIANCE' => 880, 'VARYING' => 881, 'VAR_POP' => 880, 'VAR_SAMP' => 882, 'VCPU' => 965, 'VIEW' => 883, 'VIRTUAL' => 884, 'VISIBLE' => 912, 'WAIT' => 885, 'WARNINGS' => 886, 'WEEK' => 887, 'WEIGHT_STRING' => 888, 'WHEN' => 889, 'WHERE' => 890, 'WHILE' => 891, 'WINDOW' => 953, 'WITH' => 892, 'WITHOUT' => 895, 'WORK' => 896, 'WRAPPER' => 897, 'WRITE' => 898, 'X509' => 899, 'XA' => 900, 'XID' => 901, 'XML' => 902, 'XOR' => 903, 'YEAR' => 905, 'YEAR_MONTH' => 904, 'ZEROFILL' => 906, 'ZONE' => 1157, '||' => 644 );
const FUNCTIONS = array( 'ADDDATE' => true, 'BIT_AND' => true, 'BIT_OR' => true, 'BIT_XOR' => true, 'CAST' => true, 'COUNT' => true, 'CURDATE' => true, 'CURTIME' => true, 'DATE_ADD' => true, 'DATE_SUB' => true, 'EXTRACT' => true, 'GROUP_CONCAT' => true, 'JSON_ARRAYAGG' => true, 'JSON_OBJECTAGG' => true, 'MAX' => true, 'MID' => true, 'MIN' => true, 'NOW' => true, 'POSITION' => true, 'SESSION_USER' => true, 'STD' => true, 'STDDEV' => true, 'STDDEV_POP' => true, 'STDDEV_SAMP' => true, 'ST_COLLECT' => true, 'SUBDATE' => true, 'SUBSTR' => true, 'SUBSTRING' => true, 'SUM' => true, 'SYSDATE' => true, 'SYSTEM_USER' => true, 'TRIM' => true, 'VARIANCE' => true, 'VAR_POP' => true, 'VAR_SAMP' => true );
// phpcs:enable
// END: AUTO-GENERATED MYSQL GRAMMAR SYMBOLS
/**
* The SQL modes that affect the lexer behavior.
*
* These values are intended to be used in a bitmask. See "$this->sql_modes".
* The list of the SQL modes is not exhaustive. Only the ones that influence
* the lexer behavior are included in this list.
*
* See:
* https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html
*/
const SQL_MODE_HIGH_NOT_PRECEDENCE = 1;
const SQL_MODE_PIPES_AS_CONCAT = 2;
const SQL_MODE_IGNORE_SPACE = 4;
const SQL_MODE_NO_BACKSLASH_ESCAPES = 8;
const SQL_MODE_ANSI_QUOTES = 16;
/**
* Character masks for frequently used character classes.
*
* These are intended to be used with "strspn()" and "strcspn()" functions
* for fast character class matching in the SQL payload.
*/
private const WHITESPACE_MASK = " \t\n\r\f\v";
private const DIGIT_MASK = '0123456789';
private const HEX_DIGIT_MASK = '0123456789abcdefABCDEF';
private const IDENTIFIER_MASK = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$';
/**
* Internal scanner sentinels.
*
* These values are produced and consumed inside the scanner only; they
* never appear in the emitted token stream, which uses the grammar token
* numbers from the generated token data above. They are negative so they
* can never collide with a grammar token number.
*/
private const EOF = -1;
private const WHITESPACE = -2;
// Comments.
private const COMMENT = -3;
private const MYSQL_COMMENT_START = -4;
private const MYSQL_COMMENT_END = -5;
/**
* Lexer states that a token sets for the next one, like MySQL's
* "next_state". They use MySQL's names from sql/sql_lex.cc, with the
* abbreviations written out:
*
* - STATE_START (MY_LEX_START): The default state.
* - STATE_IDENTIFIER_SEPARATOR (MY_LEX_IDENT_SEP): Set by a name before
* "." and a name character. The "." is then a separator, even before
* digits ("t.1"), and the name is an identifier, even if it's
* a keyword ("order.id").
* - STATE_IDENTIFIER_START (MY_LEX_IDENT_START): Set by a "." before
* a name character. The name is then always an identifier
* ("t.order", "t.1", "t.x'41'").
* - STATE_IDENTIFIER_OR_KEYWORD (MY_LEX_IDENT_OR_KEYWORD): Set by "@@"
* before a name character. The name is then an identifier or
* a keyword, even before "." ("@@session.x"), but never a number
* or a literal ("@@123").
*
* A "." that ends a number sets no state, so "1.FROM" is a number and
* a keyword. Like the scanner sentinels, the states are negative, so
* they never collide with a grammar token number.
*/
private const STATE_START = -6;
private const STATE_IDENTIFIER_SEPARATOR = -7;
private const STATE_IDENTIFIER_START = -8;
private const STATE_IDENTIFIER_OR_KEYWORD = -9;
/**
* Identifier-like strings that may represent underscore-prefixed charset names.
*
* Includes charsets from both MySQL 5 and 8; via "SHOW CHARACTER SET"/docs:
* https://dev.mysql.com/doc/refman/5.7/en/charset-charsets.html
* https://dev.mysql.com/doc/refman/8.4/en/charset-charsets.html
*
* @TODO: Make the list respect the MySQL version. The _utf8 underscore charset
* exists only on MySQL 5, and maybe some others are version-dependant too.
* We can check this using SHOW CHARACTER SET on different MySQL versions.
*/
private const UNDERSCORE_CHARSETS = array(
'_armscii8' => true,
'_ascii' => true,
'_big5' => true,
'_binary' => true,
'_cp1250' => true,
'_cp1251' => true,
'_cp1256' => true,
'_cp1257' => true,
'_cp850' => true,
'_cp852' => true,
'_cp866' => true,
'_cp932' => true,
'_dec8' => true,
'_eucjpms' => true,
'_euckr' => true,
'_gb18030' => true,
'_gb2312' => true,
'_gbk' => true,
'_geostd8' => true,
'_greek' => true,
'_hebrew' => true,
'_hp8' => true,
'_keybcs2' => true,
'_koi8r' => true,
'_koi8u' => true,
'_latin1' => true,
'_latin2' => true,
'_latin5' => true,
'_latin7' => true,
'_macce' => true,
'_macroman' => true,
'_sjis' => true,
'_swe7' => true,
'_tis620' => true,
'_ucs2' => true,
'_ujis' => true,
'_utf16' => true,
'_utf16le' => true,
'_utf32' => true,
'_utf8' => true,
'_utf8mb3' => true,
'_utf8mb4' => true,
);
/**
* The SQL payload to tokenize.
*
* @var string
*/
private $sql;
/**
* Byte length of the SQL payload.
*
* @var int
*/
private $sql_length;
/**
* The version of the MySQL server that the SQL payload is intended for.
*
* This is used to determine which tokens are valid for the given MySQL
* version, and how some tokens should be interpreted.
*
* @var int
*/
private $mysql_version;
/**
* The SQL modes that should be considered active during tokenization.
*
* This is an integer that represents currently active SQL modes as a bitmask.
* The SQL modes are defined as "SQL_MODE_"-prefixed constants in this class.
* The list of the SQL modes isn't exhaustive, as only some affect tokenization.
*
* @var int
*/
private $sql_modes = 0;
/**
* How many bytes from the original SQL payload have been read and tokenized.
*
* This is an internal cursor that is used to track the current position in
* the SQL payload during tokenization. When used as an index in the SQL
* payload, it points to the next byte to read.
*
* @var int
*/
private $bytes_already_read = 0;
/**
* Byte offset in the SQL payload where current token starts.
*
* This is used to extract the token bytes after the token is processed.
* The bytes of the current token are represented by "$this->sql" in range
* from "$this->token_starts_at" to "$this->bytes_already_read - 1".
*
* @var int
*/
private $token_starts_at = 0;
/**
* The type of the current token.
*
* When a token is successfully recognized and read, this value is set to the
* constant representing the token type. When no token was read yet, or the
* end of the SQL payload or an invalid token is reached, this value is null.
*
* @var int|null
*/
private $token_type;
/**
* The lexer state that the previous token set for the next one.
*
* @var int
*/
private $next_state = self::STATE_START;
/**
* Whether the tokenizer is inside an active MySQL-specific comment.
*
* MySQL supports a special comment syntax whose content is recognized as
* a comment by most database engines, but can be treated as SQL by MySQL:
*
* 1. /*! ... - The content is treated as SQL.
* 2. /*!12345 - The content is treated as SQL when "MySQL version >= 12345".
*
* @var bool
*/
private $in_mysql_comment = false;
/**
* Whether the NO_BACKSLASH_ESCAPES SQL mode is active, cached for the tokens.
*
* @var bool
*/
private $no_backslash_escapes = false;
/**
* A grammar token produced ahead of the cursor, waiting to be emitted.
*
* A single scan step can yield two grammar tokens (e.g. "@" plus its name,
* or the two end terminals); next_token() returns the first and holds the
* second here for the following call.
*
* @var WP_MySQL_Token|null
*/
private $pending_token = null;
/**
* A scanned-ahead grammar token held back to be processed on the next step.
*
* Contracting "WITH ROLLUP" needs a one-token lookahead (as in MySQL's
* lexer). When the token after "WITH" is not "ROLLUP", it is stashed here
* as [ type, start, length ] so the next scan step reprocesses it.
*
* @var array{0:int,1:int,2:int}|null
*/
private $lookahead = null;
/**
* Whether the token stream has ended (end markers emitted, or invalid input).
*
* @var bool
*/
private $stream_ended = false;
/**
* The token at the cursor, returned by get_token().
*
* @var WP_MySQL_Token|null
*/
private $current_token = null;
/**
* @param string $sql The SQL payload to tokenize.
* @param int $mysql_version The version of the MySQL server that the SQL payload is intended for.
* @param string[] $sql_modes The SQL modes that should be considered active during tokenization.
*/
public function __construct(
string $sql,
int $mysql_version = 80410,
array $sql_modes = array()
) {
$this->sql = $sql;
$this->sql_length = strlen( $sql );
$this->mysql_version = $mysql_version;
foreach ( $sql_modes as $sql_mode ) {
$sql_mode = strtoupper( $sql_mode );
if ( 'HIGH_NOT_PRECEDENCE' === $sql_mode ) {
$this->sql_modes |= self::SQL_MODE_HIGH_NOT_PRECEDENCE;
} elseif ( 'PIPES_AS_CONCAT' === $sql_mode ) {
$this->sql_modes |= self::SQL_MODE_PIPES_AS_CONCAT;
} elseif ( 'IGNORE_SPACE' === $sql_mode ) {
$this->sql_modes |= self::SQL_MODE_IGNORE_SPACE;
} elseif ( 'NO_BACKSLASH_ESCAPES' === $sql_mode ) {
$this->sql_modes |= self::SQL_MODE_NO_BACKSLASH_ESCAPES;
} elseif ( 'ANSI_QUOTES' === $sql_mode ) {
$this->sql_modes |= self::SQL_MODE_ANSI_QUOTES;
} elseif ( 'ANSI' === $sql_mode ) {
/*
* Expand the composite ANSI mode into its lexer-relevant components.
* The ANSI mode also implies REAL_AS_FLOAT and ONLY_FULL_GROUP_BY,
* which do not affect the lexer.
*
* See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_ansi
*/
$this->sql_modes |= self::SQL_MODE_PIPES_AS_CONCAT
| self::SQL_MODE_IGNORE_SPACE
| self::SQL_MODE_ANSI_QUOTES;
}
}
$this->no_backslash_escapes = $this->is_sql_mode_active( self::SQL_MODE_NO_BACKSLASH_ESCAPES );
}
/**
* Advance to the next token in the MySQL grammar token stream.
*
* The lexer is a pull iterator over the grammar's token stream: each call
* advances the cursor by one token, which get_token() then returns. The
* stream ends with END_OF_INPUT and Bison's end marker; on invalid input it
* ends early, right after the last valid token, with no terminators.
*
* @return bool Whether a token is available at the new cursor position.
*/
public function next_token(): bool {
if ( null !== $this->pending_token ) {
$this->current_token = $this->pending_token;
$this->pending_token = null;
return true;
}
if ( $this->stream_ended ) {
$this->current_token = null;
return false;
}
// A scan step yields one or two grammar tokens: return the first and
// hold the second (if any) back for the next call.
$out = array();
$this->produce( $out );
if ( ! $out ) {
$this->current_token = null;
return false;
}
$this->current_token = $out[0];
if ( isset( $out[1] ) ) {
$this->pending_token = $out[1];
}
return true;
}
/**
* Return the token at the current cursor position.
*
* Returns the same object until the cursor advances, and null before the
* first next_token() call or once the stream is exhausted.
*
* @return WP_MySQL_Token|null The current token, or null when there is none.
*/
public function get_token(): ?WP_MySQL_Token {
return $this->current_token;
}
/**
* Read all remaining tokens as an array, draining the stream.
*
* From a fresh lexer this runs a tight scan-and-emit loop that bypasses the
* pull iterator's per-token queue bookkeeping — the dominant cost when
* tokenizing a whole statement. The common single-token lexemes are emitted
* inline; only the multi-token ones (end markers, "WITH ROLLUP", "@") route
* through the buffered producers. A lexer already advanced by next_token()
* falls back to the iterator to stay correct.
*
* @return WP_MySQL_Token[] The remaining tokens — terminated by END_OF_INPUT
* and the end marker, or a partial list with no
* terminators on invalid input.
*/
public function remaining_tokens(): array {
if ( $this->stream_ended || null !== $this->pending_token || null !== $this->current_token ) {
$tokens = array();
while ( $this->next_token() ) {
$tokens[] = $this->current_token;
}
return $tokens;
}
$tokens = array();
$sql = $this->sql;
$nbe = $this->no_backslash_escapes;
while ( true ) {
if ( null !== $this->lookahead ) {
list( $type, $start, $length ) = $this->lookahead;
$this->lookahead = null;
} else {
// Inline scan_lexeme() for performance.
$this->bytes_already_read += strspn( $sql, self::WHITESPACE_MASK, $this->bytes_already_read );
do {
$this->token_starts_at = $this->bytes_already_read;
$this->token_type = $this->read_next_token();
} while (
self::WHITESPACE === $this->token_type
|| self::COMMENT === $this->token_type
|| self::MYSQL_COMMENT_START === $this->token_type
|| self::MYSQL_COMMENT_END === $this->token_type
);
$type = $this->token_type;
if ( null === $type ) {
// Invalid input: end the stream after the last valid token.
$this->stream_ended = true;
break;
}
$start = $this->token_starts_at;
$length = $this->bytes_already_read - $start;
}
// Common single-token lexeme — the overwhelming majority.
if (
self::EOF !== $type
&& self::WITH !== $type
&& self::AT_SIGN !== $type
) {
$tokens[] = new WP_MySQL_Token( $type, $start, $length, $sql, $nbe );
continue;
}
// Multi-token lexemes: the shared producers append straight to the list.
if ( self::EOF === $type ) {
$this->emit_end_markers( $tokens );
break;
} elseif ( self::WITH === $type ) {
$this->produce_with_or_rollup( $tokens, $start, $length );
} else {
$this->produce_at_variable( $tokens, $start );
}
if ( $this->stream_ended ) {
break;
}
}
return $tokens;
}
/**
* The version of the MySQL server that the SQL payload is intended for.
*
* This represents the MySQL server version that the lexer is set up to
* consider when tokenizing the SQL payload.
*
* @return int The MySQL server version that the lexer is set up to consider.
*/
public function get_mysql_version(): int {
return $this->mysql_version;
}
/**
* Whether an SQL mode is set to be considered as active during tokenization.
* The SQL modes are defined as "SQL_MODE_"-prefixed constants in this class.
*
* @param int $mode The SQL mode to check, an "SQL_MODE_"-prefixed constant.
* @return bool Whether the given SQL mode is active.
*/
public function is_sql_mode_active( int $mode ): bool {
return ( $this->sql_modes & $mode ) !== 0;
}
/**
* Get the name of a token for a given token ID.
*
* Names are derived from the grammar data rather than stored: a keyword
* token resolves to its keyword string via self::KEYWORDS, any other token
* to its token constant name via reflection. When several keywords share a
* token number (synonyms), plain keywords win over paren-gated function
* keywords (USER over SESSION_USER), then the first name in table order wins.
*
* This method is intended to be used only for testing and debugging purposes,
* when tokens need to be presented by their names in a human-readable form.
* It should not be used in production code, as it's not performance-optimized.
*
* @param int $token_id The numeric token ID.
* @return string The token name for the given token ID; null when not found.
*/
public static function get_token_name( int $token_id ): ?string {
static $names = null;
if ( null === $names ) {
$names = array();
foreach ( self::KEYWORDS as $keyword => $id ) {
if ( ! isset( $names[ $id ] ) && ! isset( self::FUNCTIONS[ $keyword ] ) ) {
$names[ $id ] = $keyword;
}
}
foreach ( self::KEYWORDS as $keyword => $id ) {
if ( ! isset( $names[ $id ] ) ) {
$names[ $id ] = $keyword;
}
}
// Token constants share the class with the lexer's own constants;
// grammar tokens are the non-negative ints that are not SQL modes
// (the scanner sentinels are negative, the SQL modes are bit flags).
foreach ( ( new ReflectionClass( self::class ) )->getConstants() as $name => $value ) {
if ( is_int( $value ) && $value >= 0 && 0 !== strpos( $name, 'SQL_MODE_' ) && ! isset( $names[ $value ] ) ) {
$names[ $value ] = $name;
}
}
}
return $names[ $token_id ] ?? null;
}
/**
* Produce the next grammar token (or tokens) into the buffer.
*
* Most lexemes map to a single grammar token. The exceptions mirror MySQL's
* own lexer: "@" expands to the sign plus its name, "WITH ROLLUP" contracts
* into one terminal, and the end of input yields the two end terminals.
*/
private function produce( array &$out ): void {
// Take the lexeme held back by a "WITH" lookahead, or scan a new one.
if ( null !== $this->lookahead ) {
list( $type, $start, $length ) = $this->lookahead;
$this->lookahead = null;
} elseif ( $this->scan_lexeme() ) {
$type = $this->token_type;
$start = $this->token_starts_at;
$length = $this->bytes_already_read - $start;
} else {
// Invalid input: end the stream after the last valid token.
$this->stream_ended = true;
return;
}
if ( self::EOF === $type ) {
$this->emit_end_markers( $out );
} elseif ( self::WITH === $type ) {
$this->produce_with_or_rollup( $out, $start, $length );
} elseif ( self::AT_SIGN === $type ) {
$this->produce_at_variable( $out, $start );
} else {
$out[] = $this->make_token( $type, $start, $length );
}
}
/**
* Emit "WITH", or contract "WITH ROLLUP" into a single terminal.
*
* MySQL's lexer needs a one-token lookahead here. When the scanned-ahead
* lexeme is not "ROLLUP", it is held back to be processed on the next step.
*
* @param int $with_start Byte offset of the "WITH" token.
* @param int $with_length Byte length of the "WITH" token.
*/
private function produce_with_or_rollup( array &$out, int $with_start, int $with_length ): void {
if ( ! $this->scan_lexeme() ) {
// "WITH" was the last valid token; the trailing input is invalid.
$out[] = $this->make_token( self::WITH, $with_start, $with_length );
$this->stream_ended = true;
return;
}
if ( self::ROLLUP_SYM === $this->token_type ) {
$out[] = $this->make_token(
self::WITH_ROLLUP_SYM,
$with_start,
$this->bytes_already_read - $with_start
);
return;
}
$out[] = $this->make_token( self::WITH, $with_start, $with_length );
if ( self::EOF === $this->token_type ) {
$this->emit_end_markers( $out );
return;
}
$this->lookahead = array(
$this->token_type,
$this->token_starts_at,
$this->bytes_already_read - $this->token_starts_at,
);
}
/**
* Emit a "@" or "@@" variable prefix as standalone terminals.
*
* MySQL treats "@" as its own terminal. A single "@" is followed by the
* unquoted user-variable name (always an identifier, never a keyword; it may
* contain ".", "$", and non-ASCII characters), by an empty name (MySQL's empty
* LEX_HOSTNAME, which makes "SELECT @" and "user1@" valid), or by a quoted name
* supplied by the following token. A "@@" prefix is two "@" terminals, followed
* by the system variable name. As in MySQL, that name can't be empty, and only
* backticks can quote it.
*
* @param int $at_start Byte offset of the "@" token.
*/
private function produce_at_variable( array &$out, int $at_start ): void {
$out[] = $this->make_token( self::AT_SIGN, $at_start, 1 );
$position = $this->bytes_already_read; // Right after the "@".
$next = $this->sql[ $position ] ?? '';
if ( '@' === $next ) {
$out[] = $this->make_token( self::AT_SIGN, $position, 1 );
$this->bytes_already_read = $position + 1;
// The name after it may be a keyword ("session" in "@@session.x").
if (
strspn( $this->sql, self::IDENTIFIER_MASK, $this->bytes_already_read, 1 ) > 0
|| ( $this->sql[ $this->bytes_already_read ] ?? '' ) > "\x7F"
) {
$this->next_state = self::STATE_IDENTIFIER_OR_KEYWORD;
} elseif ( '`' !== ( $this->sql[ $this->bytes_already_read ] ?? '' ) ) {
$this->stream_ended = true; // Invalid input.
}
return;
}
// Read the identifier characters and any "." between them as one name.
while ( true ) {
$this->read_identifier();
if ( '.' !== ( $this->sql[ $this->bytes_already_read ] ?? null ) ) {
break;
}
$this->bytes_already_read += 1;
}
// The "." read as a part of the name is not a qualified name separator.
$this->next_state = self::STATE_START;
$name_length = $this->bytes_already_read - $position;
if ( $name_length > 0 ) {
$out[] = $this->make_token( self::IDENT, $position, $name_length );
} elseif ( "'" !== $next && '"' !== $next && '`' !== $next ) {
$out[] = $this->make_token( self::IDENT, $position, 0 );
}
}
/**
* Emit the two terminals that close a valid stream: END_OF_INPUT and Bison's
* end marker ($end), both at the end-of-input position.
*/
private function emit_end_markers( array &$out ): void {
$end = $this->bytes_already_read;
$out[] = $this->make_token( self::END_OF_INPUT, $end, 0 );
$out[] = $this->make_token( self::YYEOF, $end, 0 );
$this->stream_ended = true;
}
/**
* Scan one lexeme from the input, skipping whitespace and comments.
*
* Sets the raw token type and byte range. Returns false only on invalid
* input; at the end of input it sets the EOF sentinel and returns true.
*
* @return bool Whether a lexeme (possibly the EOF sentinel) was scanned.
*/
private function scan_lexeme(): bool {
// Skip leading whitespace inline for optimal performance.
$this->bytes_already_read += strspn( $this->sql, self::WHITESPACE_MASK, $this->bytes_already_read );
do {
$this->token_starts_at = $this->bytes_already_read;
$this->token_type = $this->read_next_token();
} while (
self::WHITESPACE === $this->token_type
|| self::COMMENT === $this->token_type
|| self::MYSQL_COMMENT_START === $this->token_type
|| self::MYSQL_COMMENT_END === $this->token_type
);
return null !== $this->token_type;
}
/**
* Build a grammar token over the current input and SQL mode.
*
* @param int $type The grammar token number.
* @param int $start Byte offset where the token begins.
* @param int $length Byte length of the token.
* @return WP_MySQL_Token
*/
private function make_token( int $type, int $start, int $length ): WP_MySQL_Token {
return new WP_MySQL_Token(
$type,
$start,
$length,
$this->sql,
$this->no_backslash_escapes
);
}
private function read_next_token(): ?int {
// Read and reset the state that the previous token set.
$state = $this->next_state;
$this->next_state = self::STATE_START;
// After "." or "@@", read a name, never a number or a literal.
if ( self::STATE_START !== $state ) {
if ( self::STATE_IDENTIFIER_START === $state ) {
return $this->read_identifier();
}
if ( self::STATE_IDENTIFIER_OR_KEYWORD === $state ) {
$type = $this->read_identifier();
return self::IDENT === $type
? $this->determine_identifier_or_keyword_type( $this->get_current_token_bytes() )
: $type;
}
}
$byte = $this->sql[ $this->bytes_already_read ] ?? null;
$next_byte = $this->sql[ $this->bytes_already_read + 1 ] ?? null;
// A map for a single-byte symbol fast path.
static $single_byte_ops = array(
'(' => self::LEFT_PAREN,
')' => self::RIGHT_PAREN,
',' => self::COMMA,
';' => self::SEMICOLON,
'+' => self::PLUS,
'~' => self::TILDE,
'%' => self::PERCENT,
'^' => self::CARET,
'{' => self::LEFT_BRACE,
'}' => self::RIGHT_BRACE,
'=' => self::EQ,
);
// Fast path for keywords and identifiers.
// `$byte > "\x7F"` catches any non-ASCII byte (0x80-0xFF); read_identifier()
// restricts the accepted identifier codepoints to U+0080-U+10FFFF.
// `"'" !== $next_byte` defers x'..', n'..' and similar special
// literals to their dedicated branches below; only single quotes
// form those, regardless of SQL mode.
if (
(
( $byte >= 'a' && $byte <= 'z' )
|| ( $byte >= 'A' && $byte <= 'Z' )
|| $byte > "\x7F"
)
&& "'" !== $next_byte
) {
$started_at = $this->bytes_already_read;
$type = $this->read_identifier();
if ( self::IDENT === $type ) {
// Inline the keyword lookup on the hot identifier path: most
// identifiers are not keywords, so this avoids two method calls
// (token-bytes extraction + keyword determination) per token.
$word = strtoupper(
substr( $this->sql, $started_at, $this->bytes_already_read - $started_at )
);
$keyword = self::KEYWORDS[ $word ] ?? self::IDENT;
// Before "." and a name, a keyword is an identifier ("order.id").
// read_identifier() sets STATE_IDENTIFIER_SEPARATOR in that case.
if (
self::IDENT !== $keyword
&& self::STATE_IDENTIFIER_SEPARATOR !== $this->next_state
) {
$type = $this->resolve_keyword_type( $keyword, $word );
}
}
} elseif ( null !== $byte && isset( $single_byte_ops[ $byte ] ) ) {
// Fast path for single-byte symbols.
$this->bytes_already_read += 1;
$type = $single_byte_ops[ $byte ];
} elseif ( "'" === $byte || '"' === $byte || '`' === $byte ) {
$type = $this->read_quoted_text();
} elseif ( null !== $byte && strspn( $byte, self::DIGIT_MASK ) > 0 ) {
$type = $this->read_number();
} elseif ( '.' === $byte ) {
if (
null !== $next_byte
&& strspn( $next_byte, self::DIGIT_MASK ) > 0
// Right after a name, it is a qualified name separator.
&& self::STATE_IDENTIFIER_SEPARATOR !== $state
) {
$type = $this->read_number();
} else {
$this->bytes_already_read += 1;
$type = self::DOT;
// A name character right after it starts a qualified name part.
if ( null !== $next_byte && ( strspn( $next_byte, self::IDENTIFIER_MASK ) > 0 || $next_byte > "\x7F" ) ) {
$this->next_state = self::STATE_IDENTIFIER_START;
}
}
} elseif ( ':' === $byte ) {
$this->bytes_already_read += 1;
if ( '=' === $next_byte ) {
$this->bytes_already_read += 1;
$type = self::SET_VAR;
} else {
$type = self::COLON;
}
} elseif ( '<' === $byte ) {
$this->bytes_already_read += 1;
if ( '=' === $next_byte ) {
$this->bytes_already_read += 1;
if ( '>' === ( $this->sql[ $this->bytes_already_read ] ?? null ) ) {
$this->bytes_already_read += 1;
$type = self::EQUAL_SYM;
} else {
$type = self::LE;
}
} elseif ( '>' === $next_byte ) {
$this->bytes_already_read += 1;
$type = self::NE;
} elseif ( '<' === $next_byte ) {
$this->bytes_already_read += 1;
$type = self::SHIFT_LEFT;
} else {
$type = self::LT;
}
} elseif ( '>' === $byte ) {
$this->bytes_already_read += 1;
if ( '=' === $next_byte ) {
$this->bytes_already_read += 1;
$type = self::GE;
} elseif ( '>' === $next_byte ) {
$this->bytes_already_read += 1;
$type = self::SHIFT_RIGHT;
} else {
$type = self::GT_SYM;
}
} elseif ( '!' === $byte ) {
$this->bytes_already_read += 1;
if ( '=' === $next_byte ) {
$this->bytes_already_read += 1;
$type = self::NE;
} else {
$type = self::EXCLAMATION;
}
} elseif ( '-' === $byte ) {
if (
'-' === $next_byte
&& $this->bytes_already_read + 2 < $this->sql_length
&& strspn( $this->sql[ $this->bytes_already_read + 2 ], self::WHITESPACE_MASK ) > 0
) {
$type = $this->read_line_comment();
} elseif ( '>' === $next_byte ) {
$this->bytes_already_read += 2;
if ( '>' === ( $this->sql[ $this->bytes_already_read ] ?? null ) ) {
$this->bytes_already_read += 1;
if ( $this->mysql_version >= 50713 ) {
$type = self::JSON_UNQUOTED_SEPARATOR_SYM;
} else {
return null; // Invalid input.
}
} else {
if ( $this->mysql_version >= 50708 ) {
$type = self::JSON_SEPARATOR_SYM;
} else {
return null; // Invalid input.
}
}
} else {
$this->bytes_already_read += 1;
$type = self::MINUS;
}
} elseif ( '*' === $byte ) {
$this->bytes_already_read += 1;
if ( '/' === $next_byte && $this->in_mysql_comment ) {
$this->bytes_already_read += 1;
$type = self::MYSQL_COMMENT_END;
$this->in_mysql_comment = false;
} else {
$type = self::ASTERISK;
}
} elseif ( '/' === $byte ) {
if ( '*' === $next_byte ) {
if ( '!' === ( $this->sql[ $this->bytes_already_read + 2 ] ?? null ) ) {
$type = $this->read_mysql_comment();
} else {
$this->bytes_already_read += 2;
$type = $this->read_comment_content();
}
} else {
$this->bytes_already_read += 1;
$type = self::SLASH;
}
} elseif ( '&' === $byte ) {
$this->bytes_already_read += 1;
if ( '&' === $next_byte ) {
$this->bytes_already_read += 1;
$type = self::AND_AND_SYM;
} else {
$type = self::AMPERSAND;
}
} elseif ( '|' === $byte ) {
$this->bytes_already_read += 1;
if ( '|' === $next_byte ) {
$this->bytes_already_read += 1;
$type = $this->is_sql_mode_active( self::SQL_MODE_PIPES_AS_CONCAT )
? self::OR_OR_SYM
: self::OR2_SYM;
} else {
$type = self::PIPE;
}
} elseif ( '@' === $byte ) {
// MySQL treats "@" as a standalone terminal; the variable name that
// follows is emitted as its own token (see produce_at_variable()).
$this->bytes_already_read += 1;
$type = self::AT_SIGN;
} elseif ( '?' === $byte ) {
// A parameter marker can't precede a byte that can be part of an identifier.
if ( null !== $next_byte && ( strspn( $next_byte, self::IDENTIFIER_MASK ) > 0 || $next_byte > "\x7F" ) ) {
return null; // Invalid input.
}
$this->bytes_already_read += 1;
$type = self::PARAM_MARKER;
} elseif ( '\\' === $byte ) {
$this->bytes_already_read += 1;
// MySQL 8.0.1 removed "\N" as a synonym for NULL.
if ( 'N' === $next_byte && $this->mysql_version < 80001 ) {
$this->bytes_already_read += 1;
$type = self::NULL_SYM;
} else {
return null; // Invalid input.