rupg-types 0.1.84

The value model, Datum, Vector of 1024 values, TypeId, the text and binary forms of the 197 types, numeric, date and time, JSON, arrays, ranges.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
//! The `jsonpath` type, from `jsonpath_scan.l`, `jsonpath_gram.y` and `jsonpath.c`. A value is the tree of the path, as `flattenJsonPathParseItem` writes it. The text form is the text of `printJsonPathItem`, and the binary form is the version byte 1 and the text form.
//!
//! The scanner gives the tokens that the scanner of flex gives: the longest match wins, and the first rule wins a tie. The text of a token is the text that flex has in `yytext`, so a syntax error names the same text as PostgreSQL. The parser follows the grammar, and it stops at the first token that the grammar cannot take, as bison does.

use rupg_common::SqlState;

use crate::binary::Recv;
use crate::error::TypeError;
use crate::jsonb::escape;
use crate::number::int4_in;
use crate::numeric::{Numeric, numeric_in, numeric_out};
use crate::tsquery::too_deep;

/// `JSP_REGEX_ICASE`: the flag `i` of `like_regex`.
pub const JSONPATH_REGEX_ICASE: u8 = 0x01;

/// `JSP_REGEX_DOTALL`: the flag `s` of `like_regex`.
pub const JSONPATH_REGEX_DOTALL: u8 = 0x02;

/// `JSP_REGEX_MLINE`: the flag `m` of `like_regex`.
pub const JSONPATH_REGEX_MLINE: u8 = 0x04;

/// `JSP_REGEX_WSPACE`: the flag `x` of `like_regex`.
pub const JSONPATH_REGEX_WSPACE: u8 = 0x08;

/// `JSP_REGEX_QUOTE`: the flag `q` of `like_regex`.
pub const JSONPATH_REGEX_QUOTE: u8 = 0x10;

/// The bits of `cflags` in `regex.h` that [`jsonpath_regex_flags`] gives.
const REG_ADVANCED: u32 = 0o3;
const REG_QUOTE: u32 = 0o4;
const REG_ICASE: u32 = 0o10;
const REG_NLSTOP: u32 = 0o100;
const REG_NLANCH: u32 = 0o200;

/// The deepest nesting of parentheses, operators and filters. PostgreSQL stops a deep path when it reaches `max_stack_depth`. The parser here works by recursion, and this limit keeps it on the stack. It is the limit of the analyzer.
const MAX_DEPTH: usize = 1000;

/// The `last` bound of `.**{}`, as `PG_UINT32_MAX`.
pub const JSONPATH_LAST: u32 = u32::MAX;

/// An operator or a method of a path, as `JsonPathItemType` has it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PathOp {
    And,
    Or,
    Not,
    IsUnknown,
    Equal,
    NotEqual,
    Less,
    Greater,
    LessOrEqual,
    GreaterOrEqual,
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    Plus,
    Minus,
    Filter,
    Exists,
    StartsWith,
    Type,
    Size,
    Abs,
    Floor,
    Ceiling,
    Double,
    Datetime,
    KeyValue,
    Bigint,
    Boolean,
    Date,
    Decimal,
    Integer,
    Number,
    StringFunc,
    Time,
    TimeTz,
    Timestamp,
    TimestampTz,
    Replace,
    Lower,
    Upper,
    Ltrim,
    Rtrim,
    Btrim,
    Initcap,
    SplitPart,
}

impl PathOp {
    /// `jspOperationName`: the name of the operator or the method in the text form and in the errors of the executor. The operators `!`, `is unknown`, `?` and `exists` have no name there, and the name here is their text.
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            PathOp::And => "&&",
            PathOp::Or => "||",
            PathOp::Not => "!",
            PathOp::IsUnknown => "is unknown",
            PathOp::Equal => "==",
            PathOp::NotEqual => "!=",
            PathOp::Less => "<",
            PathOp::Greater => ">",
            PathOp::LessOrEqual => "<=",
            PathOp::GreaterOrEqual => ">=",
            PathOp::Add | PathOp::Plus => "+",
            PathOp::Sub | PathOp::Minus => "-",
            PathOp::Mul => "*",
            PathOp::Div => "/",
            PathOp::Mod => "%",
            PathOp::Filter => "?",
            PathOp::Exists => "exists",
            PathOp::StartsWith => "starts with",
            PathOp::Type => "type",
            PathOp::Size => "size",
            PathOp::Abs => "abs",
            PathOp::Floor => "floor",
            PathOp::Ceiling => "ceiling",
            PathOp::Double => "double",
            PathOp::Datetime => "datetime",
            PathOp::KeyValue => "keyvalue",
            PathOp::Bigint => "bigint",
            PathOp::Boolean => "boolean",
            PathOp::Date => "date",
            PathOp::Decimal => "decimal",
            PathOp::Integer => "integer",
            PathOp::Number => "number",
            PathOp::StringFunc => "string",
            PathOp::Time => "time",
            PathOp::TimeTz => "time_tz",
            PathOp::Timestamp => "timestamp",
            PathOp::TimestampTz => "timestamp_tz",
            PathOp::Replace => "replace",
            PathOp::Lower => "lower",
            PathOp::Upper => "upper",
            PathOp::Ltrim => "ltrim",
            PathOp::Rtrim => "rtrim",
            PathOp::Btrim => "btrim",
            PathOp::Initcap => "initcap",
            PathOp::SplitPart => "split_part",
        }
    }

    /// `operationPriority`: an operand with a priority that is not higher than the priority of its operator is in parentheses in the text form.
    fn priority(self) -> u8 {
        match self {
            PathOp::Or => 0,
            PathOp::And => 1,
            PathOp::Equal
            | PathOp::NotEqual
            | PathOp::Less
            | PathOp::Greater
            | PathOp::LessOrEqual
            | PathOp::GreaterOrEqual
            | PathOp::StartsWith => 2,
            PathOp::Add | PathOp::Sub => 3,
            PathOp::Mul | PathOp::Div | PathOp::Mod => 4,
            PathOp::Plus | PathOp::Minus => 5,
            _ => 6,
        }
    }
}

/// One item of a path. The items that follow an item, its `next` items in PostgreSQL, are the items after it in its [`Chain`].
#[derive(Clone, Debug, PartialEq)]
pub enum PathItem {
    /// `null`.
    Null,
    /// A string literal.
    String(String),
    /// A number literal.
    Numeric(Numeric),
    /// `true` or `false`.
    Bool(bool),
    /// `&&`, `||`, a comparison, an arithmetic operator or `starts with`, with the left and the right operand.
    Binary(PathOp, Chain, Chain),
    /// `!`, `is unknown`, unary `+` and `-`, the filter `?()` and `exists`, with the operand.
    Unary(PathOp, Chain),
    /// `like_regex`: the operand, the pattern and the bits of the flags.
    LikeRegex { expr: Chain, pattern: String, flags: u8 },
    /// `[*]`.
    AnyArray,
    /// `.*`.
    AnyKey,
    /// `[...]`: each subscript, with the end of a range `to`.
    IndexArray(Vec<(Chain, Option<Chain>)>),
    /// `.**`, with the first and the last level. [`JSONPATH_LAST`] is `last`.
    Any { first: u32, last: u32 },
    /// `.key`.
    Key(String),
    /// `@`.
    Current,
    /// `$`.
    Root,
    /// `$name`.
    Variable(String),
    /// `last`.
    Last,
    /// A method such as `.type()`, with its arguments. An argument is a string or a number.
    Method(PathOp, Vec<PathItem>),
}

impl PathItem {
    /// The priority of the item as an operand.
    fn priority(&self) -> u8 {
        match self {
            PathItem::Binary(op, ..) | PathItem::Unary(op, _) => op.priority(),
            _ => 6,
        }
    }
}

/// An item and the items that follow it. A chain is never empty.
pub type Chain = Vec<PathItem>;

/// A value of `jsonpath`.
#[derive(Clone, Debug, PartialEq)]
pub struct JsonPath {
    /// False for `strict`.
    pub lax: bool,
    /// The path.
    pub expr: Chain,
}

