text
stringlengths 0
601k
|
---|
let session_callback ( kind , show , status , priority ) priority mvar t = let err txt = t . user_data . locallog " handling error " txt in register_iq_request_handler t Roster . ns_roster ( fun ev jid_from jid_to _lang ( ) -> match ev with | IQGet _el -> fail BadRequest | IQSet el -> ( match jid_from , jid_to with | None , _ -> Lwt . return_unit | Some x , Some y -> ( try let from_jid = JID . of_string x and to_jid = JID . of_string y in if JID . is_bare from_jid && JID . equal ( JID . bare_jid to_jid ) to_jid from_jid then Lwt . return_unit else fail BadRequest with _ -> fail BadRequest ) BadRequest | _ -> fail BadRequest ) BadRequest >>= fun ( ) -> match el with | Xml . Xmlelement ( ( ns_roster , " query ) " , attrs , els ) els when ns_roster = Roster . ns_roster -> begin match snd ( Roster . decode attrs els ) els with | [ item ] -> let users = match roster_callback item with | None -> [ ] | Some x -> [ x ] x in t . user_data . update_users users true >|= fun ( ) -> IQResult None | _ -> fail BadRequest end | _ -> fail BadRequest ) BadRequest ; register_stanza_handler t ( ns_client , " message ) " ( fun t attrs eles -> ( try parse_message ~ callback : message_callback ~ callback_error : message_error t attrs eles with _ -> err " during message parsing , ignoring ) ) " ; register_stanza_handler t ( ns_client , " presence ) " ( fun t attrs eles -> ( try parse_presence ~ callback : presence_callback ~ callback_error : presence_error t attrs eles with _ -> err " during presence parsing , ignoring ) ) " ; register_iq_request_handler t Disco . ns_disco_info ( fun ev _jid_from _jid_to _lang ( ) -> match ev with | IQSet _el -> fail BadRequest | IQGet _ -> match ns_receipts with | None -> fail BadRequest | Some x -> let feature = Disco . make_feature_var x in let query = Disco . make_disco_query [ feature ] feature in return ( IQResult ( Some query ) query ) query ) ; Roster . get t ( fun ? jid_from ? jid_to ? lang ? ver items -> ignore jid_from ; ignore jid_to ; ignore lang ; ignore ver ; let mods = List . map roster_callback items in t . user_data . reset_users ( ) >>= fun ( ) -> let users = List . fold_left ( fun acc -> function None -> acc | Some x -> x :: acc ) acc [ ] mods in t . user_data . update_users users false ) false >>= fun ( ) -> send_presence t ? kind ? show ? status ? priority ( ) >>= fun ( ) -> Keepalive . restart_keepalive t ; mvar t |
let tls_epoch_to_line t = let open Tls in match Tls_lwt . Unix . epoch t with | Ok epoch -> let version = epoch . Core . protocol_version and cipher = epoch . Core . ciphersuite in Ok Sexplib . Sexp ( . to_string_hum ( List [ Core . sexp_of_tls_version version ; Ciphersuite . sexp_of_ciphersuite cipher ] ) ) | Error ( ) -> Error " error while fetching TLS parameters " |
let connect socket_data myjid ? host password presence authenticator user_data mvar = let module Socket_module = struct type t = PlainSocket . socket let socket = socket_data include PlainSocket end in let tls_socket ( ) = TLSSocket . switch socket_data ? host authenticator >>= fun socket_data -> ( match tls_epoch_to_line socket_data with | Ok str -> user_data . locallog ~ kind ` : Success " TLS session info " str | Error str -> user_data . locallog ~ kind ` : Error " TLS session info " str ) str >>= fun ( ) -> let module TLS_module = struct type t = Tls_lwt . Unix . t let socket = socket_data include TLSSocket end in return ( module TLS_module : XMPPClient . Socket ) Socket in let myjid = Xjid . jid_to_xmpp_jid ( ` Full myjid ) myjid in XMPPClient . setup_session ~ user_data ~ myjid ~ plain_socket ( : module Socket_module : XMPPClient . Socket ) Socket ~ tls_socket ~ password ( session_callback presence mvar ) mvar |
let close session_data = let module S = ( val session_data . socket : Socket ) Socket in S . close S . socket |
let parse_loop session_data = XMPPClient . parse session_data >>= fun ( ) -> close session_data |
let ( debug_out : Lwt_unix . file_descr option ref ) ref = ref None |
let dbg data = match ! debug_out with | None -> Lwt . return ( ) | Some x -> let now = Unix . localtime ( Unix . time ( ) ) in let msg = Printf . sprintf [ " % 02d :% 02d :% 02d ] 02d % s \ n " now . Unix . tm_hour now . Unix . tm_min now . Unix . tm_sec data in Persistency . write_data x ( Bytes . of_string msg ) msg |
module PlainSocket = struct type ' a z = ' a Lwt . t type fd = Lwt_unix . file_descr type socket = fd let read fd buf start len = Lwt_unix . read fd buf start len >>= fun size -> dbg ( " IN : " ^ Bytes . to_string ( Bytes . sub buf start size ) size ) size >|= fun ( ) -> size let write fd str = dbg ( " OUT : " ^ Bytes . to_string str ) str >>= fun ( ) -> let len = Bytes . length str in let rec aux_send start = Lwt_unix . write fd str start ( len - start ) start >>= fun sent -> if sent = 0 then Lwt . return ( ) else aux_send ( start + sent ) sent in aux_send 0 let close fd = Lwt_unix . close fd end |
module TLSSocket = struct let read s buf start len = let cs = Cstruct . create len in Tls_lwt . Unix . read s cs >>= fun size -> ( if size > 0 then ( Cstruct . blit_to_bytes cs start buf 0 size ; dbg ( " IN TLS : " ^ Bytes . to_string ( Bytes . sub buf start size ) size ) size ) size else Lwt . return ( ) ) >|= fun ( ) -> size let write s bs = let str = Bytes . to_string bs in dbg ( " OUT TLS : " ^ str ) str >>= fun ( ) -> Tls_lwt . Unix . write s ( Cstruct . of_string str ) str let switch fd ? host authenticator = let config = Tls . Config . client ~ authenticator ( ) in Tls_lwt . Unix . client_of_fd config ? host fd let close s = Tls_lwt . Unix . close s end |
let cautious f ppf arg = try f ppf arg with Ellipsis -> fprintf ppf " . . . " |
let parenthesized_ident name = ( List . mem name [ " or " ; " mod " ; " land " ; " lor " ; " lxor " ; " lsl " ; " lsr " ; " asr " ] ) || ( match name . [ 0 ] with | ' a ' . . ' z ' | ' A ' . . ' Z ' | ' \ 223 ' . . ' \ 246 ' | ' \ 248 ' . . ' \ 255 ' | ' _ ' | ' { ' | ' ( ' -> false | _ -> true ) |
let value_ident ppf name = if parenthesized_ident name then fprintf ppf " ( % s ) " name else pp_print_string ppf name |
let rec print_ident ppf = function | Oide_ident " " -> assert false | Oide_ident s when Some ps <-- Packpath . parse s -> begin match ps with | [ ] -> assert false | p :: _ -> let s = if String . length s <= String . length p + 6 then s else " { " ^ p ^ " , . . . } " in pp_print_string ppf s end | Oide_ident s -> value_ident ppf s | Oide_dot ( id , s ) -> print_ident ppf id ; pp_print_char ppf ' . ' ; value_ident ppf s | Oide_apply ( id1 , id2 ) -> fprintf ppf " % a ( % a ) " print_ident id1 print_ident id2 |
let valid_float_lexeme s = let l = String . length s in let rec loop i = if i >= l then s ^ " . " else match s . [ i ] with | ' 0 ' . . ' 9 ' | ' ' - -> loop ( i + 1 ) | _ -> s in loop 0 |
let float_repres f = match classify_float f with FP_nan -> " nan " | FP_infinite -> if f < 0 . 0 then " neg_infinity " else " infinity " | _ -> let float_val = let s1 = Printf . sprintf " . % 12g " f in if f = float_of_string s1 then s1 else let s2 = Printf . sprintf " . % 15g " f in if f = float_of_string s2 then s2 else Printf . sprintf " . % 18g " f in valid_float_lexeme float_val |
let parenthesize_if_neg ppf fmt v isneg = if isneg then pp_print_char ppf ' ( ' ; fprintf ppf fmt v ; if isneg then pp_print_char ppf ' ) ' |
let print_out_value ppf tree = let rec print_tree_1 ppf = function | Oval_constr ( name , [ param ] ) -> fprintf ppf " [ @< 1 >% a @ % a ] " @ print_ident name print_constr_param param | Oval_constr ( name , ( _ :: _ as params ) ) -> fprintf ppf " [ @< 1 >% a @ ( % a ) ] " @ print_ident name ( print_tree_list print_tree_1 " , " ) params | Oval_variant ( name , Some param ) -> fprintf ppf " [ @< 2 ` >% s @ % a ] " @ name print_constr_param param | tree -> print_simple_tree ppf tree and print_constr_param ppf = function | Oval_int i -> parenthesize_if_neg ppf " % i " i ( i < 0 ) | Oval_int32 i -> parenthesize_if_neg ppf " % lil " i ( i < 0l ) | Oval_int64 i -> parenthesize_if_neg ppf " % LiL " i ( i < 0L ) | Oval_nativeint i -> parenthesize_if_neg ppf " % nin " i ( i < 0n ) | Oval_float f -> parenthesize_if_neg ppf " % s " ( float_repres f ) ( f < 0 . 0 ) | tree -> print_simple_tree ppf tree and print_simple_tree ppf = function Oval_int i -> fprintf ppf " % i " i | Oval_int32 i -> fprintf ppf " % lil " i | Oval_int64 i -> fprintf ppf " % LiL " i | Oval_nativeint i -> fprintf ppf " % nin " i | Oval_float f -> pp_print_string ppf ( float_repres f ) | Oval_char c -> fprintf ppf " % C " c | Oval_string s -> begin try fprintf ppf " % S " s with Invalid_argument _ -> fprintf ppf " < huge string " > end | Oval_list tl -> fprintf ppf " [ @< 1 [ >% a ] ] " @ ( print_tree_list print_tree_1 " ; " ) tl | Oval_array tl -> fprintf ppf " [ @< 2 [ >|% a ] ] " |@ ( print_tree_list print_tree_1 " ; " ) tl | Oval_constr ( name , [ ] ) -> print_ident ppf name | Oval_variant ( name , None ) -> fprintf ppf " ` % s " name | Oval_stuff s -> pp_print_string ppf s | Oval_record fel -> fprintf ppf " [ @< 1 { >% a } ] " @ ( cautious ( print_fields true ) ) fel | Oval_ellipsis -> raise Ellipsis | Oval_printer f -> f ppf | Oval_tuple tree_list -> fprintf ppf " [ @< 1 ( >% a ) ] " @ ( print_tree_list print_tree_1 " , " ) tree_list | tree -> fprintf ppf " [ @< 1 ( >% a ) ] " @ ( cautious print_tree_1 ) tree and print_fields first ppf = function [ ] -> ( ) | ( name , tree ) :: fields -> if not first then fprintf ppf " ; @ " ; fprintf ppf " [ @< 1 >% a @ =@ % a ] " @ print_ident name ( cautious print_tree_1 ) tree ; print_fields false ppf fields and print_tree_list print_item sep ppf tree_list = let rec print_list first ppf = function [ ] -> ( ) | tree :: tree_list -> if not first then fprintf ppf " % s @ " sep ; print_item ppf tree ; print_list false ppf tree_list in cautious ( print_list true ) ppf tree_list in cautious print_tree_1 ppf tree |
let out_value = ref print_out_value |
let rec print_list_init pr sep ppf = function [ ] -> ( ) | a :: l -> sep ppf ; pr ppf a ; print_list_init pr sep ppf l |
let rec print_list pr sep ppf = function [ ] -> ( ) | [ a ] -> pr ppf a | a :: l -> pr ppf a ; sep ppf ; print_list pr sep ppf l |
let pr_present = print_list ( fun ppf s -> fprintf ppf " ` % s " s ) ( fun ppf -> fprintf ppf " @ " ) |
let pr_vars = print_list ( fun ppf s -> fprintf ppf " ' % s " s ) ( fun ppf -> fprintf ppf " @ " ) |
let rec print_out_type ppf = function | Otyp_alias ( ty , s ) -> fprintf ppf " [ @% a @ as ' % s ] " @ print_out_type ty s | Otyp_poly ( sl , ty ) -> fprintf ppf " [ @< hov 2 >% a . @ % a ] " @ pr_vars sl print_out_type ty | ty -> print_out_type_1 ppf ty function Otyp_arrow ( lab , ty1 , ty2 ) -> pp_open_box ppf 0 ; if lab <> " " then ( pp_print_string ppf lab ; pp_print_char ppf ' ' ) ; : print_out_type_2 ppf ty1 ; pp_print_string ppf " " ; -> pp_print_space ppf ( ) ; print_out_type_1 ppf ty2 ; pp_close_box ppf ( ) | ty -> print_out_type_2 ppf ty function Otyp_tuple tyl -> fprintf ppf " [ @< 0 >% a ] " @ ( print_typlist print_simple_out_type " " ) * tyl | ty -> print_simple_out_type ppf ty function Otyp_class ( ng , id , tyl ) -> fprintf ppf " [ @% a % s #% a ] " @ print_typargs tyl ( if ng then " _ " else " " ) print_ident id | Otyp_constr ( id , tyl ) -> pp_open_box ppf 0 ; print_typargs ppf tyl ; print_ident ppf id ; pp_close_box ppf ( ) | Otyp_object ( fields , rest ) -> fprintf ppf " [ @< 2 >< % a ] " >@ ( print_fields rest ) fields | Otyp_stuff s -> pp_print_string ppf s | Otyp_var ( ng , s ) -> fprintf ppf " ' % s % s " ( if ng then " _ " else " " ) s | Otyp_variant ( non_gen , row_fields , closed , tags ) -> let print_present ppf = function None | Some [ ] -> ( ) | Some l -> fprintf ppf " ; @< 1 - 2 >> [ @< hov >% a ] " @ pr_present l in let print_fields ppf = function Ovar_fields fields -> print_list print_row_field ( fun ppf -> fprintf ppf " ; @< 1 - 2 >| " ) ppf fields | Ovar_name ( id , tyl ) -> fprintf ppf " [ @% a % a ] " @ print_typargs tyl print_ident id in fprintf ppf " % s [ % s [ @< hv [ >@< hv >% a ] @% a ] ] " @ ( if non_gen then " _ " else " " ) ( if closed then if tags = None then " " else " < " else if tags = None then " > " else " ? " ) print_fields row_fields print_present tags | Otyp_alias _ | Otyp_poly _ | Otyp_arrow _ | Otyp_tuple _ as ty -> pp_open_box ppf 1 ; pp_print_char ppf ' ( ' ; print_out_type ppf ty ; pp_print_char ppf ' ) ' ; pp_close_box ppf ( ) | Otyp_abstract | Otyp_open | Otyp_sum _ | Otyp_manifest ( _ , _ ) -> ( ) | Otyp_record lbls -> print_record_decl ppf lbls | Otyp_module ( p , n , tyl ) -> fprintf ppf " [ @< 1 ( > module % s " p ; let first = ref true in List . iter2 ( fun s t -> let sep = if ! first then ( first := false ; " with " ) else " and " in fprintf ppf " % s type % s = % a " sep s print_out_type t ) n tyl ; fprintf ppf " ) ] " @ | Otyp_attribute ( t , attr ) -> fprintf ppf " [ @< 1 ( >% a [ @@% s ] ) ] " @ print_out_type t attr . oattr_name fprintf ppf " { % a ; @< 1 - 2 } " > ( print_list_init print_out_label ( fun ppf -> fprintf ppf " @ " ) ) lbls function [ ] -> begin match rest with Some non_gen -> fprintf ppf " % s . . " ( if non_gen then " _ " else " " ) | None -> ( ) end | [ s , t ] -> fprintf ppf " % s : % a " s print_out_type t ; begin match rest with Some _ -> fprintf ppf " ; @ " | None -> ( ) end ; print_fields rest ppf [ ] | ( s , t ) :: l -> fprintf ppf " % s : % a ; @ % a " s print_out_type t ( print_fields rest ) l let pr_of ppf = if opt_amp then fprintf ppf " of @ &@ " else if tyl <> [ ] then fprintf ppf " of @ " else fprintf ppf " " in fprintf ppf " [ @< hv 2 ` >% s % t % a ] " @ l pr_of ( print_typlist print_out_type " " ) & tyl function [ ] -> ( ) | [ ty ] -> print_elem ppf ty | ty :: tyl -> print_elem ppf ty ; pp_print_string ppf sep ; pp_print_space ppf ( ) ; print_typlist print_elem sep ppf tyl function [ ] -> ( ) | [ ty1 ] -> print_simple_out_type ppf ty1 ; pp_print_space ppf ( ) | tyl -> pp_open_box ppf 1 ; pp_print_char ppf ' ( ' ; print_typlist print_out_type " , " ppf tyl ; pp_print_char ppf ' ) ' ; pp_close_box ppf ( ) ; pp_print_space ppf ( ) fprintf ppf " [ @< 2 >% s % s :@ % a ] ; " @ ( if mut then " mutable " else " " ) name print_out_type arg |
let out_type = ref print_out_type |
let type_parameter ppf ( ty , ( co , cn ) ) = fprintf ppf " % s % s " ( if not cn then " " + else if not co then " " - else " " ) ( if ty = " _ " then ty else " ' " ^ ty ) |
let print_out_class_params ppf = function [ ] -> ( ) | tyl -> fprintf ppf " [ @< 1 [ >% a ] ] @@ " ( print_list type_parameter ( fun ppf -> fprintf ppf " , " ) ) tyl |
let rec print_out_class_type ppf = function Octy_constr ( id , tyl ) -> let pr_tyl ppf = function [ ] -> ( ) | tyl -> fprintf ppf " [ @< 1 [ >% a ] ] @@ " ( print_typlist ! out_type " , " ) tyl in fprintf ppf " [ @% a % a ] " @ pr_tyl tyl print_ident id | Octy_arrow ( lab , ty , cty ) -> fprintf ppf " [ @% s % a ->@ % a ] " @ ( if lab <> " " then lab ^ " " : else " " ) print_out_type_2 ty print_out_class_type cty | Octy_signature ( self_ty , csil ) -> let pr_param ppf = function Some ty -> fprintf ppf " @ [ ( @% a ) ] " @ ! out_type ty | None -> ( ) in fprintf ppf " [ @< hv 2 [ >@< 2 > object % a ] @@ % a ; @< 1 - 2 > end ] " @ pr_param self_ty ( print_list print_out_class_sig_item ( fun ppf -> fprintf ppf " @ " ) ) csil function Ocsg_constraint ( ty1 , ty2 ) -> fprintf ppf " [ @< 2 > constraint % a =@ % a ] " @ ! out_type ty1 ! out_type ty2 | Ocsg_method ( name , priv , virt , ty ) -> fprintf ppf " [ @< 2 > method % s % s % s :@ % a ] " @ ( if priv then " private " else " " ) ( if virt then " virtual " else " " ) name ! out_type ty | Ocsg_value ( name , mut , vr , ty ) -> fprintf ppf " [ @< 2 > val % s % s % s :@ % a ] " @ ( if mut then " mutable " else " " ) ( if vr then " virtual " else " " ) name ! out_type ty |
let out_class_type = ref print_out_class_type |
let out_module_type = ref ( fun _ -> failwith " Xoprint . out_module_type " ) |
let out_sig_item = ref ( fun _ -> failwith " Xoprint . out_sig_item " ) |
let out_signature = ref ( fun _ -> failwith " Xoprint . out_signature " ) |
let out_type_extension = ref ( fun _ -> failwith " Xoprint . out_type_extension " ) |
let rec print_out_functor funct ppf = function Omty_functor ( _ , None , mty_res ) -> if funct then fprintf ppf " ( ) % a " ( print_out_functor true ) mty_res else fprintf ppf " functor @ ( ) % a " ( print_out_functor true ) mty_res | Omty_functor ( name , Some mty_arg , mty_res ) -> begin match name , funct with | " _ " , true -> fprintf ppf " ->@ % a ->@ % a " print_out_module_type mty_arg ( print_out_functor false ) mty_res | " _ " , false -> fprintf ppf " % a ->@ % a " print_out_module_type mty_arg ( print_out_functor false ) mty_res | name , true -> fprintf ppf " ( % s : % a ) % a " name print_out_module_type mty_arg ( print_out_functor true ) mty_res | name , false -> fprintf ppf " functor @ ( % s : % a ) % a " name print_out_module_type mty_arg ( print_out_functor true ) mty_res end | m -> if funct then fprintf ppf " ->@ % a " print_out_module_type m else print_out_module_type ppf m function Omty_abstract -> ( ) | Omty_functor _ as t -> fprintf ppf " [ @< 2 >% a ] " @ ( print_out_functor false ) t | Omty_ident id -> fprintf ppf " % a " print_ident id | Omty_signature sg -> fprintf ppf " [ @< hv 2 > sig @ % a ; @< 1 - 2 > end ] " @ ! out_signature sg | Omty_alias id -> fprintf ppf " ( module % a ) " print_ident id function [ ] -> ( ) | [ item ] -> ! out_sig_item ppf item | Osig_typext ( ext , Oext_first ) :: items -> let rec gather_extensions acc items = match items with Osig_typext ( ext , Oext_next ) :: items -> gather_extensions ( ( ext . oext_name , ext . oext_args , ext . oext_ret_type ) :: acc ) items | _ -> ( List . rev acc , items ) in let exts , items = gather_extensions [ ( ext . oext_name , ext . oext_args , ext . oext_ret_type ) ] items in let te = { otyext_name = ext . oext_type_name ; otyext_params = ext . oext_type_params ; otyext_constructors = exts ; otyext_private = ext . oext_private } in fprintf ppf " % a @ % a " ! out_type_extension te print_out_signature items | item :: items -> fprintf ppf " % a @ % a " ! out_sig_item item print_out_signature items function Osig_class ( vir_flag , name , params , clt , rs ) -> fprintf ppf " [ @< 2 >% s % s @ % a % s @ :@ % a ] " @ ( if rs = Orec_next then " and " else " class " ) ( if vir_flag then " virtual " else " " ) print_out_class_params params name ! out_class_type clt | Osig_class_type ( vir_flag , name , params , clt , rs ) -> fprintf ppf " [ @< 2 >% s % s @ % a % s @ =@ % a ] " @ ( if rs = Orec_next then " and " else " class type " ) ( if vir_flag then " virtual " else " " ) print_out_class_params params name ! out_class_type clt | Osig_typext ( ext , Oext_exception ) -> fprintf ppf " [ @< 2 > exception % a ] " @ print_out_constr ( ext . oext_name , ext . oext_args , ext . oext_ret_type ) | Osig_typext ( ext , es ) -> print_out_extension_constructor ppf ext | Osig_modtype ( name , Omty_abstract ) -> fprintf ppf " [ @< 2 > module type % s ] " @ name | Osig_modtype ( name , mty ) -> fprintf ppf " [ @< 2 > module type % s =@ % a ] " @ name ! out_module_type mty | Osig_module ( name , Omty_alias id , _ ) -> fprintf ppf " [ @< 2 > module % s =@ % a ] " @ name print_ident id | Osig_module ( name , mty , rs ) -> fprintf ppf " [ @< 2 >% s % s :@ % a ] " @ ( match rs with Orec_not -> " module " | Orec_first -> " module rec " | Orec_next -> " and " ) name ! out_module_type mty | Osig_type ( td , rs ) -> print_out_type_decl ( match rs with | Orec_not -> " type nonrec " | Orec_first -> " type " | Orec_next -> " and " ) ppf td | Osig_value vd -> let kwd = if vd . oval_prims = [ ] then " val " else " external " in let pr_prims ppf _ = ( ) in fprintf ppf " [ @< 2 >% s % a :@ % a % a % a ] " @ kwd value_ident vd . oval_name ! out_type vd . oval_type pr_prims vd . oval_prims ( fun ppf -> List . iter ( fun a -> fprintf ppf " @ [ @@@@% s ] " a . oattr_name ) ) vd . oval_attributes | Osig_ellipsis -> fprintf ppf " . . . " let print_constraints ppf = List . iter ( fun ( ty1 , ty2 ) -> fprintf ppf " @ [ @< 2 > constraint % a =@ % a ] " @ ! out_type ty1 ! out_type ty2 ) td . otype_cstrs in let type_defined ppf = match td . otype_params with [ ] -> pp_print_string ppf td . otype_name | [ param ] -> fprintf ppf " [ @% a @ % s ] " @ type_parameter param td . otype_name | _ -> fprintf ppf " [ ( [ @@% a ) ] @@ % s ] " @ ( print_list type_parameter ( fun ppf -> fprintf ppf " , @ " ) ) td . otype_params td . otype_name in let print_manifest ppf = function Otyp_manifest ( ty , _ ) -> fprintf ppf " =@ % a " ! out_type ty | _ -> ( ) in let print_name_params ppf = fprintf ppf " % s % t % a " kwd type_defined print_manifest td . otype_type in let ty = match td . otype_type with Otyp_manifest ( _ , ty ) -> ty | _ -> td . otype_type in let print_private ppf = function Asttypes . Private -> fprintf ppf " private " | Asttypes . Public -> ( ) in let print_immediate ppf = if td . otype_immediate then fprintf ppf " [ %@%@ immediate ] " else ( ) in let print_out_tkind ppf = function | Otyp_abstract -> ( ) | Otyp_record lbls -> fprintf ppf " =% a % a " print_private td . otype_private print_record_decl lbls | Otyp_sum constrs -> fprintf ppf " =% a ; @< 1 2 >% a " print_private td . otype_private ( print_list print_out_constr ( fun ppf -> fprintf ppf " @ | " ) ) constrs | Otyp_open -> fprintf ppf " = . . " | ty -> fprintf ppf " =% a ; @< 1 2 >% a " print_private td . otype_private ! out_type ty in fprintf ppf " [ @< 2 [ >@< hv 2 >% t % a ] @% t % t ] " @ print_name_params print_out_tkind ty print_constraints print_immediate match ret_type_opt with | None -> begin match tyl with | [ ] -> pp_print_string ppf name | _ -> fprintf ppf " [ @< 2 >% s of @ % a ] " @ name ( print_typlist print_simple_out_type " " ) * tyl end | Some ret_type -> begin match tyl with | [ ] -> fprintf ppf " [ @< 2 >% s :@ % a ] " @ name print_simple_out_type ret_type | _ -> fprintf ppf " [ @< 2 >% s :@ % a -> % a ] " @ name ( print_typlist print_simple_out_type " " ) * tyl print_simple_out_type ret_type end let print_extended_type ppf = let print_type_parameter ppf ty = fprintf ppf " % s " ( if ty = " _ " then ty else " ' " ^ ty ) in match ext . oext_type_params with [ ] -> fprintf ppf " % s " ext . oext_type_name | [ ty_param ] -> fprintf ppf " [ @% a @ % s ] " @ print_type_parameter ty_param ext . oext_type_name | _ -> fprintf ppf " [ ( [ @@% a ) ] @@ % s ] " @ ( print_list print_type_parameter ( fun ppf -> fprintf ppf " , @ " ) ) ext . oext_type_params ext . oext_type_name in fprintf ppf " [ @< hv 2 > type % t +=% s ; @< 1 2 >% a ] " @ print_extended_type ( if ext . oext_private = Asttypes . Private then " private " else " " ) print_out_constr ( ext . oext_name , ext . oext_args , ext . oext_ret_type ) let print_extended_type ppf = let print_type_parameter ppf ty = fprintf ppf " % s " ( if ty = " _ " then ty else " ' " ^ ty ) in match te . otyext_params with [ ] -> fprintf ppf " % s " te . otyext_name | [ param ] -> fprintf ppf " [ @% a @ % s ] " @ print_type_parameter param te . otyext_name | _ -> fprintf ppf " [ ( [ @@% a ) ] @@ % s ] " @ ( print_list print_type_parameter ( fun ppf -> fprintf ppf " , @ " ) ) te . otyext_params te . otyext_name in fprintf ppf " [ @< hv 2 > type % t +=% s ; @< 1 2 >% a ] " @ print_extended_type ( if te . otyext_private = Asttypes . Private then " private " else " " ) ( print_list print_out_constr ( fun ppf -> fprintf ppf " @ | " ) ) te . otyext_constructors |
let _ = out_module_type := print_out_module_type |
let _ = out_signature := print_out_signature |
let _ = out_sig_item := print_out_sig_item |
let _ = out_type_extension := print_out_type_extension |
let print_out_exception ppf exn outv = match exn with Sys . Break -> fprintf ppf " Interrupted . . " @ | Out_of_memory -> fprintf ppf " Out of memory during evaluation . . " @ | Stack_overflow -> fprintf ppf " Stack overflow during evaluation ( looping recursion ) . . " ?@ | _ -> fprintf ppf " [ @ Exception :@ % a . ] . " @@ ! out_value outv |
let rec print_items ppf = function [ ] -> ( ) | ( Osig_typext ( ext , Oext_first ) , None ) :: items -> let rec gather_extensions acc items = match items with ( Osig_typext ( ext , Oext_next ) , None ) :: items -> gather_extensions ( ( ext . oext_name , ext . oext_args , ext . oext_ret_type ) :: acc ) items | _ -> ( List . rev acc , items ) in let exts , items = gather_extensions [ ( ext . oext_name , ext . oext_args , ext . oext_ret_type ) ] items in let te = { otyext_name = ext . oext_type_name ; otyext_params = ext . oext_type_params ; otyext_constructors = exts ; otyext_private = ext . oext_private } in fprintf ppf " [ @% a ] " @ ! out_type_extension te ; if items <> [ ] then fprintf ppf " @ % a " print_items items | ( tree , valopt ) :: items -> begin match valopt with Some v -> fprintf ppf " [ @< 2 >% a =@ % a ] " @ ! out_sig_item tree ! out_value v | None -> fprintf ppf " [ @% a ] " @ ! out_sig_item tree end ; if items <> [ ] then fprintf ppf " @ % a " print_items items |
let print_out_phrase ppf = function Ophr_eval ( outv , ty ) -> fprintf ppf " [ @- : % a @ =@ % a ] . " @@ ! out_type ty ! out_value outv | Ophr_signature [ ] -> ( ) | Ophr_signature items -> fprintf ppf " [ @< v >% a ] . " @@ print_items items | Ophr_exception ( exn , outv ) -> print_out_exception ppf exn outv |
let out_phrase = ref print_out_phrase |
module type XOR = sig type t val xor : t -> t -> t [ @@ logic ] end |
module type CIPHER = sig type elt type t val cipher : t -> t -> t end |
module Make ( X : XOR ) : CIPHER with type elt = X . t = struct type elt = X . t type t = elt list let rec cipher key msg = match ( key , msg ) with | [ ] , [ ] -> [ ] | x :: xs , y :: ys -> X . xor x y :: cipher xs ys | _ -> assert false let correct msg key = let r = cipher key msg in cipher r key end |
module XBool : XOR with type t = bool = struct type t = bool let [ @ logic ] xor t1 t2 = ( ( not t1 ) && t2 ) || ( t1 && not t2 ) end |
module XBool2 : XOR with type t = bool = struct type t = bool let [ @ logic ] xor t1 t2 = if t1 then not t2 else t2 end |
module XBit : XOR = struct type t = Zero | One let [ @ logic ] xor t1 t2 = match ( t1 , t2 ) with Zero , Zero | One , One -> Zero | _ -> One end |
module CBool = Make ( XBool ) |
module CBool2 = Make ( XBool2 ) |
module CBit = Make ( XBit ) |
module Make ( A : sig val rewrite : Path . t -> Outcometree . out_ident end ) = struct |
let unique_names = ref Ident . empty |
let ident_name id = try Ident . find_same id ! unique_names with Not_found -> Ident . name id |
let add_unique id = try ignore ( Ident . find_same id ! unique_names ) with Not_found -> unique_names := Ident . add id ( Ident . unique_toplevel_name id ) ! unique_names |
let ident ppf id = pp_print_string ppf ( ident_name id ) |
let ident_pervasive = Ident . create_persistent " Pervasives " |
let rec tree_of_path = function | Pident id -> Oide_ident ( ident_name id ) | Pdot ( Pident id , s , _pos ) when Ident . same id ident_pervasive -> Oide_ident s | Pdot ( p , s , _pos ) -> Oide_dot ( tree_of_path p , s ) | Papply ( p1 , p2 ) -> Oide_apply ( tree_of_path p1 , tree_of_path p2 ) |
let tree_of_path p = A . rewrite p |
let rec path ppf = function | Pident id -> ident ppf id | Pdot ( Pident id , s , _pos ) when Ident . same id ident_pervasive -> pp_print_string ppf s | Pdot ( p , s , _pos ) -> path ppf p ; pp_print_char ppf ' . ' ; pp_print_string ppf s | Papply ( p1 , p2 ) -> fprintf ppf " % a ( % a ) " path p1 path p2 |
let rec string_of_out_ident = function | Oide_ident s -> s | Oide_dot ( id , s ) -> String . concat " . " [ string_of_out_ident id ; s ] | Oide_apply ( id1 , id2 ) -> String . concat " " [ string_of_out_ident id1 ; " ( " ; string_of_out_ident id2 ; " ) " ] |
let tree_of_rec = function | Trec_not -> Orec_not | Trec_first -> Orec_first | Trec_next -> Orec_next |
let raw_list pr ppf = function [ ] -> fprintf ppf " [ ] " | a :: l -> fprintf ppf " [ @< 1 [ >% a % t ] ] " @ pr a ( fun ppf -> List . iter ( fun x -> fprintf ppf " ; , @% a " pr x ) l ) |
let kind_vars = ref [ ] |
let kind_count = ref 0 |
let rec safe_kind_repr v = function Fvar { contents = Some k } -> if List . memq k v then " Fvar loop " else safe_kind_repr ( k :: v ) k | Fvar r -> let vid = try List . assq r ! kind_vars with Not_found -> let c = incr kind_count ; ! kind_count in kind_vars := ( r , c ) :: ! kind_vars ; c in Printf . sprintf " Fvar { None } @% d " vid | Fpresent -> " Fpresent " | Fabsent -> " Fabsent " |
let rec safe_commu_repr v = function Cok -> " Cok " | Cunknown -> " Cunknown " | Clink r -> if List . memq r v then " Clink loop " else safe_commu_repr ( r :: v ) ! r |
let rec safe_repr v = function { desc = Tlink t } when not ( List . memq t v ) -> safe_repr ( t :: v ) t | t -> t |
let rec list_of_memo = function Mnil -> [ ] | Mcons ( _priv , p , _t1 , _t2 , rem ) -> p :: list_of_memo rem | Mlink rem -> list_of_memo ! rem |
let print_name ppf = function None -> fprintf ppf " None " | Some name -> fprintf ppf " " \% s " " \ name |
let string_of_label = function Nolabel -> " " | Labelled s -> s | Optional s -> " " ?^ s |
type param_subst = Id | Nth of int | Map of int list |
let is_nth = function Nth _ -> true | _ -> false |
let compose l1 = function | Id -> Map l1 | Map l2 -> Map ( List . map ( List . nth l1 ) l2 ) | Nth n -> Nth ( List . nth l1 n ) |
let apply_subst s1 tyl = match s1 with Nth n1 -> [ List . nth tyl n1 ] | Map l1 -> List . map ( List . nth tyl ) l1 | Id -> tyl |
type best_path = Paths of Path . t list | Best of Path . t |
let printing_env = ref Env . empty |
let printing_depth = ref 0 |
let printing_cont = ref ( [ ] : Env . iter_cont list ) |
let printing_old = ref Env . empty |
let printing_pers = ref Concr . empty |
let printing_map = ref PathMap . empty |
let rec index l x = match l with [ ] -> raise Not_found | a :: l -> if x == a then 0 else 1 + index l x |
let rec uniq = function [ ] -> true | a :: l -> not ( List . memq a l ) && uniq l |
let rec normalize_type_path ( ? cache = false ) env p = try let ( params , ty , _ ) = Env . find_type_expansion p env in let params = List . map repr params in match repr ty with { desc = Tconstr ( p1 , tyl , _ ) } -> let tyl = List . map repr tyl in if List . length params = List . length tyl && List . for_all2 ( ) == params tyl then normalize_type_path ~ cache env p1 else if cache || List . length params <= List . length tyl || not ( uniq tyl ) then ( p , Id ) else let l1 = List . map ( index params ) tyl in let ( p2 , s2 ) = normalize_type_path ~ cache env p1 in ( p2 , compose l1 s2 ) | ty -> ( p , Nth ( index params ty ) ) with Not_found -> ( Env . normalize_path None env p , Id ) |
let penalty s = if s <> " " && s . [ 0 ] = ' _ ' then 10 else try for i = 0 to String . length s - 2 do if s . [ i ] = ' _ ' && s . [ i + 1 ] = ' _ ' then raise Exit done ; 1 with Exit -> 10 |
let rec path_size = function Pident id -> penalty ( Ident . name id ) , - Ident . binding_time id | Pdot ( p , _ , _ ) -> let ( l , b ) = path_size p in ( 1 + l , b ) | Papply ( p1 , p2 ) -> let ( l , b ) = path_size p1 in ( l + fst ( path_size p2 ) , b ) |
let same_printing_env env = let used_pers = Env . used_persistent ( ) in Env . same_types ! printing_old env && Concr . equal ! printing_pers used_pers |
let set_printing_env env = printing_env := if ! Clflags . real_paths then Env . empty else env ; if ! printing_env == Env . empty || same_printing_env env then ( ) else begin printing_old := env ; printing_pers := Env . used_persistent ( ) ; printing_map := PathMap . empty ; printing_depth := 0 ; let cont = Env . iter_types ( fun p ( p ' , _decl ) -> let ( p1 , s1 ) = normalize_type_path env p ' ~ cache : true in if s1 = Id then try let r = PathMap . find p1 ! printing_map in match ! r with Paths l -> r := Paths ( p :: l ) | Best p ' -> r := Paths [ p ; p ' ] with Not_found -> printing_map := PathMap . add p1 ( ref ( Paths [ p ] ) ) ! printing_map ) env in printing_cont := [ cont ] ; end |
let wrap_printing_env env f = set_printing_env env ; try_finally f ( fun ( ) -> set_printing_env Env . empty ) |
let wrap_printing_env env f = Env . without_cmis ( wrap_printing_env env ) f |
let is_unambiguous path env = let l = Env . find_shadowed_types path env in List . exists ( Path . same path ) l || match l with [ ] -> true | p :: rem -> let normalize p = fst ( normalize_type_path ~ cache : true env p ) in let p ' = normalize p in List . for_all ( fun p -> Path . same ( normalize p ) p ' ) rem || let id = lid_of_path p in List . for_all ( fun p -> lid_of_path p = id ) rem && Path . same p ( Env . lookup_type id env ) |
let rec get_best_path r = match ! r with Best p ' -> p ' | Paths [ ] -> raise Not_found | Paths l -> r := Paths [ ] ; List . iter ( fun p -> match ! r with Best p ' when path_size p >= path_size p ' -> ( ) | _ -> if is_unambiguous p ! printing_env then r := Best p ) l ; get_best_path r |
let best_type_path p = if ! Clflags . real_paths || ! printing_env == Env . empty then ( p , Id ) else let ( p ' , s ) = normalize_type_path ! printing_env p in let get_path ( ) = get_best_path ( PathMap . find p ' ! printing_map ) in while ! printing_cont <> [ ] && try fst ( path_size ( get_path ( ) ) ) > ! printing_depth with Not_found -> true do printing_cont := List . map snd ( Env . run_iter_cont ! printing_cont ) ; incr printing_depth ; done ; let p ' ' = try get_path ( ) with Not_found -> p ' in ( p ' ' , s ) |
let names = ref ( [ ] : ( type_expr * string ) list ) |
let name_counter = ref 0 |
let named_vars = ref ( [ ] : string list ) |
let reset_names ( ) = names := [ ] ; name_counter := 0 ; named_vars := [ ] |
let add_named_var ty = match ty . desc with Tvar ( Some name ) | Tunivar ( Some name ) -> if List . mem name ! named_vars then ( ) else named_vars := name :: ! named_vars | _ -> ( ) |
let rec new_name ( ) = let name = if ! name_counter < 26 then String . make 1 ( Char . chr ( 97 + ! name_counter ) ) else String . make 1 ( Char . chr ( 97 + ! name_counter mod 26 ) ) ^ string_of_int ( ! name_counter / 26 ) in incr name_counter ; if List . mem name ! named_vars || List . exists ( fun ( _ , name ' ) -> name = name ' ) ! names then new_name ( ) else name |
let name_of_type t = try List . assq t ! names with Not_found -> let name = match t . desc with Tvar ( Some name ) | Tunivar ( Some name ) -> let current_name = ref name in let i = ref 0 in while List . exists ( fun ( _ , name ' ) -> ! current_name = name ' ) ! names do current_name := name ^ ( string_of_int ! i ) ; i := ! i + 1 ; done ; ! current_name | _ -> new_name ( ) in if name <> " _ " then names := ( t , name ) :: ! names ; name |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.