Module Dune_lang.DecoderSource

Reading OCaml values from dune lang ones

Sourcetype ast = Ast.t =
  1. | Atom of Stdune.Loc.t * Atom.t
  2. | Quoted_string of Stdune.Loc.t * string
  3. | Template of Template.t
  4. | List of Stdune.Loc.t * ast list
Sourcetype hint = {
  1. on : string;
  2. candidates : string list;
}
Sourcetype ('a, 'kind) parser

Monad producing a value of type 'a by parsing an input composed of a sequence of S-expressions.

The input can be seen either as a plain sequence of S-expressions or a list of fields. The 'kind parameter indicates how the input is seen:

  • with 'kind = [values], the input is seen as an ordered sequence of S-expressions
  • with !'kind = [fields], the input is seen as an unordered sequence of fields

A field is a S-expression of the form: (<atom> <values>...) where atom is a plain atom, i.e. not a quoted string and not containing variables. values is a sequence of zero, one or more S-expressions.

It is possible to switch between the two mode at any time using the appropriate combinator. Some primitives can be used in both mode while some are specific to one mode.

Sourcetype values
Sourcetype fields
Sourcetype 'a t = ('a, values) parser
Sourcetype 'a fields_parser = ('a, fields) parser
Sourceval parse : 'a t -> Stdune.Univ_map.t -> ast -> 'a

parse parser context sexp parse a S-expression using the following parser. The input consist of a single S-expression. context allows to pass extra information such as versions to individual parsers.

Sourceval set_input : ast list -> (unit, 'k) parser
Sourceval return : 'a -> ('a, _) parser
Sourceval (>>=) : ('a, 'k) parser -> ('a -> ('b, 'k) parser) -> ('b, 'k) parser
Sourceval (>>|) : ('a, 'k) parser -> ('a -> 'b) -> ('b, 'k) parser
Sourceval (>>>) : (unit, 'k) parser -> ('a, 'k) parser -> ('a, 'k) parser
Sourceval map : ('a, 'k) parser -> f:('a -> 'b) -> ('b, 'k) parser
Sourceval try_ : ('a, 'k) parser -> (exn -> ('a, 'k) parser) -> ('a, 'k) parser
Sourceval traverse : 'a list -> f:('a -> ('b, 'k) parser) -> ('b list, 'k) parser
Sourceval all : ('a, 'k) parser list -> ('a list, 'k) parser
Sourceval get : 'a Stdune.Univ_map.Key.t -> ('a option, _) parser

Access to the context

Sourceval set : 'a Stdune.Univ_map.Key.t -> 'a -> ('b, 'k) parser -> ('b, 'k) parser
Sourceval get_all : (Stdune.Univ_map.t, _) parser
Sourceval set_many : Stdune.Univ_map.t -> ('a, 'k) parser -> ('a, 'k) parser
Sourceval loc : (Stdune.Loc.t, _) parser

Return the location of the list currently being parsed.

Sourceval (<|>) : ('a, 'k) parser -> ('a, 'k) parser -> ('a, 'k) parser

a <|> b is either a or b. If a fails to parse the input, then try b. If b fails as well, raise the error from the parser that consumed the most input.

