Commit 9d5280e
committed
Fully generalize "whole match" in the engine and enable transforming custom types
* Track the whole match as an element of the "capture list" in the matching engine. Do so by emitting code as an implicit `capture` around the root node.
* No longer handle `matcher` as a special case within `capture` lowering, because the matcher can be arbitrarily nested within "output-forwarding" nodes, such as a `changeMatchingOptions` non-capturing group. Instead, make the bytecode emitter carry a result value so that a custom output can be propagated through any forwarding nodes.
```swift
Regex {
Capture(
SemanticVersionParser()
.ignoringCase()
.matchingSemantics(.unicodeScalar)
) // This would not work previously.
}
```
* Collapse DSLTree node `transform` into `capture`, because a transform can never be standalone (without a `capture` parent). This greatly simplifies `capture` lowering.
* Make the bytecode's capture transform use type `(Input, _StoredCapture) -> Any` so that it can transform any whole match, not just `Substring`. This means you can now transform any captured value, including a custom-consuming regex component's result!
```swift
Regex {
"version:"
OneOrMore(.whitespace)
Capture {
SemanticVersionParser() // Regex<SemanticVersion>
} transform: {
// (SemanticVersion) -> SomethingElse
}
}
```
The transforms of `Capture` and `TryCapture` are now generalized from taking `Substring` to taking generic parameter `W` (the whole match).
* Fix an issue where initial options were applied based solely on whether the bytecode had any instructions, failing examples such as `((?i:.))`. It now checks whether the first matchable atom has been emitted.1 parent a7001b1 commit 9d5280e
File tree
27 files changed
+676
-943
lines changed- Sources
- RegexBuilder
- VariadicsGenerator
- _RegexParser/Regex/Parse
- _StringProcessing
- Engine
- Regex
- Utility
- Tests
- RegexBuilderTests
- RegexTests
27 files changed
+676
-943
lines changedLarge diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
646 | 646 | | |
647 | 647 | | |
648 | 648 | | |
649 | | - | |
| 649 | + | |
650 | 650 | | |
651 | | - | |
652 | | - | |
653 | | - | |
654 | | - | |
655 | | - | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
656 | 654 | | |
657 | 655 | | |
658 | 656 | | |
659 | 657 | | |
660 | 658 | | |
661 | 659 | | |
662 | | - | |
| 660 | + | |
663 | 661 | | |
664 | 662 | | |
665 | 663 | | |
666 | | - | |
667 | | - | |
668 | | - | |
669 | | - | |
670 | | - | |
| 664 | + | |
| 665 | + | |
671 | 666 | | |
672 | 667 | | |
673 | 668 | | |
| |||
676 | 671 | | |
677 | 672 | | |
678 | 673 | | |
679 | | - | |
| 674 | + | |
680 | 675 | | |
681 | | - | |
682 | | - | |
683 | | - | |
684 | | - | |
685 | | - | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
686 | 679 | | |
687 | 680 | | |
688 | 681 | | |
689 | 682 | | |
690 | 683 | | |
691 | 684 | | |
692 | | - | |
| 685 | + | |
693 | 686 | | |
694 | 687 | | |
695 | 688 | | |
696 | | - | |
697 | | - | |
698 | | - | |
699 | | - | |
700 | | - | |
| 689 | + | |
| 690 | + | |
701 | 691 | | |
702 | 692 | | |
703 | 693 | | |
| |||
725 | 715 | | |
726 | 716 | | |
727 | 717 | | |
728 | | - | |
| 718 | + | |
729 | 719 | | |
730 | | - | |
731 | | - | |
732 | | - | |
733 | | - | |
734 | | - | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
735 | 723 | | |
736 | 724 | | |
737 | 725 | | |
738 | 726 | | |
739 | 727 | | |
740 | 728 | | |
741 | | - | |
| 729 | + | |
742 | 730 | | |
743 | 731 | | |
744 | 732 | | |
745 | | - | |
746 | | - | |
747 | | - | |
748 | | - | |
749 | | - | |
| 733 | + | |
| 734 | + | |
750 | 735 | | |
751 | 736 | | |
752 | 737 | | |
| |||
755 | 740 | | |
756 | 741 | | |
757 | 742 | | |
758 | | - | |
| 743 | + | |
759 | 744 | | |
760 | | - | |
761 | | - | |
762 | | - | |
763 | | - | |
764 | | - | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
765 | 748 | | |
766 | 749 | | |
767 | 750 | | |
768 | 751 | | |
769 | 752 | | |
770 | 753 | | |
771 | | - | |
| 754 | + | |
772 | 755 | | |
773 | 756 | | |
774 | 757 | | |
775 | | - | |
776 | | - | |
777 | | - | |
778 | | - | |
779 | | - | |
| 758 | + | |
| 759 | + | |
780 | 760 | | |
781 | 761 | | |
782 | 762 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | 125 | | |
132 | 126 | | |
133 | 127 | | |
134 | | - | |
| 128 | + | |
135 | 129 | | |
136 | | - | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
137 | 134 | | |
138 | 135 | | |
139 | 136 | | |
| |||
151 | 148 | | |
152 | 149 | | |
153 | 150 | | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
| 151 | + | |
160 | 152 | | |
161 | 153 | | |
162 | 154 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
225 | 225 | | |
226 | 226 | | |
227 | 227 | | |
228 | | - | |
| 228 | + | |
229 | 229 | | |
230 | 230 | | |
231 | 231 | | |
| |||
246 | 246 | | |
247 | 247 | | |
248 | 248 | | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
| 249 | + | |
253 | 250 | | |
254 | 251 | | |
255 | 252 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
| 80 | + | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| |||
0 commit comments