Refactor a bit the parsing code for bindings

Signed-off-by: Jeremie Dimino <jeremie@dimino.org>
This commit is contained in:
Jeremie Dimino 2018-07-09 16:43:10 +01:00 committed by Rudi Grinberg
parent 205d12755e
commit 17d4a7c3df
1 changed files with 19 additions and 21 deletions

View File

@ -262,31 +262,29 @@ module Bindings = struct
let singleton x = [Unnamed x] let singleton x = [Unnamed x]
let t elem = let t elem =
let binding = let rec loop vars acc =
peek_exn >>= function peek >>= function
| List (_, Atom (loc, A s) :: _) when | None -> return (List.rev acc)
| Some (List (_, Atom (loc, A s) :: _)) when
String.length s > 1 && s.[0] = ':' -> String.length s > 1 && s.[0] = ':' ->
let name = String.sub s ~pos:1 ~len:(String.length s - 1) in let name = String.sub s ~pos:1 ~len:(String.length s - 1) in
enter (junk >>= fun () -> let vars =
repeat elem >>| fun values -> if not (String.Set.mem vars name) then
Left (loc, name, values)) String.Set.add vars name
| _ -> else
elem >>| fun elem -> Right elem of_sexp_errorf loc "Variable %s is defined for the second time."
name
in in
list binding >>| (fun bindings -> enter (junk >>= fun () -> repeat elem)
let used_names = Hashtbl.create 8 in >>= fun values ->
List.fold_right bindings ~init:[] ~f:(fun x acc -> loop vars (Named (name, values) :: acc)
match x with | _ ->
| Right x -> Unnamed x :: acc elem >>= fun x ->
| Left (loc, name, values) -> loop vars (Unnamed x :: acc)
begin match Hashtbl.find used_names name with in
| None -> Stanza.file_kind () >>= function
Hashtbl.add used_names name loc; | Jbuild -> list (elem >>| fun x -> Unnamed x)
Named (name, values) :: acc | Dune -> loop String.Set.empty []
| Some loc_old ->
of_sexp_errorf loc "Variable %s is already defined in %s"
name (Loc.to_file_colon_line loc_old)
end))
let sexp_of_t sexp_of_a bindings = let sexp_of_t sexp_of_a bindings =
Sexp.List ( Sexp.List (