Skip to content

Yaml Value

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

YamlValue struct

Represents a single YAML node.

Composite nodes (sequences and mappings) own their children as heap pointers, so a YamlValue is always reached via YamlValue* once it has been parsed. Destroying a node recursively destroys everything below it, so a whole tree is released with a single deleteYamlValue call on its root. Accessors like getField hand out borrowed pointers that stay owned by their parent, while addSequenceItem and addMappingField take ownership of what they are given.

Note that the root cannot simply be held in a heap YamlValue* and left to the compiler: the heap qualifier only emits a raw deallocation when the variable goes out of scope and never runs the destructor of the pointee, so the children below the root would be lost.

Following the YAML 1.2 core schema, plain scalars resolve to null, booleans, integers or floats and everything else stays a string, while quoted scalars are always strings. Mapping entries preserve document order, and unlike TOML tables a mapping may hold an explicit null value.

Parsing lives in "std/text/yaml/yaml-parser" and serialization in "std/text/yaml/yaml-serializer", so programs only pull in what they use.

Constructors

ctor

Spice
public p YamlValue.ctor()

Construct an empty YAML mapping, which is what a YAML document is usually rooted at

ctor

Spice
public p YamlValue.ctor(YamlValueKind kind)

Construct an empty YAML node of the given kind. Useful for building sequences and mappings from scratch.

Parameters

Name Type Description
kind YamlValueKind Kind of the node

ctor

Spice
public p YamlValue.ctor(const String& value)

Construct a YAML string node

Parameters

Name Type Description
value const String& String value

ctor

Spice
public p YamlValue.ctor(bool value)

Construct a YAML boolean node

Parameters

Name Type Description
value bool Boolean value

ctor

Spice
public p YamlValue.ctor(long value)

Construct a YAML integer node

Parameters

Name Type Description
value long Integer value

ctor

Spice
public p YamlValue.ctor(double value)

Construct a YAML float node

Parameters

Name Type Description
value double Float value

dtor

Spice
public p YamlValue.dtor()

Destruct the YAML node, releasing every sequence item and mapping entry it owns. Since the children are destroyed the same way, this tears down the whole subtree below the node.

Methods

getKind

Spice
public f<YamlValueKind> YamlValue.getKind()

Retrieve the kind of the YAML node

Returns: YamlValueKind — Kind of the node

isNull

Spice
public f<bool> YamlValue.isNull()

Check whether the YAML node is null

Returns: bool — true if the node is null, false otherwise

isString

Spice
public f<bool> YamlValue.isString()

Check whether the YAML node is a string

Returns: bool — true if the node is a string, false otherwise

isInteger

Spice
public f<bool> YamlValue.isInteger()

Check whether the YAML node is an integer

Returns: bool — true if the node is an integer, false otherwise

isFloat

Spice
public f<bool> YamlValue.isFloat()

Check whether the YAML node is a float

Returns: bool — true if the node is a float, false otherwise

isBool

Spice
public f<bool> YamlValue.isBool()

Check whether the YAML node is a boolean

Returns: bool — true if the node is a boolean, false otherwise

isSequence

Spice
public f<bool> YamlValue.isSequence()

Check whether the YAML node is a sequence

Returns: bool — true if the node is a sequence, false otherwise

isMapping

Spice
public f<bool> YamlValue.isMapping()

Check whether the YAML node is a mapping

Returns: bool — true if the node is a mapping, false otherwise

getString

Spice
public f<const String&> YamlValue.getString()

Retrieve the string payload. Panics if the node is not a string.

Returns: const String& — String value

getInteger

Spice
public f<long> YamlValue.getInteger()

Retrieve the integer payload. Panics if the node is not an integer.

Returns: long — Integer value

getFloat

Spice
public f<double> YamlValue.getFloat()

Retrieve the float payload. Panics if the node is not a float.

Returns: double — Float value

getBool

Spice
public f<bool> YamlValue.getBool()

Retrieve the boolean payload. Panics if the node is not a boolean.

Returns: bool — Boolean value

getSequenceSize

Spice
public f<unsigned long> YamlValue.getSequenceSize()

Retrieve the number of items in the sequence. Panics if the node is not a sequence.

Returns: unsigned long — Number of sequence items

getSequenceItem

Spice
public f<YamlValue*> YamlValue.getSequenceItem(unsigned long idx)

Retrieve the sequence item at the given index. Panics if the node is not a sequence.

Parameters

Name Type Description
idx unsigned long Index of the item

Returns: YamlValue* — Pointer to the sequence item

getSequenceItem

Spice
public f<YamlValue*> YamlValue.getSequenceItem(unsigned int idx)

Retrieve the sequence item at the given index. Panics if the node is not a sequence.

Parameters

Name Type Description
idx unsigned int Index of the item

Returns: YamlValue* — Pointer to the sequence item

addSequenceItem

Spice
public p YamlValue.addSequenceItem(YamlValue* item)

Append an item to the sequence. Panics if the node is not a sequence. The sequence takes ownership of the heap-allocated item.

Parameters

Name Type Description
item YamlValue* Item to append

getMappingSize

Spice
public f<unsigned long> YamlValue.getMappingSize()

Retrieve the number of entries in the mapping. Panics if the node is not a mapping.

Returns: unsigned long — Number of mapping entries

getMappingKey

