From b4dd6565b2ff2d434ff3114ea980cb758447acc1 Mon Sep 17 00:00:00 2001 From: Jeremie Dimino Date: Sat, 30 Jun 2018 00:02:57 +0100 Subject: [PATCH] Add Ordered_set_lang.Unexpanded.fold_strings Signed-off-by: Jeremie Dimino --- src/ordered_set_lang.ml | 20 ++++++++++++++++++++ src/ordered_set_lang.mli | 11 +++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/ordered_set_lang.ml b/src/ordered_set_lang.ml index d9a10958..6301e562 100644 --- a/src/ordered_set_lang.ml +++ b/src/ordered_set_lang.ml @@ -271,6 +271,26 @@ module Unexpanded = struct in loop t.ast + type position = Pos | Neg + + let fold_strings t ~init ~f = + let rec loop (t : ast) pos acc = + let open Ast in + match t with + | Standard | Include _ -> acc + | Element x -> f pos x acc + | Union l -> List.fold_left l ~init:acc ~f:(fun acc x -> loop x pos acc) + | Diff (l, r) -> + let acc = loop l pos acc in + let pos = + match pos with + | Pos -> Neg + | Neg -> Pos + in + loop r pos acc + in + loop t.ast Pos init + let expand t ~files_contents ~f = let context = t.context in let rec expand (t : ast) : ast_expanded = diff --git a/src/ordered_set_lang.mli b/src/ordered_set_lang.mli index 189f7013..be36d9cb 100644 --- a/src/ordered_set_lang.mli +++ b/src/ordered_set_lang.mli @@ -76,6 +76,17 @@ module Unexpanded : sig -> files_contents:Sexp.Ast.t String.Map.t -> f:(String_with_vars.t -> string) -> expanded + + type position = Pos | Neg + + (** Fold a function over all strings in a set. The callback receive + whether the string is in position or negative position, i.e. on + the left or right of a [\] operator. *) + val fold_strings + : t + -> init:'a + -> f:(position -> String_with_vars.t -> 'a -> 'a) + -> 'a end with type expanded := t module String : S with type value = string and type 'a map = 'a String.Map.t