Skip to content

Yaml Parser

Spice
import "std/text/yaml-parser";

YamlParser struct

Recursive-descent parser for YAML 1.2.

Use parseYaml for one-shot parsing of a single document and parseYamlDocuments for a multi-document stream; the struct is exposed mainly so the parser state (position, error message) can be inspected when needed.

Supported are comments, block mappings and block sequences with the usual indentation rules, flow sequences ([a, b]) and flow mappings ({a: 1}), plain, single-quoted and double-quoted scalars, literal (|) and folded (>) block scalars including their chomping and indentation indicators, anchors (&name) and aliases (*name), document markers (--- and ...) and directives (%YAML), which are skipped.

Plain scalars are resolved according to the YAML 1.2 core schema, so null, ~ and the empty scalar become a null node, true/false become booleans, decimal, 0x and 0o literals become integers, and numbers with a fractional or exponent part as well as .inf and .nan become floats. Everything else stays a string, and quoted scalars are always strings. Mapping keys are always exposed as their literal text.

Deliberately not supported, since they hardly ever appear in configuration files and each of them would need a data model of its own: explicit tags (!!str), explicit key indicators (? key), merge keys (<<), sets (?), anchors inside flow collections, quoted scalars spanning multiple lines and --- key: value with block content on the marker line. An alias resolves to a deep copy of its anchor, so the resulting tree never shares nodes and can be released with a single deleteYamlValue call.

The YamlValue data model lives in "std/text/yaml/yaml-value" and serialization in "std/text/yaml/yaml-serializer".

Constructors

ctor

Spice
public p YamlParser.ctor(const String& input)

Construct a YAML parser over the given input string

Parameters

Name Type Description
input const String& YAML text to parse

dtor

Spice
public p YamlParser.dtor()

Destruct the YAML parser, releasing the bookkeeping it built up while parsing

Methods

parse

Spice
public f<Result<YamlValue*>> YamlParser.parse()

Parse the input string into a YAML node tree, expecting exactly one document

Returns: Result<YamlValue*> — Result holding the root node, or an error if the input is malformed

parseAll

Spice
public f<Result<YamlValue*>> YamlParser.parseAll()

Parse the input string into a sequence of YAML node trees, one per document

Returns: Result<YamlValue*> — Result holding a sequence node with one item per document, or an error

Functions

parseYaml

Spice
public f<Result<YamlValue*>> parseYaml(const String& input)

Parse a YAML document.

On success returns the root node, which the caller owns and releases again with deleteYamlValue from "std/text/yaml/yaml-value". On failure the partially built tree is released by the parser and an error describing the first problem encountered is returned. An input that holds more than one document is rejected; use parseYamlDocuments for those.

Parameters

Name Type Description
input const String&

Returns: Result<YamlValue*>

parseYamlDocuments

Spice
public f<Result<YamlValue*>> parseYamlDocuments(const String& input)

Parse a multi-document YAML stream.

On success returns a sequence node holding one item per document, which the caller owns and releases again with deleteYamlValue. An empty stream yields an empty sequence.

Parameters

Name Type Description
input const String&

Returns: Result<YamlValue*>