Commit ec3b86c
[Pratt Parser] Add a fuzzer comparing ANTLR and Pratt parser outputs and fix uncovered discrepancies
Added `CelPrattParserFuzzer` to fuzz CEL inputs against both `AntlrParser` and `PrattParser`, asserting error parity and AST equality.
Parser differences uncovered and fixed in `PrattParser` / `Lexer`:
1. **Mixed unary operator chains (e.g., `-!ll`, `!-x`)**: ANTLR requires consecutive unary operators to be homogeneous (`!`/`-`) and only allows `-` after `!` when immediately followed by an integer or floating-point literal (e.g., `!-42`), whereas Pratt previously allowed arbitrary mixtures of `!` and `-` without parentheses.
2. **Vertical tab (`\v`, ASCII 11)**: ANTLR does not treat `\v` as whitespace, whereas `Lexer` and `PrattParser` previously skipped it.
3. **Unquoted `.in` field selector**: ANTLR treats `in` as a keyword token and rejects unquoted `x.in` (requiring backtick-quoted `` x.`in` ``), whereas Pratt previously accepted unquoted `.in` after `.`.
4. **Chained optional select (`T.?a.?a`) AST positions**: ANTLR records the position of the field constant in `_?._` at the start of the `member` expression (`T`), whereas Pratt stopped at intermediate `.?`/`[]`/`()` nodes.
5. **Numeric literals immediately followed by identifier characters (e.g., `9in-x`)**: ANTLR tokenizes numeric literals (`NUM_INT`, `NUM_UINT`, `NUM_FLOAT`) without rejecting trailing identifier characters so `9in-x` parses as `9 in -x`, whereas Pratt's `Lexer` previously rejected trailing identifier characters at lexing time.
6. **Invalid quoted field selectors inside `has(...)` (e.g., `` has(a.`$b`) ``)**: When `normalizeIdent()` rejects an invalid backtick-quoted field name, `PrattParser` previously still constructed a `CelSelect` with an empty field string, causing `CelExprFactory.newSelect()` to throw `IllegalArgumentException` during `has()` macro expansion instead of returning an unset error expression like `AntlrParser`.
Intentional parser differences ignored by `CelPrattParserFuzzer` (where `PrattParser` behavior is preferred):
1. **Raw byte string literal prefixes (`rb'...'`, `rB'...'`, `Rb'...'`, `RB'...'`)**: ANTLR only accepts `br`/`bR`/`Br`/`BR` prefix order, whereas Pratt accepts both `br` and `rb`.
2. **Standalone commas in empty collection literals (`[,]`, `{,}`, `Msg{,}`)**: ANTLR accepts empty collections containing only a comma, whereas Pratt requires at least one element/entry before a trailing comma.
3. **Leading-dot identifier positions (`.R`)**: Pratt records the position of leading-dot identifiers at the `.` token, whereas ANTLR records it at the identifier token after `.`.
PiperOrigin-RevId: 9822252631 parent c182a1d commit ec3b86c
11 files changed
Lines changed: 610 additions & 128 deletions
File tree
- common/src/main/java/dev/cel/common/internal
- parser/src
- main/java/dev/cel/parser
- test
- java/dev/cel/parser
- resources
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
| 60 | + | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
| 86 | + | |
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
428 | 428 | | |
429 | 429 | | |
430 | 430 | | |
431 | | - | |
432 | 431 | | |
433 | 432 | | |
434 | 433 | | |
| |||
589 | 588 | | |
590 | 589 | | |
591 | 590 | | |
592 | | - | |
| 591 | + | |
593 | 592 | | |
594 | 593 | | |
595 | 594 | | |
596 | 595 | | |
597 | 596 | | |
598 | | - | |
599 | | - | |
600 | | - | |
601 | | - | |
602 | | - | |
603 | | - | |
604 | 597 | | |
605 | 598 | | |
606 | 599 | | |
| |||
622 | 615 | | |
623 | 616 | | |
624 | 617 | | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | | - | |
629 | 618 | | |
630 | 619 | | |
631 | 620 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | 19 | | |
21 | 20 | | |
22 | 21 | | |
| |||
524 | 523 | | |
525 | 524 | | |
526 | 525 | | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
531 | 544 | | |
532 | | - | |
| 545 | + | |
533 | 546 | | |
534 | 547 | | |
535 | 548 | | |
536 | | - | |
| 549 | + | |
537 | 550 | | |
538 | 551 | | |
539 | 552 | | |
540 | 553 | | |
541 | | - | |
| 554 | + | |
| 555 | + | |
542 | 556 | | |
543 | 557 | | |
544 | 558 | | |
| |||
558 | 572 | | |
559 | 573 | | |
560 | 574 | | |
561 | | - | |
562 | | - | |
563 | | - | |
| 575 | + | |
564 | 576 | | |
565 | 577 | | |
566 | 578 | | |
| |||
570 | 582 | | |
571 | 583 | | |
572 | 584 | | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
573 | 590 | | |
574 | 591 | | |
575 | 592 | | |
576 | | - | |
| 593 | + | |
577 | 594 | | |
| 595 | + | |
578 | 596 | | |
579 | 597 | | |
580 | 598 | | |
| |||
584 | 602 | | |
585 | 603 | | |
586 | 604 | | |
| 605 | + | |
587 | 606 | | |
588 | 607 | | |
| 608 | + | |
589 | 609 | | |
590 | 610 | | |
591 | 611 | | |
| |||
607 | 627 | | |
608 | 628 | | |
609 | 629 | | |
| 630 | + | |
610 | 631 | | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
611 | 635 | | |
612 | 636 | | |
613 | 637 | | |
614 | 638 | | |
615 | 639 | | |
| 640 | + | |
616 | 641 | | |
617 | 642 | | |
618 | 643 | | |
| |||
622 | 647 | | |
623 | 648 | | |
624 | 649 | | |
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 | | - | |
| 650 | + | |
| 651 | + | |
660 | 652 | | |
661 | 653 | | |
662 | | - | |
663 | | - | |
| 654 | + | |
664 | 655 | | |
665 | 656 | | |
666 | 657 | | |
667 | | - | |
668 | | - | |
669 | | - | |
670 | | - | |
671 | | - | |
672 | | - | |
673 | | - | |
674 | | - | |
675 | | - | |
676 | | - | |
677 | | - | |
678 | | - | |
679 | | - | |
680 | | - | |
681 | | - | |
682 | | - | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
683 | 671 | | |
684 | | - | |
685 | | - | |
686 | | - | |
687 | | - | |
688 | | - | |
| 672 | + | |
689 | 673 | | |
690 | 674 | | |
691 | | - | |
692 | | - | |
693 | | - | |
694 | | - | |
695 | | - | |
696 | | - | |
697 | | - | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
698 | 681 | | |
699 | 682 | | |
700 | 683 | | |
701 | 684 | | |
| 685 | + | |
702 | 686 | | |
703 | 687 | | |
704 | 688 | | |
| |||
707 | 691 | | |
708 | 692 | | |
709 | 693 | | |
710 | | - | |
711 | | - | |
712 | | - | |
713 | | - | |
714 | | - | |
715 | | - | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
716 | 706 | | |
717 | | - | |
| 707 | + | |
718 | 708 | | |
719 | 709 | | |
720 | 710 | | |
| |||
1044 | 1034 | | |
1045 | 1035 | | |
1046 | 1036 | | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
1047 | 1044 | | |
1048 | 1045 | | |
1049 | 1046 | | |
| |||
1066 | 1063 | | |
1067 | 1064 | | |
1068 | 1065 | | |
1069 | | - | |
1070 | | - | |
1071 | | - | |
1072 | | - | |
1073 | | - | |
1074 | | - | |
1075 | | - | |
1076 | 1066 | | |
1077 | 1067 | | |
1078 | 1068 | | |
| |||
1188 | 1178 | | |
1189 | 1179 | | |
1190 | 1180 | | |
1191 | | - | |
| 1181 | + | |
1192 | 1182 | | |
1193 | 1183 | | |
1194 | 1184 | | |
| |||
0 commit comments