package core_extended

  1. Overview
  2. Docs

Source file read.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
open Core_kernel
open! Int.Replace_polymorphic_compare

exception Bad_csv_formatting = Parse_state.Bad_csv_formatting

module On_invalid_row = struct
  type 'a t =
    int String.Map.t
    -> string Append_only_buffer.t
    -> exn
    -> [`Skip | `Yield of 'a | `Raise of exn]

  let raise _ _ exn = `Raise exn
  let skip _ _ _ = `Skip
  let create = Fn.id
end

module Header = Header

module Builder = struct
  type _ t =
    | Column : int -> string t
    | Header : string -> string t
    | Header_opt : string -> string option t
    | Return : 'a -> 'a t
    | Apply : ('b -> 'a) t * 'b t -> 'a t
    | Map : ('b -> 'a) * 'b t -> 'a t
    | Map2 : ('b -> 'c -> 'a) * 'b t * 'c t -> 'a t
    | Both : 'a t * 'b t -> ('a * 'b) t
    | Lambda : (int String.Map.t -> string Append_only_buffer.t -> 'a) -> 'a t

  module T = struct
    let return x = Return x

    let apply f x =
      match f with
      | Return f -> Map (f, x)
      | Map (f, w) -> Map2 (f, w, x)
      | _ -> Apply (f, x)
    ;;

    let map x ~f =
      match x with
      | Return x -> Return (f x)
      | _ -> Map (f, x)
    ;;

    let ( >>| ) t f = map t ~f

    let map2 x y ~f =
      match x, y with
      | Return x, Return y -> Return (f x y)
      | _ -> Map2 (f, x, y)
    ;;

    let map3 x y z ~f =
      match x, y, z with
      | Return x, Return y, Return z -> Return (f x y z)
      | _ -> Apply (Map2 (f, x, y), z)
    ;;

    let all ts = List.fold_right ts ~init:(return []) ~f:(map2 ~f:(fun x xs -> x :: xs))
    let all_unit ts = map ~f:ignore (all ts)
    let all_ignore = all_unit
    let both x y = Both (x, y)
    let ( <*> ) = apply
    let ( *> ) u v = return (fun () y -> y) <*> u <*> v
    let ( <* ) u v = return (fun x () -> x) <*> u <*> v

    module Applicative_infix = struct
      let ( <*> ) = ( <*> )
      let ( *> ) = ( *> )
      let ( <* ) = ( <* )
      let ( >>| ) = ( >>| )
    end
  end

  include T

  let at_index i ~f = Map (f, Column i)
  let at_header h ~f = Map (f, Header h)
  let at_header_opt h ~f = Map (f, Header_opt h)
  let lambda f = Lambda f

  module Let_syntax = struct
    module Let_syntax = struct
      include T

      module Open_on_rhs = struct
        let at_index = at_index
        let at_header = at_header
        let at_header_opt = at_header_opt
      end
    end
  end

  module Without_headers = struct
    type 'a t =
      | Column : int -> string t
      | Return : 'a -> 'a t
      | Apply : ('b -> 'a) t * 'b t -> 'a t
      | Map : ('b -> 'a) * 'b t -> 'a t
      | Map2 : ('b -> 'c -> 'a) * 'b t * 'c t -> 'a t
      | Both : ('a t * 'b t) -> ('a * 'b) t
      | Lambda :
          (int String.Map.t -> string Append_only_buffer.t -> 'a) * int String.Map.t
        -> 'a t

    let get_fields_used t =
      let open Option.Let_syntax in
      let rec fields : type a. a t -> Int.Set.t option = function
        | Return _ -> Some Int.Set.empty
        | Column i -> Some (Int.Set.singleton i)
        | Apply (f, x) ->
          let%bind f = fields f in
          let%map x = fields x in
          Set.union f x
        | Map (_, x) -> fields x
        | Map2 (_, x, y) ->
          let%bind x = fields x in
          let%map y = fields y in
          Set.union x y
        | Both (x, y) ->
          let%bind x = fields x in
          let%map y = fields y in
          Set.union x y
        | Lambda _ -> None
      in
      fields t
    ;;

    let build t =
      let rec build' : type a. a t -> string Append_only_buffer.t -> a =
        fun t row ->
          match t with
          | Return x -> x
          | Column i -> Append_only_buffer.nth_exn row i
          | Apply (f, x) -> (build' f row) (build' x row)
          | Map (f, x) -> f (build' x row)
          | Map2 (f, x, y) -> f (build' x row) (build' y row)
          | Both (x, y) -> build' x row, build' y row
          | Lambda (f, header_map) -> f header_map row
      in
      let fields_used = get_fields_used t in
      match fields_used with
      | None -> build' t, None
      | Some fields_used ->
        let fields_used = Set.to_list fields_used in
        let mapping =
          List.mapi fields_used ~f:(fun i field_index -> field_index, i)
          |> Int.Map.of_alist_exn
        in
        let rec remap : type a. a t -> a t =
          fun t ->
            match t with
            | Column i -> Column (Map.find_exn mapping i)
            | Return x -> Return x
            | Apply (f, x) -> Apply (remap f, remap x)
            | Map (f, x) -> Map (f, remap x)
            | Map2 (f, x, y) -> Map2 (f, remap x, remap y)
            | Both (x, y) -> Both (remap x, remap y)
            | Lambda _ -> t
        in
        build' (remap t), Some (Array.of_list fields_used)
    ;;
  end

  let build ~header_map t =
    let rec transform : type a. a t -> a Without_headers.t = function
      | Return x -> Without_headers.Return x
      | Column i -> Without_headers.Column i
      | Header h ->
        (match String.Map.find_exn header_map h with
         | index -> Without_headers.Column index
         | exception (Not_found_s _ | Caml.Not_found) ->
           raise_s
             [%message "Header not found" (h : string) (header_map : int String.Map.t)])
      | Header_opt h ->
        (match String.Map.find_exn header_map h with
         | index -> Without_headers.Map (Option.some, Column index)
         | exception (Not_found_s _ | Caml.Not_found) -> Without_headers.Return None)
      | Apply (f, x) -> Without_headers.Apply (transform f, transform x)
      | Map (f, x) -> Without_headers.Map (f, transform x)
      | Map2 (f, x, y) -> Without_headers.Map2 (f, transform x, transform y)
      | Both (x, y) -> Without_headers.Both (transform x, transform y)
      | Lambda f -> Without_headers.Lambda (f, header_map)
    in
    let transformed = transform t in
    Without_headers.build transformed
  ;;
end

include Builder

module Parse_header = struct
  (* This exception is used to return early from the parser, so we don't consume more
     input than necessary. This is almost [With_return], except declaring the exception at
     top-level so we can freely pass around a closure that raises it. *)
  exception Header_parsed of string array * int

  type t =
    { state : unit Parse_state.t
    ; transform : string array -> int String.Map.t
    }

  let is_at_beginning_of_row t = Parse_state.is_at_beginning_of_row t.state

  let header_map_opt header_row =
    Array.foldi header_row ~init:String.Map.empty ~f:(fun i map header ->
      match header with
      | None -> map
      | Some header -> Map.set map ~key:header ~data:i)
  ;;

  let header_map header_row = header_map_opt (Array.map ~f:Option.some header_row)

  let require_header required_headers' csv_headers' =
    let required_headers = String.Set.of_list required_headers' in
    let csv_headers = String.Set.of_array csv_headers' in
    let missing = Set.diff required_headers csv_headers in
    if not (Set.is_empty missing)
    then
      raise_s
        [%message
          "Header specified in `Require not present in csv document"
            (required_headers : String.Set.t)
            (csv_headers : String.Set.t)
            (missing : String.Set.t)];
    header_map csv_headers'
  ;;

  let create' ?strip ?sep ?quote transform =
    let f offset () row =
      raise (Header_parsed (Append_only_buffer.to_array row, offset))
    in
    { state = Parse_state.create ?strip ?sep ?quote ~fields_used:None ~init:() ~f ()
    ; transform
    }
  ;;

  let create ?strip ?sep ?quote ?(header = `No) () =
    match header with
    | `No -> Second String.Map.empty
    | `Add headers -> Second (header_map (Array.of_list headers))
    | `Yes ->
      let f headers = header_map headers in
      First (create' ?strip ?sep ?quote f)
    | `Require headers ->
      let f csv_headers = require_header headers csv_headers in
      First (create' ?strip ?sep ?quote f)
    | `Replace headers ->
      let f _ = header_map (Array.of_list headers) in
      First (create' ?strip ?sep ?quote f)
    | `Transform f ->
      let f headers = header_map (Array.of_list (f (Array.to_list headers))) in
      First (create' ?strip ?sep ?quote f)
    | `Filter_map f ->
      let f headers = header_map_opt (Array.of_list (f (Array.to_list headers))) in
      First (create' ?strip ?sep ?quote f)
  ;;

  let input_string t ~len input =
    try First { t with state = Parse_state.input_string t.state ~len input } with
    | Header_parsed (row, offset) ->
      Second (t.transform row, String.sub input ~pos:offset ~len:(len - offset))
  ;;

  let input t ~len input =
    try First { t with state = Parse_state.input t.state ~len input } with
    | Header_parsed (row, offset) ->
      Second (t.transform row, Bytes.To_string.sub input ~pos:offset ~len:(len - offset))
  ;;
end

module Expert = struct
  module Append_only_buffer = Append_only_buffer
  module Parse_state = Parse_state
  module Builder = Builder
  module Parse_header = Parse_header

  let create_parse_state
        ?strip
        ?sep
        ?quote
        ?(on_invalid_row = On_invalid_row.raise)
        ~header_map
        builder
        ~init
        ~f
    =
    let row_to_'a, fields_used = Builder.build ~header_map builder in
    let f _offset init row =
      try f init (row_to_'a row) with
      | exn ->
        (match on_invalid_row header_map row exn with
         | `Yield x -> f init x
         | `Skip -> init
         | `Raise exn -> raise exn)
    in
    Parse_state.create ?strip ?sep ?quote ~fields_used ~init ~f ()
  ;;

  let manual_parse_state ?strip ?sep ?quote header_map =
    Parse_state.create
      ?strip
      ?sep
      ?quote
      ~fields_used:None
      ~init:(Append_only_buffer.create ())
      ~f:(fun _ queue row ->
        Append_only_buffer.append queue (Row.Expert.of_buffer header_map row);
        queue)
      ()
  ;;

  let manual_parse_data parse_state input =
    let parse_state =
      match input with
      | `Eof -> Parse_state.finish parse_state
      | `Data s -> Parse_state.input_string parse_state s
    in
    let queue = Parse_state.acc parse_state in
    let result = Append_only_buffer.to_list queue in
    Append_only_buffer.lax_clear queue;
    Second parse_state, result
  ;;

  let manual_parse_header ?strip ?sep ?quote header_state input =
    let input =
      match input with
      | `Eof -> ""
      | `Data s -> s
    in
    match Parse_header.input_string header_state ~len:(String.length input) input with
    | First header_state -> First header_state, []
    | Second (header_map, input) ->
      let state = manual_parse_state ?strip ?sep ?quote header_map in
      manual_parse_data state (`Data input)
  ;;

  let create_partial ?strip ?sep ?quote ?header () =
    let state =
      Parse_header.create ?strip ?sep ?quote ?header ()
      |> Either.Second.map ~f:(manual_parse_state ?strip ?sep ?quote)
      |> ref
    in
    let parse_chunk input =
      let state', results =
        match !state with
        | First state -> manual_parse_header ?strip ?sep state input
        | Second state -> manual_parse_data state input
      in
      state := state';
      results
    in
    stage parse_chunk
  ;;
end

let fold_string ?strip ?sep ?quote ?header ?on_invalid_row builder ~init ~f csv_string =
  match
    match Parse_header.create ?strip ?sep ?quote ?header () with
    | Second header_map -> Some (header_map, csv_string)
    | First header_parse ->
      (match
         Parse_header.input_string
           header_parse
           ~len:(String.length csv_string)
           csv_string
       with
       | First _ ->
         if String.is_empty csv_string
         then None
         else
           raise_s
             [%message
               "String ended mid-header row"
                 (csv_string : string)
                 (sep : char option)
                 (header : Header.t option)]
       | Second (header_map, csv_string) -> Some (header_map, csv_string))
  with
  | None -> init
  | Some (header_map, csv_string) ->
    Parse_state.input_string
      (Expert.create_parse_state
         ?strip
         ?sep
         ?quote
         ?on_invalid_row
         ~header_map
         builder
         ~init
         ~f)
      csv_string
    |> Parse_state.finish
    |> Parse_state.acc
;;

let list_of_string ?strip ?sep ?quote ?header ?on_invalid_row builder csv_string =
  fold_string
    ?strip
    ?sep
    ?quote
    ?header
    ?on_invalid_row
    builder
    csv_string
    ~init:(Append_only_buffer.create ())
    ~f:(fun queue row ->
      Append_only_buffer.append queue row;
      queue)
  |> Append_only_buffer.to_list
;;

module Row = struct
  type 'a builder_t = 'a t

  include Row

  let builder = lambda Row.Expert.of_buffer
end
OCaml

Innovation. Community. Security.