Re-indent string_with_vars with ocp-indent

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-06-01 12:54:29 +07:00
parent d708cc714d
commit 576ff5293e
2 changed files with 41 additions and 41 deletions

View File

@ -62,7 +62,7 @@ let items_of_string s = of_tokens (Token.tokenise s)
let t : Sexp.Of_sexp.ast -> t = function
| Atom(loc, A s) -> { items = items_of_string s; loc; quoted = false }
| Quoted_string (loc, s) ->
{ items = items_of_string s; loc; quoted = true }
{ items = items_of_string s; loc; quoted = true }
| List _ as sexp -> Sexp.Of_sexp.of_sexp_error sexp "Atom expected"
let loc t = t.loc
@ -89,13 +89,13 @@ let sexp_of_ast t = Sexp.To_sexp.list sexp_of_item t.items
let fold t ~init ~f =
List.fold_left t.items ~init ~f:(fun acc item ->
match item with
| Text _ -> acc
| Var (_, v) -> f acc t.loc v)
match item with
| Text _ -> acc
| Var (_, v) -> f acc t.loc v)
let iter t ~f = List.iter t.items ~f:(function
| Text _ -> ()
| Var (_, v) -> f t.loc v)
| Text _ -> ()
| Var (_, v) -> f t.loc v)
let vars t = fold t ~init:String.Set.empty ~f:(fun acc _ x -> String.Set.add acc x)
@ -153,20 +153,20 @@ module Expand_to(V: EXPANSION) = struct
end
| Text s :: items -> loop (s :: acc_text) acc items
| Var (syntax, v) as it :: items ->
match f t.loc v with
| None -> loop [] (it :: commit_text acc_text acc) items
| Some x ->
if not t.quoted && V.is_multivalued x then
Loc.fail t.loc "please quote the string containing the \
list variable %s" (string_of_var syntax v)
else loop (V.to_string ctx x :: acc_text) acc items
match f t.loc v with
| None -> loop [] (it :: commit_text acc_text acc) items
| Some x ->
if not t.quoted && V.is_multivalued x then
Loc.fail t.loc "please quote the string containing the \
list variable %s" (string_of_var syntax v)
else loop (V.to_string ctx x :: acc_text) acc items
in
match t.items with
| [Var (_, v)] when not t.quoted ->
(* Unquoted single var *)
(match f t.loc v with
| Some e -> Left (Left e)
| None -> Right t)
(* Unquoted single var *)
(match f t.loc v with
| Some e -> Left (Left e)
| None -> Right t)
| _ -> loop [] [] t.items
end

View File

@ -1,7 +1,7 @@
(** 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. *)
Variables cannot contain "${", "$(", ")" or "}". For instance in "$(cat
${x})", only "${x}" will be considered a variable, the rest is text. *)
open Import
@ -10,8 +10,8 @@ type t
val t : t Sexp.Of_sexp.t
(** [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 "${@}". *)
function distinguishes between unquoted variables such as ${@}
and quoted variables such as "${@}". *)
val loc : t -> Loc.t
(** [loc t] returns the location of [t] — typically, in the jbuild file. *)
@ -25,9 +25,9 @@ val sexp_of_ast : t -> Sexp.t
val to_string : t -> string
(** [t] generated by the OCaml code. The first argument should be
[__POS__]. The second is either a string to parse, a variable name
or plain text. [quoted] says whether the string is quoted ([false]
by default). *)
[__POS__]. The second is either a string to parse, a variable name
or plain text. [quoted] says whether the string is quoted ([false]
by default). *)
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
@ -37,11 +37,11 @@ val vars : t -> String.Set.t
val fold : t -> init:'a -> f:('a -> Loc.t -> string -> 'a) -> 'a
(** [fold t ~init ~f] fold [f] on all variables of [t], the text
portions being ignored. *)
portions being ignored. *)
val iter : t -> f:(Loc.t -> string -> unit) -> unit
(** [iter t ~f] iterates [f] over all variables of [t], the text
portions being ignored. *)
portions being ignored. *)
val is_var : t -> name:string -> bool
@ -51,42 +51,42 @@ module type EXPANSION = sig
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. *)
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]. *)
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) ->
(V.t, string) Either.t
(V.t, string) Either.t
(** [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. *)
using [f]. If [f loc var] return [Some x], the variable [var] is
replaced by [x]; otherwise, the variable is inserted using the syntax
it was originally defined with: ${..} or $(..) *)
val partial_expand :
V.context -> t -> f:(Loc.t -> string -> V.t option) ->
((V.t, string) either, t) Either.t
(** [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. *)
(** [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
val expand :
t -> f:(Loc.t -> string -> string option) -> string
(** Specialized version [Expand_to.expand] that returns a string (so
variables are assumed to expand to a single value). *)
variables are assumed to expand to a single value). *)
val partial_expand :
t -> f:(Loc.t -> string -> string option) -> (string, t) Either.t
(** [partial_expand] is a specialized version of
[Expand_to.partial_expand] that returns a string. *)
[Expand_to.partial_expand] that returns a string. *)