text
stringlengths
0
601k
type ' a or_error = [ ` Ok of ' a | ` Error of string ]
type ' a sequence = ( ' a -> unit ) unit -> unit
type ' a gen = unit -> ' a option
type t = [ | ` Atom of string | ` List of t list ]
let _with_in filename f = let ic = open_in_bin filename in try let x = f ic in close_in ic ; x with e -> close_in ic ; ` Error ( Printexc . to_string e ) e
let _with_out filename f = let oc = open_out filename in try let x = f oc in close_out oc ; x with e -> close_out oc ; raise e
let _must_escape s = try for i = 0 to String . length s - 1 do let c = String . unsafe_get s i in match c with | ' ' | ' ; ' | ' ) ' ' | ' ( ' ' | ' " ' | ' \\ ' | ' \ n ' | ' \ t ' -> raise Exit | _ when Char . code c > 127 -> raise Exit | _ -> ( ) done ; false with Exit -> true
let rec to_buf b t = match t with | ` Atom s when _must_escape s -> Printf . bprintf b " " \% s " " \ ( String . escaped s ) s | ` Atom s -> Buffer . add_string b s | ` List [ ] -> Buffer . add_string b ( ) " " | ` List [ x ] x -> Printf . bprintf b ( " % a ) a " to_buf x | ` List l -> Buffer . add_char b ' ( ' ' ; Lis...
let to_string t = let b = Buffer . create 128 in to_buf b t ; Buffer . contents b
let rec print fmt t = match t with | ` Atom s when _must_escape s -> Format . fprintf fmt " " \% s " " \ ( String . escaped s ) s | ` Atom s -> Format . pp_print_string fmt s | ` List [ ] -> Format . pp_print_string fmt ( ) " " | ` List [ x ] x -> Format . fprintf fmt [ " @< hov2 ( >% a ) a ] " @ print x | ` List l -> ...
let rec print_noindent fmt t = match t with | ` Atom s when _must_escape s -> Format . fprintf fmt " " \% s " " \ ( String . escaped s ) s | ` Atom s -> Format . pp_print_string fmt s | ` List [ ] -> Format . pp_print_string fmt ( ) " " | ` List [ x ] x -> Format . fprintf fmt ( " % a ) a " print_noindent x | ` List l ...
let to_chan oc t = let fmt = Format . formatter_of_out_channel oc in print fmt t ; Format . pp_print_flush fmt ( )
let to_file_seq filename seq = _with_out filename ( fun oc -> seq ( fun t -> to_chan oc t ; output_char oc ' \ n ' ) n ' )
let to_file filename t = to_file_seq filename ( fun k -> k t ) t
module type MONAD = sig type ' a t val return : ' a -> ' a t val ( ) >>= : ' a t -> ( ' a -> ' b t ) t -> ' b t end
type ' a parse_result = [ ' a or_error | ` End ]
module MakeDecode ( MakeDecodeM : MONAD ) MONAD = struct let ( ) >>= = M ( ) . >>= type t = { buf : Bytes . t ; refill : Bytes . t -> int -> int -> int M . t ; atom : Buffer . t ; mutable i : int ; mutable len : int ; mutable line : int ; mutable col : int ; } let make ( ? bufsize = 1024 ) 1024 refill = let bufsize = m...
module ID_MONAD = struct type ' a t = ' a let return x = x let ( ) >>= x f = f x end
let parse_string s : t or_error = let n = String . length s in let stop = ref false in let refill bytes i _len = if ! stop then 0 else ( stop := true ; Bytes . blit_string s 0 bytes i n ; n ) n in let d = D . make ~ bufsize : n refill in match D . next d with | ` End -> ` Error " unexpected end of file " | ( ` Ok _ | `...
let parse_chan ? bufsize ic = let d = D . make ? bufsize ( input ic ) ic in match D . next d with | ` End -> ` Error " unexpected end of file " | ( ` Ok _ | ` Error _ ) _ as res -> res
let parse_chan_gen ? bufsize ic = let d = D . make ? bufsize ( input ic ) ic in fun ( ) -> match D . next d with | ` End -> None | ` Error _ as e -> Some e | ` Ok _ as res -> Some res
let parse_chan_list ? bufsize ic = let d = D . make ? bufsize ( input ic ) ic in let rec iter acc = match D . next d with | ` End -> ` Ok ( List . rev acc ) acc | ` Ok x -> iter ( x :: acc ) acc | ` Error _ as e -> e in iter [ ]
let parse_file filename = _with_in filename ( fun ic -> parse_chan ic ) ic
let parse_file_list filename = _with_in filename ( fun ic -> parse_chan_list ic ) ic
type sexpr = Atom of string | Expr of sexpr list
type state = | Parse_root of sexpr list | Parse_content of sexpr list | Parse_word of string * sexpr list
let mkstr c = String . make 1 c ; ;
let parse pop_char = let rec aux st = match pop_char ( ) with | None -> begin match st with | Parse_root sl -> ( List . rev sl ) | Parse_content _ | Parse_word _ -> failwith " Parsing error : content not closed by parenthesis " end | Some c -> match c with | ' ( ' -> begin match st with | Parse_root sl -> let this = au...
let string_pop_char str = let len = String . length str in let i = ref ( - 1 ) in ( function ( ) -> incr i ; if ! i >= len then None else Some ( str . [ ! i ] ) ) ; ;
let parse_string str = parse ( string_pop_char str ) ; ;
let ic_pop_char ic = ( function ( ) -> try Some ( input_char ic ) with End_of_file -> ( None ) ) ; ;
let parse_ic ic = parse ( ic_pop_char ic ) ; ;
let parse_file filename = let ic = open_in filename in let res = parse_ic ic in close_in ic ; ( res ) ; ;
let print_sexpr s = let rec aux acc = function | ( Atom tag ) :: tl -> aux ( tag :: acc ) tl | ( Expr e ) :: tl -> let s = " ( " ^ ( String . concat " " ( aux [ ] e ) ) ^ " ) " in aux ( s :: acc ) tl | [ ] -> ( List . rev acc ) in print_endline ( String . concat " " ( aux [ ] s ) ) ; ; ;
type ' a sexp_option = ' a option
type ' a sexp_list = ' a list
type ' a sexp_array = ' a array
type ' a sexp_opaque = ' a
let default_string_of_float = ref ( fun x -> let y = format_float " . % 15G " x in if ( float_of_string y ) = x then y else format_float " . % 17G " x ) ; ;
let read_old_option_format = ref true
let write_old_option_format = ref true
let list_map f l = List . rev ( List . rev_map l ~ f )
let sexp_of_unit ( ) = List [ ]
let sexp_of_bool b = Atom ( string_of_bool b )
let sexp_of_string str = Atom str
let sexp_of_bytes bytes = Atom ( Bytes . to_string bytes )
let sexp_of_char c = Atom ( String . make 1 c )
let sexp_of_int n = Atom ( string_of_int n )
let sexp_of_float n = Atom ( ! default_string_of_float n )
let sexp_of_int32 n = Atom ( Int32 . to_string n )
let sexp_of_int64 n = Atom ( Int64 . to_string n )
let sexp_of_nativeint n = Atom ( Nativeint . to_string n )
let sexp_of_ref sexp_of__a rf = sexp_of__a ! rf
let sexp_of_lazy_t sexp_of__a lv = sexp_of__a ( Lazy . force lv )
let sexp_of_option sexp_of__a = function | Some x when ! write_old_option_format -> List [ sexp_of__a x ] | Some x -> List [ Atom " some " ; sexp_of__a x ] | None when ! write_old_option_format -> List [ ] | None -> Atom " none "
let sexp_of_pair sexp_of__a sexp_of__b ( a , b ) = List [ sexp_of__a a ; sexp_of__b b ]
let sexp_of_triple sexp_of__a sexp_of__b sexp_of__c ( a , b , c ) = List [ sexp_of__a a ; sexp_of__b b ; sexp_of__c c ]
let sexp_of_list sexp_of__a lst = List ( List . rev ( List . rev_map lst ~ f : sexp_of__a ) )
let sexp_of_array sexp_of__a ar = let lst_ref = ref [ ] in for i = Array . length ar - 1 downto 0 do lst_ref := sexp_of__a ar . ( i ) :: ! lst_ref done ; List ! lst_ref
let sexp_of_hashtbl sexp_of_key sexp_of_val htbl = let coll ~ key : k ~ data : v acc = List [ sexp_of_key k ; sexp_of_val v ] :: acc in List ( Hashtbl . fold htbl ~ init [ ] : ~ f : coll )
let sexp_of_opaque _ = Atom " < opaque " >
let sexp_of_fun _ = Atom " < fun " >
module Exn_converter = struct module Int = struct type t = int let compare t1 t2 = polymorphic_compare ( t1 : int ) t2 end module Exn_ids = Map . Make ( Int ) module Obj = struct module Extension_constructor = struct [ @@@ ocaml . warning " - 3 " ] type t = extension_constructor let id = Obj . extension_id let of_val =...
let sexp_of_exn_opt exn = Exn_converter . find_auto exn
let sexp_of_exn exn = match sexp_of_exn_opt exn with | None -> List [ Atom ( Printexc . to_string exn ) ] | Some sexp -> sexp
let exn_to_string e = Sexp . to_string_hum ( sexp_of_exn e )
let ( ) = Printexc . register_printer ( fun exn -> match sexp_of_exn_opt exn with | None -> None | Some sexp -> Some ( Sexp . to_string_hum ~ indent : 2 sexp ) )
let record_check_extra_fields = ref true
let of_sexp_error_exn exc sexp = raise ( Of_sexp_error ( exc , sexp ) )
let of_sexp_error what sexp = raise ( Of_sexp_error ( Failure what , sexp ) )
let unit_of_sexp sexp = match sexp with | List [ ] -> ( ) | Atom _ | List _ -> of_sexp_error " unit_of_sexp : empty list needed " sexp
let bool_of_sexp sexp = match sexp with | Atom ( " true " | " True " ) -> true | Atom ( " false " | " False " ) -> false | Atom _ -> of_sexp_error " bool_of_sexp : unknown string " sexp | List _ -> of_sexp_error " bool_of_sexp : atom needed " sexp
let string_of_sexp sexp = match sexp with | Atom str -> str | List _ -> of_sexp_error " string_of_sexp : atom needed " sexp
let bytes_of_sexp sexp = match sexp with | Atom str -> Bytes . of_string str | List _ -> of_sexp_error " bytes_of_sexp : atom needed " sexp
let char_of_sexp sexp = match sexp with | Atom str -> if String . length str <> 1 then of_sexp_error " char_of_sexp : atom string must contain one character only " sexp ; str . [ 0 ] | List _ -> of_sexp_error " char_of_sexp : atom needed " sexp
let int_of_sexp sexp = match sexp with | Atom str -> ( try int_of_string str with exc -> of_sexp_error ( " int_of_sexp : " ^ exn_to_string exc ) sexp ) | List _ -> of_sexp_error " int_of_sexp : atom needed " sexp
let float_of_sexp sexp = match sexp with | Atom str -> ( try float_of_string str with exc -> of_sexp_error ( " float_of_sexp : " ^ exn_to_string exc ) sexp ) | List _ -> of_sexp_error " float_of_sexp : atom needed " sexp
let int32_of_sexp sexp = match sexp with | Atom str -> ( try Int32 . of_string str with exc -> of_sexp_error ( " int32_of_sexp : " ^ exn_to_string exc ) sexp ) | List _ -> of_sexp_error " int32_of_sexp : atom needed " sexp
let int64_of_sexp sexp = match sexp with | Atom str -> ( try Int64 . of_string str with exc -> of_sexp_error ( " int64_of_sexp : " ^ exn_to_string exc ) sexp ) | List _ -> of_sexp_error " int64_of_sexp : atom needed " sexp
let nativeint_of_sexp sexp = match sexp with | Atom str -> ( try Nativeint . of_string str with exc -> of_sexp_error ( " nativeint_of_sexp : " ^ exn_to_string exc ) sexp ) | List _ -> of_sexp_error " nativeint_of_sexp : atom needed " sexp
let ref_of_sexp a__of_sexp sexp = ref ( a__of_sexp sexp )
let lazy_t_of_sexp a__of_sexp sexp = Lazy . from_val ( a__of_sexp sexp )
let option_of_sexp a__of_sexp sexp = if ! read_old_option_format then match sexp with | List [ ] | Atom ( " none " | " None " ) -> None | List [ el ] | List [ Atom ( " some " | " Some " ) ; el ] -> Some ( a__of_sexp el ) | List _ -> of_sexp_error " option_of_sexp : list must represent optional value " sexp | Atom _ -> ...
let pair_of_sexp a__of_sexp b__of_sexp sexp = match sexp with | List [ a_sexp ; b_sexp ] -> let a = a__of_sexp a_sexp in let b = b__of_sexp b_sexp in a , b | List _ -> of_sexp_error " pair_of_sexp : list must contain exactly two elements only " sexp | Atom _ -> of_sexp_error " pair_of_sexp : list needed " sexp
let triple_of_sexp a__of_sexp b__of_sexp c__of_sexp sexp = match sexp with | List [ a_sexp ; b_sexp ; c_sexp ] -> let a = a__of_sexp a_sexp in let b = b__of_sexp b_sexp in let c = c__of_sexp c_sexp in a , b , c | List _ -> of_sexp_error " triple_of_sexp : list must contain exactly three elements only " sexp | Atom _ ->...
let list_of_sexp a__of_sexp sexp = match sexp with | List lst -> let rev_lst = List . rev_map lst ~ f : a__of_sexp in List . rev rev_lst | Atom _ -> of_sexp_error " list_of_sexp : list needed " sexp
let array_of_sexp a__of_sexp sexp = match sexp with | List [ ] -> [ ] || | List ( h :: t ) -> let len = List . length t + 1 in let res = Array . make len ( a__of_sexp h ) in let rec loop i = function | [ ] -> res | h :: t -> res . ( i ) <- a__of_sexp h ; loop ( i + 1 ) t in loop 1 t | Atom _ -> of_sexp_error " array_of...
let hashtbl_of_sexp key_of_sexp val_of_sexp sexp = match sexp with | List lst -> let htbl = Hashtbl . create 0 in let act = function | List [ k_sexp ; v_sexp ] -> Hashtbl . add htbl ~ key ( : key_of_sexp k_sexp ) ~ data ( : val_of_sexp v_sexp ) | List _ | Atom _ -> of_sexp_error " hashtbl_of_sexp : tuple list needed " ...
let opaque_of_sexp sexp = of_sexp_error " opaque_of_sexp : cannot convert opaque values " sexp
let fun_of_sexp sexp = of_sexp_error " fun_of_sexp : cannot convert function values " sexp
let get_flc_error name ( file , line , chr ) = Atom ( sprintf " % s % s :% d :% d " name file line chr )
let ( ) = List . iter ~ f ( : fun ( extension_constructor , handler ) -> Exn_converter . add ~ finalise : false extension_constructor handler ) [ ( [ % extension_constructor Assert_failure ] , ( function | Assert_failure arg -> get_flc_error " Assert_failure " arg | _ -> assert false ) ) ; ( [ % extension_constructor E...
let tuple_of_size_n_expected loc n sexp = of_sexp_error ( sprintf " % s_of_sexp : tuple of size % d expected " loc n ) sexp
let stag_no_args loc sexp = of_sexp_error ( loc ^ " _of_sexp : sum tag does not take arguments " ) sexp
let stag_incorrect_n_args loc tag sexp = let msg = sprintf " % s_of_sexp : sum tag % S has incorrect number of arguments " loc tag in of_sexp_error msg sexp
let stag_takes_args loc sexp = of_sexp_error ( loc ^ " _of_sexp : sum tag must be a structured value " ) sexp
let nested_list_invalid_sum loc sexp = of_sexp_error ( loc ^ " _of_sexp : a nested list is an invalid sum " ) sexp
let empty_list_invalid_sum loc sexp = of_sexp_error ( loc ^ " _of_sexp : the empty list is an invalid sum " ) sexp
let unexpected_stag loc sexp = of_sexp_error ( loc ^ " _of_sexp : unexpected sum tag " ) sexp
let record_sexp_bool_with_payload loc sexp = let msg = loc ^ " _of_sexp : record conversion : a [ sexp . bool ] field was given a payload . " in of_sexp_error msg sexp