MarcoPolo.GenericParser

Provides facilities for parsing binary data with support for incomplete data.

This module provides functions for parsing binary data through given parsers (which are just functions). What makes this module useful over manually parsing these data is its declarativeness (you just list what data you expect) as well as its support for incomplete data.

Incomplete data means data that ends before they can be fully parsed. For example, an OrientDB long takes 8 bytes, so if you want to parse a long and the binary contains less than 8 bytes than it's incomplete. Incomplete responses are not handled in this module (which just returns :incomplete when a response is incomplete), but on a higher level (the connection server, which caches the incomplete data until it receives new data, then tries to parse again).

Summary

array_parser(nelems_fn, elem_parsers)

Returns a parser that parses arrays

parse(data, parsers)

Parses data based on the given list of parsers. Returns :incomplete when the data is not enough to satisfy all parsers, {value, rest} otherwise

Types

ok_or_incomplete :: {term, binary} | :incomplete

Type returned by the parsing functions in this module.

parser :: (binary -> ok_or_incomplete)

A basic parser is just a function that takes a binary and returns {value, rest} or :incomplete; a parser can be a basic parser or a more complex parser usually based on basic ones.

Functions

array_parser(nelems_fn, elem_parsers)

Specs:

Returns a parser that parses arrays.

The returned parser will first parse the number of elements in the array from the given binary using the nelems_fn parser; then, it will parse elements using the elem_parsers parsers that number of times.

parse(data, parsers)

Specs:

Parses data based on the given list of parsers. Returns :incomplete when the data is not enough to satisfy all parsers, {value, rest} otherwise.