Module Ocaml_parser

The file_type and file_info types are used to represent OCaml source files, and the module_info type represents the metadata of an OCaml module. The collect_ocaml_files function is used to collect OCaml source files from a directory and its subdirectories.

type ocaml_source =
  1. | Interface
  2. | Implementation
type parse_result = {
  1. location : string;
  2. module_path : string;
  3. comments : string list;
  4. contents : string;
  5. ocaml_source : ocaml_source;
}
type traverse_input
val traverse : traverse_input -> parse_result list
val parse : Eio.Fs.dir_ty Eio.Path.t -> string -> ocaml_source -> string -> traverse_input

parse dir file ocaml_source module_name parses the given file with the specified ocaml_source (either Interface or Implementation) and module name module_name. It returns a list of parse_result records containing the location, module path, comments, contents, and file type for each parsed item.

  • parameter dir

    is the directory in which the function is executed.

  • parameter file

    The file to be parsed.

  • parameter ocaml_source

    The type of the file, either Interface or Implementation.

  • parameter module_name

    The name of the module being parsed.

  • returns

    A list of parse_result records containing information about the parsed items.

type _ file_type =
  1. | Mli : mli file_type
  2. | Ml : ml file_type
and mli =
  1. | MLI
and ml =
  1. | ML
type 'a file_info = {
  1. file_type : 'a file_type;
  2. file_name : string;
}

file_info is a record type that contains the file_type and file_name.

Now, the file_type is encoded in the type system, and you can create file_info values with specific file types:

  let mli_file = mli file_info { file_type = Mli; file_name = "example.mli" }
  let ml_file : ml file_info = { file_type = Ml; file_name = "example.ml" }

module_info is a record type representing the metadata of an OCaml module, combining the interface (mli) and implementation (ml) files.

type module_info = {
  1. mli_file : mli file_info option;
  2. ml_file : ml file_info option;
  3. module_path : string;
}
val parse_module_info : Eio.Fs.dir_ty Eio.Path.t -> module_info -> traverse_input option * traverse_input option

parse_module_info module_info parses the given module_info. It returns a list of parse_result records containing the location, module path, comments, contents, and file type for each parsed item.

  • parameter module_info

    The module information to be parsed.

  • returns

    A pair of traverse_input option * traverse_input option thunks containing information about the parsed items.

collect_ocaml_files dir path recursively collects OCaml source files from the directory specified by path and its subdirectories.

val collect_ocaml_files : Eio.Fs.dir_ty Eio.Path.t -> string -> (module_info list, string) result
val format_parse_result : parse_result -> string * string

format_parse_result parse_result formats the given parse_result into a string.

  • parameter parse_result

    The parse result to be formatted.

  • returns

    A formatted string containing the parse result information.