/// `jspConvertRegexFlags`: the `cflags` of the regular expression of `like_regex` with the flags `flags`.
///
/// # Errors
///
/// `0A000` for the flag `x` with no flag `q`.
pub fn jsonpath_regex_flags(flags: u8) -> Result<u32, TypeError> {
    let mut cflags = REG_ADVANCED;
    if flags & JSONPATH_REGEX_ICASE != 0 {
        cflags |= REG_ICASE;
    }
    if flags & JSONPATH_REGEX_QUOTE != 0 {
        cflags &= !REG_ADVANCED;
        cflags |= REG_QUOTE;
    } else {
        if flags & JSONPATH_REGEX_DOTALL == 0 {
            cflags |= REG_NLSTOP;
        }
        if flags & JSONPATH_REGEX_MLINE != 0 {
            cflags |= REG_NLANCH;
        }
        if flags & JSONPATH_REGEX_WSPACE != 0 {
            return Err(TypeError::new(
                SqlState::FEATURE_NOT_SUPPORTED,
                "XQuery \"x\" flag (expanded regular expressions) is not implemented".to_string(),
            ));
        }
    }
    Ok(cflags)
}

/// The check of a regular expression: the pattern and the `cflags`, and the message of `pg_regerror` for a pattern that is not valid.
pub type RegexCheck<'a> = &'a dyn Fn(&str, u32) -> Result<(), String>;

/// The error `22P02` of the type with a detail.
fn bad_input(detail: &str) -> TypeError {
    let mut error = TypeError::new(
        SqlState::INVALID_TEXT_REPRESENTATION,
        "invalid input syntax for type jsonpath".to_string(),
    );
    error.detail = Some(detail.to_string());
    error
}

/// `jsonpath_yyerror`: the message and the text of the token, or the end of the input when the text is empty.
fn yyerror(message: &str, text: &[u8]) -> TypeError {
    let message = if text.is_empty() {
        format!("{message} at end of jsonpath input")
    } else {
        format!("{message} at or near \"{}\" of jsonpath input", String::from_utf8_lossy(text))
    };
    TypeError::new(SqlState::SYNTAX_ERROR, message)
}

/// `special`: a character that is a token by itself.
fn is_special(b: u8) -> bool {
    b"?%$.[]{}()|&!=<>@#,*:-+/".contains(&b)
}

/// `blank`.
fn is_blank(b: u8) -> bool {
    matches!(b, b' ' | b'\t' | b'\n' | b'\r' | 0x0c)
}

/// `other`: a byte that is not special, not blank and not `\` or `"`.
fn is_other(b: u8) -> bool {
    !is_special(b) && !is_blank(b) && b != b'\\' && b != b'"'
}

/// A key word of the scanner.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Kw {
    Is,
    To,
    Lax,
    Flag,
    Last,
    Null,
    True,
    With,
    False,
    Exists,
    Starts,
    Strict,
    Unknown,
    LikeRegex,
    /// A key word that names a method.
    Method(PathOp),
}

/// `keywords`: the key words, and true for a key word that matches only in lower case.
const KEYWORDS: [(&str, bool, Kw); 41] = [
    ("is", false, Kw::Is),
    ("to", false, Kw::To),
    ("abs", false, Kw::Method(PathOp::Abs)),
    ("lax", false, Kw::Lax),
    ("date", false, Kw::Method(PathOp::Date)),
    ("flag", false, Kw::Flag),
    ("last", false, Kw::Last),
    ("null", true, Kw::Null),
    ("size", false, Kw::Method(PathOp::Size)),
    ("time", false, Kw::Method(PathOp::Time)),
    ("true", true, Kw::True),
    ("type", false, Kw::Method(PathOp::Type)),
    ("with", false, Kw::With),
    ("btrim", false, Kw::Method(PathOp::Btrim)),
    ("false", true, Kw::False),
    ("floor", false, Kw::Method(PathOp::Floor)),
    ("lower", false, Kw::Method(PathOp::Lower)),
    ("ltrim", false, Kw::Method(PathOp::Ltrim)),
    ("rtrim", false, Kw::Method(PathOp::Rtrim)),
    ("upper", false, Kw::Method(PathOp::Upper)),
    ("bigint", false, Kw::Method(PathOp::Bigint)),
    ("double", false, Kw::Method(PathOp::Double)),
    ("exists", false, Kw::Exists),
    ("number", false, Kw::Method(PathOp::Number)),
    ("starts", false, Kw::Starts),
    ("strict", false, Kw::Strict),
    ("string", false, Kw::Method(PathOp::StringFunc)),
    ("boolean", false, Kw::Method(PathOp::Boolean)),
    ("ceiling", false, Kw::Method(PathOp::Ceiling)),
    ("decimal", false, Kw::Method(PathOp::Decimal)),
    ("initcap", false, Kw::Method(PathOp::Initcap)),
    ("integer", false, Kw::Method(PathOp::Integer)),
    ("replace", false, Kw::Method(PathOp::Replace)),
    ("time_tz", false, Kw::Method(PathOp::TimeTz)),
    ("unknown", false, Kw::Unknown),
    ("datetime", false, Kw::Method(PathOp::Datetime)),
    ("keyvalue", false, Kw::Method(PathOp::KeyValue)),
    ("timestamp", false, Kw::Method(PathOp::Timestamp)),
    ("like_regex", false, Kw::LikeRegex),
    ("split_part", false, Kw::Method(PathOp::SplitPart)),
    ("timestamp_tz", false, Kw::Method(PathOp::TimestampTz)),
];

/// `checkKeyword`: the key word that a name is, if any.
fn keyword(name: &[u8]) -> Option<Kw> {
    KEYWORDS.iter().find_map(|&(word, lower, kw)| {
        let found = if lower {
            name == word.as_bytes()
        } else {
            name.eq_ignore_ascii_case(word.as_bytes())
        };
        found.then_some(kw)
    })
}

/// The kind of a token.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Tok {
    End,
    And,
    Or,
    Not,
    /// `**`.
    AnyLevels,
    Less,
    LessEqual,
    Equal,
    NotEqual,
    GreaterEqual,
    Greater,
    /// A special character.
    Char(u8),
    Variable,
    Str,
    Numeric,
    Int,
    Ident,
    Kw(Kw),
}

/// A token: the kind, the bounds of the text of flex in the input, and the string of a name, a string, a variable or a number.
struct Token {
    tok: Tok,
    text: (usize, usize),
    value: Vec<u8>,
}

/// The rules of the start state of the scanner that can match at a position, in the order of `jsonpath_scan.l`.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Rule {
    Op(Tok, usize),
    Variable,
    QuotedVariable,
    Special,
    Blank,
    Comment,
    Numeric,
    Int,
    RealFail,
    Junk,
    Quote,
    Backslash,
    Name,
}

/// The rules of an escape in a name or a string, in the order of `jsonpath_scan.l`.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Escape {
    Char(u8),
    Unicode,
    Hex,
    UnicodeFail,
    HexFail,
    UnicodeBackslash,
    Any,
    Lone,
}

/// The scanner, at a position of the input.
struct Lexer<'a> {
    s: &'a [u8],
    at: usize,
    /// True for the soft input of `pg_input_is_valid`, which changes the error for a code point that is too large.
    soft: bool,
}

impl Lexer<'_> {
    fn byte(&self, i: usize) -> u8 {
        self.s.get(i).copied().unwrap_or(0)
    }

    fn has(&self, i: usize, f: fn(u8) -> bool) -> bool {
        i < self.s.len() && f(self.s[i])
    }

    /// The ends of the matches of `(_?{digit})*` after a digit that ends at `q`.
    fn digits_from(&self, mut q: usize, digit: fn(u8) -> bool, ends: &mut Vec<usize>) {
        ends.push(q);
        loop {
            if self.has(q, digit) {
                q += 1;
            } else if self.byte(q) == b'_' && self.has(q + 1, digit) {
                q += 2;
            } else {
                return;
            }
            ends.push(q);
        }
    }

    /// The ends of the matches of `decinteger` at `p`.
    fn decinteger(&self, p: usize) -> Vec<usize> {
        let mut ends = Vec::new();
        match self.byte(p) {
            b'0' => ends.push(p + 1),
            b'1'..=b'9' => self.digits_from(p + 1, |b| b.is_ascii_digit(), &mut ends),
            _ => {}
        }
        ends
    }

    /// The ends of the matches of `decdigits` at `p`.
    fn decdigits(&self, p: usize, ends: &mut Vec<usize>) {
        if self.has(p, |b| b.is_ascii_digit()) {
            self.digits_from(p + 1, |b| b.is_ascii_digit(), ends);
        }
    }

    /// The ends of the matches of `decimal` at `p`.
    fn decimal(&self, p: usize) -> Vec<usize> {
        let mut ends = Vec::new();
        for end in self.decinteger(p) {
            if self.byte(end) == b'.' {
                ends.push(end + 1);
                self.decdigits(end + 1, &mut ends);
            }
        }
        if self.byte(p) == b'.' {
            self.decdigits(p + 1, &mut ends);
        }
        ends
    }

    /// The ends of the matches of `real` at `p`, and of `realfail`.
    fn real(&self, p: usize) -> (Vec<usize>, Vec<usize>) {
        let (mut ends, mut fails) = (Vec::new(), Vec::new());
        let mut bases = self.decinteger(p);
        bases.extend(self.decimal(p));
        for base in bases {
            if matches!(self.byte(base), b'e' | b'E') {
                self.decdigits(base + 1, &mut ends);
                if matches!(self.byte(base + 1), b'+' | b'-') {
                    fails.push(base + 2);
                    self.decdigits(base + 2, &mut ends);
                }
            }
        }
        (ends, fails)
    }

    /// The end of the match of `0x`, `0o` or `0b` and its digits at `p`.
    fn radix(&self, p: usize, letter: u8, digit: fn(u8) -> bool) -> Option<usize> {
        if self.byte(p) == b'0'
            && self.byte(p + 1).to_ascii_lowercase() == letter
            && self.has(p + 2, digit)
        {
            let mut ends = Vec::new();
            self.digits_from(p + 3, digit, &mut ends);
            ends.last().copied()
        } else {
            None
        }
    }

    /// The end of a match of a rule `{x}_junk`: the longest match of `x` that one `other` byte follows.
    fn junk(&self, ends: &[usize]) -> Option<usize> {
        ends.iter().filter(|&&end| self.has(end, is_other)).map(|&end| end + 1).max()
    }

    /// The end of a run of bytes from `p` that `f` takes.
    fn run(&self, mut p: usize, f: fn(u8) -> bool) -> usize {
        while self.has(p, f) {
            p += 1;
        }
        p
    }

    /// The longest rule of the start state at `p`, and the end of its match. The first rule wins a tie.
    fn longest(&self, p: usize) -> (Rule, usize) {
        let mut best: Option<(Rule, usize)> = None;
        let mut offer = |rule: Rule, end: Option<usize>| {
            if let Some(end) = end
                && best.is_none_or(|(_, e)| end > e)
            {
                best = Some((rule, end));
            }
        };
        let ops: [(&[u8], Tok); 11] = [
            (b"&&", Tok::And),
            (b"||", Tok::Or),
            (b"!", Tok::Not),
            (b"**", Tok::AnyLevels),
            (b"<", Tok::Less),
            (b"<=", Tok::LessEqual),
            (b"==", Tok::Equal),
            (b"<>", Tok::NotEqual),
            (b"!=", Tok::NotEqual),
            (b">=", Tok::GreaterEqual),
            (b">", Tok::Greater),
        ];
        for (text, tok) in ops {
            offer(
                Rule::Op(tok, text.len()),
                self.s[p..].starts_with(text).then_some(p + text.len()),
            );
        }
        let dollar = self.byte(p) == b'$';
        offer(
            Rule::Variable,
            (dollar && self.has(p + 1, is_other)).then(|| self.run(p + 1, is_other)),
        );
        offer(Rule::QuotedVariable, (dollar && self.byte(p + 1) == b'"').then_some(p + 2));
        offer(Rule::Special, self.has(p, is_special).then_some(p + 1));
        offer(Rule::Blank, self.has(p, is_blank).then(|| self.run(p, is_blank)));
        offer(Rule::Comment, self.s[p..].starts_with(b"/*").then_some(p + 2));
        let (real, realfail) = self.real(p);
        let decimal = self.decimal(p);
        let decinteger = self.decinteger(p);
        offer(Rule::Numeric, real.iter().max().copied());
        offer(Rule::Numeric, decimal.iter().max().copied());
        offer(Rule::Int, decinteger.iter().max().copied());
        offer(Rule::Int, self.radix(p, b'x', |b| b.is_ascii_hexdigit()));
        offer(Rule::Int, self.radix(p, b'o', |b| matches!(b, b'0'..=b'7')));
        offer(Rule::Int, self.radix(p, b'b', |b| matches!(b, b'0' | b'1')));
        offer(Rule::RealFail, realfail.iter().max().copied());
        offer(Rule::Junk, self.junk(&decinteger));
        offer(Rule::Junk, self.junk(&decimal));
        offer(Rule::Junk, self.junk(&real));
        offer(Rule::Quote, (self.byte(p) == b'"' && p < self.s.len()).then_some(p + 1));
        // The rule `\\` gives back its match, so it ties with nothing.
        offer(Rule::Backslash, (self.byte(p) == b'\\' && p < self.s.len()).then_some(p + 1));
        offer(Rule::Name, self.has(p, is_other).then(|| self.run(p, is_other)));
        best.unwrap_or((Rule::Special, p))
    }

    /// The next token. A name that a comment follows gives no token, as in PostgreSQL.
    fn next(&mut self) -> Result<Token, TypeError> {
        loop {
            let p = self.at;
            if p >= self.s.len() {
                return Ok(Token { tok: Tok::End, text: (p, p), value: Vec::new() });
            }
            let (rule, end) = self.longest(p);
            let token = |tok| Token { tok, text: (p, end), value: self.s[p..end].to_vec() };
            self.at = end;
            match rule {
                Rule::Op(tok, _) => return Ok(token(tok)),
                Rule::Variable => {
                    return Ok(Token {
                        tok: Tok::Variable,
                        text: (p, end),
                        value: self.s[p + 1..end].to_vec(),
                    });
                }
                Rule::QuotedVariable => return self.quoted(Tok::Variable),
                Rule::Special => return Ok(token(Tok::Char(self.s[p]))),
                Rule::Blank => {}
                Rule::Comment => self.comment()?,
                Rule::Numeric => return Ok(token(Tok::Numeric)),
                Rule::Int => return Ok(token(Tok::Int)),
                Rule::RealFail => return Err(yyerror("invalid numeric literal", &self.s[p..end])),
                Rule::Junk => {
                    return Err(yyerror("trailing junk after numeric literal", &self.s[p..end]));
                }
                Rule::Quote => return self.quoted(Tok::Str),
                Rule::Backslash => {
                    self.at = p;
                    if let Some(token) = self.name(Vec::new())? {
                        return Ok(token);
                    }
                }
                Rule::Name => {
                    if let Some(token) = self.name(self.s[p..end].to_vec())? {
                        return Ok(token);
                    }
                }
            }
        }
    }

    /// The state `xc`: the rest of a comment.
    fn comment(&mut self) -> Result<(), TypeError> {
        match self.s[self.at..].windows(2).position(|w| w == b"*/") {
            Some(at) => {
                self.at += at + 2;
                Ok(())
            }
            None => {
                self.at = self.s.len();
                Err(yyerror("unexpected end of comment", b""))
            }
        }
    }

    /// The state `xnq`: the rest of a name. The result is `None` when a comment follows the name, as the scanner of PostgreSQL then drops the name.
    fn name(&mut self, mut value: Vec<u8>) -> Result<Option<Token>, TypeError> {
        loop {
            let p = self.at;
            let end_token = |value: Vec<u8>, text| {
                let tok = keyword(&value).map_or(Tok::Ident, Tok::Kw);
                Some(Token { tok, text, value })
            };
            if p >= self.s.len() {
                return Ok(end_token(value, (p, p)));
            }
            let b = self.s[p];
            if is_other(b) {
                self.at = self.run(p, is_other);
                value.extend_from_slice(&self.s[p..self.at]);
            } else if is_blank(b) {
                self.at = self.run(p, is_blank);
                return Ok(end_token(value, (p, self.at)));
            } else if self.s[p..].starts_with(b"/*") {
                self.at = p + 2;
                self.comment()?;
                return Ok(None);
            } else if b == b'\\' {
                self.escape(&mut value)?;
            } else {
                return Ok(end_token(value, (p, p)));
            }
        }
    }

    /// The states `xq` and `xvq`: the rest of a quoted string or variable.
    fn quoted(&mut self, tok: Tok) -> Result<Token, TypeError> {
        let mut value = Vec::new();
        loop {
            let p = self.at;
            match self.s.get(p) {
                None => return Err(yyerror("unterminated quoted string", b"")),
                Some(b'"') => {
                    self.at = p + 1;
                    return Ok(Token { tok, text: (p, p + 1), value });
                }
                Some(b'\\') => self.escape(&mut value)?,
                Some(_) => {
                    self.at = self.run(p, |b| b != b'\\' && b != b'"');
                    value.extend_from_slice(&self.s[p..self.at]);
                }
            }
        }
    }

    /// The end of a match of `unicode` at `p`.
    fn unicode(&self, p: usize) -> Option<usize> {
        if self.byte(p) != b'\\' || self.byte(p + 1) != b'u' {
            return None;
        }
        if self.byte(p + 2) == b'{' {
            let end = self.run(p + 3, |b| b.is_ascii_hexdigit()).min(p + 9);
            (end > p + 3 && self.byte(end) == b'}').then_some(end + 1)
        } else {
            (p + 2..p + 6).all(|i| self.has(i, |b| b.is_ascii_hexdigit())).then_some(p + 6)
        }
    }

    /// The end of the longest match of `unicodefail` at `p`.
    fn unicode_fail(&self, p: usize) -> Option<usize> {
        if self.byte(p) != b'\\' || self.byte(p + 1) != b'u' {
            return None;
        }
        Some(if self.byte(p + 2) == b'{' {
            self.run(p + 3, |b| b.is_ascii_hexdigit()).min(p + 9)
        } else {
            self.run(p + 2, |b| b.is_ascii_hexdigit()).min(p + 5)
        })
    }

    /// An escape at the backslash at `self.at`, with the rules of `jsonpath_scan.l`.
    fn escape(&mut self, value: &mut Vec<u8>) -> Result<(), TypeError> {
        let p = self.at;
        let mut bounds = vec![p];
        while let Some(end) = self.unicode(bounds[bounds.len() - 1]) {
            bounds.push(end);
        }
        let chain = bounds[bounds.len() - 1];
        let next = self.s.get(p + 1).copied();
        let mut best: Option<(Escape, usize)> = None;
        let mut offer = |rule: Escape, end: Option<usize>| {
            if let Some(end) = end
                && best.is_none_or(|(_, e)| end > e)
            {
                best = Some((rule, end));
            }
        };
        let simple = match next {
            Some(b'b') => Some(0x08),
            Some(b'f') => Some(0x0c),
            Some(b'n') => Some(b'\n'),
            Some(b'r') => Some(b'\r'),
            Some(b't') => Some(b'\t'),
            Some(b'v') => Some(0x0b),
            _ => None,
        };
        if let Some(c) = simple {
            offer(Escape::Char(c), Some(p + 2));
        }
        offer(Escape::Unicode, (chain > p).then_some(chain));
        let hex = next == Some(b'x');
        offer(
            Escape::Hex,
            (hex && (p + 2..p + 4).all(|i| self.has(i, |b| b.is_ascii_hexdigit())))
                .then_some(p + 4),
        );
        offer(Escape::UnicodeFail, bounds.iter().filter_map(|&at| self.unicode_fail(at)).max());
        offer(Escape::HexFail, hex.then(|| self.run(p + 2, |b| b.is_ascii_hexdigit()).min(p + 3)));
        offer(
            Escape::UnicodeBackslash,
            (chain > p && self.byte(chain) == b'\\').then_some(chain + 1),
        );
        offer(Escape::Any, next.filter(|&b| b != b'\n').map(|_| p + 2));
        offer(Escape::Lone, Some(p + 1));
        let (rule, end) = best.unwrap_or((Escape::Lone, p + 1));
        let text = &self.s[p..end];
        match rule {
            Escape::Char(c) => value.push(c),
            Escape::Unicode | Escape::UnicodeBackslash => {
                self.unicode_chain(&bounds, value)?;
                self.at = chain;
                return Ok(());
            }
            Escape::Hex => {
                let code = u32::from_str_radix(std::str::from_utf8(&text[2..]).unwrap_or("0"), 16)
                    .unwrap_or(0);
                self.add_char(code, value)?;
            }
            Escape::UnicodeFail => return Err(yyerror("invalid Unicode escape sequence", text)),
            Escape::HexFail => return Err(yyerror("invalid hexadecimal character sequence", text)),
            Escape::Any => value.push(text[1]),
            Escape::Lone => return Err(yyerror("unexpected end after backslash", text)),
        }
        self.at = end;
        Ok(())
    }

    /// `parseUnicode`: the escapes `\u` between the bounds, with the pairs of surrogates.
    fn unicode_chain(&self, bounds: &[usize], value: &mut Vec<u8>) -> Result<(), TypeError> {
        let mut high: Option<u32> = None;
        let low_must_follow = || bad_input("Unicode low surrogate must follow a high surrogate.");
        for pair in bounds.windows(2) {
            let digits = &self.s[pair[0] + 2..pair[1]];
            let digits = digits.strip_prefix(b"{").map_or(digits, |d| &d[..d.len() - 1]);
            let mut code =
                u32::from_str_radix(std::str::from_utf8(digits).unwrap_or("0"), 16).unwrap_or(0);
            if (0xd800..=0xdbff).contains(&code) {
                if high.is_some() {
                    return Err(bad_input(
                        "Unicode high surrogate must not follow a high surrogate.",
                    ));
                }
                high = Some(code);
                continue;
            }
            if (0xdc00..=0xdfff).contains(&code) {
                let Some(first) = high.take() else { return Err(low_must_follow()) };
                code = ((first & 0x3ff) << 10) + 0x10000 + (code & 0x3ff);
            } else if high.is_some() {
                return Err(low_must_follow());
            }
            self.add_char(code, value)?;
        }
        if high.is_some() {
            return Err(low_must_follow());
        }
        Ok(())
    }

    /// `addUnicodeChar`: one code point in UTF-8.
    fn add_char(&self, code: u32, value: &mut Vec<u8>) -> Result<(), TypeError> {
        if code == 0 {
            let mut error = TypeError::new(
                SqlState::UNTRANSLATABLE_CHARACTER,
                "unsupported Unicode escape sequence".to_string(),
            );
            error.detail = Some("\\u0000 cannot be converted to text.".to_string());
            return Err(error);
        }
        let Some(c) = char::from_u32(code) else {
            let message = if self.soft {
                "could not convert Unicode to server encoding"
            } else {
                "invalid Unicode code point"
            };
            return Err(TypeError::new(SqlState::SYNTAX_ERROR, message.to_string()));
        };
        value.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes());
        Ok(())
    }
}

/// The parser, with the token that it reads ahead.
struct Parser<'a> {
    lexer: Lexer<'a>,
    ahead: Option<Token>,
    depth: usize,
    regex: RegexCheck<'a>,
}

/// The text of a token as a string.
fn text_of(value: Vec<u8>) -> String {
    String::from_utf8(value).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
}

impl Parser<'_> {
    fn peek(&mut self) -> Result<Tok, TypeError> {
        if self.ahead.is_none() {
            self.ahead = Some(self.lexer.next()?);
        }
        Ok(self.ahead.as_ref().map_or(Tok::End, |t| t.tok))
    }

    fn next(&mut self) -> Result<Token, TypeError> {
        self.peek()?;
        Ok(self.ahead.take().unwrap_or(Token { tok: Tok::End, text: (0, 0), value: Vec::new() }))
    }

    /// The syntax error at the token that the parser reads ahead.
    fn error(&mut self) -> TypeError {
        match &self.ahead {
            Some(token) => yyerror("syntax error", &self.lexer.s[token.text.0..token.text.1]),
            None => yyerror("syntax error", b""),
        }
    }

    fn expect(&mut self, tok: Tok) -> Result<Token, TypeError> {
        if self.peek()? == tok { self.next() } else { Err(self.error()) }
    }

    /// The guard of the depth of the recursion.
    fn deeper<T>(
        &mut self,
        f: impl FnOnce(&mut Self) -> Result<T, TypeError>,
    ) -> Result<T, TypeError> {
        if self.depth >= MAX_DEPTH {
            return Err(too_deep());
        }
        self.depth += 1;
        let result = f(self);
        self.depth -= 1;
        result
    }

    /// `expr_or_predicate`, or `predicate` when `pred` is true. The flag of the result is true for a predicate.
    fn or(&mut self, pred: bool) -> Result<(Chain, bool), TypeError> {
        let (mut left, mut is_pred) = self.and(pred)?;
        while self.peek()? == Tok::Or {
            if !is_pred {
                return Err(self.error());
            }
            self.next()?;
            let right = self.and(true)?.0;
            left = vec![PathItem::Binary(PathOp::Or, left, right)];
            is_pred = true;
        }
        Ok((left, is_pred))
    }

    fn and(&mut self, pred: bool) -> Result<(Chain, bool), TypeError> {
        let (mut left, mut is_pred) = self.not(pred)?;
        while self.peek()? == Tok::And {
            if !is_pred {
                return Err(self.error());
            }
            self.next()?;
            let right = self.not(true)?.0;
            left = vec![PathItem::Binary(PathOp::And, left, right)];
            is_pred = true;
        }
        Ok((left, is_pred))
    }

    fn not(&mut self, pred: bool) -> Result<(Chain, bool), TypeError> {
        if self.peek()? != Tok::Not {
            return self.comparison(pred);
        }
        self.next()?;
        self.deeper(|p| {
            let arg = match p.peek()? {
                Tok::Char(b'(') => {
                    p.next()?;
                    let arg = p.or(true)?.0;
                    p.expect(Tok::Char(b')'))?;
                    arg
                }
                Tok::Kw(Kw::Exists) => p.exists()?,
                _ => return Err(p.error()),
            };
            Ok((vec![PathItem::Unary(PathOp::Not, arg)], true))
        })
    }

    /// `EXISTS '(' expr ')'`.
    fn exists(&mut self) -> Result<Chain, TypeError> {
        self.next()?;
        self.expect(Tok::Char(b'('))?;
        let arg = self.expr()?;
        self.expect(Tok::Char(b')'))?;
        Ok(vec![PathItem::Unary(PathOp::Exists, arg)])
    }

    /// An `expr`, where a predicate cannot be.
    fn expr(&mut self) -> Result<Chain, TypeError> {
        Ok(self.additive(true)?.0)
    }

    fn comparison(&mut self, pred: bool) -> Result<(Chain, bool), TypeError> {
        if self.peek()? == Tok::Kw(Kw::Exists) {
            return Ok((self.exists()?, true));
        }
        let (left, is_pred) = self.additive(false)?;
        if is_pred {
            return Ok((left, true));
        }
        let op = match self.peek()? {
            Tok::Equal => PathOp::Equal,
            Tok::NotEqual => PathOp::NotEqual,
            Tok::Less => PathOp::Less,
            Tok::Greater => PathOp::Greater,
            Tok::LessEqual => PathOp::LessOrEqual,
            Tok::GreaterEqual => PathOp::GreaterOrEqual,
            Tok::Kw(Kw::Starts) => {
                self.next()?;
                self.expect(Tok::Kw(Kw::With))?;
                let item = match self.peek()? {
                    Tok::Str => PathItem::String(text_of(self.next()?.value)),
                    Tok::Variable => PathItem::Variable(text_of(self.next()?.value)),
                    _ => return Err(self.error()),
                };
                return Ok((vec![PathItem::Binary(PathOp::StartsWith, left, vec![item])], true));
            }
            Tok::Kw(Kw::LikeRegex) => {
                self.next()?;
                let pattern = text_of(self.expect(Tok::Str)?.value);
                let flags = if self.peek()? == Tok::Kw(Kw::Flag) {
                    self.next()?;
                    Some(text_of(self.expect(Tok::Str)?.value))
                } else {
                    None
                };
                return Ok((vec![self.like_regex(left, pattern, flags.as_deref())?], true));
            }
            _ if pred => return Err(self.error()),
            _ => return Ok((left, false)),
        };
        self.next()?;
        let right = self.expr()?;
        Ok((vec![PathItem::Binary(op, left, right)], true))
    }

    /// `makeItemLikeRegex`: the flags and the check of the pattern.
    fn like_regex(
        &self,
        expr: Chain,
        pattern: String,
        flags: Option<&str>,
    ) -> Result<PathItem, TypeError> {
        let mut bits = 0;
        for c in flags.unwrap_or_default().chars() {
            bits |= match c {
                'i' => JSONPATH_REGEX_ICASE,
                's' => JSONPATH_REGEX_DOTALL,
                'm' => JSONPATH_REGEX_MLINE,
                'x' => JSONPATH_REGEX_WSPACE,
                'q' => JSONPATH_REGEX_QUOTE,
                c => {
                    let mut error = bad_input("");
                    error.sqlstate = SqlState::SYNTAX_ERROR;
                    error.detail = Some(format!(
                        "Unrecognized flag character \"{c}\" in LIKE_REGEX predicate."
                    ));
                    return Err(error);
                }
            };
        }
        let cflags = jsonpath_regex_flags(bits)?;
        (self.regex)(&pattern, cflags).map_err(|message| {
            TypeError::new(
                SqlState::INVALID_REGULAR_EXPRESSION,
                format!("invalid regular expression: {message}"),
            )
        })?;
        Ok(PathItem::LikeRegex { expr, pattern, flags: bits })
    }

    fn additive(&mut self, expr: bool) -> Result<(Chain, bool), TypeError> {
        let (mut left, is_pred) = self.multiplicative(expr)?;
        if is_pred {
            return Ok((left, true));
        }
        loop {
            let op = match self.peek()? {
                Tok::Char(b'+') => PathOp::Add,
                Tok::Char(b'-') => PathOp::Sub,
                _ => return Ok((left, false)),
            };
            self.next()?;
            let right = self.multiplicative(true)?.0;
            left = vec![PathItem::Binary(op, left, right)];
        }
    }

    fn multiplicative(&mut self, expr: bool) -> Result<(Chain, bool), TypeError> {
        let (mut left, is_pred) = self.unary(expr)?;
        if is_pred {
            return Ok((left, true));
        }
        loop {
            let op = match self.peek()? {
                Tok::Char(b'*') => PathOp::Mul,
                Tok::Char(b'/') => PathOp::Div,
                Tok::Char(b'%') => PathOp::Mod,
                _ => return Ok((left, false)),
            };
            self.next()?;
            let right = self.unary(true)?.0;
            left = vec![PathItem::Binary(op, left, right)];
        }
    }

    /// Unary `+` and `-`, which `makeItemUnary` folds into a number.
    fn unary(&mut self, expr: bool) -> Result<(Chain, bool), TypeError> {
        self.deeper(|p| {
            let op = match p.peek()? {
                Tok::Char(b'+') => PathOp::Plus,
                Tok::Char(b'-') => PathOp::Minus,
                _ => return p.accessor_expr(expr),
            };
            p.next()?;
            let arg = p.unary(true)?.0;
            Ok((unary(op, arg), false))
        })
    }

    /// `accessor_expr`, or a predicate in parentheses with no accessor. When `expr` is true, a predicate in parentheses must have an accessor.
    fn accessor_expr(&mut self, expr: bool) -> Result<(Chain, bool), TypeError> {
        let item = match self.peek()? {
            Tok::Char(b'(') => {
                self.next()?;
                let (mut inner, is_pred) = self.or(false)?;
                self.expect(Tok::Char(b')'))?;
                if is_pred && !matches!(self.peek()?, Tok::Char(b'.' | b'[' | b'?')) {
                    if expr {
                        return Err(self.error());
                    }
                    if self.peek()? == Tok::Kw(Kw::Is) {
                        self.next()?;
                        self.expect(Tok::Kw(Kw::Unknown))?;
                        return Ok((vec![PathItem::Unary(PathOp::IsUnknown, inner)], true));
                    }
                    return Ok((inner, true));
                }
                self.accessors(&mut inner)?;
                return Ok((inner, false));
            }
            Tok::Str => PathItem::String(text_of(self.next()?.value)),
            Tok::Kw(Kw::Null) => {
                self.next()?;
                PathItem::Null
            }
            Tok::Kw(Kw::True | Kw::False) => PathItem::Bool(self.next()?.tok == Tok::Kw(Kw::True)),
            Tok::Numeric | Tok::Int => PathItem::Numeric(number(self.next()?)?),
            Tok::Variable => PathItem::Variable(text_of(self.next()?.value)),
            Tok::Char(b'$') => {
                self.next()?;
                PathItem::Root
            }
            Tok::Char(b'@') => {
                self.next()?;
                PathItem::Current
            }
            Tok::Kw(Kw::Last) => {
                self.next()?;
                PathItem::Last
            }
            _ => return Err(self.error()),
        };
        let mut chain = vec![item];
        self.accessors(&mut chain)?;
        Ok((chain, false))
    }

    /// The `accessor_op` items after an item.
    fn accessors(&mut self, chain: &mut Chain) -> Result<(), TypeError> {
        loop {
            match self.peek()? {
                Tok::Char(b'.') => {
                    self.next()?;
                    let item = self.after_dot()?;
                    chain.push(item);
                }
                Tok::Char(b'[') => {
                    self.next()?;
                    if self.peek()? == Tok::Char(b'*') {
                        self.next()?;
                        self.expect(Tok::Char(b']'))?;
                        chain.push(PathItem::AnyArray);
                        continue;
                    }
                    let mut elems = Vec::new();
                    loop {
                        let from = self.expr()?;
                        let to = if self.peek()? == Tok::Kw(Kw::To) {
                            self.next()?;
                            Some(self.expr()?)
                        } else {
                            None
                        };
                        elems.push((from, to));
                        if self.peek()? != Tok::Char(b',') {
                            break;
                        }
                        self.next()?;
                    }
                    self.expect(Tok::Char(b']'))?;
                    chain.push(PathItem::IndexArray(elems));
                }
                Tok::Char(b'?') => {
                    self.next()?;
                    self.expect(Tok::Char(b'('))?;
                    let arg = self.deeper(|p| p.or(true))?.0;
                    self.expect(Tok::Char(b')'))?;
                    chain.push(PathItem::Unary(PathOp::Filter, arg));
                }
                _ => return Ok(()),
            }
        }
    }

    /// The accessor after `.`: a key, `*`, `**` or a method.
    fn after_dot(&mut self) -> Result<PathItem, TypeError> {
        match self.peek()? {
            Tok::Char(b'*') => {
                self.next()?;
                Ok(PathItem::AnyKey)
            }
            Tok::AnyLevels => {
                self.next()?;
                if self.peek()? != Tok::Char(b'{') {
                    return Ok(PathItem::Any { first: 0, last: JSONPATH_LAST });
                }
                self.next()?;
                let first = self.level()?;
                let last = if self.peek()? == Tok::Kw(Kw::To) {
                    self.next()?;
                    self.level()?
                } else {
                    first
                };
                self.expect(Tok::Char(b'}'))?;
                Ok(PathItem::Any { first, last })
            }
            Tok::Ident | Tok::Str => Ok(PathItem::Key(text_of(self.next()?.value))),
            Tok::Kw(kw) => {
                let token = self.next()?;
                match kw {
                    Kw::Method(op) if self.peek()? == Tok::Char(b'(') => {
                        self.next()?;
                        self.method(op)
                    }
                    _ => Ok(PathItem::Key(text_of(token.value))),
                }
            }
            _ => Err(self.error()),
        }
    }

    /// `any_level`: a number or `last`. The number is an `int4`.
    fn level(&mut self) -> Result<u32, TypeError> {
        match self.peek()? {
            Tok::Int => {
                let value = text_of(self.next()?.value);
                Ok(u32::try_from(int4_in(&value)?).unwrap_or(0))
            }
            Tok::Kw(Kw::Last) => {
                self.next()?;
                Ok(JSONPATH_LAST)
            }
            _ => Err(self.error()),
        }
    }

    /// The arguments of a method after `(`, and the `)`.
    fn method(&mut self, op: PathOp) -> Result<PathItem, TypeError> {
        let mut args = Vec::new();
        match op {
            PathOp::Decimal => {
                if self.peek()? != Tok::Char(b')') {
                    args.push(self.int_elem()?);
                    while self.peek()? == Tok::Char(b',') {
                        self.next()?;
                        args.push(self.int_elem()?);
                    }
                }
                self.expect(Tok::Char(b')'))?;
                if args.len() > 2 {
                    let mut error =
                        bad_input(".decimal() can only have an optional precision[,scale].");
                    error.sqlstate = SqlState::SYNTAX_ERROR;
                    return Err(error);
                }
                return Ok(PathItem::Method(op, args));
            }
            PathOp::Datetime | PathOp::Ltrim | PathOp::Rtrim | PathOp::Btrim => {
                if self.peek()? == Tok::Str {
                    args.push(PathItem::String(text_of(self.next()?.value)));
                }
            }
            PathOp::Time | PathOp::TimeTz | PathOp::Timestamp | PathOp::TimestampTz => {
                if self.peek()? == Tok::Int {
                    args.push(PathItem::Numeric(number(self.next()?)?));
                }
            }
            PathOp::Replace | PathOp::SplitPart => {
                args.push(PathItem::String(text_of(self.expect(Tok::Str)?.value)));
                self.expect(Tok::Char(b','))?;
                args.push(if op == PathOp::Replace {
                    PathItem::String(text_of(self.expect(Tok::Str)?.value))
                } else {
                    self.int_elem()?
                });
            }
            _ => {}
        }
        self.expect(Tok::Char(b')'))?;
        Ok(PathItem::Method(op, args))
    }

    /// `int_elem`: an integer with an optional sign.
    fn int_elem(&mut self) -> Result<PathItem, TypeError> {
        let negate = match self.peek()? {
            Tok::Char(b'+') => false,
            Tok::Char(b'-') => true,
            Tok::Int => return Ok(PathItem::Numeric(number(self.next()?)?)),
            _ => return Err(self.error()),
        };
        self.next()?;
        let value = number(self.expect(Tok::Int)?)?;
        Ok(PathItem::Numeric(if negate { value.negate() } else { value }))
    }
}

/// `makeItemNumeric`: the number of a token. An error of `numeric_in` is a hard error.
fn number(token: Token) -> Result<Numeric, TypeError> {
    numeric_in(&text_of(token.value), -1)
}

/// `makeItemUnary` for `+` and `-`: a number with no accessor takes the sign.
fn unary(op: PathOp, arg: Chain) -> Chain {
    if let [PathItem::Numeric(value)] = arg.as_slice() {
        if op == PathOp::Minus {
            return vec![PathItem::Numeric(value.negate())];
        }
        return arg;
    }
    vec![PathItem::Unary(op, arg)]
}

/// The checks of `flattenJsonPathParseItem`: `@` must be in a filter and `last` must be in a subscript. The order of the checks is the order of PostgreSQL: an item, then its operands, then the next item.
fn check(chain: &[PathItem], nesting: u32, subscript: bool) -> Result<(), TypeError> {
    for item in chain {
        match item {
            PathItem::Current if nesting == 0 => {
                return Err(TypeError::new(
                    SqlState::SYNTAX_ERROR,
                    "@ is not allowed in root expressions".to_string(),
                ));
            }
            PathItem::Last if !subscript => {
                return Err(TypeError::new(
                    SqlState::SYNTAX_ERROR,
                    "LAST is allowed only in array subscripts".to_string(),
                ));
            }
            PathItem::Binary(_, left, right) => {
                check(left, nesting, subscript)?;
                check(right, nesting, subscript)?;
            }
            PathItem::Unary(op, arg) => {
                check(arg, nesting + u32::from(*op == PathOp::Filter), subscript)?
            }
            PathItem::LikeRegex { expr, .. } => check(expr, nesting, subscript)?,
            PathItem::IndexArray(elems) => {
                for (from, to) in elems {
                    check(from, nesting, true)?;
                    if let Some(to) = to {
                        check(to, nesting, true)?;
                    }
                }
            }
            _ => {}
        }
    }
    Ok(())
}

/// `jsonpath_in`: a path from its text. `soft` is true for the soft input of `pg_input_is_valid`, and `regex` checks the pattern of `like_regex`.
///
/// # Errors
///
/// `42601` and `22P02` for text that is not a path, `22P05` for `\u0000`, `0A000` for the flag `x`, `2201B` for a pattern that is not valid, `22003` for a number that is too large and `54001` for a path that is too deep. The last two are hard errors in PostgreSQL.
pub fn jsonpath_parse(
    text: &str,
    soft: bool,
    regex: RegexCheck<'_>,
) -> Result<JsonPath, TypeError> {
    let mut parser =
        Parser { lexer: Lexer { s: text.as_bytes(), at: 0, soft }, ahead: None, depth: 0, regex };
    let lax = match parser.peek()? {
        Tok::End => return Err(TypeError::syntax("jsonpath", text)),
        Tok::Kw(Kw::Strict) => {
            parser.next()?;
            false
        }
        Tok::Kw(Kw::Lax) => {
            parser.next()?;
            true
        }
        _ => true,
    };
    let expr = parser.or(false)?.0;
    if parser.peek()? != Tok::End {
        return Err(parser.error());
    }
    check(&expr, 0, false)?;
    Ok(JsonPath { lax, expr })
}

/// `printJsonPathItem` for a chain: the first item, then the items that follow it.
fn print_chain(chain: &[PathItem], brackets: bool, out: &mut String) {
    for (i, item) in chain.iter().enumerate() {
        print_item(item, i > 0, i > 0 || brackets, i + 1 < chain.len(), out);
    }
}

