|
14 | 14 | * along with this library. If not, see <http://www.gnu.org/licenses/>.
|
15 | 15 | *)
|
16 | 16 |
|
17 |
| -let f = |
| 17 | +let test1 = |
18 | 18 | (function%pcre
|
19 | 19 | | {|^(?<k>.*): *(?<v>.+)?$|} -> `Attr (k, v)
|
20 | 20 | | {|^# (?<comment>.+)$|} -> `Comment comment
|
| 21 | + | {|^((?<last>[@%]){2}){0,2}$|} -> `Even_sigils last |
| 22 | + | {|^[@%]|} -> `Odd_sigils |
21 | 23 | | _ -> `Unknown)
|
22 | 24 |
|
23 | 25 | let () =
|
24 |
| - assert (f "x: 1" = `Attr ("x", Some "1")); |
25 |
| - assert (f "# Kommentar" = `Comment "Kommentar") |
| 26 | + assert (test1 "x: 1" = `Attr ("x", Some "1")); |
| 27 | + assert (test1 "# Kommentar" = `Comment "Kommentar"); |
| 28 | + assert (test1 "" = `Even_sigils None); |
| 29 | + assert (test1 "%%%@" = `Even_sigils (Some "@")); |
| 30 | + assert (test1 "%%@" = `Odd_sigils) |
26 | 31 |
|
| 32 | +let last_elt s = |
| 33 | + let n = String.length s in |
| 34 | + assert (s.[n - 1] = ';'); |
| 35 | + let i = try String.rindex_from s (n - 2) ';' + 1 with Not_found -> 0 in |
| 36 | + String.sub s i (n - i - 1) |
| 37 | + |
| 38 | +let rec test2 s = |
| 39 | + (match%pcre s with |
| 40 | + | {|^<>$|} -> assert (s = "<>") |
| 41 | + | {|^<(?<x>[^<>]+)>$|} -> assert (s = "<" ^ x ^ ">") |
| 42 | + | {|^<(?<x>[^<>]+)><(?<y>[^<>]+)>$|} -> assert (s = "<" ^ x ^ "><" ^ y ^ ">") |
| 43 | + | {|^((?<elt>[^;<>]);)+$|} -> assert (elt = last_elt s) |
| 44 | + | {|^[^{}]*\{(?<s'>.*)\}|} -> test2 s' |
| 45 | + | _ -> assert false) |
| 46 | + |
| 47 | +let () = |
| 48 | + test2 "<>"; |
| 49 | + test2 "<a>"; |
| 50 | + test2 "<ab>"; |
| 51 | + test2 "<a><b>"; |
| 52 | + test2 "<ab><cde>"; |
| 53 | + test2 "a;"; |
| 54 | + test2 "a;b;c;d;"; |
| 55 | + test2 "<a;b>"; |
| 56 | + test2 "Xx{--{a;b;c;}--}yY." |
| 57 | + |
| 58 | +(* It should work in a functor, and Re_pcre.regxp should be lifted to the |
| 59 | + * top-level. *) |
27 | 60 | module F (M : Map.OrderedType) = struct
|
28 | 61 | let f x =
|
29 | 62 | (match%pcre x with
|
30 | 63 | | {|#(?<space>\s)?(?<comment>.*)|} -> Some (space <> None, comment)
|
31 | 64 | | _ -> None)
|
32 | 65 | end
|
33 | 66 |
|
| 67 | +(* It should work as a top-level eval. *) |
34 | 68 | let r = ref false
|
35 | 69 | ;;(match%pcre "" with
|
36 | 70 | | "$^" -> r := true
|
|
0 commit comments