Source file Types.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
type ('a, 'b) tag = Parsed of 'a * 'b option | Failed of 'b option | Empty
type ('stream, 'b, 'c) result = ('b * 'stream, 'c) tag
exception Retry
let emptyResult = Failed None
let failWith x = Failed (Some x)
module K :
sig
type ('a, 'stream, 'b, 'c) t = 'a -> 'stream -> ('stream, 'b, 'c) result
type ks
val singleton : ('a, 'stream, 'b, 'c) t -> ks
val add : ('a, 'stream, 'b, 'c) t -> ks -> ks
val fold : (('a, 'stream, 'b, 'c) t -> ('stream, 'b, 'c) result -> ('stream, 'b, 'c) result) -> ks -> ('stream, 'b, 'c) result -> ('stream, 'b, 'c) result
val empty : ks
val length : ks -> int
end =
struct
type ('a, 'stream, 'b, 'c) t = 'a -> 'stream -> ('stream, 'b, 'c) result
module Ks = Set.Make (
struct
type t = Obj.t
let compare x y = (Stdlib.compare : int -> int -> int) (Obj.magic x) (Obj.magic y)
end
)
type ks = Ks.t
let singleton k = Ks.add (Obj.repr k) Ks.empty
let add k ks = Ks.add (Obj.repr k) ks
let fold f ks acc = Ks.fold (fun k acc -> f (Obj.magic k) acc) ks acc
let empty = Ks.empty
let length ks = Ks.cardinal ks
end
type ('a, 'stream, 'b, 'c) k = ('a, 'stream, 'b, 'c) K.t
type ('a, 'stream, 'b, 'c) parser = 'stream -> ('a, 'stream, 'b, 'c) k -> ('stream, 'b, 'c) result
type ('a, 'stream, 'b, 'c) parser' = ('a, 'stream, 'b, 'c) k -> ('stream, 'b, 'c) result
let bind p k f =
p (fun a' s' ->
match k a' s' with
| Parsed ((v, s), err) ->
(match f v with
| `Ok v' -> Parsed ((v', s), err)
| `Fail err' -> Failed (Some err')
)
| Empty -> Empty
| Failed x -> Failed x)