Move the multivalue error to a function

Since it's used more than once

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-06-04 21:37:02 +07:00
parent bb58cf8599
commit 8351fcb466
1 changed files with 18 additions and 16 deletions

View File

@ -132,6 +132,12 @@ end
module Expand_to(V: EXPANSION) = struct module Expand_to(V: EXPANSION) = struct
let check_valid_multivalue syntax ~var t ctx =
if not t.quoted && V.is_multivalued ctx then
Loc.fail t.loc "please quote the string \
containing the list variable %s"
(string_of_var syntax var)
let expand ctx t ~f = let expand ctx t ~f =
match t.items with match t.items with
| [Var (syntax, v)] when not t.quoted -> | [Var (syntax, v)] when not t.quoted ->
@ -140,18 +146,16 @@ module Expand_to(V: EXPANSION) = struct
| Some e -> Expand.Full.Expansion e | Some e -> Expand.Full.Expansion e
| None -> Expand.Full.String (string_of_var syntax v)) | None -> Expand.Full.String (string_of_var syntax v))
| _ -> | _ ->
Expand.Full.String (List.map t.items ~f:(function Expand.Full.String (
| Text s -> s List.map t.items ~f:(function
| Var (syntax, v) -> | Text s -> s
match f t.loc v with | Var (syntax, v) ->
| Some x -> match f t.loc v with
if not t.quoted && V.is_multivalued x then | Some x ->
Loc.fail t.loc "please quote the string \ check_valid_multivalue syntax ~var:v t x;
containing the list variable %s" V.to_string ctx x
(string_of_var syntax v) | None -> string_of_var syntax v)
else V.to_string ctx x |> String.concat ~sep:"")
| None -> string_of_var syntax v)
|> 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 =
@ -170,10 +174,8 @@ module Expand_to(V: EXPANSION) = struct
match f t.loc v with match f t.loc v with
| None -> loop [] (it :: commit_text acc_text acc) items | None -> loop [] (it :: commit_text acc_text acc) items
| Some x -> | Some x ->
if not t.quoted && V.is_multivalued x then check_valid_multivalue syntax ~var:v t x;
Loc.fail t.loc "please quote the string containing the \ loop (V.to_string ctx x :: acc_text) acc items
list variable %s" (string_of_var syntax v)
else loop (V.to_string ctx x :: acc_text) acc items
in in
match t.items with match t.items with
| [Var (_, v)] when not t.quoted -> | [Var (_, v)] when not t.quoted ->