YAMLxYAMLx — pure-OCaml YAML 1.2 parser.
Typical usage:
(* Read a single-document config file — most common pattern *)
match YAMLx.Value.of_yaml_file "config.yaml" with
| Ok value -> (* process value *)
| Error msg -> (* handle error *)
(* Parse a YAML string into a single typed value — raising variant *)
let value = YAMLx.Value.of_yaml_exn "answer: 42\nflag: true"
(* Multi-document YAML stream *)
match YAMLx.Values.of_yaml "doc1\n---\ndoc2" with
| Ok docs -> (* process docs *)
| Error msg -> (* handle error *)
(* Serialize typed values back to YAML *)
let yaml = YAMLx.Values.to_yaml docs
(* Parse preserving the full AST (tags, anchors, positions) *)
let nodes = YAMLx.Nodes.of_yaml_exn "- foo\n- bar"
(* Serialize nodes back to YAML *)
let yaml = YAMLx.Nodes.to_yaml nodesAll errors are reported by raising Error. Use Value.of_yaml or Value.of_yaml_file to get a result instead of raising.
type pos = {line : int;column : int;0-based Unicode codepoint column
*)column_bytes : int;0-based UTF-8 byte column
*)offset : int;codepoint index from the start of the input
*)offset_bytes : int;UTF-8 byte offset from the start of the input
*)}A location in the YAML source text. line is 1-based. column and column_bytes are 0-based distances from the start of the line, in codepoints and UTF-8 bytes respectively. offset and offset_bytes are absolute distances from the start of the input, measured the same way. The byte fields make it easy to slice the original string without re-encoding.
val pp_pos :
Ppx_deriving_runtime.Format.formatter ->
pos ->
Ppx_deriving_runtime.unitval show_pos : pos -> Ppx_deriving_runtime.stringA source range. start_pos is the first character of the node; end_pos is the position immediately after the last character.
val pp_loc :
Ppx_deriving_runtime.Format.formatter ->
loc ->
Ppx_deriving_runtime.unitval show_loc : loc -> Ppx_deriving_runtime.stringval zero_pos : posThe position at the very start of an empty input: line=1, column=0, offset=0, all byte fields 0. Useful for constructing nodes programmatically when source positions are not meaningful.
val zero_loc : locA zero-length location at the start of an empty input: both start_pos and end_pos equal zero_pos. Useful for constructing nodes programmatically when source locations are not meaningful.
type schema = | Yaml_1_2| Yaml_1_1YAML schema used to resolve untagged plain scalars to typed values.
Yaml_1_2 (the default): YAML 1.2 JSON schema. Booleans are only true/false; octal uses 0o… prefix; sexagesimal not recognised.Yaml_1_1: YAML 1.1 schema. Extended booleans (yes/no/on/off etc.), 0…-prefixed octal, sexagesimal integers and floats, and merge-key (<<) expansion. Use this to read legacy YAML files.New projects should use YAML 1.2. Pass ~schema:Yaml_1_1 to Values.of_yaml_exn (and friends) when reading older files. A %YAML 1.1 or %YAML 1.2 directive in the stream selects the schema automatically for that document (use ~strict_schema:true to make a mismatch an error instead).
type error = | Scan_error of yaml_errorInvalid character sequence or encoding error detected by the scanner. Carries a position.
*)| Parse_error of yaml_errorWell-formed tokens in an invalid order detected by the parser. Carries a position.
*)| Expansion_limit_exceeded of intAlias expansion visited more nodes than the configured limit. The payload is the limit that was exceeded. See default_expansion_limit.
| Depth_limit_exceeded of intYAML nesting depth exceeded the configured maximum during composition. The payload is the limit that was exceeded. See default_max_depth.
| Printer_error of stringA feature unsupported by the plain-YAML printer was encountered (e.g. a tag, a complex mapping key).
*)| Document_count_error of stringThe input contained the wrong number of documents for a single-document operation.
*)| Schema_error of yaml_errorA schema conflict: the document's %YAML directive disagrees with the requested schema (when ~strict_schema:true), or a plain scalar is ambiguous between YAML 1.1 and 1.2 (when ~reject_ambiguous:true).
| Simplicity_error of yaml_errorA YAML feature not allowed in plain mode was encountered: an anchor, alias, explicit tag, or (in YAML 1.1 mode) a merge key (<<). Raised when ~plain:true is passed to Values functions.
| Duplicate_key_error of yaml_errorA mapping contains a duplicate key. Raised when ~strict_keys:true is passed to Values functions. The location points to the second (duplicate) occurrence.
| Cycle_error of yaml_errorA cyclic alias was encountered during value resolution. The YAML structure is valid (e.g. &doc {a: *doc}) but cannot be represented as a finite value tree. The location points to the alias that closes the cycle.
exception Error of errorThe single exception raised by this library. Match on the payload to distinguish error kinds:
match YAMLx.Nodes.of_yaml_exn input with
| nodes -> ...
| exception YAMLx.Error (YAMLx.Scan_error e) -> ...
| exception YAMLx.Error (YAMLx.Parse_error e) -> ...
| exception YAMLx.Error (YAMLx.Depth_limit_exceeded n) -> ...
| exception YAMLx.Error _ -> ...val format_loc : ?file:string -> loc -> stringDefault location formatter used by catch_errors and register_exception_printers.
The output format depends on the extent of loc:
"line 3, column 8""line 3, columns 8-11""lines 3-12, columns 8-4"When ~file is given, a "file <name>, " prefix is prepended, e.g. "file foo.yaml, line 3, columns 8-11".
Columns are 0-based Unicode codepoint offsets from the start of the line, matching the pos fields column (not column_bytes).
val default_format_loc : ?file:string -> loc -> stringval catch_errors :
?file:string ->
?format_loc:(?file:string -> loc -> string) ->
(unit -> 'a) ->
('a, string) Stdlib.resultCatch Error and return Ok _ or Error msg.
When ~file is given it is prepended to every error message: positional errors (scan/parse/schema) become "file foo.yaml, line L, columns C1-C2: msg" and non-positional errors become "file foo.yaml: msg".
~format_loc overrides how source locations are formatted (default: format_loc). Provide a custom implementation to adapt the output for editors, LSP servers, or structured logging.
val register_exception_printers :
?format_loc:(?file:string -> loc -> string) ->
unit ->
unitRegister a printer for Error so it displays legibly in uncaught-exception output. ~format_loc overrides location formatting (default: default_format_loc).
val show_yaml_error :
?format_loc:(?file:string -> loc -> string) ->
yaml_error ->
stringFormat a yaml_error as "location: message". Uses default_format_loc by default; pass ~format_loc to customise location formatting.
How a scalar value was written in the source. Preserved in AST nodes so callers can distinguish, for example, a quoted empty string from an unquoted null.
val pp_scalar_style :
Ppx_deriving_runtime.Format.formatter ->
scalar_style ->
Ppx_deriving_runtime.unitval show_scalar_style : scalar_style -> Ppx_deriving_runtime.stringtype node = | Scalar_node of {anchor : string option;&name if present
tag : string option;resolved tag URI if present
*)value : string;style : scalar_style;loc : loc;height : int;always 1 for scalars
*)head_comments : string list;line_comment : string option;foot_comments : string list;}| Sequence_node of {anchor : string option;tag : string option;items : node list;flow : bool;true for [a, b] style, false for block
loc : loc;height : int;1 + max height of items, or 1 if empty
*)head_comments : string list;line_comment : string option;foot_comments : string list;}| Mapping_node of {anchor : string option;tag : string option;pairs : (node * node) list;flow : bool;true for {a: b} style, false for block
loc : loc;height : int;1 + max height of keys and values, or 1 if empty
*)head_comments : string list;line_comment : string option;foot_comments : string list;}| Alias_node of {name : string;the anchor name, without the *
resolved : node Stdlib.Lazy.t;The node this alias refers to. Lazy to allow cycles (e.g. a node that contains an alias to itself). Force with Lazy.force when traversing.
loc : loc;height : int;always 1 for alias nodes
*)head_comments : string list;line_comment : string option;foot_comments : string list;}An in-memory representation of a parsed YAML document that preserves all source-level detail: tags, anchors, scalar styles, source positions, and — on a best-effort basis — comments.
Comment preservation is best-effort. Comments inside flow collections are dropped. The attachment rules may change in future versions.
head_comments: standalone comment lines immediately before the node. Each string is one comment line's text, without the leading '#'.line_comment: a comment on the same source line as the node, after its content. Text does not include the leading '#'.foot_comments (collections only): standalone comment lines after the last child of the collection, before the next sibling.val pp_node :
Ppx_deriving_runtime.Format.formatter ->
node ->
Ppx_deriving_runtime.unitval show_node : node -> Ppx_deriving_runtime.stringA YAML value resolved according to the YAML 1.2 JSON schema.
Plain (unquoted) scalars are matched against the following patterns:
null, Null, NULL, ~, or empty string → Nulltrue/True/TRUE/false/False/FALSE → Bool0x… hex, or 0o… octal integers → Int.inf, .nan variants → FloatStringEach constructor carries a loc giving the source range of the corresponding YAML node. Use Value.equal for location-independent structural equality.
pp_value and show_value are derived by @@deriving show and are primarily useful when another type embeds value and also uses @@deriving show. For direct use prefer Value.pp and Value.show.
val pp_value :
Ppx_deriving_runtime.Format.formatter ->
value ->
Ppx_deriving_runtime.unitval show_value : value -> Ppx_deriving_runtime.stringmodule Nodes : sig ... endOperations on the lossless AST node representation.
module Node : sig ... endOperations on a single lossless AST node.
module Values : sig ... endOperations on typed YAML values for multi-document streams.
module Value : sig ... endSingle-document interface for typed YAML values.