Sourceval either : ('a, 'k) parser -> ('b, 'k) parser -> (('a, 'b) Stdune.Either.t, 'k) parser
Sourceval atom_matching : (string -> 'a option) -> desc:string -> 'a t

atom_matching f expects the next element to be an atom for which f returns Some v. desc is used to describe the atom in case of error. f must not raise.

Sourceval keyword : string -> unit t

keyword s is a short-hand for

 atom_matching (String.equal s) ~desc:(sprintf "'%s'" s) 
Sourceval until_keyword : string -> before:'a t -> after:'b t -> ('a list * 'b option) t

Use before to parse elements until the keyword is reached. Then use after to parse the rest.

Sourcetype kind =
  1. | Values of Stdune.Loc.t * string option
  2. | Fields of Stdune.Loc.t * string option

What is currently being parsed. The second argument is the atom at the beginnig of the list when inside a sum ... or field ....

Sourceval kind : (kind, _) parser
Sourceval repeat : 'a t -> 'a list t

repeat t uses t to consume all remaining elements of the input until the end of sequence is reached.

Sourceval repeat1 : 'a t -> 'a list t

Like repeat but the list of elements must be non-empty.

Sourceval capture : ('a t -> 'a) t

Capture the rest of the input for later parsing

Sourceval enter : 'a t -> 'a t

enter t expect the next element of the input to be a list and parse its contents with t.

Sourceval fields : 'a fields_parser -> 'a t

fields fp converts the rest of the current input to a list of fields and parse them with fp. This operation fails if one the S-expression in the input is not of the form (<atom> <values>...)

Sourceval string : string t

Consume the next element of the input as a string, int, bool, ...

Sourceval int : int t
Sourceval float : float t
Sourceval bool : bool t
Sourceval pair : 'a t -> 'b t -> ('a * 'b) t
Sourceval triple : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t
Sourceval maybe : 'a t -> 'a option t

maybe t is a short-hand for:

  (let+ x = t in
   Some x)
  <|> return None
Sourceval duration : int t

Consume the next element as a duration, requiring 's', 'm' or 'h' suffix

Sourceval bytes_unit : int64 t

Consume the next element as a number of bytes, requiring 'B', 'KB', 'MB' or 'GB' suffix

Sourceval raw : ast t

Unparsed next element of the input

Sourceval peek : ast option t

Inspect the next element of the input without consuming it

Sourceval peek_exn : ast t

Same as peek but fail if the end of input is reached

Sourceval junk : unit t

Consume and ignore the next element of the input

Sourceval junk_everything : (unit, _) parser

Ignore all the rest of the input

Sourceval plain_string : (loc:Stdune.Loc.t -> string -> 'a) -> 'a t

plain_string f expects the next element of the input to be a plain string, i.e. either an atom or a quoted string, but not a template nor a list.

Sourceval filename : string t

A valid filename, i.e. a string other than "." or ".."

Sourceval relative_file : string t

A relative filename

Sourceval fix : ('a t -> 'a t) -> 'a t
Sourceval located : ('a, 'k) parser -> (Stdune.Loc.t * 'a, 'k) parser
Sourceval enum : (string * 'a) list -> 'a t
Sourceval sum : ?force_parens:bool -> (string * 'a t) list -> 'a t

Parser that parse a S-expression of the form (<atom> <s-exp1> <s-exp2> ...) or <atom>. <atom> is looked up in the list and the remaining s-expressions are parsed using the corresponding list parser.

If force_parens is true, then the form <atom> is never accepted. The default is false.

Sourceval map_validate : ('a, 'k) parser -> f:('a -> ('b, Stdune.User_message.t) Stdune.Result.t) -> ('b, 'k) parser

Check the result of a list parser, and raise a properly located error in case of failure.

Parsing record fields

Sourceval field : string -> ?default:'a -> ?on_dup:(Stdune.Univ_map.t -> string -> Ast.t list -> unit) -> 'a t -> 'a fields_parser
Sourceval field_o : string -> ?on_dup:(Stdune.Univ_map.t -> string -> Ast.t list -> unit) -> 'a t -> 'a option fields_parser
Sourceval fields_mutually_exclusive : ?on_dup:(Stdune.Univ_map.t -> string -> Ast.t list -> unit) -> ?default:'a -> (string * 'a t) list -> 'a fields_parser

Parser for mutually exclusive fields. If default is provided, allow fields absence.

Sourceval field_b : ?check:unit t -> ?on_dup:(Stdune.Univ_map.t -> string -> Ast.t list -> unit) -> string -> bool fields_parser

Test if the field is present

Sourceval field_o_b : ?check:unit t -> ?on_dup:(Stdune.Univ_map.t -> string -> Ast.t list -> unit) -> string -> bool option fields_parser

Differentiate between not present and set to true or false

Sourceval multi_field : string -> 'a t -> 'a list fields_parser

A field that can appear multiple times

Sourceval leftover_fields_as_sums : (string * 'a t) list -> 'a list fields_parser

Treat the remainig fields as a list of sum values

Sourceval field_present_too_many_times : Stdune.Univ_map.t -> string -> Ast.t list -> _

Default value for on_dup. It fails with an appropriate error message.

Sourceval leftover_fields : Ast.t list fields_parser
Sourceval (let*) : ('a, 'k) parser -> ('a -> ('b, 'k) parser) -> ('b, 'k) parser
Sourceval (let+) : ('a, 'k) parser -> ('a -> 'b) -> ('b, 'k) parser
Sourceval (and+) : ('a, 'k) parser -> ('b, 'k) parser -> ('a * 'b, 'k) parser