text
stringlengths
12
786k
let get_cached var f = match ! var with | Some x -> x | None -> let x = f ( ) in var := Some x ; x
let visible_modules ( ) = get_cached visible_modules ( fun ( ) -> List . fold_left ( fun acc dir -> try Array . fold_left ( fun acc fname -> if Filename . check_suffix fname " . cmi " then String_set . add ( String . capitalize_ascii ( Filename . chop_suffix fname " . cmi " ...
let field_name { ld_id = id } = Ident . name id
let constructor_name { cd_id = id } = Ident . name id
let add_fields_of_type decl acc = match decl . type_kind with | Type_variant ( constructors , _ ) -> # else | Type_variant constructors -> # endif acc | Type_record ( fields , _ ) -> List . fold_left ( fun acc field -> add ( field_name field ) acc ) acc fields | Type_abstract -> ac...
let add_names_of_type decl acc = match decl . type_kind with | Type_variant ( constructors , _ ) -> # else | Type_variant constructors -> # endif List . fold_left ( fun acc cstr -> add ( constructor_name cstr ) acc ) acc constructors | Type_record ( fields , _ ) -> List . fold_left...
let path_of_mty_alias = function | Mty_alias path -> path | _ -> assert false
let path_of_mty_alias = function | Mty_alias ( _ , path ) -> path | _ -> assert false # else
let path_of_mty_alias = function | Mty_alias path -> path | _ -> assert false # endif
let rec names_of_module_type = function | Mty_signature decls -> List . fold_left ( fun acc decl -> match decl with | Sig_value ( id , _ , _ ) | Sig_typext ( id , _ , _ , _ ) | Sig_module ( id , _ , _ , _ , _ ) | Sig_modtype ( id , _ , _ ) | Sig_class ( id...
let rec fields_of_module_type = function | Mty_signature decls -> List . fold_left ( fun acc decl -> match decl with | Sig_value _ | Sig_typext _ | Sig_module _ | Sig_modtype _ | Sig_class _ | Sig_class_type _ -> acc | Sig_type ( _ , decl , _ , _ ) -> # else | Sig_type ( _ , d...
let lookup_module id env = let path , decl = Env . find_module_by_name id env in ( path , decl . md_type ) # else let path = Env . lookup_module id env ~ load : true in ( path , ( Env . find_module path env ) . md_type ) # endif
let find_module path env = ( Env . find_module path env ) . md_type
let names_of_module longident = try Longident_map . find longident ! local_names_by_longident with Not_found -> match lookup_env lookup_module longident ! Toploop . toplevel_env with | Some ( path , module_type ) -> let names = names_of_module_type module_type in local_names_by_path := Path_map . ad...
let fields_of_module longident = try Longident_map . find longident ! local_fields_by_longident with Not_found -> match lookup_env lookup_module longident ! Toploop . toplevel_env with | Some ( path , module_type ) -> let fields = fields_of_module_type module_type in local_fields_by_path := Path_map ....
let list_global_names ( ) = let rec loop acc = function | Env . Env_empty -> acc | Env . Env_value_unbound _ -> acc | Env . Env_module_unbound _ -> acc # endif | Env . Env_value ( summary , id , _ ) -> loop ( add ( Ident . name id ) acc ) summary | Env . Env_type ( summa...
let global_names ( ) = get_cached global_names list_global_names
let replace x y set = if String_set . mem x set then String_set . add y ( String_set . remove x set ) else set
let list_global_fields ( ) = let rec loop acc = function | Env . Env_empty -> acc | Env . Env_value_unbound _ -> acc | Env . Env_module_unbound _ -> acc # endif | Env . Env_value ( summary , id , _ ) -> loop ( add ( Ident . name id ) acc ) summary | Env . Env_type ( summ...
let global_fields ( ) = get_cached global_fields list_global_fields
let rec find_method meth type_expr = match get_desc type_expr with | Tlink type_expr -> find_method meth type_expr | Tobject ( type_expr , _ ) -> find_method meth type_expr | Tfield ( name , _ , type_expr , rest ) -> if name = meth then Some type_expr else find_method meth rest | Tpoly ( ty...
let rec methods_of_type acc type_expr = match get_desc type_expr with | Tlink type_expr -> methods_of_type acc type_expr | Tobject ( type_expr , _ ) -> methods_of_type acc type_expr | Tfield ( name , _ , _ , rest ) -> methods_of_type ( add name acc ) rest | Tpoly ( type_expr , _ ) ...
let rec find_object meths type_expr = match meths with | [ ] -> Some type_expr | meth :: meths -> match find_method meth type_expr with | Some type_expr -> find_object meths type_expr | None -> None
let methods_of_object longident meths = let lookup_value = Env . find_value_by_name # else Env . lookup_value # endif in match lookup_env lookup_value longident ! Toploop . toplevel_env with | None -> [ ] | Some ( path , { val_type = type_expr } ) -> match find_object meths type_expr with ...
let rec labels_of_type acc type_expr = match get_desc type_expr with | Tlink te -> labels_of_type acc te | Tpoly ( te , _ ) -> labels_of_type acc te | Tarrow ( label , _ , te , _ ) -> if label = " " then labels_of_type acc te else if label . [ 0 ] = ' ' ? then labels_of_type ...
let labels_of_function longident meths = let lookup_value = Env . find_value_by_name # else Env . lookup_value # endif in match lookup_env lookup_value longident ! Toploop . toplevel_env with | None -> [ ] | Some ( path , { val_type = type_expr } ) -> match find_object meths type_expr with...
let labels_of_newclass longident = let lookup_class = Env . find_class_by_name # else Env . lookup_class # endif in match lookup_env lookup_class longident ! Toploop . toplevel_env with | None -> [ ] | Some ( path , { cty_new = None } ) -> [ ] | Some ( path , { cty_new = Some t...
let filter tokens = let rec aux acc = function | [ ] -> acc | [ ( ( Blanks | Comment ( _ , true ) ) , loc ) ] -> ( Blanks , loc ) :: acc | ( ( Blanks | Comment ( _ , true ) ) , _ ) :: rest -> aux acc rest | x :: rest -> aux ( x :: acc ) rest in List ....
let rec rev_filter acc tokens = match tokens with | [ ] -> acc | [ ( ( Blanks | Comment ( _ , true ) ) , loc ) ] -> ( Blanks , loc ) :: acc | ( ( Blanks | Comment ( _ , true ) ) , _ ) :: rest -> rev_filter acc rest | x :: rest -> rev_filter ( x :: acc ) ...
let rec find_context tokens = function | [ ] -> Some ( rev_filter [ ] tokens ) | [ ( Quotation ( items , false ) , _ ) ] -> find_context_in_quotation items | _ :: rest -> find_context tokens rest | [ ] -> None | [ ( Quot_anti { a_closing = None ; a_contents = token...
let longident_parse str = let lexbuf = Lexing . from_string str in Parse . longident lexbuf # endif
let complete ~ phrase_terminator ~ input = let true_name , false_name = ( " true " , " false " ) in let tokens = UTop_lexer . lex_string input in let tokens = filter tokens in match tokens with | [ ( Symbol " " , # { idx2 = stop } ) ] | [ ( Symbol " " , # _ ) ...
let complete ~ phrase_terminator ~ input = try ( complete ~ phrase_terminator ~ input : int * ( string * string ) list ) with Cmi_format . Error _ -> ( 0 , [ ] )
type entry = | Input of string | Output of string | Error of string | Warnings of string | Bad_input of string
let create ( ) : t = ref [ ]
let contents ( t : t ) = ! t
let strip_colors s = let len = String . length s in let find_escape offset = try let i = String . index_from s offset ' \ 027 ' in if i = len - 1 || s . [ i + 1 ] <> ' [ ' then None else Some i with | Not_found -> None in let find_color_escapes offset = let rec aux acc offset = matc...
let add history v = history := v :: ! history
let add_input history input = add history @@ Input ( String . trim input )
let add_output history output = let output = String . trim output in if output <> " " then add history @@ Output output
let add_error history error = add history @@ Error ( strip_colors @@ String . trim error )
let add_bad_input history input = add history @@ Bad_input ( String . trim input )
let add_warnings history warnings = let warnings = String . trim warnings in if warnings <> " " then add history @@ Warnings warnings
let get_desc x = Types . get_desc x # else x . Types . desc # endif
let toploop_use_silently fmt name = Toploop . use_silently fmt ( File name ) # else Toploop . use_silently fmt name # endif
let return , ( ) >>= = Lwt . return , Lwt . ( ) >>=
let save_history ( ) = match ! UTop . history_file_name with | None -> return ( ) | Some fn -> Lwt . catch ( fun ( ) -> LTerm_history . save UTop . history ? max_size :! UTop . history_file_max_size ? max_entries :! UTop . history_file_max_entries fn ) ( function | Unix . Uni...
let init_history ( ) = Lwt_main . at_exit save_history ; match ! UTop . history_file_name with | None -> return ( ) | Some fn -> Lwt . catch ( fun ( ) -> LTerm_history . load UTop . history fn ) ( function | Unix . Unix_error ( error , func , arg ) -> Lwt_log . error_f...
let index_of_offset src ofs = let rec aux idx ofs ' = if ofs ' = ofs then idx else if ofs ' > ofs then idx - 1 else if ofs ' = String . length src then - 1 else aux ( idx + 1 ) ( Zed_utf8 . unsafe_next src ofs ' ) in aux 0 0
let convert_loc str ( a , b ) = ( index_of_offset str a , index_of_offset str b )
let convert_locs str locs = List . map ( fun ( a , b ) -> convert_loc str ( a , b ) ) locs
let get_line src line = let rec aux line ' ofs skipped = if ofs >= String . length src then ( " " , 0 ) else if line ' = line then ( String . sub src ofs ( String . length src - ofs ) , skipped ) else let ch , next_ofs = Zed_utf8 . unsafe_extract_next src ofs in if Zed_utf8 . ...
let convert_one_line str line ofs = let selected , skipped = get_line str line in index_of_offset selected ofs + skipped
let convert_line str ( start_ofs , end_ofs ) lines = ( convert_one_line str lines . start start_ofs , convert_one_line str lines . stop end_ofs )
let convert_loc_line input locs lines = List . map2 ( fun loc line -> match line with | None -> convert_loc input loc | Some line -> convert_line input loc line ) locs lines
let ast_impl_kind = Pparse . Structure # else
let ast_impl_kind = Config . ast_impl_magic_number # endif
let preprocess input = match input with | Parsetree . Ptop_def pstr -> Parsetree . Ptop_def ( Pparse . apply_rewriters ~ tool_name " : ocaml " ast_impl_kind pstr ) | _ -> input
let parse_input_multi input = let buf = Buffer . create 32 in let result = UTop . collect_formatters buf [ Format . err_formatter ] ( fun ( ) -> match ! UTop . parse_use_file input false with | UTop . Error ( locs , msg ) -> UTop . Error ( convert_locs input locs , " Error : "...
let parse_and_check input eos_is_error = let buf = Buffer . create 32 in let result = UTop . collect_formatters buf [ Format . err_formatter ] ( fun ( ) -> match ! UTop . parse_toplevel_phrase input eos_is_error with | UTop . Error ( locs , msg ) -> UTop . Error ( convert_locs inpu...
let add_terminator s = let terminator = UTop . get_phrase_terminator ( ) |> Zed_string . unsafe_of_utf8 in if Zed_string . ends_with s ~ suffix : terminator then s else Zed_string . append s terminator
let is_accept : LTerm_read_line . action -> bool = function | Accept -> true | action -> action == UTop . end_and_accept_current_phrase inherit [ Parsetree . toplevel_phrase UTop . result * string ] LTerm_read_line . engine ~ history ( : LTerm_history . contents UTop . history ) ( ) ...
let fix_string str = let len = String . length str in let ofs , _ , _ = Zed_utf8 . next_error str 0 in if ofs = len then str else begin let buf = Buffer . create ( len + 128 ) in if ofs > 0 then Buffer . add_substring buf str 0 ofs ; let rec loop ofs = Zed_utf8 . add buf ( UChar . ...
let render_out_phrase term string = if String . length string >= 100 * 1024 then LTerm . fprint term string else begin let string = fix_string string in let styled = LTerm_text . of_utf8 string in let stylise loc token_style = for i = loc . idx1 to loc . idx2 - 1 do let ch , style = styled . (...
let is_implicit_name name = name <> " " && name . [ 0 ] = ' _ ' && try let _ = int_of_string @@ String . sub name 1 ( String . length name - 1 ) in true with Failure _ -> false
let map_items unwrap wrap items = let rec aux acc = function | [ ] -> acc | item :: items -> let sig_item , _ = unwrap item in let name , rec_status = match sig_item with | Outcometree . Osig_class ( _ , name , _ , _ , rs ) | Outcometree . Osig_class_type ( _ , name , _ , ...
let print_out_signature pp items = if UTop . get_hide_reserved ( ) then orig_print_out_signature pp ( map_items ( fun x -> ( x , ( ) ) ) ( fun x ( ) -> x ) items ) else orig_print_out_signature pp items
let print_out_phrase pp phrase = if UTop . get_hide_reserved ( ) then let phrase = match phrase with | Outcometree . Ophr_eval _ | Outcometree . Ophr_exception _ -> phrase | Outcometree . Ophr_signature items -> Outcometree . Ophr_signature ( map_items ( fun x -> x ) ( fun x y -> ( x ...
let ( ) = Toploop . print_out_signature := print_out_signature ; Toploop . print_out_phrase := print_out_phrase
let with_loc loc str = { Location . txt = str ; Location . loc = loc ; }
type rewrite_rule = { type_to_rewrite : Longident . t ; mutable path_to_rewrite : Path . t option ; required_values : Longident . t list ; rewrite : Location . t -> Parsetree . expression -> Parsetree . expression ; enabled : bool React . signal ; }
let longident_parse str = let lexbuf = Lexing . from_string str in Parse . longident lexbuf # endif
let longident_lwt_main_run = Longident . Ldot ( Longident . Lident " Lwt_main " , " run " )
let longident_async_thread_safe_block_on_async_exn = Longident . ( Ldot ( Ldot ( Lident " Async " , " Thread_safe " ) , " block_on_async_exn " ) )
let longident_unit = Longident . Lident " ( ) "
let nolabel = Asttypes . Nolabel # else
let nolabel = " " # endif
let rewrite_rules = [ { type_to_rewrite = Longident . ( Ldot ( Lident " Lwt " , " t " ) ) ; path_to_rewrite = None ; required_values = [ longident_lwt_main_run ] ; rewrite = ( fun loc e -> let open Ast_helper in with_default_loc loc ( fun ( ) -> Exp . apply ( Exp . ...
let lookup_type longident env = Env . find_type_by_name longident env
let lookup_type longident env = let path = Env . lookup_type longident env in ( path , Env . find_type path env ) # else
let lookup_type = Env . lookup_type # endif
let rule_path rule = match rule . path_to_rewrite with | Some _ as x -> x | None -> try let env = ! Toploop . toplevel_env in let path = match lookup_type rule . type_to_rewrite env with | path , { Types . type_kind = Types . Type_abstract ; Types . type_private = Asttypes . Public ; T...
let rec is_persistent_path = function | Path . Pident id -> Ident . persistent id | Path . Pdot ( p , _ ) -> is_persistent_path p # else | Path . Pdot ( p , _ , _ ) -> is_persistent_path p # endif | Path . Papply ( _ , p ) -> is_persistent_path p
let is_persistent_in_env longident = let lookup_value = Env . find_value_by_name # else Env . lookup_value # endif in try is_persistent_path ( fst ( lookup_value longident ! Toploop . toplevel_env ) ) with Not_found -> false
let rule_matches rule path = React . S . value rule . enabled && ( match rule_path rule with | None -> false | Some path ' -> Path . same path path ' ) && List . for_all is_persistent_in_env rule . required_values
let is_eval = function | { Parsetree . pstr_desc = Parsetree . Pstr_eval _ } -> true | _ -> false
let rule_of_type typ = match get_desc ( Ctype . expand_head ! Toploop . toplevel_env typ ) with | Types . Tconstr ( path , _ , _ ) -> begin try Some ( List . find ( fun rule -> rule_matches rule path ) rewrite_rules ) with _ -> None end | _ -> None
let rewrite_str_item pstr_item tstr_item = match pstr_item , tstr_item . Typedtree . str_desc with | ( { Parsetree . pstr_desc = Parsetree . Pstr_eval ( e , _ ) ; Parsetree . pstr_loc = loc } , Typedtree . Tstr_eval ( { Typedtree . exp_type = typ } , _ ) ) -> begin ma...
let type_structure env pstr = let tstr , _ , _ , _ , _ = Typemod . type_structure env pstr in let tstr , _ , _ , _ = Typemod . type_structure env pstr in let tstr , _ , _ , _ = Typemod . type_structure env pstr Location . none in # else let tstr , _ , _ = Typemod . ty...
let rewrite phrase = match phrase with | Parsetree . Ptop_def pstr -> if ( UTop . get_auto_run_lwt ( ) || UTop . get_auto_run_async ( ) ) && List . exists is_eval pstr then let tstr = type_structure ! Toploop . toplevel_env pstr in Parsetree . Ptop_def ( List . map2 rewrite_str_item p...
let add_let binding_name def = let open Parsetree in match def with | { pstr_desc = Pstr_eval ( expr , attr ) ; pstr_loc } -> { pstr_loc ; pstr_desc = Pstr_value ( Asttypes . Nonrecursive , [ { pvb_pat = { ppat_desc = Ppat_var { txt = binding_name ; loc = pstr_loc ; } ; pp...
let bind_expressions name phrase = match phrase with | Parsetree . Ptop_def pstr -> Parsetree . Ptop_def ( List . map ( add_let name ) pstr ) | Parsetree . Ptop_dir _ -> phrase module Persistent_signature = Persistent_env . Persistent_signature # else module Persistent_signature = Env . Per...
let execute_phrase = let new_cmis = ref [ ] in let default_load = ! Persistent_signature . load in let load ~ unit_name = let res = default_load ~ unit_name in ( match res with None -> ( ) | Some x -> new_cmis := x . cmi :: ! new_cmis ) ; res in Persistent_signature . load := load ; ...
let execute_phrase = Toploop . execute_phrase # endif
let rec read_phrase term = Lwt . catch ( fun ( ) -> let read_line = new read_phrase ~ term in ( match ! UTop . edit_mode with | LTerm_editor . Default -> ( ) | LTerm_editor . Vi as mode -> read_line # set_editor_mode mode ) ; let vi_state = read_line # vi_state in vi_state # set_reg...
let print_error term msg = LTerm . set_style term styles . style_error >>= fun ( ) -> Lwt_io . print msg >>= fun ( ) -> LTerm . set_style term LTerm_style . none >>= fun ( ) -> LTerm . flush term
let rec loop term = UTop_complete . reset ( ) ; UTop_private . set_count ( S . value UTop_private . count + 1 ) ; LTerm_dlist . iter_l ( fun f -> f ( ) ) UTop . new_command_hooks ; let phrase_opt = Lwt_main . run ( Lwt . finalize ( fun ( ) -> read_phrase term >>= fun...
let welcome term = let size = LTerm . size term in let size = { rows = 3 ; cols = size . cols } in let matrix = LTerm_draw . make_matrix size in let ctx = LTerm_draw . context matrix size in let message = Printf . sprintf " Welcome to utop version % s ( using OCaml version % s ) " ! ...
let read_input_classic prompt buffer len = let rec loop i = if i = len then return ( i , false ) else Lwt_io . read_char_opt Lwt_io . stdin >>= function | Some c -> Bytes . set buffer i c ; if c = ' \ n ' then return ( i + 1 , false ) else loop ( i + 1 ) | None -> return ( i ...
module Emacs ( M : sig end ) = struct let command_oc = Unix . out_channel_of_descr ( Unix . dup Unix . stdout ) let split_at ( ? trim = false ) ch str = let rec aux acc i j = if j = String . length str then if trim && i = j then acc else ( String . sub str i ( j - i ) ) :: acc ...