implement expand in terms of partial_expand
Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
parent
124d942310
commit
243f3437f2
|
@ -143,36 +143,13 @@ let invalid_multivalue syntax ~var t x =
|
||||||
(string_of_var syntax var) (List.length x)
|
(string_of_var syntax var) (List.length x)
|
||||||
|
|
||||||
let expand_var syntax ~var ~dir ~f t =
|
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 ->
|
| Some ([] | _::_::_ as e) , false ->
|
||||||
invalid_multivalue syntax ~var t e
|
invalid_multivalue syntax ~var t e
|
||||||
| Some ([_] as t), false
|
| Some ([_] as t), false
|
||||||
| Some t, true -> Some (Value.to_strings ~dir t)
|
| Some t, true -> Some (Value.to_strings ~dir t)
|
||||||
| None, _ -> None
|
| 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 partial_expand t ~mode ~dir ~f =
|
||||||
let commit_text acc_text acc =
|
let commit_text acc_text acc =
|
||||||
let s = concat_rev acc_text in
|
let s = concat_rev acc_text in
|
||||||
|
@ -195,7 +172,7 @@ let partial_expand t ~mode ~dir ~f =
|
||||||
match t.items with
|
match t.items with
|
||||||
| [Var (syntax, v)] when not t.quoted ->
|
| [Var (syntax, v)] when not t.quoted ->
|
||||||
(* Unquoted single var *)
|
(* Unquoted single var *)
|
||||||
begin match f t.loc v with
|
begin match f syntax t.loc v with
|
||||||
| Some e -> Partial.Expanded (
|
| Some e -> Partial.Expanded (
|
||||||
match Mode.value mode e with
|
match Mode.value mode e with
|
||||||
| None -> invalid_multivalue syntax ~var:v t e
|
| None -> invalid_multivalue syntax ~var:v t e
|
||||||
|
@ -204,6 +181,19 @@ let partial_expand t ~mode ~dir ~f =
|
||||||
end
|
end
|
||||||
| _ -> loop [] [] t.items
|
| _ -> 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 =
|
let to_string t =
|
||||||
match t.items with
|
match t.items with
|
||||||
(* [to_string is only called from action.ml, always on [t]s of this form *)
|
(* [to_string is only called from action.ml, always on [t]s of this form *)
|
||||||
|
|
Loading…
Reference in New Issue