YAML 1.2 parser in pure Erlang
Go to file
Jean-Sébastien Pédron 9d54432344
yamerl.app.src: Do not use non-US-ASCII characters...
It breaks publishing to Hex.pm.
2016-07-20 09:49:20 +02:00
debian Update copyright everywhere 2016-07-20 09:10:22 +02:00
doc Document how to run the testsuite 2014-07-15 16:41:26 +02:00
ebin Fix generation of yamerl.app with nawk(1) 2014-08-06 11:27:29 +02:00
include Update copyright everywhere 2016-07-20 09:10:22 +02:00
m4 Initial version of a YAML 1.2 parser 2011-03-07 11:43:52 +01:00
src yamerl.app.src: Do not use non-US-ASCII characters... 2016-07-20 09:49:20 +02:00
testsuite Update copyright everywhere 2016-07-20 09:10:22 +02:00
.gitignore Git: Ignore rebar.lock 2016-07-20 09:25:12 +02:00
.travis.yml Travis CI: Drop Erlang R14B 2016-07-20 08:52:35 +02:00
AUTHORS AUTHORS: Add Eric sagnes 2015-03-28 01:51:02 +01:00
configure.ac Prepare release 0.3.2-1 2015-03-28 01:46:59 +01:00
COPYING Update copyright everywhere 2016-07-20 09:10:22 +02:00
Makefile.am Distribute README.md, AUTHORS and COPYING 2012-12-12 15:38:35 +01:00
package.exs Update Hex metadata 2016-07-20 09:23:59 +02:00
README.md fix minor nits 2015-01-15 20:23:53 -08:00
rebar.config rebar: Support "rebar eunit" to run the testsuite 2014-07-15 14:13:13 +02:00

yamerl: YAML 1.2 parser in Erlang

YAML is a human-friendly data serialization format. The specification for this language and many examples are available from the Official YAML web site. You may also want to check the YAML Wikipedia article.

yamerl is a pure Erlang application which is able to parse YAML 1.1 and YAML 1.2 documents, as well as JSON documents. It only depends on standard Erlang/OTP applications; no external dependency is required. It doesn't use native code either (neither port drivers nor NIFs).

yamerl is distributed under the terms of the 2-clause BSD license; see COPYING.

Build Status

Installation

Rebar

If you use rebar, you can run the following command to build the application:

rebar compile

Autotools

If you use the Autotools and make(1), run the following commands to build the application:

# Generate Autotools files.
autoreconf -vif

# Build the application.
./configure
make

# Install it.
sudo make install

The default installation path is your Erlang's distribution libraries directory (see code:lib_dir()).

Getting started

Before using yamerl, the application must be started:

application:start(yamerl).

Now, one can use the yamerl_constr module to parse and construct a list of documents from:

  • an in-memory document (string or binary);
  • a file;
  • a stream.

Because a YAML input stream may contain multiple documents, yamerl_constr always returns a list of documents, even if the input stream only contains one.

Parsing an in-memory document

yamerl_constr:string("Hello World!").
% List of documents; here, only one document.
[
 % Document #1; contains a single scalar.
 "Hello World!"
]

Here, the returned value is a list of documents containing one document. This document has a scalar as its sole node.

Parsing a file

Considering the following YAML file:

# applications.yaml
- application: kernel
  version:     2.15.3
  path:        /usr/local/lib/erlang/lib/kernel-2.15.3
- application: stdlib
  version:     1.18.3
  path:        /usr/local/lib/erlang/lib/stdlib-1.18.3
- application: sasl
  version:     2.2.1
  path:        /usr/local/lib/erlang/lib/sasl-2.2.1
yamerl_constr:file("applications.yaml").
% List of documents; again, only one document here.
[
 % List of mappings.
 [
  % Mapping, represented as a proplist: each entry has the form {Key, Value}.
  [
   {"application", "kernel"},
   {"version", "2.15.3"},
   {"path", "/usr/local/lib/erlang/lib/kernel-2.15.3"}
  ], [
   {"application", "stdlib"},
   {"version", "1.18.3"},
   {"path", "/usr/local/lib/erlang/lib/stdlib-1.18.3"}
  ], [
   {"application", "sasl"},
   {"version", "2.2.1"},
   {"path", "/usr/local/lib/erlang/lib/sasl-2.2.1"}
  ]
 ]
]

Parsing a stream

The developer is responsible for reading the stream and provide the chunks to yamerl.

% Initialize a new construction state. It takes a term describing the
% source; it may be any Erlang term.
Parser0 = yamerl_constr:new({file, "<stdin>"}),

% Read chunks and feed the parser. A new parser state is returned.
{continue, Parser1} = yamerl_constr:next_chunk(Parser0, Chunk1),
% ...
{continue, Parser2} = yamerl_constr:next_chunk(Parser1, Chunk2),

% When the stream ends, tell the parser it's the last chunk.
Documents = yamerl_constr:last_chunk(Parser2, Chunk3).

Simple vs. full document structures

yamerl_constr comes with two built-in modes:

  • It can output simple documents, eg. documents based on basic Erlang structures (strings, numbers, lists, proplists). This is the default mode.
  • It can output detailed documents using records. These records carry more information such as line/column, tag URI, YAML node type, module used to construct it, etc.

If we use the following YAML document:

# system.yaml
- os: FreeBSD
  version: 9.0-RELEASE-p3

Simple documents:

yamerl_constr:file("system.yaml").
% List of documents.
[
 % List of mappings.
 [
  % Mapping with two entries.
  [
   {"os", "FreeBSD"},
   {"version","9.0-RELEASE-p3"}
  ]
 ]
]

Full documents:

yamerl_constr:file("system.yaml", [{detailed_constr, true}]).
% List of documents.
[
 % Document with a list as its root node.
 {yamerl_doc,
  {yamerl_seq, yamerl_node_seq, "tag:yaml.org,2002:seq", [{line, 2}, {column, 1}], [
   % Mapping #1.
   {yamerl_map, yamerl_node_map, "tag:yaml.org,2002:map", [{line, 2}, {column, 3}], [
    {
     % Mapping entry #1.
     {yamerl_str, yamerl_node_str, "tag:yaml.org,2002:str", [{line, 2}, {column, 3}], "os"},
     {yamerl_str, yamerl_node_str, "tag:yaml.org,2002:str", [{line, 2}, {column, 7}], "FreeBSD"}
    }, {
     % Mapping entry #2.
     {yamerl_str, yamerl_node_str, "tag:yaml.org,2002:str", [{line, 3}, {column, 3}], "version"},
     {yamerl_str, yamerl_node_str, "tag:yaml.org,2002:str", [{line, 3}, {column, 12}], "9.0-RELEASE-p3"}
    }
   ]}
  ],
  1}
 }
]

Complete documentation

See the doc subdirectory for a complete user guide and reference manual.