Skip to content

Commit

Permalink
refactor: move toplevel stanza to own module (ocaml#9483)
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg authored Dec 13, 2023
1 parent f3959e9 commit 4f30e74
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 55 deletions.
42 changes: 2 additions & 40 deletions src/dune_rules/dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1723,44 +1723,6 @@ module Tests = struct
let single = gen_parse (field "name" (located string) >>| List.singleton)
end

module Toplevel = struct
type t =
{ name : string
; libraries : (Loc.t * Lib_name.t) list
; loc : Loc.t
; pps : Preprocess.Without_instrumentation.t Preprocess.t
}

include Stanza.Make (struct
type nonrec t = t

include Poly
end)

let decode =
let open Dune_lang.Decoder in
fields
(let+ loc = loc
and+ name = field "name" string
and+ libraries = field "libraries" (repeat (located Lib_name.decode)) ~default:[]
and+ pps =
field
"preprocess"
(Dune_lang.Syntax.since Stanza.syntax (2, 5) >>> Preprocess.decode)
~default:Preprocess.No_preprocessing
in
match pps with
| Preprocess.Pps _ | No_preprocessing -> { name; libraries; loc; pps }
| Action (loc, _) | Future_syntax loc ->
User_error.raise
~loc
[ Pp.text
"Toplevel does not currently support action or future_syntax \
preprocessing."
])
;;
end

module Documentation = struct
type t =
{ loc : Loc.t
Expand Down Expand Up @@ -2004,8 +1966,8 @@ module Stanzas = struct
[ Include_subdirs.make_stanza (loc, t) ] )
; ( "toplevel"
, let+ () = Dune_lang.Syntax.since Stanza.syntax (1, 7)
and+ t = Toplevel.decode in
[ Toplevel.make_stanza t ] )
and+ t = Toplevel_stanza.decode in
[ Toplevel_stanza.make_stanza t ] )
; ( "deprecated_library_name"
, let+ () = Dune_lang.Syntax.since Stanza.syntax (2, 0)
and+ t = Deprecated_library_name.decode in
Expand Down
11 changes: 0 additions & 11 deletions src/dune_rules/dune_file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,6 @@ module Tests : sig
include Stanza.S with type t := t
end

module Toplevel : sig
type t =
{ name : string
; libraries : (Loc.t * Lib_name.t) list
; loc : Loc.t
; pps : Preprocess.Without_instrumentation.t Preprocess.t
}

include Stanza.S with type t := t
end

module Include_subdirs : sig
type qualification =
| Unqualified
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/gen_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ end = struct
let toplevel_setup = Toplevel.Stanza.setup in
let open Dune_file in
match Stanza.repr stanza with
| Toplevel.T toplevel ->
| Toplevel_stanza.T toplevel ->
let+ () = toplevel_setup ~sctx ~dir ~toplevel in
empty_none
| Library.T lib ->
Expand Down
36 changes: 36 additions & 0 deletions src/dune_rules/stanzas/toplevel_stanza.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
open Import

type t =
{ name : string
; libraries : (Loc.t * Lib_name.t) list
; loc : Loc.t
; pps : Preprocess.Without_instrumentation.t Preprocess.t
}

include Stanza.Make (struct
type nonrec t = t

include Poly
end)

let decode =
let open Dune_lang.Decoder in
fields
(let+ loc = loc
and+ name = field "name" string
and+ libraries = field "libraries" (repeat (located Lib_name.decode)) ~default:[]
and+ pps =
field
"preprocess"
(Dune_lang.Syntax.since Stanza.syntax (2, 5) >>> Preprocess.decode)
~default:Preprocess.No_preprocessing
in
match pps with
| Preprocess.Pps _ | No_preprocessing -> { name; libraries; loc; pps }
| Action (loc, _) | Future_syntax loc ->
User_error.raise
~loc
[ Pp.text
"Toplevel does not currently support action or future_syntax preprocessing."
])
;;
12 changes: 12 additions & 0 deletions src/dune_rules/stanzas/toplevel_stanza.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
open Import

type t =
{ name : string
; libraries : (Loc.t * Lib_name.t) list
; loc : Loc.t
; pps : Preprocess.Without_instrumentation.t Preprocess.t
}

include Stanza.S with type t := t

val decode : t Dune_sexp.Decoder.t
4 changes: 2 additions & 2 deletions src/dune_rules/toplevel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module Source = struct

let make ~dir ~loc ~main ~name = { dir; main; name; loc }

let of_stanza ~dir ~(toplevel : Dune_file.Toplevel.t) =
let of_stanza ~dir ~(toplevel : Toplevel_stanza.t) =
{ dir = Path.Build.relative dir (toplevel_dir_prefix ^ toplevel.name)
; name = toplevel.name
; loc = toplevel.loc
Expand Down Expand Up @@ -174,7 +174,7 @@ let print_toplevel_init_file { include_paths; files_to_load; uses; pp; ppx; code
;;

module Stanza = struct
let setup ~sctx ~dir ~(toplevel : Dune_file.Toplevel.t) =
let setup ~sctx ~dir ~(toplevel : Toplevel_stanza.t) =
let open Memo.O in
let source = Source.of_stanza ~dir ~toplevel in
let* expander = Super_context.expander sctx ~dir in
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/toplevel.mli
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ module Stanza : sig
val setup
: sctx:Super_context.t
-> dir:Path.Build.t
-> toplevel:Dune_file.Toplevel.t
-> toplevel:Toplevel_stanza.t
-> unit Memo.t
end

0 comments on commit 4f30e74

Please sign in to comment.