Share Ordered_set_lang.Make(String)(String)

This commit is contained in:
Rudi Grinberg 2018-03-15 14:25:04 +08:00
parent 286f93a963
commit 25158fc702
3 changed files with 50 additions and 22 deletions

View File

@ -19,7 +19,6 @@ type 'ast generic =
type ast_expanded = (Loc.t * string, Ast.expanded) Ast.t type ast_expanded = (Loc.t * string, Ast.expanded) Ast.t
type t = ast_expanded generic type t = ast_expanded generic
let loc t = t.loc let loc t = t.loc
let parse_general sexp ~f = let parse_general sexp ~f =
@ -68,6 +67,23 @@ module type Key = sig
module Map : Map.S with type key = t module Map : Map.S with type key = t
end end
module type S = sig
type value
type 'a map
val eval
: t
-> parse:(loc:Loc.t -> string -> value)
-> standard:value list
-> value list
val eval_unordered
: t
-> parse:(loc:Loc.t -> string -> value)
-> standard:value map
-> value map
end
module Make(Key : Key)(Value : Value with type key = Key.t) = struct module Make(Key : Key)(Value : Value with type key = Key.t) = struct
module type Named_values = sig module type Named_values = sig
type t type t
@ -129,6 +145,9 @@ module Make(Key : Key)(Value : Value with type key = Key.t) = struct
| _ -> None) | _ -> None)
end) end)
type value = Value.t
type 'a map = 'a Key.Map.t
let eval t ~parse ~standard = let eval t ~parse ~standard =
if is_standard t then if is_standard t then
standard (* inline common case *) standard (* inline common case *)
@ -231,3 +250,13 @@ module Unexpanded = struct
in in
{ t with ast = expand t.ast } { t with ast = expand t.ast }
end end
module String = Make(struct
type t = string
let compare = String.compare
module Map = String_map
end)(struct
type t = string
type key = string
let key x = x
end)

View File

@ -22,23 +22,30 @@ module type Key = sig
module Map : Map.S with type key = t module Map : Map.S with type key = t
end end
module Make(Key : Key)(Value : Value with type key = Key.t) : sig module type S = sig
(** Evaluate an ordered set. [standard] is the interpretation of [:standard] inside the (** Evaluate an ordered set. [standard] is the interpretation of [:standard]
DSL. *) inside the DSL. *)
type value
type 'a map
val eval val eval
: t : t
-> parse:(loc:Loc.t -> string -> Value.t) -> parse:(loc:Loc.t -> string -> value)
-> standard:Value.t list -> standard:value list
-> Value.t list -> value list
(** Same as [eval] but the result is unordered *) (** Same as [eval] but the result is unordered *)
val eval_unordered val eval_unordered
: t : t
-> parse:(loc:Loc.t -> string -> Value.t) -> parse:(loc:Loc.t -> string -> value)
-> standard:Value.t Key.Map.t -> standard:value map
-> Value.t Key.Map.t -> value map
end end
module Make(Key : Key)(Value : Value with type key = Key.t)
: S with type value = Value.t
and type 'a map = 'a Key.Map.t
val standard : t val standard : t
val is_standard : t -> bool val is_standard : t -> bool
@ -63,3 +70,5 @@ module Unexpanded : sig
-> f:(String_with_vars.t -> string) -> f:(String_with_vars.t -> string)
-> expanded -> expanded
end with type expanded := t end with type expanded := t
module String : S with type value = string and type 'a map = 'a String_map.t

View File

@ -669,16 +669,6 @@ module Action = struct
| fail :: _ -> Build.fail fail >>> build | fail :: _ -> Build.fail fail >>> build
end end
module Eval_strings = Ordered_set_lang.Make(struct
type t = string
let compare = String.compare
module Map = String_map
end)(struct
type t = string
type key = string
let key x = x
end)
let expand_and_eval_set t ~scope ~dir ?extra_vars set ~standard = let expand_and_eval_set t ~scope ~dir ?extra_vars set ~standard =
let open Build.O in let open Build.O in
let f = expand_vars t ~scope ~dir ?extra_vars in let f = expand_vars t ~scope ~dir ?extra_vars in
@ -688,11 +678,11 @@ let expand_and_eval_set t ~scope ~dir ?extra_vars set ~standard =
let set = let set =
Ordered_set_lang.Unexpanded.expand set ~files_contents:String_map.empty ~f Ordered_set_lang.Unexpanded.expand set ~files_contents:String_map.empty ~f
in in
Build.return (Eval_strings.eval set ~standard ~parse) Build.return (Ordered_set_lang.String.eval set ~standard ~parse)
| files -> | files ->
let paths = List.map files ~f:(Path.relative dir) in let paths = List.map files ~f:(Path.relative dir) in
Build.all (List.map paths ~f:Build.read_sexp) Build.all (List.map paths ~f:Build.read_sexp)
>>^ fun sexps -> >>^ fun sexps ->
let files_contents = List.combine files sexps |> String_map.of_list_exn in let files_contents = List.combine files sexps |> String_map.of_list_exn in
let set = Ordered_set_lang.Unexpanded.expand set ~files_contents ~f in let set = Ordered_set_lang.Unexpanded.expand set ~files_contents ~f in
Eval_strings.eval set ~standard ~parse Ordered_set_lang.String.eval set ~standard ~parse