Spice
public f<const String&> YamlValue.getMappingKey(unsigned long idx)

Retrieve the key of the mapping entry at the given index, in document order. Panics if the node is not a mapping.

Parameters

Name Type Description
idx unsigned long Index of the entry

Returns: const String& — Entry key

getMappingValue

Spice
public f<YamlValue*> YamlValue.getMappingValue(unsigned long idx)

Retrieve the value of the mapping entry at the given index, in document order. Panics if the node is not a mapping.

Parameters

Name Type Description
idx unsigned long Index of the entry

Returns: YamlValue* — Pointer to the entry value

hasField

Spice
public f<bool> YamlValue.hasField(const String& key)

Check whether the mapping contains an entry with the given key

Parameters

Name Type Description
key const String& Entry key to look for

Returns: bool — true if the entry exists, false otherwise

hasField

Spice
public f<bool> YamlValue.hasField(string key)

Check whether the mapping contains an entry with the given key

Parameters

Name Type Description
key string Entry key to look for

Returns: bool — true if the entry exists, false otherwise

getFieldOrNil

Spice
public f<YamlValue*> YamlValue.getFieldOrNil(const String& key)

Retrieve the value of the mapping entry with the given key, or nil if the node is not a mapping or the entry does not exist.

Parameters

Name Type Description
key const String& Entry key to look up

Returns: YamlValue* — Pointer to the entry value, or nil

getFieldOrNil

Spice
public f<YamlValue*> YamlValue.getFieldOrNil(string key)

Retrieve the value of the mapping entry with the given key, or nil if the node is not a mapping or the entry does not exist.

Parameters

Name Type Description
key string Entry key to look up

Returns: YamlValue* — Pointer to the entry value, or nil

getField

Spice
public f<YamlValue*> YamlValue.getField(const String& key)

Retrieve the value of the mapping entry with the given key. Panics if the node is not a mapping or the entry does not exist.

Parameters

Name Type Description
key const String& Entry key to look up

Returns: YamlValue* — Pointer to the entry value

getField

Spice
public f<YamlValue*> YamlValue.getField(string key)

Retrieve the value of the mapping entry with the given key. Panics if the node is not a mapping or the entry does not exist.

Parameters

Name Type Description
key string Entry key to look up

Returns: YamlValue* — Pointer to the entry value

addMappingField

Spice
public p YamlValue.addMappingField(const String& key, YamlValue* value)

Append an entry to the mapping. Panics if the node is not a mapping. The mapping takes ownership of the heap-allocated entry value.

Parameters

Name Type Description
key const String& Entry key
value YamlValue* Entry value

Functions

deleteYamlValue

Spice
public p deleteYamlValue(YamlValue*& value)

Destroys a heap-allocated YAML node together with everything it owns and sets the given handle to nil, so releasing a whole tree only takes one call on its root.

Note that this is what sDelete would normally be used for. It cannot be used here, because a node tree is destroyed recursively and the compiler does not emit an explicit dtor that is only reached through sDestruct.

Parameters

Name Type Description
value YamlValue*& Handle of the node to destroy

copyYamlValue

Spice
public f<YamlValue*> copyYamlValue(YamlValue* value)

Creates a deep copy of the given node, which the caller owns and releases again with deleteYamlValue. This is what the parser uses to resolve an alias against its anchor, so that the resulting tree stays free of shared nodes.

Parameters

Name Type Description
value YamlValue* Node to copy

Returns: YamlValue* — Pointer to the freshly allocated copy

yamlPlainScalarKind

Spice
public f<YamlValueKind> yamlPlainScalarKind(const String& text)

Determine which kind a plain scalar resolves to under the YAML 1.2 core schema. Both the parser and the serializer go through this, so that a string which would be read back as another kind can be quoted when it is written out again.

Parameters

Name Type Description
text const String& Literal text of the plain scalar

Returns: YamlValueKind — Kind the scalar resolves to, never YAML_SEQUENCE or YAML_MAPPING

resolveYamlScalar

Spice
public f<YamlValue*> resolveYamlScalar(const String& text)

Build the node a plain scalar denotes under the YAML 1.2 core schema. The caller owns the returned node and releases it again with deleteYamlValue.

Parameters

Name Type Description
text const String& Literal text of the plain scalar

Returns: YamlValue* — Pointer to the freshly allocated node

yamlInfinity

Spice
public f<double> yamlInfinity()

Retrieve positive infinity, which YAML spells .inf. Spice has no literal for it, so it is provided here for building and inspecting float values.

Returns: double — Positive infinity

yamlNaN

Spice
public f<double> yamlNaN()

Retrieve a quiet NaN, which YAML spells .nan. Spice has no literal for it, so it is provided here for building float values.

Returns: double — Not a number

yamlIsNaN

Spice
public f<bool> yamlIsNaN(double value)

Checks whether the given float is a NaN. The usual value != value trick does not work in Spice, since its float comparisons are ordered ones.

Parameters

Name Type Description
value double Float value to check

Returns: bool — true if the value is a NaN, false otherwise

YamlValueKind enum

Tag for the variant stored inside a YamlValue.

Item Value Description
YAML_NULL
YAML_STRING
YAML_INTEGER
YAML_FLOAT
YAML_BOOL
YAML_SEQUENCE
YAML_MAPPING