Debian Codegraph @

vendor/re/src/core.ml

raw
1 (*
2 RE - A regular expression library
3
4 Copyright (C) 2001 Jerome Vouillon
5 email: [email protected]
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation, with
10 linking exception; either version 2.1 of the License, or (at
11 your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *)
22
23 open Import
24
25 include struct
26 let cset = Ast.cset
27 let rg c c' = cset (Cset.cseq c c')
28 let notnl = cset Cset.notnl
29 let lower = cset Cset.lower
30 let upper = cset Cset.upper
31 let alpha = cset Cset.alpha
32 let digit = cset Cset.cdigit
33 let alnum = cset Cset.alnum
34 let wordc = cset Cset.wordc
35 let ascii = cset Cset.ascii
36 let blank = cset Cset.blank
37 let cntrl = cset Cset.cntrl
38 let graph = cset Cset.graph
39 let print = cset Cset.print
40 let punct = cset Cset.punct
41 let space = cset Cset.space
42 let xdigit = cset Cset.xdigit
43 end
44
45 include Ast.Export
46
47 let exec_internal ?(pos = 0) ?(len = -1) ~partial ~groups re s =
48 Compile.match_str ~groups ~partial re s ~pos ~len
49 ;;
50
51 let exec ?pos ?len re s =
52 match exec_internal ?pos ?len ~groups:true ~partial:false re s with
53 | Match substr -> substr
54 | _ -> raise Not_found
55 ;;
56
57 let exec_opt ?pos ?len re s =
58 match exec_internal ?pos ?len ~groups:true ~partial:false re s with
59 | Match substr -> Some substr
60 | _ -> None
61 ;;
62
63 let execp ?(pos = 0) ?(len = -1) re s = Compile.match_str_p ~pos ~len re s
64
65 let exec_partial ?pos ?len re s =
66 match exec_internal ~groups:false ~partial:true ?pos ?len re s with
67 | Match _ -> `Full
68 | Running _ -> `Partial
69 | Failed -> `Mismatch
70 ;;
71
72 let exec_partial_detailed ?pos ?len re s =
73 match exec_internal ~groups:true ~partial:true ?pos ?len re s with
74 | Match group -> `Full group
75 | Running { no_match_starts_before } -> `Partial no_match_starts_before
76 | Failed -> `Mismatch
77 ;;
78
79 module Mark = struct
80 type t = Pmark.t
81
82 let test (g : Group.t) p = Pmark.Set.mem p (Group.pmarks g)
83 let all (g : Group.t) = Group.pmarks g
84
85 module Set = Pmark.Set
86
87 let equal = Pmark.equal
88 let compare = Pmark.compare
89 end
90
91 type split_token =
92 [ `Text of string
93 | `Delim of Group.t
94 ]
95
96 module Gen = struct
97 type 'a gen = unit -> 'a option
98
99 let gen_of_seq (s : 'a Seq.t) : 'a gen =
100 let r = ref s in
101 fun () ->
102 match !r () with
103 | Seq.Nil -> None
104 | Seq.Cons (x, tl) ->
105 r := tl;
106 Some x
107 ;;
108
109 let split ?pos ?len re s : _ gen = Search.split ?pos ?len re s |> gen_of_seq
110 let split_full ?pos ?len re s : _ gen = Search.split_full ?pos ?len re s |> gen_of_seq
111 let all ?pos ?len re s = Search.all ?pos ?len re s |> gen_of_seq
112 let matches ?pos ?len re s = Search.matches ?pos ?len re s |> gen_of_seq
113 end
114
115 module Group = Group
116
117 (** {2 Deprecated functions} *)
118
119 let split_full_seq = Search.split_full
120 let split_seq = Search.split
121 let matches_seq = Search.matches
122 let all_seq = Search.all
123
124 type 'a gen = 'a Gen.gen
125
126 let all_gen = Gen.all
127 let matches_gen = Gen.matches
128 let split_gen = Gen.split
129 let split_full_gen = Gen.split_full
130
131 type substrings = Group.t
132
133 let get = Group.get
134 let get_ofs = Group.offset
135 let get_all = Group.all
136 let get_all_ofs = Group.all_offset
137 let test = Group.test
138
139 type markid = Mark.t
140
141 let marked = Mark.test
142 let mark_set = Mark.all
143
144 type groups = Group.t
145
146 module List = struct
147 let list_of_seq (s : 'a Seq.t) : 'a list =
148 Seq.fold_left (fun l x -> x :: l) [] s |> List.rev
149 ;;
150
151 let all ?pos ?len re s = Search.all ?pos ?len re s |> list_of_seq
152 let matches ?pos ?len re s = Search.matches ?pos ?len re s |> list_of_seq
153 let split_full ?pos ?len re s = Search.split_full ?pos ?len re s |> list_of_seq
154 let split ?pos ?len re s = Search.split ?pos ?len re s |> list_of_seq
155 let split_delim ?pos ?len re s = Search.split_delim ?pos ?len re s |> list_of_seq
156 end
157
158 include List
159
160 include struct
161 open Compile
162
163 type nonrec re = re
164
165 let compile = compile
166 let pp_re = pp_re
167 let print_re = pp_re
168 let group_names = group_names
169 let group_count = group_count
170 end
171
172 module Seq = Search