Source file ast.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
open Import
type ('a, _) ast =
| Alternative : 'a list -> ('a, [> `Uncased ]) ast
| No_case : 'a -> ('a, [> `Cased ]) ast
| Case : 'a -> ('a, [> `Cased ]) ast
let empty_alternative : ('a, 'b) ast = Alternative []
let equal_ast (type a) eq (x : (a, [ `Uncased ]) ast) (y : (a, [ `Uncased ]) ast) =
match x, y with
| Alternative a, Alternative b -> List.equal ~eq a b
;;
let pp_ast (type a b) f fmt (ast : (a, b) ast) =
let open Fmt in
let var s re = sexp fmt s f re in
match ast with
| Alternative alt -> sexp fmt "Alternative" (list f) alt
| Case c -> var "Case" c
| No_case c -> var "No_case" c
;;
type cset =
| Cset of Cset.t
| Intersection of cset list
| Complement of cset list
| Difference of cset * cset
| Cast of (cset, [ `Cased | `Uncased ]) ast
type ('a, 'case) gen =
| Set of 'a
| Ast of (('a, 'case) gen, 'case) ast
| Sequence of ('a, 'case) gen list
| Repeat of ('a, 'case) gen * int * int option
| Beg_of_line
| End_of_line
| Beg_of_word
| End_of_word
| Not_bound
| Beg_of_str
| End_of_str
| Last_end_of_line
| Start
| Stop
| Group of string option * ('a, 'case) gen
| No_group of ('a, 'case) gen
| Nest of ('a, 'case) gen
| Pmark of Pmark.t * ('a, 'case) gen
| Sem of Automata.Sem.t * ('a, 'case) gen
| Sem_greedy of Automata.Rep_kind.t * ('a, 'case) gen
let rec pp_gen pp_cset fmt t =
let open Format in
let open Fmt in
let pp = pp_gen pp_cset in
let var s re = sexp fmt s pp re in
let seq s rel = sexp fmt s (list pp) rel in
match t with
| Set cset -> pp_cset fmt cset
| Sequence sq -> seq "Sequence" sq
| Repeat (re, start, stop) ->
let pp' fmt () = fprintf fmt "%a@ %d%a" pp re start optint stop in
sexp fmt "Repeat" pp' ()
| Beg_of_line -> str fmt "Beg_of_line"
| End_of_line -> str fmt "End_of_line"
| Beg_of_word -> str fmt "Beg_of_word"
| End_of_word -> str fmt "End_of_word"
| Not_bound -> str fmt "Not_bound"
| Beg_of_str -> str fmt "Beg_of_str"
| End_of_str -> str fmt "End_of_str"
| Last_end_of_line -> str fmt "Last_end_of_line"
| Start -> str fmt "Start"
| Stop -> str fmt "Stop"
| Group (None, c) -> var "Group" c
| Group (Some n, c) -> sexp fmt "Named_group" (pair str pp) (n, c)
| Nest c -> var "Nest" c
| Pmark (m, r) -> sexp fmt "Pmark" (pair Pmark.pp pp) (m, r)
| Ast a -> pp_ast pp fmt a
| Sem (sem, a) -> sexp fmt "Sem" (pair Automata.Sem.pp pp) (sem, a)
| Sem_greedy (k, re) -> sexp fmt "Sem_greedy" (pair Automata.Rep_kind.pp pp) (k, re)
| No_group c -> var "No_group" c
;;
let rec pp_cset fmt cset =
let open Fmt in
let seq s rel = sexp fmt s (list pp_cset) rel in
match cset with
| Cast s -> pp_ast pp_cset fmt s
| Cset s -> sexp fmt "Set" Cset.pp s
| Intersection c -> seq "Intersection" c
| Complement c -> seq "Complement" c
| Difference (a, b) -> sexp fmt "Difference" (pair pp_cset pp_cset) (a, b)
;;
let rec equal cset x1 x2 =
match x1, x2 with
| Set s1, Set s2 -> cset s1 s2
| Sequence l1, Sequence l2 -> List.equal ~eq:(equal cset) l1 l2
| Repeat (x1', i1, j1), Repeat (x2', i2, j2) ->
Int.equal i1 i2 && Option.equal Int.equal j1 j2 && equal cset x1' x2'
| Beg_of_line, Beg_of_line
| End_of_line, End_of_line
| Beg_of_word, Beg_of_word
| End_of_word, End_of_word
| Not_bound, Not_bound
| Beg_of_str, Beg_of_str
| End_of_str, End_of_str
| Last_end_of_line, Last_end_of_line
| Start, Start
| Stop, Stop -> true
| Group _, Group _ ->
false
| Pmark (m1, r1), Pmark (m2, r2) -> Pmark.equal m1 m2 && equal cset r1 r2
| Nest x, Nest y -> equal cset x y
| Ast x, Ast y -> equal_ast (equal cset) x y
| Sem (sem, a), Sem (sem', a') -> Poly.equal sem sem' && equal cset a a'
| Sem_greedy (rep, a), Sem_greedy (rep', a') -> Poly.equal rep rep' && equal cset a a'
| _ -> false
;;
type t = (cset, [ `Cased | `Uncased ]) gen
type no_case = (Cset.t, [ `Uncased ]) gen
let pp = pp_gen pp_cset
let cset cset = Set (Cset cset)
let rec handle_case_cset ign_case = function
| Cset s -> if ign_case then Cset.case_insens s else s
| Cast (Alternative l) -> List.map ~f:(handle_case_cset ign_case) l |> Cset.union_all
| Complement l ->
List.map ~f:(handle_case_cset ign_case) l |> Cset.union_all |> Cset.diff Cset.cany
| Difference (r, r') ->
Cset.inter
(handle_case_cset ign_case r)
(Cset.diff Cset.cany (handle_case_cset ign_case r'))
| Intersection l -> List.map ~f:(handle_case_cset ign_case) l |> Cset.intersect_all
| Cast (No_case a) -> handle_case_cset true a
| Cast (Case a) -> handle_case_cset false a
;;
let rec handle_case ign_case : t -> (Cset.t, [ `Uncased ]) gen = function
| Set s -> Set (handle_case_cset ign_case s)
| Sequence l -> Sequence (List.map ~f:(handle_case ign_case) l)
| Ast (Alternative l) ->
let l = List.map ~f:(handle_case ign_case) l in
Ast (Alternative l)
| Repeat (r, i, j) -> Repeat (handle_case ign_case r, i, j)
| ( Beg_of_line
| End_of_line
| Beg_of_word
| End_of_word
| Not_bound
| Beg_of_str
| End_of_str
| Last_end_of_line
| Start
| Stop ) as r -> r
| Sem (k, r) -> Sem (k, handle_case ign_case r)
| Sem_greedy (k, r) -> Sem_greedy (k, handle_case ign_case r)
| Group (n, r) -> Group (n, handle_case ign_case r)
| No_group r -> No_group (handle_case ign_case r)
| Nest r -> Nest (handle_case ign_case r)
| Ast (Case r) -> handle_case false r
| Ast (No_case r) -> handle_case true r
| Pmark (i, r) -> Pmark (i, handle_case ign_case r)
;;
module Export = struct
type nonrec t = t
let pp = pp
let seq = function
| [ r ] -> r
| l -> Sequence l
;;
let str s =
let l = ref [] in
for i = String.length s - 1 downto 0 do
l := Set (Cset (Cset.csingle s.[i])) :: !l
done;
seq !l
;;
let as_set_elems elems =
match
List.map elems ~f:(function
| Set e -> e
| _ -> raise_notrace Exit)
with
| exception Exit -> None
| e -> Some e
;;
let empty : t = Ast empty_alternative
let alt (elems : t list) : t =
match elems with
| [] -> empty
| [ x ] -> x
| _ ->
(match as_set_elems elems with
| None -> Ast (Alternative elems)
| Some elems -> Set (Cast (Alternative elems)))
;;
let epsilon = seq []
let repn r i j =
if i < 0 then invalid_arg "Re.repn";
match j, i with
| Some j, _ when j < i -> invalid_arg "Re.repn"
| Some 0, 0 -> epsilon
| Some 1, 1 -> r
| _ -> Repeat (r, i, j)
;;
let rep r = repn r 0 None
let rep1 r = repn r 1 None
let opt r = repn r 0 (Some 1)
let bol = Beg_of_line
let eol = End_of_line
let bow = Beg_of_word
let eow = End_of_word
let word r = seq [ bow; r; eow ]
let not_boundary = Not_bound
let bos = Beg_of_str
let eos = End_of_str
let whole_string r = seq [ bos; r; eos ]
let leol = Last_end_of_line
let start = Start
let stop = Stop
type 'b f = { f : 'a. 'a -> ('a, 'b) ast }
let make_set f t =
match t with
| Set x -> Set (Cast (f.f x))
| _ -> Ast (f.f t)
;;
let preserve_set f t =
match t with
| Set _ -> t
| _ -> f t
;;
let longest = preserve_set (fun t -> Sem (`Longest, t))
let shortest = preserve_set (fun t -> Sem (`Shortest, t))
let first = preserve_set (fun t -> Sem (`First, t))
let greedy = preserve_set (fun t -> Sem_greedy (`Greedy, t))
let non_greedy = preserve_set (fun t -> Sem_greedy (`Non_greedy, t))
let group ?name r = Group (name, r)
let no_group = preserve_set (fun t -> No_group t)
let nest r = Nest r
let set str = cset (Cset.set str)
let mark r =
let i = Pmark.gen () in
i, Pmark (i, r)
;;
let as_set_or_error name elems =
match as_set_elems elems with
| None -> invalid_arg name
| Some s -> s
;;
let inter elems = Set (Intersection (as_set_or_error "Re.inter" elems))
let compl elems = Set (Complement (as_set_or_error "Re.compl" elems))
let diff r r' =
match r, r' with
| Set r, Set r' -> Set (Difference (r, r'))
| _, _ -> invalid_arg "Re.diff"
;;
let case =
let f = { f = (fun r -> Case r) } in
fun t -> make_set f t
;;
let no_case =
let f = { f = (fun r -> No_case r) } in
fun t -> make_set f t
;;
let witness t =
let rec witness (t : no_case) =
match t with
| Set c -> String.make 1 (Cset.to_char (Cset.pick c))
| Sequence xs -> String.concat "" (List.map ~f:witness xs)
| Ast (Alternative (x :: _)) -> witness x
| Ast (Alternative []) -> assert false
| Repeat (r, from, _to) ->
let w = witness r in
let b = Buffer.create (String.length w * from) in
for _i = 1 to from do
Buffer.add_string b w
done;
Buffer.contents b
| No_group r -> witness r
| Sem_greedy (_, r) | Sem (_, r) | Nest r | Pmark (_, r) | Group (_, r) -> witness r
| Beg_of_line
| End_of_line
| Beg_of_word
| End_of_word
| Not_bound
| Beg_of_str
| Last_end_of_line
| Start
| Stop
| End_of_str -> ""
in
witness (handle_case false t)
;;
end
open Export
let rec merge_sequences = function
| [] -> []
| Ast (Alternative l') :: r -> merge_sequences (l' @ r)
| Sequence (x :: y) :: r ->
(match merge_sequences r with
| Sequence (x' :: y') :: r' when equal Cset.equal x x' ->
Sequence [ x; Ast (Alternative [ seq y; seq y' ]) ] :: r'
| r' -> Sequence (x :: y) :: r')
| x :: r -> x :: merge_sequences r
;;
let colorize color_map (regexp : no_case) =
let lnl = ref false in
let rec colorize regexp =
match (regexp : no_case) with
| Set s -> Color_map.split color_map s
| Sequence l -> List.iter ~f:colorize l
| Ast (Alternative l) -> List.iter ~f:colorize l
| Repeat (r, _, _) -> colorize r
| Beg_of_line | End_of_line -> Color_map.split color_map Cset.nl
| Beg_of_word | End_of_word | Not_bound -> Color_map.split color_map Cset.cword
| Beg_of_str | End_of_str | Start | Stop -> ()
| Last_end_of_line -> lnl := true
| No_group r | Group (_, r) | Nest r | Pmark (_, r) -> colorize r
| Sem (_, r) | Sem_greedy (_, r) -> colorize r
in
colorize regexp;
!lnl
;;
let rec anchored_ast : (t, _) ast -> bool = function
| Alternative als -> List.for_all ~f:anchored als
| No_case r | Case r -> anchored r
and anchored : t -> bool = function
| Ast a -> anchored_ast a
| Sequence l -> List.exists ~f:anchored l
| Repeat (r, i, _) -> i > 0 && anchored r
| No_group r | Sem (_, r) | Sem_greedy (_, r) | Group (_, r) | Nest r | Pmark (_, r) ->
anchored r
| Set _
| Beg_of_line
| End_of_line
| Beg_of_word
| End_of_word
| Not_bound
| End_of_str
| Last_end_of_line
| Stop -> false
| Beg_of_str | Start -> true
;;
let t_of_cset x = Set x