Refactor a bit the parsing code for bindings
Signed-off-by: Jeremie Dimino <jeremie@dimino.org>
This commit is contained in:
parent
205d12755e
commit
17d4a7c3df
|
@ -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
|
||||||
|
of_sexp_errorf loc "Variable %s is defined for the second time."
|
||||||
|
name
|
||||||
|
in
|
||||||
|
enter (junk >>= fun () -> repeat elem)
|
||||||
|
>>= fun values ->
|
||||||
|
loop vars (Named (name, values) :: acc)
|
||||||
| _ ->
|
| _ ->
|
||||||
elem >>| fun elem -> Right elem
|
elem >>= fun x ->
|
||||||
|
loop vars (Unnamed x :: acc)
|
||||||
in
|
in
|
||||||
list binding >>| (fun bindings ->
|
Stanza.file_kind () >>= function
|
||||||
let used_names = Hashtbl.create 8 in
|
| Jbuild -> list (elem >>| fun x -> Unnamed x)
|
||||||
List.fold_right bindings ~init:[] ~f:(fun x acc ->
|
| Dune -> loop String.Set.empty []
|
||||||
match x with
|
|
||||||
| Right x -> Unnamed x :: acc
|
|
||||||
| Left (loc, name, values) ->
|
|
||||||
begin match Hashtbl.find used_names name with
|
|
||||||
| None ->
|
|
||||||
Hashtbl.add used_names name loc;
|
|
||||||
Named (name, values) :: acc
|
|
||||||
| 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 (
|
||||||
|
|
Loading…
Reference in New Issue