Change return values of SW expansions

The nested either types obscure the meaning of the return values

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-06-01 13:35:28 +07:00
parent 576ff5293e
commit 52d0f3f552
3 changed files with 57 additions and 21 deletions

View File

@ -374,8 +374,8 @@ module Unexpanded = struct
| Left x -> map x | Left x -> map x
| Right template -> | Right template ->
match To_VE.expand dir template ~f with match To_VE.expand dir template ~f with
| Left e -> special dir e | Expansion e -> special dir e
| Right s -> generic dir s | String s -> generic dir s
[@@inlined always] [@@inlined always]
let string ~dir ~f x = let string ~dir ~f x =
@ -465,9 +465,9 @@ module Unexpanded = struct
module E = struct module E = struct
let expand ~generic ~special ~dir ~f template = let expand ~generic ~special ~dir ~f template =
match To_VE.partial_expand dir template ~f with match To_VE.partial_expand dir template ~f with
| Left (Left e) -> Left (special dir e) | Expansion e -> Left (special dir e)
| Left (Right s) -> Left (generic dir s) | String s -> Left (generic dir s)
| Right _ as x -> x | Unexpanded x -> Right x
let string ~dir ~f x = let string ~dir ~f x =
expand ~dir ~f x expand ~dir ~f x

View File

@ -116,6 +116,20 @@ let concat_rev = function
| [s] -> s | [s] -> s
| l -> String.concat (List.rev l) ~sep:"" | l -> String.concat (List.rev l) ~sep:""
module Expand = struct
module Full = struct
type nonrec 'a t =
| Expansion of 'a
| String of string
end
module Partial = struct
type nonrec 'a t =
| Expansion of 'a
| String of string
| Unexpanded of t
end
end
module Expand_to(V: EXPANSION) = struct module Expand_to(V: EXPANSION) = struct
let expand ctx t ~f = let expand ctx t ~f =
@ -123,10 +137,10 @@ module Expand_to(V: EXPANSION) = struct
| [Var (syntax, v)] when not t.quoted -> | [Var (syntax, v)] when not t.quoted ->
(* Unquoted single var *) (* Unquoted single var *)
(match f t.loc v with (match f t.loc v with
| Some e -> Left e | Some e -> Expand.Full.Expansion e
| None -> Right (string_of_var syntax v)) | None -> Expand.Full.String (string_of_var syntax v))
| _ -> | _ ->
Right (List.map t.items ~f:(function Expand.Full.String (List.map t.items ~f:(function
| Text s -> s | Text s -> s
| Var (syntax, v) -> | Var (syntax, v) ->
match f t.loc v with match f t.loc v with
@ -137,7 +151,7 @@ module Expand_to(V: EXPANSION) = struct
(string_of_var syntax v) (string_of_var syntax v)
else V.to_string ctx x else V.to_string ctx x
| None -> string_of_var syntax v) | None -> string_of_var syntax v)
|> String.concat ~sep:"") |> String.concat ~sep:"")
let partial_expand ctx t ~f = let partial_expand ctx t ~f =
let commit_text acc_text acc = let commit_text acc_text acc =
@ -148,8 +162,8 @@ module Expand_to(V: EXPANSION) = struct
match items with match items with
| [] -> begin | [] -> begin
match acc with match acc with
| [] -> Left (Right (concat_rev acc_text)) | [] -> Expand.Partial.String (concat_rev acc_text)
| _ -> Right { t with items = List.rev (commit_text acc_text acc) } | _ -> Unexpanded { t with items = List.rev (commit_text acc_text acc) }
end end
| Text s :: items -> loop (s :: acc_text) acc items | Text s :: items -> loop (s :: acc_text) acc items
| Var (syntax, v) as it :: items -> | Var (syntax, v) as it :: items ->
@ -165,8 +179,8 @@ module Expand_to(V: EXPANSION) = struct
| [Var (_, v)] when not t.quoted -> | [Var (_, v)] when not t.quoted ->
(* Unquoted single var *) (* Unquoted single var *)
(match f t.loc v with (match f t.loc v with
| Some e -> Left (Left e) | Some e -> Expand.Partial.Expansion e
| None -> Right t) | None -> Expand.Partial.Unexpanded t)
| _ -> loop [] [] t.items | _ -> loop [] [] t.items
end end
@ -180,12 +194,15 @@ end
module S = Expand_to(String_expansion) module S = Expand_to(String_expansion)
let expand t ~f = let expand t ~f =
match S.expand () t ~f with Left s | Right s -> s match S.expand () t ~f with
| Expand.Full.String s
| Expansion s -> s
let partial_expand t ~f = let partial_expand t ~f =
match S.partial_expand () t ~f with match S.partial_expand () t ~f with
| Left (Left s | Right s) -> Left s | Expand.Partial.Expansion s -> Left s
| Right _ as x -> x | String s -> Left s
| Unexpanded s -> Right s
let to_string t = let to_string t =
match t.items with match t.items with

View File

@ -63,17 +63,36 @@ module type EXPANSION = sig
using [to_string]. *) using [to_string]. *)
end end
module Expand : sig
module Full : sig
type nonrec 'a t =
| Expansion of 'a
| String of string
end
module Partial : sig
type nonrec 'a t =
| Expansion of 'a
| String of string
| Unexpanded of t
end
end
module Expand_to(V : EXPANSION) : sig module Expand_to(V : EXPANSION) : sig
val expand : V.context -> t -> f:(Loc.t -> string -> V.t option) -> val expand
(V.t, string) Either.t : V.context
-> t
-> f:(Loc.t -> string -> V.t option)
-> V.t Expand.Full.t
(** [expand t ~f] return [t] where all variables have been expanded (** [expand t ~f] return [t] where all variables have been expanded
using [f]. If [f loc var] return [Some x], the variable [var] is using [f]. If [f loc var] return [Some x], the variable [var] is
replaced by [x]; otherwise, the variable is inserted using the syntax replaced by [x]; otherwise, the variable is inserted using the syntax
it was originally defined with: ${..} or $(..) *) it was originally defined with: ${..} or $(..) *)
val partial_expand : val partial_expand
V.context -> t -> f:(Loc.t -> string -> V.t option) -> : V.context
((V.t, string) either, t) Either.t -> t
-> f:(Loc.t -> string -> V.t option)
-> V.t Expand.Partial.t
(** [partial_expand t ~f] is like [expand_generic] where all (** [partial_expand t ~f] is like [expand_generic] where all
variables that could be expanded (i.e., those for which [f] variables that could be expanded (i.e., those for which [f]
returns [Some _]) are. If all the variables of [t] were returns [Some _]) are. If all the variables of [t] were