• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Preface</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../metaparse.html" title="Chapter 24. Boost.Metaparse">
10<link rel="prev" href="related_publications_and_blogs.html" title="Related publications and blogs">
11<link rel="next" href="getting_started_with_boost_metap.html" title="Getting started with Boost.Metaparse">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="related_publications_and_blogs.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../metaparse.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="getting_started_with_boost_metap.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="metaparse.preface"></a><a class="link" href="preface.html" title="Preface">Preface</a>
29</h2></div></div></div>
30<div class="toc"><dl class="toc">
31<dt><span class="section"><a href="preface.html#metaparse.preface.description">Description</a></span></dt>
32<dt><span class="section"><a href="preface.html#metaparse.preface.scope">Scope</a></span></dt>
33<dt><span class="section"><a href="preface.html#metaparse.preface.advantages_of_using_this_library">Advantages
34      of using this library</a></span></dt>
35<dt><span class="section"><a href="preface.html#metaparse.preface.cost_of_using_metaparse">Cost of using
36      Metaparse</a></span></dt>
37<dt><span class="section"><a href="preface.html#metaparse.preface.supported_platforms">Supported platforms</a></span></dt>
38</dl></div>
39<div class="section">
40<div class="titlepage"><div><div><h3 class="title">
41<a name="metaparse.preface.description"></a><a class="link" href="preface.html#metaparse.preface.description" title="Description">Description</a>
42</h3></div></div></div>
43<p>
44        Metaparse is a compile-time parser generator library. Metaparse provides
45        tools to write parsers parsing the content of string literals at compile-time,
46        which makes it possible to embed domain specific languages (DSLs) into C++
47        without altering their original syntax (Note that the DSL code snippets will
48        be written in string literals, therefore they may need to be escaped).
49      </p>
50<p>
51        Assuming that the following template class is available for representing
52        rational numbers in template metaprogramming:
53      </p>
54<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Num</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Denom</span><span class="special">&gt;</span>
55<span class="keyword">struct</span> <span class="identifier">rational</span><span class="special">;</span>
56</pre>
57<p>
58        Metaparse can be used to construct such values (instantiate the <code class="computeroutput"><span class="identifier">rational</span></code> template class) from string literals.
59        Instead of <code class="computeroutput"><span class="identifier">rational</span><span class="special">&lt;</span><span class="number">1</span><span class="special">,</span> <span class="number">3</span><span class="special">&gt;</span></code> one can write <code class="computeroutput"><span class="identifier">RATIONAL</span><span class="special">(</span><span class="string">"1/3"</span><span class="special">)</span></code> which can be processed by any standard-compliant
60        C++11 compiler (and mean the same). This can be implemented using Metaparse
61        the following way:
62      </p>
63<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">metaparse</span><span class="special">;</span>
64
65<span class="keyword">typedef</span>
66  <span class="identifier">sequence_apply2</span><span class="special">&lt;</span>
67    <span class="identifier">rational</span><span class="special">,</span>
68
69    <span class="identifier">token</span><span class="special">&lt;</span><span class="identifier">int_</span><span class="special">&gt;,</span>
70    <span class="identifier">last_of</span><span class="special">&lt;</span><span class="identifier">lit_c</span><span class="special">&lt;</span><span class="char">'/'</span><span class="special">&gt;,</span> <span class="identifier">token</span><span class="special">&lt;</span><span class="identifier">int_</span><span class="special">&gt;&gt;</span>
71  <span class="special">&gt;</span>
72  <span class="identifier">rational_grammar</span><span class="special">;</span>
73
74<span class="keyword">typedef</span> <span class="identifier">build_parser</span><span class="special">&lt;</span><span class="identifier">entire_input</span><span class="special">&lt;</span><span class="identifier">rational_grammar</span><span class="special">&gt;&gt;</span> <span class="identifier">rational_parser</span><span class="special">;</span>
75
76<span class="preprocessor">#define</span> <span class="identifier">RATIONAL</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span> <span class="special">\</span>
77  <span class="special">(::</span><span class="identifier">rational_parser</span><span class="special">::</span><span class="identifier">apply</span><span class="special">&lt;</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="identifier">s</span><span class="special">)&gt;::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">run</span><span class="special">())</span>
78</pre>
79<p>
80        Note that this is the entire implementation. Also note that this implementation
81        can be extended to improve the error reports in certain situations.
82      </p>
83</div>
84<div class="section">
85<div class="titlepage"><div><div><h3 class="title">
86<a name="metaparse.preface.scope"></a><a class="link" href="preface.html#metaparse.preface.scope" title="Scope">Scope</a>
87</h3></div></div></div>
88<div class="toc"><dl class="toc">
89<dt><span class="section"><a href="preface.html#metaparse.preface.scope.comparsion_to_boost_proto">Comparsion
90        to Boost.Proto</a></span></dt>
91<dt><span class="section"><a href="preface.html#metaparse.preface.scope.comparison_to_boost_spirit">Comparison
92        to Boost.Spirit</a></span></dt>
93</dl></div>
94<p>
95        Metaparse is intended to be used by library authors to make their APIs follow
96        the usual notation of the library's problem domain.
97      </p>
98<div class="section">
99<div class="titlepage"><div><div><h4 class="title">
100<a name="metaparse.preface.scope.comparsion_to_boost_proto"></a><a class="link" href="preface.html#metaparse.preface.scope.comparsion_to_boost_proto" title="Comparsion to Boost.Proto">Comparsion
101        to Boost.Proto</a>
102</h4></div></div></div>
103<p>
104          Boost.Proto is a tool for building expression templates. Expression templates
105          can be used for DSL embedding by reinterpreting valid C++ expressions as
106          expressions written in the DSL to embed.
107        </p>
108<p>
109          This technique has the advantages over parsing the content of string literals
110          (which is Metaparse's approach) that:
111        </p>
112<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
113<li class="listitem">
114              is faster in most cases
115            </li>
116<li class="listitem">
117              APIs using this technique can "emerge" as a process of advancing
118              the API of a library step-by-step. Moving to a completely new DSL (with
119              its own syntax) is a relatively big step.
120            </li>
121</ul></div>
122<p>
123          Using expression templates for DSL embedding has the following disadvantages:
124        </p>
125<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
126              the syntax of the embedded DSL is limited. It has to be a valid C++
127              expression. For most libraries, people familiar with the original DSL
128              usually need to learn the library's syntax to understand the embedded
129              code snippets.
130            </li></ul></div>
131<p>
132          Proto helps embedding DSLs based on expression templates, while Metaparse
133          helps embedding DSLs based on parsing the content of string literals.
134        </p>
135</div>
136<div class="section">
137<div class="titlepage"><div><div><h4 class="title">
138<a name="metaparse.preface.scope.comparison_to_boost_spirit"></a><a class="link" href="preface.html#metaparse.preface.scope.comparison_to_boost_spirit" title="Comparison to Boost.Spirit">Comparison
139        to Boost.Spirit</a>
140</h4></div></div></div>
141<p>
142          Spirit is a tool that can be used to build parsers parsing (among others)
143          the content of string literals at runtime, while Metaparse is a tool that
144          can be used to parse the content of string literals at compile-time.
145        </p>
146</div>
147</div>
148<div class="section">
149<div class="titlepage"><div><div><h3 class="title">
150<a name="metaparse.preface.advantages_of_using_this_library"></a><a class="link" href="preface.html#metaparse.preface.advantages_of_using_this_library" title="Advantages of using this library">Advantages
151      of using this library</a>
152</h3></div></div></div>
153<p>
154        This library is useful to provide an API for C++ libraries dealing with a
155        problem domain with its own notation. Interfaces built with Metaparse make
156        it possible for the users of the interface to use the domain's own notation,
157        which makes it easier to write and maintain the code. Users of the interface
158        don't need to learn a new notation (trying to follow the problem domain's
159        original one) library authors constrained by the C++ syntax can provide.
160        Example problem domains are regular expressions and SQL queries.
161      </p>
162<p>
163        Metaparse can also be useful to build libraries validating the content of
164        string literals at compile time instead of doing it at runtime or not doing
165        it at all. This can help finding (and fixing) bugs in the code early (during
166        compilation). An example problem domain is <code class="computeroutput"><span class="identifier">printf</span></code>.
167      </p>
168</div>
169<div class="section">
170<div class="titlepage"><div><div><h3 class="title">
171<a name="metaparse.preface.cost_of_using_metaparse"></a><a class="link" href="preface.html#metaparse.preface.cost_of_using_metaparse" title="Cost of using Metaparse">Cost of using
172      Metaparse</a>
173</h3></div></div></div>
174<p>
175        The parsers built with Metaparse process the content of the string literals
176        using template metaprograms. This impacts the library using Metaparse the
177        following way:
178      </p>
179<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
180<li class="listitem">
181            The maintainer of the API built with Metaparse will need to understand
182            template metaprogramming.
183          </li>
184<li class="listitem">
185            The content of the string literals will be (re)parsed during every compilation.
186            This will impact the compiler's memory consumption and the compilation
187            speed.
188          </li>
189<li class="listitem">
190            The users of the library will receive the error reports coming from the
191            parsers as template error messages of their compiler. (Note that Metaparse
192            actively tries improving their quality and provides <a class="link" href="getting_started_with_boost_metap.html#dealing_with_invalid_input">tools</a>
193            for parser authors).
194          </li>
195</ul></div>
196</div>
197<div class="section">
198<div class="titlepage"><div><div><h3 class="title">
199<a name="metaparse.preface.supported_platforms"></a><a class="link" href="preface.html#metaparse.preface.supported_platforms" title="Supported platforms">Supported platforms</a>
200</h3></div></div></div>
201<p>
202        Metaparse is based on C++98. The only exception is the <a class="link" href="reference.html#BOOST_METAPARSE_STRING">BOOST_METAPARSE_STRING</a>
203        macro, which needs C++11 <code class="computeroutput"><span class="keyword">constexpr</span></code>.
204      </p>
205<p>
206        Compilers Metaparse is actively (in a CI environment) tested on:
207      </p>
208<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
209<li class="listitem">
210            GCC 4.6, 4.7, 4.8, 4.9
211          </li>
212<li class="listitem">
213            Clang 3.4, 3.5, 3.6
214          </li>
215<li class="listitem">
216            Visual C++ 2015
217          </li>
218</ul></div>
219<p>
220        Metaparse is expected to work on Visual C++ 2012 and 2010.
221      </p>
222</div>
223</div>
224<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
225<td align="left"></td>
226<td align="right"><div class="copyright-footer">Copyright © 2015 Abel Sinkovics<p>
227        Distributed under the Boost Software License, Version 1.0. (See accompanying
228        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
229      </p>
230</div></td>
231</tr></table>
232<hr>
233<div class="spirit-nav">
234<a accesskey="p" href="related_publications_and_blogs.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../metaparse.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="getting_started_with_boost_metap.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
235</div>
236</body>
237</html>
238