Skip to content

Conversation

@rgrinberg
Copy link
Member

  • Stop opening Decoder everywhere
  • Wrap things to 80 chars
  • Extract a helper

Signed-off-by: Rudi Grinberg <[email protected]>
* Stop opening Decoder everywhere
* Wrap things to 80 chars
* Extract a helper


JJ: diff --git a/src/dune_lang/dune_project.ml b/src/dune_lang/dune_project.ml
JJ: index dca3584..1bd07ad 100644
JJ: --- a/src/dune_lang/dune_project.ml
JJ: +++ b/src/dune_lang/dune_project.ml
JJ: @@ -1,5 +1,4 @@
JJ:  open Import
JJ: -open Decoder
JJ:  module Versioned_file = Dune_sexp.Versioned_file
JJ:  module Execution_parameters = Dune_engine.Execution_parameters
JJ:  module Compound_user_error = Dune_rpc_private.Compound_user_error
JJ: @@ -42,6 +41,7 @@
JJ:      ;;
JJ:  
JJ:      let decode =
JJ: +      let open Decoder in
JJ:        let check ver = Syntax.since Stanza.syntax ver in
JJ:        enum'
JJ:          [ "true", check (1, 7) >>> return Enabled
JJ: @@ -103,9 +103,9 @@
JJ:  
JJ:  let get_exn () =
JJ:    get ()
JJ: -  >>| function
JJ: -  | Some t -> t
JJ: -  | None -> Code_error.raise "Current project is unset" []
JJ: +  |> Decoder.map ~f:(function
JJ: +    | Some t -> t
JJ: +    | None -> Code_error.raise "Current project is unset" [])
JJ:  ;;
JJ:  
JJ:  let equal : t -> t -> bool = ( == )
JJ: @@ -268,6 +268,7 @@
JJ:  
JJ:    let register_unit syntax stanzas =
JJ:      let unit_stanzas =
JJ: +      let open Decoder in
JJ:        let+ r = stanzas in
JJ:        (), r
JJ:      in
JJ: @@ -335,44 +336,46 @@
JJ:    | Ok map -> map
JJ:  ;;
JJ:  
JJ: +let make_parsing_context ~(lang : Lang.Instance.t) extensions =
JJ: +  let init =
JJ: +    let init = Univ_map.singleton (Syntax.key lang.syntax) (Active lang.version) in
JJ: +    let extensions =
JJ: +      List.fold_left extensions ~init:[] ~f:(fun acc (ext : Extension.automatic) ->
JJ: +        match ext with
JJ: +        | Not_selected _ -> acc
JJ: +        | Selected ext ->
JJ: +          let syntax =
JJ: +            let (Extension.Packed ext) = ext.extension in
JJ: +            ext.syntax
JJ: +          in
JJ: +          (syntax, ext.version) :: acc)
JJ: +    in
JJ: +    Univ_map.set
JJ: +      init
JJ: +      String_with_vars.decoding_env_key
JJ: +      (Pform.Env.initial ~stanza:lang.version ~extensions)
JJ: +  in
JJ: +  List.fold_left extensions ~init ~f:(fun acc (ext : Extension.automatic) ->
JJ: +    let syntax =
JJ: +      let (Extension.Packed ext) =
JJ: +        match ext with
JJ: +        | Selected e -> e.extension
JJ: +        | Not_selected e -> e
JJ: +      in
JJ: +      ext.syntax
JJ: +    in
JJ: +    let status : Syntax.Key.t =
JJ: +      match ext with
JJ: +      | Selected ext -> Active ext.version
JJ: +      | Not_selected (Packed e) ->
JJ: +        Inactive { lang = e.syntax; dune_lang_ver = lang.version }
JJ: +    in
JJ: +    Univ_map.set acc (Syntax.key syntax) status)
JJ: +;;
JJ: +
JJ:  let interpret_lang_and_extensions ~(lang : Lang.Instance.t) ~explicit_extensions =
JJ:    let extensions = Extension.automatic ~explicitly_selected:explicit_extensions in
JJ: -  let parsing_context =
JJ: -    let init =
JJ: -      let init = Univ_map.singleton (Syntax.key lang.syntax) (Active lang.version) in
JJ: -      let extensions =
JJ: -        List.fold_left extensions ~init:[] ~f:(fun acc (ext : Extension.automatic) ->
JJ: -          match ext with
JJ: -          | Not_selected _ -> acc
JJ: -          | Selected ext ->
JJ: -            let syntax =
JJ: -              let (Extension.Packed ext) = ext.extension in
JJ: -              ext.syntax
JJ: -            in
JJ: -            (syntax, ext.version) :: acc)
JJ: -      in
JJ: -      Univ_map.set
JJ: -        init
JJ: -        String_with_vars.decoding_env_key
JJ: -        (Pform.Env.initial ~stanza:lang.version ~extensions)
JJ: -    in
JJ: -    List.fold_left extensions ~init ~f:(fun acc (ext : Extension.automatic) ->
JJ: -      let syntax =
JJ: -        let (Extension.Packed ext) =
JJ: -          match ext with
JJ: -          | Selected e -> e.extension
JJ: -          | Not_selected e -> e
JJ: -        in
JJ: -        ext.syntax
JJ: -      in
JJ: -      let status : Syntax.Key.t =
JJ: -        match ext with
JJ: -        | Selected ext -> Active ext.version
JJ: -        | Not_selected (Packed e) ->
JJ: -          Inactive { lang = e.syntax; dune_lang_ver = lang.version }
JJ: -      in
JJ: -      Univ_map.set acc (Syntax.key syntax) status)
JJ: -  in
JJ: +  let parsing_context = make_parsing_context ~lang extensions in
JJ:    let extension_args, extension_stanzas =
JJ:      List.fold_left
JJ:        extensions
JJ: @@ -381,7 +384,6 @@
JJ:          match ext with
JJ:          | Not_selected (Packed e) ->
JJ:            let stanzas =
JJ: -            let open Decoder in
JJ:              let stanzas =
JJ:                match Syntax.greatest_supported_version e.syntax with
JJ:                | None -> []
JJ: @@ -395,21 +397,26 @@
JJ:                      (Syntax.key e.syntax)
JJ:                      (Active greatest_supported_version)
JJ:                  in
JJ: -                parse (enter e.stanzas) parsing_context (List (Loc.of_pos __POS__, []))
JJ: +                Decoder.parse
JJ: +                  (Decoder.enter e.stanzas)
JJ: +                  parsing_context
JJ: +                  (List (Loc.of_pos __POS__, []))
JJ:                  |> snd
JJ:              in
JJ:              List.map stanzas ~f:(fun (name, _) ->
JJ:                ( name
JJ: -              , let+ _ = Syntax.get_exn e.syntax in
JJ: -                (* The above [get_exn] will raise because the extension is
JJ: +              , Syntax.get_exn e.syntax
JJ: +                |> Decoder.map ~f:(fun _ ->
JJ: +                  (* The above [get_exn] will raise because the extension is
JJ:                     inactive *)
JJ: -                assert false ))
JJ: +                  assert false) ))
JJ:            in
JJ:            args_acc, stanzas :: stanzas_acc
JJ:          | Selected instance ->
JJ:            let (Packed e) = instance.extension in
JJ:            let args_acc, stanzas =
JJ:              let args =
JJ: +              let open Decoder in
JJ:                let+ arg, stanzas = Decoder.set_many parsing_context e.stanzas in
JJ:                Univ_map.set args_acc e.key arg, stanzas
JJ:              in
JJ: @@ -417,8 +424,10 @@
JJ:            in
JJ:            args_acc, stanzas :: stanzas_acc)
JJ:    in
JJ: -  let stanzas = List.concat (lang.data :: extension_stanzas) in
JJ: -  let stanza_parser = Decoder.(set_many parsing_context (sum stanzas)) in
JJ: +  let stanza_parser =
JJ: +    let stanzas = List.concat (lang.data :: extension_stanzas) in
JJ: +    Decoder.(set_many parsing_context (sum stanzas))
JJ: +  in
JJ:    parsing_context, stanza_parser, extension_args
JJ:  ;;
JJ:  
JJ: @@ -688,8 +697,12 @@
JJ:  let map_workspace_root t = t.map_workspace_root
JJ:  let executables_implicit_empty_intf t = t.executables_implicit_empty_intf
JJ:  let accept_alternative_dune_file_name t = t.accept_alternative_dune_file_name
JJ: -let () = Extension.register_simple Action.Action_plugin.syntax (return [])
JJ: -let dune_site_extension = Extension.register_unit Site.dune_site_syntax (return [])
JJ: +let () = Extension.register_simple Action.Action_plugin.syntax (Decoder.return [])
JJ: +
JJ: +let dune_site_extension =
JJ: +  Extension.register_unit Site.dune_site_syntax (Decoder.return [])
JJ: +;;
JJ: +
JJ:  let strict_package_deps t = t.strict_package_deps
JJ:  let allow_approximate_merlin t = t.allow_approximate_merlin
JJ:  let cram t = t.cram
JJ: @@ -841,6 +854,7 @@
JJ:  ;;
JJ:  
JJ:  let parse ~dir ~(lang : Lang.Instance.t) ~file =
JJ: +  let open Decoder in
JJ:    String_with_vars.set_decoding_env
JJ:      (Pform.Env.initial ~stanza:lang.version ~extensions:[])
JJ:    @@ fields
JJ: @@ -1068,6 +1082,7 @@
JJ:  ;;
JJ:  
JJ:  let _oxcaml_extension =
JJ: -  (* This is required to register the extension because OxCaml doesn't have a specific decoder. *)
JJ: +  (* This is required to register the extension because OxCaml doesn't have a
JJ: +     specific decoder. *)
JJ:    Extension.register Oxcaml.syntax (Decoder.return ((), [])) Dyn.unit
JJ:  ;;
Signed-off-by: Rudi Grinberg <[email protected]>
@rgrinberg rgrinberg merged commit 318959d into ocaml:main Oct 19, 2025
26 checks passed
davesnx added a commit to davesnx/dune that referenced this pull request Oct 22, 2025
…without-system

* 'main' of github.com:/ocaml/dune: (33 commits)
  Fix difference between bootstrap & dune behaviour w.r.t include-subdirs ambiguity (ocaml#12607)
  Revert "Fix difference between bootstrap & dune behaviour w.r.t include-subdirs ambiguity"
  fix(include-subdirs-qualified): prefer closer module of the same name
  Add 2 tests showing difference between bootstrap & dune w.r.t include-subdirs
  dune init: use source path (ocaml#12601)
  chore(deps): bump actions/setup-node from 5 to 6
  test(melange): show melange compilation isn't part of `@all` alias (ocaml#12603)
  Disable flaky test (ocaml#12600)
  Rename path to dir in dune_init (ocaml#12599)
  Dune project: remove dead code (ocaml#12598)
  Dune_project refactor (ocaml#12597)
  doc(melange): reference `dirs` stanza (ocaml#12596)
  Stanza simplifications (ocaml#12595)
  Reduce stanza definition boilerplate (ocaml#12592)
  Rename Stanza_common to Stanza_pkg (ocaml#12593)
  Remove copied code between library and library_parameter stanzas (ocaml#12594)
  Split modules settings (ocaml#12591)
  Move the (include_subdirs qualified) check to a single place (ocaml#12590)
  refactor: simplify loc/ocamllex/ocamlyacc stanza handling (ocaml#12589)
  pkg: Implement a package search command
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant