text
stringlengths
0
601k
let set_load_path path = Config . load_path := path # endif
let smart_accept = ref true
let new_prompt_hooks = LTerm_dlist . create ( )
let at_new_prompt f = ignore ( LTerm_dlist . add_l f new_prompt_hooks )
let prompt_continue = ref ( S . const [ | ] ) |
let prompt_comment = ref ( S . const [ | ] ) |
let set_of_list = List . fold_left ( fun set x -> String_set . add x set ) String_set . empty
let get_desc x = Types . get_desc x # else x . Types . desc # endif
let toploop_get_directive name = Toploop . get_directive name # else try Some ( Hashtbl . find Toploop . directive_table name ) with Not_found -> None # endif
let toploop_all_directive_names ( ) = Toploop . all_directive_names ( ) # else Hashtbl . fold ( fun dir _ acc -> dir :: acc ) Toploop . directive_table [ ] # endif
let longident_of_list = function | [ ] -> invalid_arg " UTop_complete . longident_of_list " | component :: rest -> let rec loop acc = function | [ ] -> acc | component :: rest -> loop ( Longident . Ldot ( acc , component ) ) rest in loop ( Longident . Lident component ) rest
let is_valid_identifier id = id <> " " && ( match id . [ 0 ] with | ' A ' . . ' Z ' | ' a ' . . ' z ' | ' _ ' -> true | _ -> false )
let add id set = if is_valid_identifier id then String_set . add id set else set
let lookup_env f x env = try Some ( f x env ) with Not_found | Env . Error _ -> None
type value_or_field = Value | Field
let parse_longident tokens = let rec loop acc tokens = match tokens with | ( Symbol " . " , _ ) :: ( Uident id , _ ) :: tokens -> loop ( id :: acc ) tokens | ( Symbol " . " , _ ) :: ( Lident id , _ ) :: tokens -> ( Field , match acc with | [ ] -> None | l -> Some ( longident_of_list l ) ) | _ -> ( Value , match acc wit...
let parse_method tokens = let rec loop_uidents acc tokens = match tokens with | ( Symbol " . " , _ ) :: ( Uident id , _ ) :: tokens -> loop_uidents ( id :: acc ) tokens | _ -> longident_of_list acc in let rec loop_methods acc tokens = match tokens with | ( Lident meth , _ ) :: ( Symbol " " , # _ ) :: tokens -> loop_met...
type label_kind = Required | Optional
type fun_or_new = Fun | New
let parse_label tokens = let rec loop_uidents acc_uidents acc_methods tokens = match tokens with | ( Lident " new " , _ ) :: _ -> Some ( New , longident_of_list acc_uidents , acc_methods ) | ( ( Lident id | Uident id ) , _ ) :: _ when String_set . mem id ! UTop . keywords -> Some ( Fun , longident_of_list acc_uidents ,...
let list_directives phrase_terminator = String_map . bindings ( List . fold_left ( fun map dir -> let suffix = match toploop_get_directive dir with | Some ( Toploop . Directive_none _ ) -> phrase_terminator | Some ( Toploop . Directive_string _ ) -> " " " \ | Some ( Toploop . Directive_bool _ | Toploop . Directive_int ...
type file_kind = Directory | File
let basename name = let name ' = Filename . basename name in if name ' = " . " && not ( Zed_utf8 . ends_with name " . " ) then " " else name '
let add_files filter acc dir = Array . fold_left ( fun map name -> let absolute_name = Filename . concat dir name in if try Sys . is_directory absolute_name with Sys_error _ -> false then String_map . add ( Filename . concat name " " ) Directory map else if filter name then String_map . add name File map else map ) acc...
let list_directories dir = String_set . elements ( Array . fold_left ( fun set name -> let absolute_name = Filename . concat dir name in if try Sys . is_directory absolute_name with Sys_error _ -> false then String_set . add name set else set ) String_set . empty ( try Sys . readdir ( if dir = " " then Filename . curre...
let path ( ) = let path_separator = match Sys . os_type with | " Unix " | " Cygwin " -> ' ' : | " Win32 " -> ' ; ' | _ -> assert false in let split str sep = let rec split_rec pos = if pos >= String . length str then [ ] else begin match try Some ( String . index_from str pos sep ) with Not_found -> None with | Some ne...
module Path_map = Map . Make ( struct type t = Path . t let compare = compare end )
module Longident_map = Map . Make ( struct type t = Longident . t let compare = compare end )
let global_names = ref None
let global_names_revised = ref None
let local_names_by_path = ref Path_map . empty
let local_names_by_longident = ref Longident_map . empty
let global_fields = ref None
let local_fields_by_path = ref Path_map . empty
let local_fields_by_longident = ref Longident_map . empty
let visible_modules = ref None
let reset ( ) = visible_modules := None ; global_names := None ; global_names_revised := None ; local_names_by_path := Path_map . empty ; local_names_by_longident := Longident_map . empty ; global_fields := None ; local_fields_by_path := Path_map . empty ; local_fields_by_longident := Longident_map . empty
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 " ) ) acc else acc ) acc ( Sys . readdir ( if d...
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 -> acc | Type_open -> acc
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 ( fun acc field -> add ( field_name f...
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 , _ , _ , _ ) | Sig_class_type ( id , _ , _ , _ ) -> # else | S...
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 ( _ , decl , _ ) -> # endif add_fields_of_type dec...
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 . add path names ! local...
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 . add path fields !...
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 ( summary , id , decl ) -> loop ( add_names_of_type decl...
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 ( summary , id , decl ) -> loop ( add_fields_of_type de...
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 ( type_expr , _ ) -> find_method ...
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 , _ ) -> methods_of_type acc type_expr | Tco...
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 | None -> [ ] | Some type_expr...
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 ( String_map . add ( String . sub label 1 ( ...
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 | None -> [ ] | Some type_exp...
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 type_expr } ) -> labels_of_type String_map ...
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 . rev ( aux [ ] tokens )
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 ) rest
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 = tokens } , _ ) ] -> find_context tokens tokens | _ :: rest -...
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 " " , # _ ) ; ( Blanks , { idx2 = stop } ) ] -> ( stop , list_directives...
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 = match find_escape offset with | None -> (...
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 . Unix_error ( error , func , arg ) -> Lwt_log . ...
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 " cannot load history from % S : % s : % s " fn fu...
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 . escaped_char ch = " \\ n " then aux ( line ...
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 : " ^ msg ^ " \ n " ) | UTop . Value phrases -...
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 input locs , " Error : " ^ msg ^ " \ n " )...
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 ) ( ) as super inherit [ Parsetree . tople...
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 . of_char str . [ ofs ] ) ; let ofs1 = o...