/// An operand, in parentheses when its priority is not higher than `priority`.
fn print_operand(chain: &[PathItem], priority: u8, out: &mut String) {
    print_chain(chain, chain[0].priority() <= priority, out);
}

/// `printJsonPathItem` for one item.
fn print_item(item: &PathItem, in_key: bool, brackets: bool, has_next: bool, out: &mut String) {
    let open = |out: &mut String| {
        if brackets {
            out.push('(');
        }
    };
    let close = |out: &mut String| {
        if brackets {
            out.push(')');
        }
    };
    let dot = |out: &mut String| {
        if in_key {
            out.push('.');
        }
    };
    match item {
        PathItem::Null => out.push_str("null"),
        PathItem::String(s) => escape(s, out),
        PathItem::Numeric(value) => {
            let mut bytes = Vec::new();
            numeric_out(value, &mut bytes);
            let text = String::from_utf8_lossy(&bytes);
            if has_next {
                out.push('(');
                out.push_str(&text);
                out.push(')');
            } else {
                out.push_str(&text);
            }
        }
        PathItem::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
        PathItem::Binary(op, left, right) => {
            open(out);
            print_operand(left, op.priority(), out);
            out.push(' ');
            out.push_str(op.name());
            out.push(' ');
            print_operand(right, op.priority(), out);
            close(out);
        }
        PathItem::Unary(op @ (PathOp::Plus | PathOp::Minus), arg) => {
            open(out);
            out.push_str(op.name());
            print_operand(arg, op.priority(), out);
            close(out);
        }
        PathItem::Unary(op, arg) => {
            let (before, after) = match op {
                PathOp::Not => ("!(", ")"),
                PathOp::IsUnknown => ("(", ") is unknown"),
                PathOp::Filter => ("?(", ")"),
                _ => ("exists (", ")"),
            };
            out.push_str(before);
            print_chain(arg, false, out);
            out.push_str(after);
        }
        PathItem::LikeRegex { expr, pattern, flags } => {
            open(out);
            print_operand(expr, 6, out);
            out.push_str(" like_regex ");
            escape(pattern, out);
            if *flags != 0 {
                out.push_str(" flag \"");
                for (bit, c) in [
                    (JSONPATH_REGEX_ICASE, 'i'),
                    (JSONPATH_REGEX_DOTALL, 's'),
                    (JSONPATH_REGEX_MLINE, 'm'),
                    (JSONPATH_REGEX_WSPACE, 'x'),
                    (JSONPATH_REGEX_QUOTE, 'q'),
                ] {
                    if flags & bit != 0 {
                        out.push(c);
                    }
                }
                out.push('"');
            }
            close(out);
        }
        PathItem::AnyArray => out.push_str("[*]"),
        PathItem::AnyKey => {
            dot(out);
            out.push('*');
        }
        PathItem::IndexArray(elems) => {
            out.push('[');
            for (i, (from, to)) in elems.iter().enumerate() {
                if i > 0 {
                    out.push(',');
                }
                print_chain(from, false, out);
                if let Some(to) = to {
                    out.push_str(" to ");
                    print_chain(to, false, out);
                }
            }
            out.push(']');
        }
        PathItem::Any { first, last } => {
            dot(out);
            let bound =
                |n: u32| if n == JSONPATH_LAST { "last".to_string() } else { n.to_string() };
            if *first == 0 && *last == JSONPATH_LAST {
                out.push_str("**");
            } else if first == last {
                out.push_str(&format!("**{{{}}}", bound(*first)));
            } else {
                out.push_str(&format!("**{{{} to {}}}", bound(*first), bound(*last)));
            }
        }
        PathItem::Key(key) => {
            dot(out);
            escape(key, out);
        }
        PathItem::Current => out.push('@'),
        PathItem::Root => out.push('$'),
        PathItem::Variable(name) => {
            out.push('$');
            escape(name, out);
        }
        PathItem::Last => out.push_str("last"),
        PathItem::Method(op, args) => {
            out.push('.');
            out.push_str(op.name());
            out.push('(');
            for (i, arg) in args.iter().enumerate() {
                if i > 0 {
                    out.push(',');
                }
                print_item(arg, false, false, false, out);
            }
            out.push(')');
        }
    }
}

/// `jsonpath_out`: the text form of a path.
pub fn jsonpath_deparse(path: &JsonPath, out: &mut Vec<u8>) {
    let mut text = String::new();
    if !path.lax {
        text.push_str("strict ");
    }
    print_chain(&path.expr, true, &mut text);
    out.extend_from_slice(text.as_bytes());
}

/// `jsonpath_send`: the version 1 and the text form.
pub fn jsonpath_send(path: &JsonPath, out: &mut Vec<u8>) {
    out.push(1);
    jsonpath_deparse(path, out);
}

