Add length function to exapnsions

This is useful for an error message that includes the number of items we've
expanded to.

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-06-04 22:06:58 +07:00
parent 731b61b0b9
commit 9545d9a854
3 changed files with 12 additions and 4 deletions

View File

@ -275,6 +275,10 @@ module Var_expansion = struct
| Paths of Path.t list | Paths of Path.t list
| Strings of string list | Strings of string list
let length = function
| Paths x -> List.length x
| Strings x -> List.length x
let is_multivalued = function let is_multivalued = function
| Paths [_] -> false | Paths [_] -> false
| Strings [_] -> false | Strings [_] -> false

View File

@ -106,6 +106,7 @@ let string_of_var syntax v =
module type EXPANSION = sig module type EXPANSION = sig
type t type t
val length : t -> int
val is_multivalued : t -> bool val is_multivalued : t -> bool
type context type context
val to_string : context -> t -> string val to_string : context -> t -> string
@ -132,12 +133,12 @@ end
module Expand_to(V: EXPANSION) = struct module Expand_to(V: EXPANSION) = struct
let check_valid_multivalue syntax ~var t ctx = let check_valid_multivalue syntax ~var t x =
if not t.quoted && V.is_multivalued ctx then if not t.quoted && V.is_multivalued x then
Loc.fail t.loc "Variable %s expands to multiple values, \ Loc.fail t.loc "Variable %s expands to %d values, \
however a single value is expected here. \ however a single value is expected here. \
Please quote this atom. " Please quote this atom. "
(string_of_var syntax var) (string_of_var syntax var) (V.length x)
let expand ctx t ~f = let expand ctx t ~f =
match t.items with match t.items with
@ -189,6 +190,7 @@ end
module String_expansion = struct module String_expansion = struct
type t = string type t = string
let length _ = 1
let is_multivalued _ = false let is_multivalued _ = false
type context = unit type context = unit
let to_string () (s: string) = s let to_string () (s: string) = s

View File

@ -49,6 +49,8 @@ module type EXPANSION = sig
type t type t
(** The value to which variables are expanded. *) (** The value to which variables are expanded. *)
val length : t -> int
val is_multivalued : t -> bool val is_multivalued : t -> bool
(** Report whether the value is a multivalued one (such as for (** Report whether the value is a multivalued one (such as for
example ${@}) which much be in quoted strings to be concatenated example ${@}) which much be in quoted strings to be concatenated