implement expand in terms of partial_expand

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-06-06 19:30:03 +07:00
parent 124d942310
commit 243f3437f2
1 changed files with 15 additions and 25 deletions

View File

@ -143,36 +143,13 @@ let invalid_multivalue syntax ~var t x =
(string_of_var syntax var) (List.length x)
let expand_var syntax ~var ~dir ~f t =
match f t.loc var, t.quoted with
match f syntax t.loc var, t.quoted with
| Some ([] | _::_::_ as e) , false ->
invalid_multivalue syntax ~var t e
| Some ([_] as t), false
| Some t, true -> Some (Value.to_strings ~dir t)
| None, _ -> None
let expand t ~mode ~dir ~f =
match t.items with
| [Var (syntax, v)] when not t.quoted ->
(* Unquoted single var *)
begin match f t.loc v with
| Some e ->
begin match Mode.value mode e with
| None -> invalid_multivalue syntax ~var:v t e
| Some s -> s
end
| None -> Mode.string mode (string_of_var syntax v)
end
| _ ->
Mode.string mode (
List.concat_map t.items ~f:(function
| Text s -> [s]
| Var (syntax, v) ->
begin match expand_var syntax ~var:v ~dir ~f t with
| Some values -> values
| None -> [string_of_var syntax v]
end)
|> String.concat ~sep:"")
let partial_expand t ~mode ~dir ~f =
let commit_text acc_text acc =
let s = concat_rev acc_text in
@ -195,7 +172,7 @@ let partial_expand t ~mode ~dir ~f =
match t.items with
| [Var (syntax, v)] when not t.quoted ->
(* Unquoted single var *)
begin match f t.loc v with
begin match f syntax t.loc v with
| Some e -> Partial.Expanded (
match Mode.value mode e with
| None -> invalid_multivalue syntax ~var:v t e
@ -204,6 +181,19 @@ let partial_expand t ~mode ~dir ~f =
end
| _ -> loop [] [] t.items
let expand t ~mode ~dir ~f =
match
partial_expand t ~mode ~dir ~f:(fun syntax loc var ->
match f loc var with
| None -> Some [Value.String (string_of_var syntax var)]
| s -> s)
with
| Partial.Expanded s -> s
| Unexpanded _ -> assert false (* we are expanding every variable *)
let partial_expand t ~mode ~dir ~f =
partial_expand t ~mode ~dir ~f:(fun _ loc v -> f loc v)
let to_string t =
match t.items with
(* [to_string is only called from action.ml, always on [t]s of this form *)