Metaparse is a compile-time parser generator library. Metaparse provides tools to write parsers parsing the content of string literals at compile-time, which makes it possible to embed domain specific languages (DSLs) into C++ without altering their original syntax (Note that the DSL code snippets will be written in string literals, therefore they may need to be escaped).
Assuming that the following template class is available for representing rational numbers in template metaprogramming:
template <class Num, class Denom> struct rational;
Metaparse can be used to construct such values (instantiate the
rational template class) from string literals.
rational<1, 3> one can write
RATIONAL("1/3") which can be processed by any standard-compliant
C++11 compiler (and mean the same). This can be implemented using Metaparse
the following way:
using namespace boost::metaparse; typedef sequence_apply2< rational, token<int_>, last_of<lit_c<'/'>, token<int_>> > rational_grammar; typedef build_parser<entire_input<rational_grammar>> rational_parser; #define RATIONAL(s) \ (::rational_parser::apply<BOOST_METAPARSE_STRING(s)>::type::run())
Note that this is the entire implementation. Also note that this implementation can be extended to improve the error reports in certain situations.
Metaparse is intended to be used by library authors to make their APIs follow the usual notation of the library's problem domain.
Boost.Proto is a tool for building expression templates. Expression templates can be used for DSL embedding by reinterpreting valid C++ expressions as expressions written in the DSL to embed.
This technique has the advantages over parsing the content of string literals (which is Metaparse's approach) that:
Using expression templates for DSL embedding has the following disadvantages:
Proto helps embedding DSLs based on expression templates, while Metaparse helps embedding DSLs based on parsing the content of string literals.
Spirit is a tool that can be used to build parsers parsing (among others) the content of string literals at runtime, while Metaparse is a tool that can be used to parse the content of string literals at compile-time.
This library is useful to provide an API for C++ libraries dealing with a problem domain with its own notation. Interfaces built with Metaparse make it possible for the users of the interface to use the domain's own notation, which makes it easier to write and maintain the code. Users of the interface don't need to learn a new notation (trying to follow the problem domain's original one) library authors constrained by the C++ syntax can provide. Example problem domains are regular expressions and SQL queries.
Metaparse can also be useful to build libraries validating the content of
string literals at compile time instead of doing it at runtime or not doing
it at all. This can help finding (and fixing) bugs in the code early (during
compilation). An example problem domain is
The parsers built with Metaparse process the content of the string literals using template metaprograms. This impacts the library using Metaparse the following way:
Metaparse is based on C++98. The only exception is the BOOST_METAPARSE_STRING
macro, which needs C++11
Compilers Metaparse is actively (in a CI environment) tested on:
Metaparse is expected to work on Visual C++ 2012 and 2010.