···7575end
76767777type 'a memo_table = ('a * Char_input.t, error) result Memo.table
7878+7979+module Stream_input : sig
8080+ include INPUT with type token = char
8181+ val of_channel : in_channel -> t
8282+ val of_seq : char Seq.t -> t
8383+end = struct
8484+ type token = char
8585+ type t = {
8686+ mutable buffer : char list;
8787+ mutable source : char Seq.t;
8888+ mutable pos : int;
8989+ }
9090+9191+ let of_seq seq = { buffer = []; source = seq; pos = 0 }
9292+9393+ let of_channel ch =
9494+ let rec seq () =
9595+ match input_char ch with
9696+ | c -> Seq.Cons (c, seq)
9797+ | exception End_of_file -> Seq.Nil
9898+ in
9999+ of_seq seq
100100+101101+ let ensure_buffered t n =
102102+ let rec fill needed =
103103+ if needed <= 0 then ()
104104+ else match t.source () with
105105+ | Seq.Nil -> ()
106106+ | Seq.Cons (c, rest) ->
107107+ t.buffer <- t.buffer @ [c];
108108+ t.source <- rest;
109109+ fill (needed - 1)
110110+ in
111111+ let buffered = List.length t.buffer in
112112+ if buffered < n then fill (n - buffered)
113113+114114+ let peek t =
115115+ ensure_buffered t 1;
116116+ match t.buffer with
117117+ | [] -> None
118118+ | c :: _ -> Some c
119119+120120+ let advance t =
121121+ ensure_buffered t 1;
122122+ match t.buffer with
123123+ | [] -> t
124124+ | _ :: rest -> { t with buffer = rest; pos = t.pos + 1 }
125125+126126+ let position t = t.pos
127127+ let show_token c = Printf.sprintf "'%c'" c
128128+end
129129+130130+module type TOKEN = sig
131131+ type t
132132+ val equal : t -> t -> bool
133133+ val show : t -> string
134134+end
135135+136136+module Make_list_input (T : TOKEN) : sig
137137+ include INPUT with type token = T.t
138138+ val of_list : T.t list -> t
139139+end = struct
140140+ type token = T.t
141141+ type t = { data : T.t list; pos : int }
142142+143143+ let of_list lst = { data = lst; pos = 0 }
144144+145145+ let peek { data; _ } =
146146+ match data with
147147+ | [] -> None
148148+ | x :: _ -> Some x
149149+150150+ let advance t =
151151+ match t.data with
152152+ | [] -> t
153153+ | _ :: rest -> { data = rest; pos = t.pos + 1 }
154154+155155+ let position t = t.pos
156156+ let show_token = T.show
157157+end
···4747 in
4848 [%expr
4949 match I.peek input with
5050- | Some tok when [%e pred] tok -> Ok (tok, I.advance input)
5050+ | Some c when [%e pred] c -> Ok (c, I.advance input)
5151 | _ -> [%e error_expr ~loc expected]]
52525353 | Any { loc } ->
5454 [%expr
5555 match I.peek input with
5656- | Some tok -> Ok (tok, I.advance input)
5656+ | Some c -> Ok (c, I.advance input)
5757 | None -> [%e error_expr ~loc (elist ~loc [estring ~loc "<any>"])]]
58585959 | Eof { loc } ->
···602602603603let compile_def env { name; expr; loc } =
604604 let body = compile ~loc env expr in
605605- let func = [%expr fun (type inp tok) (module I : Combin.INPUT with type t = inp and type token = tok) input -> [%e body]] in
605605+ let func = [%expr fun (type inp) (module I : Combin.INPUT with type t = inp and type token = char) input -> [%e body]] in
606606 value_binding ~loc ~pat:(pvar ~loc name) ~expr:func
607607608608let compile_mutual_def env { names; expr; loc } =
609609 let body = compile ~loc env expr in
610610- let func = [%expr fun (type inp tok) (module I : Combin.INPUT with type t = inp and type token = tok) input -> [%e body]] in
610610+ let func = [%expr fun (type inp) (module I : Combin.INPUT with type t = inp and type token = char) input -> [%e body]] in
611611 let pat = ppat_tuple ~loc (List.map (pvar ~loc) names) in
612612 value_binding ~loc ~pat ~expr:func