/// `jsonpath_recv`: a path from the version 1 and the text form. Each error is a hard error.
///
/// # Errors
///
/// `XX000` for another version, and the errors of [`jsonpath_parse`].
pub fn jsonpath_recv(recv: &mut Recv<'_>, regex: RegexCheck<'_>) -> Result<JsonPath, TypeError> {
    let version = recv.byte()?;
    if version != 1 {
        return Err(TypeError::new(
            SqlState::INTERNAL_ERROR,
            format!("unsupported jsonpath version number: {version}"),
        ));
    }
    jsonpath_parse(recv.text()?, false, regex)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn any_regex(_: &str, _: u32) -> Result<(), String> {
        Ok(())
    }

    fn paren_regex(pattern: &str, _: u32) -> Result<(), String> {
        if pattern.contains('(') { Err("parentheses () not balanced".to_string()) } else { Ok(()) }
    }

    fn out(text: &str) -> String {
        let path =
            jsonpath_parse(text, false, &any_regex).unwrap_or_else(|e| panic!("{text}: {e}"));
        let mut bytes = Vec::new();
        jsonpath_deparse(&path, &mut bytes);
        String::from_utf8(bytes).expect("utf8")
    }

    fn err(text: &str) -> String {
        match jsonpath_parse(text, false, &paren_regex) {
            Ok(_) => panic!("{text} is valid"),
            Err(e) => format!("{} {}", e.sqlstate.as_str(), e.message),
        }
    }

    #[test]
    fn text_form() {
        let cases = [
            ("1 + 2", "(1 + 2)"),
            ("1 + 2 + 3", "((1 + 2) + 3)"),
            ("1 * 2 + 3", "(1 * 2 + 3)"),
            ("- -1", "1"),
            ("-$.a", "(-$.\"a\")"),
            ("-(1).a", "(-(1).\"a\")"),
            ("-$.a * 2", "(-$.\"a\" * 2)"),
            ("1 / 2 % 3", "((1 / 2) % 3)"),
            ("$ ? (@ > 1)", "$?(@ > 1)"),
            ("$ ? ((@ > 1) is unknown)", "$?((@ > 1) is unknown)"),
            ("$ ? (exists (@.a))", "$?(exists (@.\"a\"))"),
            ("$ ? (!exists(@.a))", "$?(!(exists (@.\"a\")))"),
            ("$.a[1,2 to 3,last]", "$.\"a\"[1,2 to 3,last]"),
            ("$.**{last to 2}", "$.**{last to 2}"),
            ("$.**{0 to last}", "$.**"),
            ("$.**{last to last}", "$.**{last}"),
            ("$.decimal(4,-2)", "$.decimal(4,-2)"),
            ("$.Type()", "$.type()"),
            ("$.TYPE", "$.\"TYPE\""),
            ("STRICT $", "strict $"),
            ("lax 1", "1"),
            (".5", "0.5"),
            ("1e3", "1000"),
            ("0x1F", "31"),
            ("1_000.5_5", "1000.55"),
            ("$var.a", "$\"var\".\"a\""),
            ("(1 + $).a", "(1 + $).\"a\""),
            ("(($ > 1)).a", "($ > 1).\"a\""),
            ("\"\\v\\x41\\u{1F600}\"", "\"\\u000bA\u{1F600}\""),
            ("\"\\ud83d\\ude00\"", "\"\u{1F600}\""),
            ("$.\\x41", "$.\"A\""),
            ("$ /* c */ .a", "$.\"a\""),
            ("$ ? (@ like_regex \"a\" flag \"qx\")", "$?(@ like_regex \"a\" flag \"xq\")"),
            ("$ ? (-@ like_regex \"a\")", "$?((-@) like_regex \"a\")"),
            ("$[last ? (@ > last)]", "$[last?(@ > last)]"),
            ("$.split_part(\"\", +1)", "$.split_part(\"\",1)"),
            ("$.a.type ( )", "$.\"a\".type()"),
        ];
        for (text, want) in cases {
            assert_eq!(out(text), want, "{text}");
        }
    }

    #[test]
    fn errors() {
        let cases = [
            ("", "22P02 invalid input syntax for type jsonpath: \"\""),
            ("/* c */", "22P02 invalid input syntax for type jsonpath: \"/* c */\""),
            ("$ abc def", "42601 syntax error at or near \" \" of jsonpath input"),
            ("$.a/*c*/", "42601 syntax error at end of jsonpath input"),
            ("$.a/*c*/.b", "42601 syntax error at or near \".\" of jsonpath input"),
            ("$.1", "42601 syntax error at or near \".1\" of jsonpath input"),
            ("1a", "42601 trailing junk after numeric literal at or near \"1a\" of jsonpath input"),
            ("00", "42601 trailing junk after numeric literal at or near \"00\" of jsonpath input"),
            ("1e5xy", "42601 syntax error at end of jsonpath input"),
            (
                "1.5e3abc",
                "42601 trailing junk after numeric literal at or near \"1.5e3a\" of jsonpath input",
            ),
            ("1e+", "42601 invalid numeric literal at or near \"1e+\" of jsonpath input"),
            ("\"a", "42601 unterminated quoted string at end of jsonpath input"),
            ("$ /* a", "42601 unexpected end of comment at end of jsonpath input"),
            (
                "\"\\u12\"",
                "42601 invalid Unicode escape sequence at or near \"\\u12\" of jsonpath input",
            ),
            (
                "\"\\u{1234567}\"",
                "42601 invalid Unicode escape sequence at or near \"\\u{123456\" of jsonpath input",
            ),
            (
                "\"\\xZ\"",
                "42601 invalid hexadecimal character sequence at or near \"\\x\" of jsonpath input",
            ),
            ("\"\\u0000\"", "22P05 unsupported Unicode escape sequence"),
            ("\"\\ud83d\"", "22P02 invalid input syntax for type jsonpath"),
            ("\"\\u{110000}\"", "42601 invalid Unicode code point"),
            ("@", "42601 @ is not allowed in root expressions"),
            ("$.a[@]", "42601 @ is not allowed in root expressions"),
            ("last + @", "42601 LAST is allowed only in array subscripts"),
            ("$ > 1 > 2", "42601 syntax error at or near \">\" of jsonpath input"),
            ("($ > 1) + 2", "42601 syntax error at or near \"+\" of jsonpath input"),
            ("!($.a)", "42601 syntax error at or near \")\" of jsonpath input"),
            ("!($ > 1) is unknown", "42601 syntax error at or near \" \" of jsonpath input"),
            ("exists($ > 1)", "42601 syntax error at or near \">\" of jsonpath input"),
            ("exists(($ > 1))", "42601 syntax error at or near \")\" of jsonpath input"),
            ("-($ > 1)", "42601 syntax error at end of jsonpath input"),
            ("$ ? (true)", "42601 syntax error at or near \")\" of jsonpath input"),
            ("$ ? (@ == TRUE)", "42601 syntax error at end of jsonpath input"),
            ("$.time(\"x\")", "42601 syntax error at or near \"\"\" of jsonpath input"),
            ("$[1, *]", "42601 syntax error at or near \"*\" of jsonpath input"),
            ("$.decimal(1, 2, 3) junk", "42601 invalid input syntax for type jsonpath"),
            ("$ like_regex \"(a\" flag \"z\"", "42601 invalid input syntax for type jsonpath"),
            (
                "$ like_regex \"(a\" junk",
                "2201B invalid regular expression: parentheses () not balanced",
            ),
            (
                "$ like_regex \"a\" flag \"x\"",
                "0A000 XQuery \"x\" flag (expanded regular expressions) is not implemented",
            ),
            ("$.**{99999999999}", "22003 value \"99999999999\" is out of range for type integer"),
            ("lax strict $", "42601 syntax error at or near \" \" of jsonpath input"),
            ("strict", "42601 syntax error at end of jsonpath input"),
        ];
        for (text, want) in cases {
            assert_eq!(err(text), want, "{text}");
        }
        let soft = jsonpath_parse("\"\\u{110000}\"", true, &any_regex).expect_err("soft");
        assert_eq!(soft.message, "could not convert Unicode to server encoding");
    }

    #[test]
    fn binary_form() {
        let path = jsonpath_parse("strict $.a ? (@ > 1)", false, &any_regex).expect("path");
        let mut bytes = Vec::new();
        jsonpath_send(&path, &mut bytes);
        assert_eq!(bytes, b"\x01strict $.\"a\"?(@ > 1)");
        assert_eq!(jsonpath_recv(&mut Recv::new(&bytes), &any_regex).expect("recv"), path);
        let error = jsonpath_recv(&mut Recv::new(b"\x02$"), &any_regex).expect_err("version");
        assert_eq!(error.message, "unsupported jsonpath version number: 2");
    }
}