|
| 1 | +-- This algorithm is from Regular Expression Sub-Matching using Partial Derivatives - Martin Sulzmann and Kenny Zhuo Ming Lu |
| 2 | + |
| 3 | +-- Thank you Keegan Perry for simplifying and explaining this to me. |
| 4 | +import Validator.Learning.Cegex.Cegex |
| 5 | + |
| 6 | +namespace CegexCapture |
| 7 | + |
| 8 | +-- neutralize replaces all chars with emptyset. |
| 9 | +-- This way the expression will stay nullable and not change based on derivative input. |
| 10 | +-- This makes it possible to keep all the capture groups inside for later extraction. |
| 11 | +def neutralize (x: Cegex): Cegex := |
| 12 | + match x with |
| 13 | + | Cegex.emptyset => Cegex.emptyset |
| 14 | + | Cegex.epsilon => Cegex.epsilon |
| 15 | + | Cegex.char _ => Cegex.emptyset |
| 16 | + | Cegex.or y z => Cegex.smartOr (neutralize y) (neutralize z) |
| 17 | + | Cegex.concat y z => Cegex.concat (neutralize y) (neutralize z) |
| 18 | + | Cegex.star y => Cegex.star (neutralize y) |
| 19 | + | Cegex.group id c y => Cegex.group id c (neutralize y) |
| 20 | + |
| 21 | +partial def derive (x: Cegex) (char: Char): Cegex := |
| 22 | + match x with |
| 23 | + | Cegex.emptyset => Cegex.emptyset |
| 24 | + | Cegex.epsilon => Cegex.emptyset |
| 25 | + | Cegex.char char' => |
| 26 | + if char' = char |
| 27 | + then Cegex.epsilon |
| 28 | + else Cegex.emptyset |
| 29 | + | Cegex.or y z => Cegex.smartOr (derive y char) (derive z char) |
| 30 | + | Cegex.concat y z => |
| 31 | + if Cegex.nullable y |
| 32 | + then Cegex.smartOr |
| 33 | + (Cegex.smartConcat (derive y char) z) |
| 34 | + -- A difference from the usual derive algorithm: |
| 35 | + -- Instead of (derive z tree), we write: |
| 36 | + (Cegex.smartConcat (neutralize y) (derive z char)) |
| 37 | + else Cegex.concat (derive y char) z |
| 38 | + | Cegex.star y => Cegex.smartConcat (derive y char) x |
| 39 | + -- group is the new operator compared to Expr. |
| 40 | + -- We store the input tree in the expression. |
| 41 | + | Cegex.group n chars y => |
| 42 | + Cegex.group n (char :: chars) (derive y char) |
| 43 | + |
| 44 | +def extract (x: Cegex): List (Nat × List Char) := |
| 45 | + match x with |
| 46 | + -- should never be encountered, since emptyset is not nullable. |
| 47 | + | Cegex.emptyset => [] |
| 48 | + | Cegex.epsilon => [] |
| 49 | + -- should never be encountered, since char is not nullable. |
| 50 | + | Cegex.char _ => [] |
| 51 | + | Cegex.or y z => |
| 52 | + -- Under POSIX semantics, we prefer matching the left alternative. |
| 53 | + if y.nullable |
| 54 | + then extract y |
| 55 | + else extract z |
| 56 | + | Cegex.concat y z => |
| 57 | + extract y ++ extract z |
| 58 | + -- Groups under a star are ignored. |
| 59 | + -- Recursively extracting under the star causes empty captures to be reported, which we do not want under POSIX semantics. |
| 60 | + | Cegex.star _ => [] |
| 61 | + | Cegex.group id c y => (id, c) :: extract y |
| 62 | + |
| 63 | +def captures (x: Cegex) (chars: List Char): Option (List (Nat × List Char)) := |
| 64 | + let dx := List.foldl derive x chars |
| 65 | + if dx.nullable |
| 66 | + then Option.some (extract dx) |
| 67 | + else Option.none |
| 68 | + |
| 69 | +def capture (name: Nat) (x: Cegex) (input: String): Option String := |
| 70 | + match input with |
| 71 | + | String.mk chars => |
| 72 | + match captures x chars with |
| 73 | + | Option.none => Option.none |
| 74 | + | Option.some cs => |
| 75 | + let strs := List.filterMap |
| 76 | + (fun (name', str) => |
| 77 | + if name = name' |
| 78 | + then Option.some (String.mk str) |
| 79 | + else Option.none |
| 80 | + ) cs |
| 81 | + List.head? (List.reverse (List.mergeSort strs)) |
| 82 | + |
| 83 | +#guard capture 1 (Cegex.concat (Cegex.concat |
| 84 | + (Cegex.star (Cegex.char 'a')) |
| 85 | + (Cegex.group 1 [] (Cegex.char 'b'))) |
| 86 | + (Cegex.star (Cegex.char 'a')) |
| 87 | + ) |
| 88 | + "aaabaaa" = |
| 89 | + Option.some "b" |
| 90 | + |
| 91 | +#guard capture 1 (Cegex.concat (Cegex.concat |
| 92 | + (Cegex.star (Cegex.char 'a')) |
| 93 | + (Cegex.group 1 [] (Cegex.star (Cegex.char 'b')))) |
| 94 | + (Cegex.star (Cegex.char 'a')) |
| 95 | + ) |
| 96 | + "aaabbbaaa" = |
| 97 | + Option.some "bbb" |
| 98 | + |
| 99 | +#guard capture 1 (Cegex.concat (Cegex.concat |
| 100 | + (Cegex.star (Cegex.char 'a')) |
| 101 | + (Cegex.group 1 [] |
| 102 | + (Cegex.or |
| 103 | + (Cegex.star (Cegex.char 'b')) |
| 104 | + (Cegex.star (Cegex.char 'c')) |
| 105 | + ) |
| 106 | + )) |
| 107 | + (Cegex.star (Cegex.char 'a')) |
| 108 | + ) |
| 109 | + "aaacccaaa" = |
| 110 | + Option.some "ccc" |
0 commit comments