LibYAML

LibYAML is a YAML parser and emitter library.

Download and Installation

The current release of LibYAML: 0.2.5 (2020-06-01).

Download the source package: http://pyyaml.org/download/libyaml/yaml-0.2.5.tar.gz.

To build and install LibYAML, run

$ ./configure
$ make
# make install

You may check out the latest development code of LibYAML from the GitHub repository https://github.com/yaml/libyaml:

$ git clone https://github.com/yaml/libyaml

If you checked out the LibYAML source code from the GitHub repository, you can build LibYAML with the commands:

$ ./bootstrap
$ ./configure
$ make
# make install

Development and Bug Reports

You may check out the LibYAML source code from LibYAML Git repository.

If you find a bug in LibYAML, please file a bug report.

You can discuss LibYAML with the developers on IRC: #libyaml irc.freenode.net

Documentation

Scope

LibYAML covers presenting and parsing processes. Thus LibYAML defines the following two processors:

The processes of parsing and presenting are inverse to each other. Any sequence of events produced by parsing a well-formed YAML document should be acceptable by the Emitter, which should produce an equivalent document. Similarly, any document produced by emitting a sequence of events should be acceptable for the Parser, which should produce an equivalent sequence of events.

The job of resolving implicit tags, composing and serializing representation trees, as well as constructing and representing native objects is left to applications and bindings. Although some of these processes may be covered in the latter releases, they are not in the scope of the initial release of LibYAML.

Events

Event Types

The Parser produces while the Emitter accepts the following types of events:

A valid sequence of events should obey the grammar:

stream ::= STREAM-START document* STREAM-END
document ::= DOCUMENT-START node DOCUMENT-END
node ::= ALIAS | SCALAR | sequence | mapping
sequence ::= SEQUENCE-START node* SEQUENCE-END
mapping ::= MAPPING-START (node node)* MAPPING-END

Essential Event Attributes

The following attributes affect the intepretation of a YAML document.

Stylistic Event Attributes

The following attributes don’t affect the interpretation of a YAML document. While parsing a YAML document, an application should not consider these attributes for resolving implicit tags and constructing representation graphs or native objects. The Emitter may ignore these attributes if they cannot be satisfied.

API

Note: the API may change drastically. You may also check the header file: https://github.com/yaml/libyaml/blob/master/include/yaml.h

Parser API Synopsis

#include <yaml.h>

yaml_parser_t parser;
yaml_event_t event;

int done = 0;

/* Create the Parser object. */
yaml_parser_initialize(&parser);

/* Set a string input. */
char *input = "...";
size_t length = strlen(input);

yaml_parser_set_input_string(&parser, input, length);

/* Set a file input. */
FILE *input = fopen("...", "rb");

yaml_parser_set_input_file(&parser, input);

/* Set a generic reader. */
void *ext = ...;
int read_handler(void *ext, char *buffer, int size, int *length) {
    /* ... */
    *buffer = ...;
    *length = ...;
    /* ... */
    return error ? 0 : 1;
}

yaml_parser_set_input(&parser, read_handler, ext);

/* Read the event sequence. */
while (!done) {

    /* Get the next event. */
    if (!yaml_parser_parse(&parser, &event))
        goto error;

    /*
      ...
      Process the event.
      ...
    */

    /* Are we finished? */
    done = (event.type == YAML_STREAM_END_EVENT);

    /* The application is responsible for destroying the event object. */
    yaml_event_delete(&event);

}

/* Destroy the Parser object. */
yaml_parser_delete(&parser);

return 1;

/* On error. */
error:

/* Destroy the Parser object. */
yaml_parser_delete(&parser);

return 0;

Emitter API Synopsis

#include <yaml.h>

yaml_emitter_t emitter;
yaml_event_t event;

/* Create the Emitter object. */
yaml_emitter_initialize(&emitter);

/* Set a file output. */
FILE *output = fopen("...", "wb");

yaml_emitter_set_output_file(&emitter, output);

/* Set a generic writer. */
void *ext = ...;
int write_handler(void *ext, char *buffer, int size) {
    /*
       ...
       Write `size` bytes.
       ...
    */
    return error ? 0 : 1;
}

yaml_emitter_set_output(&emitter, write_handler, ext);

/* Create and emit the STREAM-START event. */
yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
if (!yaml_emitter_emit(&emitter, &event))
    goto error;

/*
  ...
  Emit more events.
  ...
*/

/* Create and emit the STREAM-END event. */
yaml_stream_end_event_initialize(&event);
if (!yaml_emitter_emit(&emitter, &event))
    goto error;

/* Destroy the Emitter object. */
yaml_emitter_delete(&emitter);

return 1;

/* On error. */
error:

/* Destroy the Emitter object. */
yaml_emitter_delete(emitter);

return 0;

Examples

You may check out tests and examples and in the source distribution.

Copyright (c) 2017-2020 Ingy döt Net Copyright (c) 2006-2016 Kirill Simonov

The LibYAML library was written by Kirill Simonov. It is now maintained by the YAML community.

LibYAML is released under the MIT license.

This project was developed for Python Software Foundation as a part of Google Summer of Code under the mentorship of Clark Evans.