2016-12-23 15:32:23 +00:00
|
|
|
(** String with variables of the form ${...} or $(...)
|
|
|
|
|
|
|
|
Variables cannot contain "${", "$(", ")" or "}". For instance in "$(cat ${x})", only
|
|
|
|
"${x}" will be considered a variable, the rest is text. *)
|
2016-12-02 13:54:32 +00:00
|
|
|
|
|
|
|
open Import
|
|
|
|
|
|
|
|
type t
|
2018-01-27 23:14:04 +00:00
|
|
|
(** A sequence of text and variables. *)
|
|
|
|
|
2017-02-25 17:53:39 +00:00
|
|
|
val t : t Sexp.Of_sexp.t
|
2018-01-29 09:14:09 +00:00
|
|
|
(** [t ast] takes an [ast] sexp and returns a string-with-vars. This
|
|
|
|
function distinguishes between unquoted variables — such as ${@} —
|
|
|
|
and quoted variables — such as "${@}". *)
|
2016-12-02 13:54:32 +00:00
|
|
|
|
2017-05-05 11:26:56 +00:00
|
|
|
val loc : t -> Loc.t
|
2018-01-27 23:14:04 +00:00
|
|
|
(** [loc t] returns the location of [t] — typically, in the jbuild file. *)
|
|
|
|
|
|
|
|
val sexp_of_t : t -> Sexp.t
|
2017-05-05 11:26:56 +00:00
|
|
|
|
2018-03-07 15:01:46 +00:00
|
|
|
(** Same as [sexp_of_t] but the S-expression encodes the internal
|
|
|
|
structure of [t]. *)
|
|
|
|
val sexp_of_ast : t -> Sexp.t
|
|
|
|
|
2017-05-30 14:40:06 +00:00
|
|
|
val to_string : t -> string
|
2017-06-08 09:54:46 +00:00
|
|
|
|
2018-01-27 23:14:04 +00:00
|
|
|
(** [t] generated by the OCaml code. The first argument should be
|
|
|
|
[__POS__]. The second is either a string to parse, a variable name
|
2018-02-01 22:45:30 +00:00
|
|
|
or plain text. [quoted] says whether the string is quoted ([false]
|
|
|
|
by default). *)
|
2018-01-30 17:08:28 +00:00
|
|
|
val virt : ?quoted: bool -> (string * int * int * int) -> string -> t
|
|
|
|
val virt_var : ?quoted: bool -> (string * int * int * int) -> string -> t
|
|
|
|
val virt_text : (string * int * int * int) -> string -> t
|
2016-12-02 13:54:32 +00:00
|
|
|
|
|
|
|
val vars : t -> String_set.t
|
2018-01-27 23:14:04 +00:00
|
|
|
(** [vars t] returns the set of all variables in [t]. *)
|
2016-12-02 13:54:32 +00:00
|
|
|
|
2017-05-05 11:26:56 +00:00
|
|
|
val fold : t -> init:'a -> f:('a -> Loc.t -> string -> 'a) -> 'a
|
2018-01-27 23:14:04 +00:00
|
|
|
(** [fold t ~init ~f] fold [f] on all variables of [t], the text
|
|
|
|
portions being ignored. *)
|
|
|
|
|
2017-05-31 07:31:52 +00:00
|
|
|
val iter : t -> f:(Loc.t -> string -> unit) -> unit
|
2018-01-27 23:14:04 +00:00
|
|
|
(** [iter t ~f] iterates [f] over all variables of [t], the text
|
|
|
|
portions being ignored. *)
|
2016-12-02 13:54:32 +00:00
|
|
|
|
2018-04-17 03:58:53 +00:00
|
|
|
val is_var : t -> name:string -> bool
|
|
|
|
|
2018-02-02 18:19:17 +00:00
|
|
|
module type EXPANSION = sig
|
|
|
|
type t
|
|
|
|
(** The value to which variables are expanded. *)
|
|
|
|
|
|
|
|
val is_multivalued : t -> bool
|
|
|
|
(** Report whether the value is a multivalued one (such as for
|
|
|
|
example ${@}) which much be in quoted strings to be concatenated
|
|
|
|
to text or other variables. *)
|
|
|
|
|
|
|
|
type context
|
|
|
|
(** Context needed to expand values of type [t] to strings. *)
|
|
|
|
|
|
|
|
val to_string : context -> t -> string
|
|
|
|
(** When needing to expand with text portions or if the
|
|
|
|
string-with-vars is quoted, the value is converted to a string
|
|
|
|
using [to_string]. *)
|
|
|
|
end
|
|
|
|
|
|
|
|
module Expand_to(V : EXPANSION) : sig
|
|
|
|
val expand : V.context -> t -> f:(Loc.t -> string -> V.t option) ->
|
2018-02-25 16:35:25 +00:00
|
|
|
(V.t, string) Either.t
|
2018-02-02 18:19:17 +00:00
|
|
|
(** [expand t ~f] return [t] where all variables have been expanded
|
|
|
|
using [f]. If [f loc var] return [Some x], the variable [var] is
|
|
|
|
replaced by [x]; otherwise, the variable is inserted as [${var}]
|
|
|
|
or [$(var)] — depending on the original concrete syntax used. *)
|
|
|
|
|
|
|
|
val partial_expand :
|
|
|
|
V.context -> t -> f:(Loc.t -> string -> V.t option) ->
|
2018-02-25 16:35:25 +00:00
|
|
|
((V.t, string) either, t) Either.t
|
2018-02-02 18:19:17 +00:00
|
|
|
(** [partial_expand t ~f] is like [expand_generic] where all
|
|
|
|
variables that could be expanded (i.e., those for which [f]
|
|
|
|
returns [Some _]) are. If all the variables of [t] were
|
|
|
|
expanded, a string is returned. If [f] returns [None] on at
|
|
|
|
least a variable of [t], it returns a string-with-vars. *)
|
|
|
|
end
|
2018-02-01 22:45:30 +00:00
|
|
|
|
|
|
|
val expand :
|
|
|
|
t -> f:(Loc.t -> string -> string option) -> string
|
2018-02-02 18:19:17 +00:00
|
|
|
(** Specialized version [Expand_to.expand] that returns a string (so
|
2018-02-01 22:45:30 +00:00
|
|
|
variables are assumed to expand to a single value). *)
|
|
|
|
|
|
|
|
val partial_expand :
|
2018-02-25 16:35:25 +00:00
|
|
|
t -> f:(Loc.t -> string -> string option) -> (string, t) Either.t
|
2018-02-01 22:45:30 +00:00
|
|
|
(** [partial_expand] is a specialized version of
|
2018-02-02 18:19:17 +00:00
|
|
|
[Expand_to.partial_expand] that returns a string. *)
|