text
stringlengths
0
601k
let find_file_in_dir d p = Unix . Find . fold [ d ] None & fun st path -> match st with | Some _ -> ` Prune , st | None -> if p path then ` Prune , Some path else ` Continue , None
let find_in_path dirs m = try Misc . find_in_path dirs m with | Not_found -> !!% " [ @< 2 > Error : % s is not found in @ [ @% a ] ] . " @@@ m Format . ( list " @ " string ) dirs ; criticalf " Error : % s is not found in % s " m ( String . concat " " dirs )
let find_global_module id = try Some ( Misc . find_in_path_uncap ! Config . load_path ( id . Ident . name ^ " . cmi " ) ) with | Not_found -> None
let module_name p = String . capitalize_ascii & fst & Filename . ( split_extension & basename p )
let around_if b start end_ f = if b then start ( ) ; let res = f ( ) in if b then end_ ( ) ; res
let top_package_name x = match String . index x ' . ' with | exception _ -> x | n -> String . sub x 0 n
let is_package_path_name = function | " " -> false | s -> String . unsafe_get s 0 = ' { '
let out_ident_of_path = let open Outcometree in let rec f = function | Path . Pident id -> Oide_ident ( Ident . name id ) | Pdot ( p , s , _ ) -> Oide_dot ( f p , s ) | _ -> assert false in f
module type Arena = sig type t val create : elt_length : int -> initial_capacity : int -> t val is_full : t -> bool val expand : t -> int -> unit type id val allocate : t -> string -> id val dereference : t -> id -> string val elt_equal : t -> id -> string -> bool end
module type Small_list = sig type ' + a t val empty : _ t val cons : ' a -> ' a t -> ' a t val iter : ( ' a -> unit ) -> ' a t -> unit val exists : ( ' a -> bool ) -> ' a t -> bool end
module type String_set = sig type t val create : elt_length : int -> initial_capacity : int -> t val add : t -> string -> unit val mem : t -> string -> bool end
module type Intf = sig module Arena : Arena module Small_list : Small_list module String_set : String_set end
let split_metadata_no_metadata1 = let open Alcotest in test_case " split_metadate - no metadata 1 " ` Quick $ fun ( ) -> let str = " " in let computed = split_metadata str in let expected = None , " " in check ( pair ( option string ) string ) " should be equal " computed expected ; ;
let split_metadata_no_metadata2 = let open Alcotest in test_case " split_metadate - no metadata 2 " ` Quick $ fun ( ) -> let str = { | Lorem ipsum dolor sit amet , consectetur adipiscing elit . Etiam in vehicula lorem . Etiam erat libero , scelerisque eu placerat a , varius ut lorem . Nunc non erat id libero facilisis ...
let split_metadata_no_metadata3 = let open Alcotest in test_case " split_metadate - no metadata 3 " ` Quick $ fun ( ) -> let str = { |--- Lorem ipsum dolor sit amet , consectetur adipiscing elit . Etiam in vehicula lorem . Etiam erat libero , scelerisque eu placerat a , varius ut lorem . Nunc non erat id libero facilis...
let split_metadata_no_metadata4 = let open Alcotest in test_case " split_metadate - no metadata 4 " ` Quick $ fun ( ) -> let str = { } |---| in let computed = split_metadata str in let expected = None , str in check ( pair ( option string ) string ) " should be equal " computed expected ; ;
let split_metadata_no_metadata5 = let open Alcotest in test_case " split_metadate - no metadata 5 " ` Quick $ fun ( ) -> let str = { |--- let computed = split_metadata str in let expected = None , str in check ( pair ( option string ) string ) " should be equal " computed expected ; ;
let split_metadata_no_metadata6 = let open Alcotest in test_case " split_metadate - no metadata 6 " ` Quick $ fun ( ) -> let str = { |-- --- let computed = split_metadata str in let expected = None , str in check ( pair ( option string ) string ) " should be equal " computed expected ; ;
let split_metadata_with_metadata1 = let open Alcotest in test_case " split_metadate - with metadata 1 " ` Quick $ fun ( ) -> let str = { |--- foo ; s let computed = split_metadata str in let expected = Some " foo ; s \ n " , " " in check ( pair ( option string ) string ) " should be equal " computed expected ; ;
let split_metadata_with_metadata2 = let open Alcotest in test_case " split_metadate - with metadata 2 " ` Quick $ fun ( ) -> let str = { |--- foo ; s --- let computed = split_metadata str in let expected = Some " foo ; s \ n " , " \ nok ok ok \ n \ n " in check ( pair ( option string ) string ) " should be equal " comp...
let split_metadata_with_metadata3 = let open Alcotest in test_case " split_metadate - with metadata 3 " ` Quick $ fun ( ) -> let str = { |--- hello -- bar --- test let computed = split_metadata str in let expected = Some " foo ; s -------\ nhello \ n --\ nfooo ---\ nbar \ n " , " test \ nok ok ok \ n \ n " in check ( p...
let split_metadata_with_metadata4 = let open Alcotest in test_case " split_metadate - with metadata 4 " ` Quick $ fun ( ) -> let str = { |--- A discared text hello -- bar --- test } | in let computed = split_metadata str in let expected = Some " foo ; s -------\ nhello \ n --\ nfooo ---\ nbar \ n " , " test \ nok ok ok...
let cases = ( " Util " , [ split_metadata_no_metadata1 ; split_metadata_no_metadata2 ; split_metadata_no_metadata3 ; split_metadata_no_metadata4 ; split_metadata_no_metadata5 ; split_metadata_no_metadata6 ; split_metadata_with_metadata1 ; split_metadata_with_metadata2 ; split_metadata_with_metadata3 ; split_metadata_wi...
let toploop_get_directive name = Toploop . get_directive name # else try Some ( Hashtbl . find Toploop . directive_table name ) with Not_found -> None # endif
let history = LTerm_history . create [ ]
let history_file_name = ref ( Some ( Filename . concat LTerm_resources . home " . utop - history " ) )
let history_file_max_size = ref None
let history_file_max_entries = ref None
let stashable_session_history = UTop_history . create ( )
let new_command_hooks = LTerm_dlist . create ( )
let at_new_command f = ignore ( LTerm_dlist . add_l f new_command_hooks )
type ui = UTop_private . ui = Console | Emacs
let get_ui ( ) = S . value UTop_private . ui
type profile = Dark | Light
let profile , set_profile = S . create Dark
let set_profile p = set_profile p
let time = ref ( Unix . time ( ) )
let ( ) = at_new_command ( fun ( ) -> time := Unix . time ( ) )
let make_variable ? eq x = let signal , set = S . create ? eq x in let set v = set v in ( signal , ( fun ( ) -> S . value signal ) , set )
let hide_reserved , get_hide_reserved , set_hide_reserved = make_variable true
let create_implicits , get_create_implicits , set_create_implicits = make_variable false
let show_box , get_show_box , set_show_box = make_variable true
let phrase_terminator , get_phrase_terminator , set_phrase_terminator = make_variable " ; ; "
let auto_run_lwt , get_auto_run_lwt , set_auto_run_lwt = make_variable true
let auto_run_async , get_auto_run_async , set_auto_run_async = make_variable true
let topfind_verbose , get_topfind_verbose , set_topfind_verbose = make_variable false
let external_editor , get_external_editor , set_external_editor = make_variable ( try Sys . getenv " EDITOR " with Not_found -> " vi " )
let end_and_accept_current_phrase : LTerm_read_line . action = Edit ( Custom ( fun ( ) -> assert false ) )
let set_margin_function f = UTop_private . set_margin_function f
let default_keywords = [ " and " ; " as " ; " assert " ; " begin " ; " class " ; " constraint " ; " do " ; " done " ; " downto " ; " else " ; " end " ; " exception " ; " external " ; " for " ; " fun " ; " function " ; " functor " ; " if " ; " in " ; " include " ; " inherit " ; " initializer " ; " lazy " ; " let " ; " m...
let keywords = ref ( String_set . of_list default_keywords )
let add_keyword kwd = keywords := String_set . add kwd ! keywords
type lines = { start : int ; stop : int ; }
let get_message func x = let buffer = Buffer . create 1024 in let pp = Format . formatter_of_buffer buffer in UTop_private . set_margin pp ; func pp x ; Format . pp_print_flush pp ( ) ; Buffer . contents buffer
let get_ocaml_error_message exn = let buffer = Buffer . create 1024 in let pp = Format . formatter_of_buffer buffer in UTop_private . set_margin pp ; Errors . report_error pp exn ; Format . pp_print_flush pp ( ) ; let str = Buffer . contents buffer in try Scanf . sscanf str " Characters % d -% d :\ n [ %\ 000 -\ 255 ] ...
let collect_formatters buf pps f = List . iter ( fun pp -> Format . pp_print_flush pp ( ) ) pps ; let save = List . map ( fun pp -> Format . pp_get_formatter_out_functions pp ( ) ) pps in let restore ( ) = List . iter2 ( fun pp out_functions -> Format . pp_print_flush pp ( ) ; Format . pp_set_formatter_out_functions pp...
let discard_formatters pps f = List . iter ( fun pp -> Format . pp_print_flush pp ( ) ) pps ; let save = List . map ( fun pp -> Format . pp_get_formatter_out_functions pp ( ) ) pps in let restore ( ) = List . iter2 ( fun pp out_functions -> Format . pp_print_flush pp ( ) ; Format . pp_set_formatter_out_functions pp out...
type location = int * int
type ' a result = | Value of ' a | Error of location list * string
let lexbuf_of_string eof str = let pos = ref 0 in let lexbuf = Lexing . from_function ( fun buf len -> if ! pos = String . length str then begin eof := true ; 0 end else begin let len = min len ( String . length str - ! pos ) in String . blit str ! pos buf 0 len ; pos := ! pos + len ; len end ) in Location . init lexbu...
let mkloc loc = ( loc . Location . loc_start . Lexing . pos_cnum , loc . Location . loc_end . Lexing . pos_cnum )
let parse_default parse str eos_is_error = let eof = ref false in let lexbuf = lexbuf_of_string eof str in try let phrase = parse lexbuf in Value phrase with | _ when ! eof && not eos_is_error -> raise Need_more | End_of_file -> raise Need_more | Lexer . Error ( error , loc ) -> ( match Location . error_of_exn ( Lexer ...
let parse_toplevel_phrase_default = parse_default Parse . toplevel_phrase
let parse_toplevel_phrase = ref parse_toplevel_phrase_default
let parse_use_file_default = parse_default Parse . use_file
let parse_use_file = ref parse_use_file_default
let null = Format . make_formatter ( fun str ofs len -> ( ) ) ignore
let rec last head tail = match tail with | [ ] -> head | head :: tail -> last head tail
let with_loc loc str = { Location . txt = str ; Location . loc = loc ; }
let nolabel = Asttypes . Nolabel # else
let nolabel = " " # endif
let check_phrase phrase = let open Parsetree in match phrase with | Ptop_dir _ -> None | Ptop_def [ ] -> None | Ptop_def ( item :: items ) -> let loc = { Location . loc_start = item . pstr_loc . Location . loc_start ; Location . loc_end = ( last item items ) . pstr_loc . Location . loc_end ; Location . loc_ghost = fals...
let make_prompt ui profile count size key_sequence ( recording , macro_count , macro_counter ) = let tm = Unix . localtime ! time in let color dark light = match profile with | Dark -> dark | Light -> light in match ui with | Emacs -> [ ] || | Console -> let bold = profile = Dark in let txta = if key_sequence = [ ] the...
let default_prompt = S . l6 make_prompt UTop_private . ui profile count size key_sequence ( S . l3 ( fun x y z -> ( x , y , z ) ) ( Zed_macro . recording LTerm_read_line . macro ) ( Zed_macro . count LTerm_read_line . macro ) ( Zed_macro . counter LTerm_read_line . macro ) )
let prompt = ref default_prompt
let default_info = { Toploop . section = " UTop " ; doc = " " ; }
let ( ) = Toploop . add_directive " utop_prompt_simple " ( Toploop . Directive_none ( fun ( ) -> prompt := S . map ( Printf . ksprintf LTerm_text . of_utf8 " utop [ % d ] : " ) count ) ) default_info ; Toploop . add_directive " utop_prompt_dummy " ( Toploop . Directive_none ( fun ( ) -> prompt := S . const ( LTerm_text...
module Bindings = Zed_input . Make ( LTerm_key )
module Keys_map = Map . Make ( struct type t = LTerm_key . t list let compare = compare end )
let name_of_action action = if action == end_and_accept_current_phrase then " end - and - accept - current - phrase " else LTerm_read_line . name_of_action action
let doc_of_action action = if action == end_and_accept_current_phrase then " end the current phrase with the phrase terminator ( ; ; ) and evaluate it " else LTerm_read_line . doc_of_action action
let ( ) = Toploop . add_directive " utop_help " ( Toploop . Directive_none ( fun ( ) -> print_endline " If you can ' t see the prompt properly try : # utop_prompt_simple default_info ; Toploop . add_directive " utop_bindings " ( Toploop . Directive_none ( fun ( ) -> let make_lines keys actions acc = match actions with ...
let ( ) = Toploop . add_directive " pwd " ( Toploop . Directive_none ( fun ( ) -> print_endline ( Sys . getcwd ( ) ) ) ) default_info
let make_stash_directive entry_formatter fname = if get_ui ( ) = Emacs then print_endline " Stashing is currently not supported in Emacs " else let entries = UTop_history . contents stashable_session_history in let entries = match entries with [ ] -> [ ] | _ :: e -> e in let entries = List . rev entries in Printf . pri...
let ( ) = let fn = make_stash_directive begin function | UTop_history . Input i -> i | Output out | Error out | Bad_input out | Warnings out -> Printf . sprintf " " out end in Toploop . add_directive " utop_stash " ( Toploop . Directive_string fn ) default_info
let ( ) = let fn = make_stash_directive begin function | UTop_history . Input i | Bad_input i -> Printf . sprintf " # % s " i | Output out | Error out | Warnings out -> out end in Toploop . add_directive " utop_save " ( Toploop . Directive_string fn ) default_info
let print_error msg = Lazy . force LTerm . stdout >>= fun term -> LTerm . set_style term ! UTop_private . error_style >>= fun ( ) -> Lwt_io . print msg >>= fun ( ) -> LTerm . set_style term LTerm_style . none >>= fun ( ) -> LTerm . flush term
let handle_findlib_error = function | Failure msg -> Lwt_main . run ( print_error msg ) | Fl_package_base . No_such_package ( pkg , reason ) -> Lwt_main . run ( print_error ( Printf . sprintf " No such package : % s % s \ n " pkg ( if reason <> " " then " - " ^ reason else " " ) ) ) | Fl_package_base . Package_loop pkg...
let topfind_log , set_topfind_log = S . create ~ eq ( : fun _ _ -> false ) [ ]
let ( ) = let real_log = ! Topfind . log in Topfind . log := fun str -> set_topfind_log ( str :: S . value topfind_log ) ; if S . value topfind_verbose then real_log str
let ( ) = Toploop . add_directive " topfind_log " ( Toploop . Directive_none ( fun ( ) -> List . iter ( fun str -> print_string str ; print_char ' \ n ' ) ( S . value topfind_log ) ; flush stdout ) ) default_info ; Toploop . add_directive " topfind_verbose " ( Toploop . Directive_bool set_topfind_verbose ) default_info
let split_words str = let len = String . length str in let is_sep = function | ' ' | ' \ t ' | ' \ r ' | ' \ n ' | ' , ' -> true | _ -> false in let rec skip acc i = if i = len then acc else if is_sep str . [ i ] then skip acc ( i + 1 ) else extract acc i ( i + 1 ) and extract acc i j = if j = len then ( String . sub s...
let require packages = try let eff_packages = Findlib . package_deep_ancestors ! Topfind . predicates packages in Topfind . load eff_packages with exn -> handle_findlib_error exn
let ( ) = Toploop . add_directive " require " ( Toploop . Directive_string ( fun str -> require ( split_words str ) ) ) default_info
let try_finally ~ always work = Misc . try_finally ~ always work # else Misc . try_finally work always # endif
let use_output command = let fn = Filename . temp_file " ocaml " " _toploop . ml " in try_finally ~ always ( : fun ( ) -> try Sys . remove fn with Sys_error _ -> ( ) ) ( fun ( ) -> match Printf . ksprintf Sys . command " % s > % s " command ( Filename . quote fn ) with | 0 -> ignore ( Toploop . use_file Format . std_fo...
let ( ) = let name = " use_output " in if toploop_get_directive name = None then Toploop . add_directive name ( Toploop . Directive_string use_output ) default_info
let ( ) = Topfind . don ' t_load_deeply [ " utop " ] ; Topfind . add_predicates [ " byte " ; " toploop " ] ; Topdirs . dir_directory ( Findlib . package_directory " findlib " ) ; Topdirs . dir_directory ( Findlib . package_directory " utop " )
let get_load_path ( ) = Load_path . get_paths ( )
let set_load_path path = Load_path . init path # else