|
| 1 | +(* Copyright (C) 2017 Petter A. Urkedal <[email protected]> |
| 2 | + * |
| 3 | + * This library is free software; you can redistribute it and/or modify it |
| 4 | + * under the terms of the GNU Lesser General Public License as published by |
| 5 | + * the Free Software Foundation, either version 3 of the License, or (at your |
| 6 | + * option) any later version, with the OCaml static compilation exception. |
| 7 | + * |
| 8 | + * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 11 | + * License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU Lesser General Public License |
| 14 | + * along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + *) |
| 16 | + |
| 17 | +let test re s = |
| 18 | + match Tyre.exec re s with |
| 19 | + | Ok b -> b |
| 20 | + | Error e -> |
| 21 | + Format.eprintf "Error: %a@." Tyre.pp_error e; |
| 22 | + assert false |
| 23 | +let (%%) = test |
| 24 | +let (%%%) a b = assert (a %% b) |
| 25 | + |
| 26 | +let () = |
| 27 | + (function%tyre _ -> true) %%% "%"; |
| 28 | + (function%tyre s -> s = "%") %%%"%" |
| 29 | + |
| 30 | +type t = [ |
| 31 | + | `Attr of string * string option |
| 32 | + | `Comment of string |
| 33 | + | `Even_sigils of string option |
| 34 | + | `Odd_sigils |
| 35 | + | `Unknown ] |
| 36 | + |
| 37 | +let test1 : t Tyre.re = |
| 38 | + (function%tyre |
| 39 | + | {|^(?<k>.*): *(?<v>.+)?$|} -> `Attr (k, v) |
| 40 | + | {|^# (?<comment>.+)$|} -> `Comment comment |
| 41 | + | {|^(?<sigil>([@%]{2})+)?$|} -> `Even_sigils sigil |
| 42 | + | {|^[@%]|} -> `Odd_sigils |
| 43 | + | _ -> `Unknown) |
| 44 | + |
| 45 | +let () = |
| 46 | + assert (test1 %% "x: 1" = `Attr ("x", Some "1")); |
| 47 | + assert (test1 %% "# Kommentar" = `Comment "Kommentar"); |
| 48 | + assert (test1 %% "" = `Even_sigils None); |
| 49 | + assert (test1 %% "%%%@" = `Even_sigils (Some "%%%@")); |
| 50 | + assert (test1 %% "%%@" = `Odd_sigils) |
| 51 | + |
| 52 | +let concat_gen sep gen = |
| 53 | + let rec f () = |
| 54 | + match gen () with |
| 55 | + | None -> "" |
| 56 | + | Some s -> s ^ sep ^ f () |
| 57 | + in |
| 58 | + f () |
| 59 | + |
| 60 | +let test2 = function%tyre |
| 61 | + | {|^<>$|} -> (=) "<>" |
| 62 | + | {|^<(?<x>[^<>]+)>$|} -> fun s -> s = "<" ^ x ^ ">" |
| 63 | + | {|^<(?<x>[^<>]+)><(?<y>[^<>]+)>$|} -> fun s -> s = "<" ^ x ^ "><" ^ y ^ ">" |
| 64 | + | {|^((?<elt>[^;<>]);)*$|} -> fun s -> concat_gen ";" elt = s |
| 65 | + | {|^(?<a>one)|(?<b>two)$|} as x -> |
| 66 | + (match x with |
| 67 | + | `a a -> fun s -> a = s && a = "one" |
| 68 | + | `b b -> fun s -> b = s && b = "two") |
| 69 | + |
| 70 | +let (%%%%) re s = (re %% s) s |
| 71 | + |
| 72 | +let () = |
| 73 | + assert (test2 %%%%"<>"); |
| 74 | + assert (test2 %%%%"<a>"); |
| 75 | + assert (test2 %%%%"<ab>"); |
| 76 | + assert (test2 %%%%"<a><b>"); |
| 77 | + assert (test2 %%%%"<ab><cde>"); |
| 78 | + assert (test2 %%%%"a;"); |
| 79 | + assert (test2 %%%%"a;b;c;d;"); |
| 80 | + assert (test2 %%%%"<a;b>"); |
| 81 | + assert (test2 %%%%"one"); |
| 82 | + assert (test2 %%%%"two") |
| 83 | + |
| 84 | +(* It should work in a functor, and Re_pcre.regxp should be lifted to the |
| 85 | + * top-level. *) |
| 86 | +module F (M : Map.OrderedType) = struct |
| 87 | + let f = function%tyre |
| 88 | + | {|#(?<space>\s)?(?<comment>.*)|} -> Some (space <> None, comment) |
| 89 | + | _ -> None |
| 90 | +end |
0 commit comments