yaml
The yaml library provides predicates for parsing and generating data
in the YAML format:
It includes parsing and generation of simple scalar values, lists, mappings, and nested structures.
API documentation
Open the ../../apis/library_index.html#yaml link in a web browser.
Loading
To load all entities in this library, load the loader.lgt file:
| ?- logtalk_load(yaml(loader)).
Testing
To test this library predicates, load the tester.lgt file:
| ?- logtalk_load(yaml(tester)).
API
Parsing
The parse/2 predicate parses YAML content from various sources into
a structured Logtalk term representation.
parse(+Source, -YAML)
Sources can be:
file(Path)- parse YAML from a filestream(Stream)- parse YAML from an open streamcodes(Codes)- parse YAML from a list of character codeschars(Chars)- parse YAML from a list of charactersatom(Atom)- parse YAML from an atom
The parsed YAML is returned as a compound term with the functor
yaml/1.
Generating
The generate/2 predicate generates YAML output from a structured
term.
generate(+Sink, +YAML)
Sinks can be:
file(Path)- write YAML to a filestream(Stream)- write YAML to an open streamcodes(Codes)- get YAML as a list of character codeschars(Chars)- get YAML as a list of charactersatom(Atom)- get YAML as an atom
The input must be a compound term representing YAML data (e.g.,
yaml([...]) for mappings, [...] for sequences).
Multi-Document Parsing
The parse_all/2 predicate parses all YAML documents from a source
into a list.
parse_all(+Source, -YAMLs)
Documents are separated by --- markers and optionally terminated by
... markers. Returns a list of YAML terms, even for single-document
sources.
Multi-Document Generation
The generate_all/2 predicate generates YAML output with multiple
documents.
generate_all(+Sink, +YAMLs)
Takes a list of YAML terms and generates output with --- separators
between documents. For a single document, no separator is added.
Data Representation
YAML data is represented using the following mappings:
YAML Value |
Logtalk Representation |
|---|---|
|
|
|
|
|
|
Number (42) |
|
String (“text”) |
|
Array/List |
|
Mapping/Object |
|
Examples
Parsing a Simple YAML String
?- parse(atom('name: John\nage: 30'), YAML).
YAML = yaml([name-'John', age-30]).
Parsing a YAML List
?- parse(codes([0'-, 0' , 0'a, ...]), YAML).
YAML = yaml([apple, banana, orange]).
Parsing a YAML File
?- parse(file('config.yaml'), YAML).
% Reads and parses config.yaml
Generating YAML from a Term
?- generate(atom(Result), yaml([title-'Project', version-1])).
Result = '{title:Project,version:1}'.
Generating YAML to a File
?- Data = yaml([name-'Alice', score-95]),
generate(file('output.yaml'), Data).
% Writes YAML data to output.yaml
Working with Nested Structures
?- YAML = yaml([
person-yaml([
name-'Bob',
age-25,
skills-[logtalk, prolog, python]
])
]),
generate(atom(Result), YAML).
% Generates YAML representation of nested structure
Features
Scalar values: Supports atoms, numbers, booleans, and null
Collections: Supports lists (sequences) and mappings (objects)
Nested structures: Fully supports nested mappings and sequences
Comments: Handles YAML comments (lines starting with #)
Multiple source formats: Parse from files, streams, codes, chars, or atoms
Multiple output formats: Generate to files, streams, codes, chars, or atoms
Error handling: Provides appropriate error messages for invalid input
Supported YAML Features
The library currently supports a subset of YAML 1.2 features suitable for common configuration and data serialization tasks:
Scalars
Strings: unquoted, single-quoted (
'...'), and double-quoted ("...")Multi-line flow scalars: quoted strings spanning multiple lines with proper line folding
Single newline becomes a space
Blank lines (consecutive newlines) become literal newlines
Leading/trailing whitespace around newlines is trimmed
Multi-word keys: keys containing spaces (e.g.,
Mark McGwire: 65)Numbers:
Integers: decimal (
42), signed positive (+12345), octal (0o14), hexadecimal (0xC)Floats: decimal (
3.14), scientific notation (1.23e+3)Special values:
.inf,-.inf,.nan(represented as'@'(inf),'@'(-inf),'@'(nan))
Booleans:
trueandfalseNull:
nullor empty valuesTimestamps: ISO 8601 dates and times (parsed as strings)
Collections
Block sequences: using
-indicator with proper indentationFlow sequences: using
[ ]syntax with comma separationBlock mappings: using
key: valuesyntax with proper indentationFlow mappings: using
{ key: value }syntaxNested structures: arbitrary nesting of sequences and mappings
Trailing commas: supported in flow collections
Block Scalars
Literal block scalars:
|style preserves newlines exactlyFolded block scalars:
>style folds newlines into spacesChomping indicators:
-(strip),+(keep), default (clip) for trailing newlinesMore-indented lines: preserved with original indentation in folded scalars
Blank lines: preserved in both literal and folded block scalars
Anchors and Aliases
Anchor definitions:
&nameto define a reusable nodeAlias references:
*nameto reference a previously defined anchorMerge key:
<<to merge mappings from aliasesWorks with scalars, sequences, and mappings
Document Structure
Document start marker:
---is recognized and skippedDocument end marker:
...is recognized and handledMulti-document streams: multiple
---or...separated documents (viaparse_all/2)Comments: lines starting with
#and inline comments after valuesTags:
!tag,!!type, and!<uri>tags are recognized and skipped
Escape Sequences (in double-quoted strings)
\\- backslash\"- double quote\/- forward slash\n- newline\t- tab\r- carriage return\b- backspace\f- form feed\0- null character\xXX- hex escape (2 hex digits)\uXXXX- Unicode escape (4 hex digits)
Limitations
The following YAML features are not currently supported:
Tag interpretation: Tags are recognized and skipped, but not interpreted
Complex keys:
?indicator for multi-line or complex keysMulti-line plain scalars: plain scalars spanning multiple lines
Aliases in flow sequences:
[*alias1, *alias2](aliases work in block context)Merge with list of aliases:
<<: [*a, *b](single alias merge works)Directives:
%YAMLand%TAGdirectives
Error Handling
The library raises the following errors:
instantiation_error- when a required argument is a variabledomain_error(yaml_source, Source)- when an invalid source is provideddomain_error(yaml_sink, Sink)- when an invalid sink is provideddomain_error(yaml_term, Term)- when an invalid YAML term is provided
File Organization
yaml_protocol.lgt- Defines the protocol for YAML parser/generatoryaml.lgt- Main implementation objectloader.lgt- Library loadertester.lgt- Test runnertest_files/- Directory containing test files and sample YAML files for testing
Test Files
The library includes several sample YAML files for testing:
simple.yaml- Simple key-value pairslist.yaml- A YAML listnested.yaml- Nested structurescomplex.yaml- Lists of mappingsconfig.yaml- Configuration file examplemetadata.yaml- Metadata example with quoted strings
Performance Considerations
Parsing is done recursively, which may affect performance with deeply nested structures
Large YAML files are processed in memory
For very large files, consider splitting them into smaller chunks
Integration with Other Libraries
The YAML library can be integrated with other Logtalk libraries:
Use with
oslibrary for file path handlingUse with
readerlibrary for stream managementCompatible with
term_iolibrary for term serialization
Future Enhancements
Potential future enhancements may include:
Support for tag interpretation (e.g.,
!!binaryfor base64 decoding)Support for complex keys using
?indicatorSupport for multi-line plain scalars
Aliases in flow sequences
Merge key with list of aliases
Parametric objects for custom representation choices
See Also
json_lineslibrary for JSON Lines format parsing and generationLogtalk documentation on DCGs for grammar rules
YAML specification: https://yaml.org