2016-02-18 07:11:59 -08:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* OCaml *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique. *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. This file is distributed under the terms of *)
|
|
|
|
(* the GNU Lesser General Public License version 2.1, with the *)
|
|
|
|
(* special exception on linking described in the file LICENSE. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
1995-08-09 08:06:35 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Miscellaneous useful types and functions *)
|
|
|
|
|
|
|
|
val fatal_error: string -> 'a
|
2015-12-18 07:50:24 -08:00
|
|
|
val fatal_errorf: ('a, Format.formatter, unit, 'b) format4 -> 'a
|
1995-05-04 03:15:53 -07:00
|
|
|
exception Fatal_error
|
|
|
|
|
2003-04-01 17:32:09 -08:00
|
|
|
val try_finally : (unit -> 'a) -> (unit -> unit) -> 'a;;
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
val map_end: ('a -> 'b) -> 'a list -> 'b list -> 'b list
|
1996-01-04 04:51:11 -08:00
|
|
|
(* [map_end f l t] is [map f l @ t], just more efficient. *)
|
2002-02-08 08:55:44 -08:00
|
|
|
val map_left_right: ('a -> 'b) -> 'a list -> 'b list
|
|
|
|
(* Like [List.map], with guaranteed left-to-right evaluation order *)
|
1995-05-04 03:15:53 -07:00
|
|
|
val for_all2: ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
|
2000-11-22 05:51:07 -08:00
|
|
|
(* Same as [List.for_all] but for a binary predicate.
|
|
|
|
In addition, this [for_all2] never fails: given two lists
|
|
|
|
with different lengths, it returns false. *)
|
1996-01-04 04:51:11 -08:00
|
|
|
val replicate_list: 'a -> int -> 'a list
|
|
|
|
(* [replicate_list elem n] is the list with [n] elements
|
|
|
|
all identical to [elem]. *)
|
1999-02-04 02:32:27 -08:00
|
|
|
val list_remove: 'a -> 'a list -> 'a list
|
|
|
|
(* [list_remove x l] returns a copy of [l] with the first
|
2000-02-28 07:47:13 -08:00
|
|
|
element equal to [x] removed. *)
|
|
|
|
val split_last: 'a list -> 'a list * 'a
|
|
|
|
(* Return the last element and the other elements of the given list. *)
|
1999-11-30 08:07:38 -08:00
|
|
|
val may: ('a -> unit) -> 'a option -> unit
|
|
|
|
val may_map: ('a -> 'b) -> 'a option -> 'b option
|
|
|
|
|
2015-12-30 06:43:44 -08:00
|
|
|
module Stdlib : sig
|
|
|
|
module List : sig
|
|
|
|
type 'a t = 'a list
|
|
|
|
|
|
|
|
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
|
|
|
|
(** The lexicographic order supported by the provided order.
|
|
|
|
There is no constraint on the relative lengths of the lists. *)
|
|
|
|
|
|
|
|
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
|
|
|
|
(** Returns [true] iff the given lists have the same length and content
|
|
|
|
with respect to the given equality function. *)
|
|
|
|
|
|
|
|
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
|
2016-01-12 04:44:20 -08:00
|
|
|
(** [filter_map f l] applies [f] to every element of [l], filters
|
|
|
|
out the [None] elements and returns the list of the arguments of
|
|
|
|
the [Some] elements. *)
|
2015-12-30 06:43:44 -08:00
|
|
|
|
|
|
|
val some_if_all_elements_are_some : 'a option t -> 'a t option
|
|
|
|
(** If all elements of the given list are [Some _] then [Some xs]
|
|
|
|
is returned with the [xs] being the contents of those [Some]s, with
|
2016-01-12 04:44:20 -08:00
|
|
|
order preserved. Otherwise return [None]. *)
|
2015-12-30 06:43:44 -08:00
|
|
|
|
|
|
|
val map2_prefix : ('a -> 'b -> 'c) -> 'a t -> 'b t -> ('c t * 'b t)
|
|
|
|
(** [let r1, r2 = map2_prefix f l1 l2]
|
|
|
|
If [l1] is of length n and [l2 = h2 @ t2] with h2 of length n,
|
|
|
|
r1 is [List.map2 f l1 h1] and r2 is t2. *)
|
|
|
|
|
2016-01-12 04:44:20 -08:00
|
|
|
val split_at : int -> 'a t -> 'a t * 'a t
|
2015-12-30 06:43:44 -08:00
|
|
|
(** [split_at n l] returns the pair [before, after] where [before] is
|
|
|
|
the [n] first elements of [l] and [after] the remaining ones.
|
|
|
|
If [l] has less than [n] elements, raises Invalid_argument. *)
|
|
|
|
end
|
|
|
|
|
|
|
|
module Option : sig
|
|
|
|
type 'a t = 'a option
|
|
|
|
|
|
|
|
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
|
|
|
|
|
|
|
|
val iter : ('a -> unit) -> 'a t -> unit
|
|
|
|
val map : ('a -> 'b) -> 'a t -> 'b t
|
|
|
|
val fold : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
|
|
|
|
val value_default : ('a -> 'b) -> default:'b -> 'a t -> 'b
|
|
|
|
end
|
|
|
|
|
|
|
|
module String : sig
|
|
|
|
type t = string
|
|
|
|
|
|
|
|
val split : t -> on:char -> t list
|
2016-01-12 04:44:20 -08:00
|
|
|
(** Splits the given string at every occurrence of the given separator.
|
|
|
|
Does not return empty substrings when the separator is repeated or
|
|
|
|
present at the start or end of the string. *)
|
2015-12-30 06:43:44 -08:00
|
|
|
end
|
|
|
|
end
|
1999-11-30 08:07:38 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
val find_in_path: string list -> string -> string
|
|
|
|
(* Search a file in a list of directories. *)
|
2014-10-15 06:34:58 -07:00
|
|
|
val find_in_path_rel: string list -> string -> string
|
|
|
|
(* Search a relative file in a list of directories. *)
|
2002-06-07 00:35:38 -07:00
|
|
|
val find_in_path_uncap: string list -> string -> string
|
|
|
|
(* Same, but search also for uncapitalized name, i.e.
|
|
|
|
if name is Foo.ml, allow /path/Foo.ml and /path/foo.ml
|
|
|
|
to match. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
val remove_file: string -> unit
|
1996-01-04 04:51:11 -08:00
|
|
|
(* Delete the given file if it exists. Never raise an error. *)
|
2000-12-27 21:02:43 -08:00
|
|
|
val expand_directory: string -> string -> string
|
|
|
|
(* [expand_directory alt file] eventually expands a [+] at the
|
|
|
|
beginning of file into [alt] (an alternate root directory) *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
val create_hashtable: int -> ('a * 'b) list -> ('a, 'b) Hashtbl.t
|
|
|
|
(* Create a hashtable of the given size and fills it with the
|
|
|
|
given bindings. *)
|
|
|
|
|
|
|
|
val copy_file: in_channel -> out_channel -> unit
|
|
|
|
(* [copy_file ic oc] reads the contents of file [ic] and copies
|
|
|
|
them to [oc]. It stops when encountering EOF on [ic]. *)
|
|
|
|
val copy_file_chunk: in_channel -> out_channel -> int -> unit
|
|
|
|
(* [copy_file_chunk ic oc n] reads [n] bytes from [ic] and copies
|
|
|
|
them to [oc]. It raises [End_of_file] when encountering
|
|
|
|
EOF on [ic]. *)
|
2012-05-30 07:52:37 -07:00
|
|
|
val string_of_file: in_channel -> string
|
|
|
|
(* [string_of_file ic] reads the contents of file [ic] and copies
|
|
|
|
them to a string. It stops when encountering EOF on [ic]. *)
|
1995-06-05 06:44:14 -07:00
|
|
|
val log2: int -> int
|
2010-01-22 04:48:24 -08:00
|
|
|
(* [log2 n] returns [s] such that [n = 1 lsl s]
|
1995-06-05 06:44:14 -07:00
|
|
|
if [n] is a power of 2*)
|
|
|
|
val align: int -> int -> int
|
|
|
|
(* [align n a] rounds [n] upwards to a multiple of [a]
|
|
|
|
(a power of 2). *)
|
1995-10-26 09:25:24 -07:00
|
|
|
val no_overflow_add: int -> int -> bool
|
|
|
|
(* [no_overflow_add n1 n2] returns [true] if the computation of
|
|
|
|
[n1 + n2] does not overflow. *)
|
|
|
|
val no_overflow_sub: int -> int -> bool
|
|
|
|
(* [no_overflow_add n1 n2] returns [true] if the computation of
|
|
|
|
[n1 - n2] does not overflow. *)
|
2015-02-07 03:32:12 -08:00
|
|
|
val no_overflow_mul: int -> int -> bool
|
|
|
|
(* [no_overflow_mul n1 n2] returns [true] if the computation of
|
|
|
|
[n1 * n2] does not overflow. *)
|
|
|
|
val no_overflow_lsl: int -> int -> bool
|
|
|
|
(* [no_overflow_lsl n k] returns [true] if the computation of
|
|
|
|
[n lsl k] does not overflow. *)
|
2002-01-28 09:25:26 -08:00
|
|
|
|
2015-04-18 20:07:32 -07:00
|
|
|
module Int_literal_converter : sig
|
|
|
|
val int : string -> int
|
|
|
|
val int32 : string -> int32
|
|
|
|
val int64 : string -> int64
|
|
|
|
val nativeint : string -> nativeint
|
|
|
|
end
|
|
|
|
|
2002-01-28 09:25:26 -08:00
|
|
|
val chop_extension_if_any: string -> string
|
|
|
|
(* Like Filename.chop_extension but returns the initial file
|
|
|
|
name if it has no extension *)
|
2002-02-08 08:55:44 -08:00
|
|
|
|
2007-02-23 05:44:51 -08:00
|
|
|
val chop_extensions: string -> string
|
|
|
|
(* Return the given file name without its extensions. The extensions
|
|
|
|
is the longest suffix starting with a period and not including
|
|
|
|
a directory separator, [.xyz.uvw] for instance.
|
|
|
|
|
|
|
|
Return the given name if it does not contain an extension. *)
|
|
|
|
|
2002-02-08 08:55:44 -08:00
|
|
|
val search_substring: string -> string -> int -> int
|
|
|
|
(* [search_substring pat str start] returns the position of the first
|
|
|
|
occurrence of string [pat] in string [str]. Search starts
|
|
|
|
at offset [start] in [str]. Raise [Not_found] if [pat]
|
|
|
|
does not occur. *)
|
2002-05-07 06:16:28 -07:00
|
|
|
|
2015-02-08 03:18:29 -08:00
|
|
|
val replace_substring: before:string -> after:string -> string -> string
|
2015-09-11 04:58:31 -07:00
|
|
|
(* [search_substring ~before ~after str] replaces all
|
|
|
|
occurences of [before] with [after] in [str] and returns
|
|
|
|
the resulting string. *)
|
2015-02-08 03:18:29 -08:00
|
|
|
|
2002-05-07 06:16:28 -07:00
|
|
|
val rev_split_words: string -> string list
|
2015-12-30 06:43:44 -08:00
|
|
|
(* [rev_split_words s] splits [s] in blank-separated words, and returns
|
2002-05-07 06:16:28 -07:00
|
|
|
the list of words in reverse order. *)
|
2011-12-28 06:20:53 -08:00
|
|
|
|
|
|
|
val get_ref: 'a list ref -> 'a list
|
|
|
|
(* [get_ref lr] returns the content of the list reference [lr] and reset
|
|
|
|
its content to the empty list. *)
|
2012-05-30 07:52:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
val fst3: 'a * 'b * 'c -> 'a
|
|
|
|
val snd3: 'a * 'b * 'c -> 'b
|
|
|
|
val thd3: 'a * 'b * 'c -> 'c
|
|
|
|
|
|
|
|
val fst4: 'a * 'b * 'c * 'd -> 'a
|
|
|
|
val snd4: 'a * 'b * 'c * 'd -> 'b
|
|
|
|
val thd4: 'a * 'b * 'c * 'd -> 'c
|
2013-04-29 22:26:57 -07:00
|
|
|
val for4: 'a * 'b * 'c * 'd -> 'd
|
2012-10-16 06:54:24 -07:00
|
|
|
|
2013-04-17 02:07:00 -07:00
|
|
|
module LongString :
|
|
|
|
sig
|
2014-04-29 04:56:17 -07:00
|
|
|
type t = bytes array
|
2013-04-17 02:07:00 -07:00
|
|
|
val create : int -> t
|
|
|
|
val length : t -> int
|
|
|
|
val get : t -> int -> char
|
|
|
|
val set : t -> int -> char -> unit
|
|
|
|
val blit : t -> int -> t -> int -> int -> unit
|
|
|
|
val output : out_channel -> t -> int -> int -> unit
|
2014-04-29 04:56:17 -07:00
|
|
|
val unsafe_blit_to_bytes : t -> int -> bytes -> int -> int -> unit
|
2013-04-17 02:07:00 -07:00
|
|
|
val input_bytes : in_channel -> int -> t
|
|
|
|
end
|
2012-10-16 06:54:24 -07:00
|
|
|
|
|
|
|
val edit_distance : string -> string -> int -> int option
|
|
|
|
(** [edit_distance a b cutoff] computes the edit distance between
|
|
|
|
strings [a] and [b]. To help efficiency, it uses a cutoff: if the
|
|
|
|
distance [d] is smaller than [cutoff], it returns [Some d], else
|
|
|
|
[None].
|
|
|
|
|
|
|
|
The distance algorithm currently used is Damerau-Levenshtein: it
|
|
|
|
computes the number of insertion, deletion, substitution of
|
|
|
|
letters, or swapping of adjacent letters to go from one word to the
|
|
|
|
other. The particular algorithm may change in the future.
|
|
|
|
*)
|
2013-06-05 10:54:20 -07:00
|
|
|
|
2014-12-13 06:46:16 -08:00
|
|
|
val spellcheck : string list -> string -> string list
|
|
|
|
(** [spellcheck env name] takes a list of names [env] that exist in
|
|
|
|
the current environment and an erroneous [name], and returns a
|
|
|
|
list of suggestions taken from [env], that are close enough to
|
|
|
|
[name] that it may be a typo for one of them. *)
|
|
|
|
|
|
|
|
val did_you_mean : Format.formatter -> (unit -> string list) -> unit
|
|
|
|
(** [did_you_mean ppf get_choices] hints that the user may have meant
|
|
|
|
one of the option returned by calling [get_choices]. It does nothing
|
|
|
|
if the returned list is empty.
|
|
|
|
|
|
|
|
The [unit -> ...] thunking is meant to delay any potentially-slow
|
|
|
|
computation (typically computing edit-distance with many things
|
|
|
|
from the current environment) to when the hint message is to be
|
|
|
|
printed. You should print an understandable error message before
|
|
|
|
calling [did_you_mean], so that users get a clear notification of
|
|
|
|
the failure even if producing the hint is slow.
|
|
|
|
*)
|
|
|
|
|
2013-06-05 10:54:20 -07:00
|
|
|
val split : string -> char -> string list
|
|
|
|
(** [String.split string char] splits the string [string] at every char
|
|
|
|
[char], and returns the list of sub-strings between the chars.
|
|
|
|
[String.concat (String.make 1 c) (String.split s c)] is the identity.
|
|
|
|
@since 4.01
|
|
|
|
*)
|
|
|
|
|
|
|
|
val cut_at : string -> char -> string * string
|
|
|
|
(** [String.cut_at s c] returns a pair containing the sub-string before
|
|
|
|
the first occurrence of [c] in [s], and the sub-string after the
|
|
|
|
first occurrence of [c] in [s].
|
|
|
|
[let (before, after) = String.cut_at s c in
|
|
|
|
before ^ String.make 1 c ^ after] is the identity if [s] contains [c].
|
|
|
|
|
|
|
|
Raise [Not_found] if the character does not appear in the string
|
|
|
|
@since 4.01
|
|
|
|
*)
|
2014-08-29 07:35:10 -07:00
|
|
|
|
|
|
|
|
|
|
|
module StringSet: Set.S with type elt = string
|
|
|
|
module StringMap: Map.S with type key = string
|
|
|
|
(* TODO: replace all custom instantiations of StringSet/StringMap in various
|
|
|
|
compiler modules with this one. *)
|
2015-08-15 08:57:49 -07:00
|
|
|
|
|
|
|
(* Color handling *)
|
|
|
|
module Color : sig
|
|
|
|
type color =
|
|
|
|
| Black
|
|
|
|
| Red
|
|
|
|
| Green
|
|
|
|
| Yellow
|
|
|
|
| Blue
|
|
|
|
| Magenta
|
|
|
|
| Cyan
|
|
|
|
| White
|
|
|
|
;;
|
|
|
|
|
|
|
|
type style =
|
|
|
|
| FG of color (* foreground *)
|
|
|
|
| BG of color (* background *)
|
|
|
|
| Bold
|
|
|
|
| Reset
|
|
|
|
|
|
|
|
val ansi_of_style_l : style list -> string
|
|
|
|
(* ANSI escape sequence for the given style *)
|
|
|
|
|
|
|
|
type styles = {
|
|
|
|
error: style list;
|
|
|
|
warning: style list;
|
|
|
|
loc: style list;
|
|
|
|
}
|
|
|
|
|
|
|
|
val default_styles: styles
|
|
|
|
val get_styles: unit -> styles
|
|
|
|
val set_styles: styles -> unit
|
|
|
|
|
2016-01-12 05:35:17 -08:00
|
|
|
type setting = Auto | Always | Never
|
|
|
|
|
|
|
|
val setup : setting -> unit
|
2015-08-15 08:57:51 -07:00
|
|
|
(* [setup opt] will enable or disable color handling on standard formatters
|
|
|
|
according to the value of color setting [opt].
|
2015-08-15 08:57:49 -07:00
|
|
|
Only the first call to this function has an effect. *)
|
|
|
|
|
|
|
|
val set_color_tag_handling : Format.formatter -> unit
|
|
|
|
(* adds functions to support color tags to the given formatter. *)
|
|
|
|
end
|
2015-12-23 02:57:10 -08:00
|
|
|
|
|
|
|
val normalise_eol : string -> string
|
2016-01-18 08:40:03 -08:00
|
|
|
(** [normalise_eol s] returns a fresh copy of [s] with any '\r' characters
|
|
|
|
removed. Intended for pre-processing text which will subsequently be printed
|
|
|
|
on a channel which performs EOL transformations (i.e. Windows) *)
|