ASDF.YAML (asdf v0.1.0)

Copy Markdown View Source

One YAML 1.1 document, with tags kept as strings.

Tokens come from yamerl_parser. This module builds the tree itself so an unrecognized tag cannot select a constructor module, and so file text is never turned into an atom. Aliases share the anchored term. A wide alias list still counts against max_aliases.

Mapping keys must be booleans, integers, or strings. That is the ASDF YAML subset. Merge keys (<<) are ordinary keys: this parser does not expand them. The subset chapter we could retrieve does not require expansion.

Summary

Types

How duplicate mapping keys are handled.

t()

Parser state while one document is being built. parse/2 does not return this struct.

Functions

Parses one ASDF tree document.

Types

mode()

@type mode() :: :strict | :permissive

How duplicate mapping keys are handled.

:strict returns reason: :duplicate_key. :permissive keeps the last value and adds an %ASDF.Warning{}.

t()

@type t() :: %ASDF.YAML{
  aliases: non_neg_integer(),
  anchors: %{optional(String.t()) => term()},
  docs: non_neg_integer(),
  limits: ASDF.Limits.t() | nil,
  mode: mode(),
  nodes: non_neg_integer(),
  pending_anchor: String.t() | nil,
  stack: list(),
  warnings: [ASDF.Warning.t()]
}

Parser state while one document is being built. parse/2 does not return this struct.

  • :stack — open collections, outermost last
  • :anchors — anchor name to the anchored term
  • :pending_anchor — anchor waiting for its node, or nil
  • :aliases — alias nodes seen so far
  • :nodes — nodes seen so far
  • :warnings — permissive-mode recoveries
  • :mode — :strict or :permissive
  • :limits — the %ASDF.Limits{} for this parse
  • :docs — documents started; ASDF allows one

Functions

parse(binary, opts)

@spec parse(binary(), keyword()) ::
  {:ok, term(), [ASDF.Warning.t()]} | {:error, ASDF.Error.t()}

Parses one ASDF tree document.

binary is the tree only, from %YAML or --- through the closing ... line. opts accepts:

  • :limits — an %ASDF.Limits{} or a keyword list of its fields
  • :mode — :strict (default) or :permissive

Returns {:ok, value, warnings}. Tags stay %ASDF.Tagged{}. Mapping keys are booleans, integers, or strings. An empty %YAML 1.1 document returns {:ok, "", []}.

Returns {:error, %ASDF.Error{}} and does not raise. Reasons include :unsupported_yaml_version when the directive is missing or is not 1.1, :yaml_syntax for a token the parser rejects, :duplicate_key in strict mode, :multiple_documents, :unsupported_float for .inf and .nan, and the limit reasons from ASDF.Limits.

Examples

iex> {:ok, value, []} = ASDF.YAML.parse("%YAML 1.1\n---\nhello: world\n...\n", [])
...> value
%{"hello" => "world"}

iex> {:error, error} = ASDF.YAML.parse("---\nhello: world\n...\n", [])
...> error.reason
:unsupported_yaml_version

iex> {:error, error} = ASDF.YAML.parse("%YAML 1.1\n---\nvalue: .inf\n...\n", [])
...> error.reason
:unsupported_float