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>The design of the library</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="performance.html" title="Performance"> 11<link rel="next" href="reference.html" title="Reference"> 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="performance.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="reference.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.the_design_of_the_library"></a><a class="link" href="the_design_of_the_library.html" title="The design of the library">The design of the 29 library</a> 30</h2></div></div></div> 31<div class="toc"><dl class="toc"><dt><span class="section"><a href="the_design_of_the_library.html#metaparse.the_design_of_the_library.design_rationale">Design 32 rationale</a></span></dt></dl></div> 33<p> 34 The purpose of the library is to provide tools to build template metaprograms 35 being able to interpret the content of a string literal and generate code, 36 display error messages, etc based on the content of the string literal. Such 37 metaprograms are called <a class="link" href="reference.html#parser">parser</a>s. Metaparse is 38 based on <a href="https://en.wikipedia.org/wiki/Parser_combinator" target="_top">parser 39 combinators</a>. 40 </p> 41<p> 42 The key components of the library: 43 </p> 44<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 45<li class="listitem"> 46 <a class="link" href="reference.html#ref-string">Compile-time string representation</a>. These 47 are tools for representing the content of a string literal in a way that 48 makes it possible for template metaprograms to work on them. For this the 49 library provides the <a class="link" href="reference.html#string"><code class="computeroutput"><span class="identifier">string</span></code></a> 50 template class, which is a drop-in replacement of Boost.MPL's <code class="computeroutput"><span class="identifier">string</span></code> implementation, and the <a class="link" href="reference.html#BOOST_METAPARSE_STRING"><code class="computeroutput"><span class="identifier">BOOST_METAPARSE_STRING</span></code></a> 51 macro. 52 </li> 53<li class="listitem"> 54 <a class="link" href="reference.html#parsers">Parsers</a>. These are template metafunction 55 classes parsing a prefix of a string literal. These are simple <a class="link" href="reference.html#parser">parser</a>s 56 providing the basic building blocks for more complicated ones doing some 57 useful work. 58 </li> 59<li class="listitem"> 60 <a class="link" href="reference.html#combinators">Parser combinators</a>. These are <a class="link" href="reference.html#metafunction">template metafunction</a>s taking <a class="link" href="reference.html#parser">parser</a>s 61 as argument and/or returning <a class="link" href="reference.html#parser">parser</a>s as their 62 result. They can be used to build more and more complex <a class="link" href="reference.html#parser">parser</a>s 63 out of the simple ones. 64 </li> 65</ul></div> 66<div class="section"> 67<div class="titlepage"><div><div><h3 class="title"> 68<a name="metaparse.the_design_of_the_library.design_rationale"></a><a class="link" href="the_design_of_the_library.html#metaparse.the_design_of_the_library.design_rationale" title="Design rationale">Design 69 rationale</a> 70</h3></div></div></div> 71<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 72 <span class="bold"><strong>Why template metaprogramming?</strong></span> 73 </li></ul></div> 74<p> 75 An alternative is using <code class="computeroutput"><span class="keyword">constexpr</span></code> 76 functions instead of template metaprograms. There are certain things that 77 are difficult (if possible) using <code class="computeroutput"><span class="keyword">constexpr</span></code> 78 functions: building containers (at compile-time) the length of which depend 79 on the parsed text (eg. parsing a JSON list), generating and validating types 80 (eg. <code class="computeroutput"><span class="identifier">printf</span></code>). 81 </p> 82<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 83 <span class="bold"><strong>Why are there so many folding parsers?</strong></span> 84 </li></ul></div> 85<p> 86 Compilation speed and memory consumption is a critical part of template metaprogramming-based 87 libraries. Users of the library interfaces built with Metaparse will have 88 to pay for that every time they compile their code. Therefore it is important 89 to provide the parser authors the ability to use the parser combinators with 90 minimal overhead, while it is also important to provide convenient combinators 91 for beginners and for the cases where that is the best option anyway. 92 </p> 93<p> 94 <a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a> 95 combined with <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a>, 96 <a class="link" href="reference.html#accept_when"><code class="computeroutput"><span class="identifier">accept_when</span></code></a> 97 and <a class="link" href="reference.html#transform"><code class="computeroutput"><span class="identifier">transform</span></code></a> 98 can replace any of the folding parsers, however, for the cost of constructing 99 intermediate containers, that are (usually) processed sequentially after 100 that. 101 </p> 102<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 103 <span class="bold"><strong>Why external code generator for <code class="computeroutput"><span class="identifier">BOOST_METAPARSE_STRING</span></code>?</strong></span> 104 </li></ul></div> 105<p> 106 To be able to support longer strings. It generates code using macros to reduce 107 the size of the header files (the reducion is multiples of MBs). 108 </p> 109<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 110 <span class="bold"><strong>Why defining a custom version of Boost.Preprocessor 111 macros?</strong></span> 112 </li></ul></div> 113<p> 114 There are two reasons for the library defining its own set of preprocessor 115 metaprogramming macros: to have control over the upper limit of iteration 116 steps and to be able to clean the macros up once they have done their job 117 (and avoid polluting the macros of the users). 118 </p> 119<p> 120 Note that these macros live in the <code class="computeroutput"><span class="identifier">impl</span></code> 121 directory, which means that they are an implementation detail of the library 122 and should be used internally only. 123 </p> 124</div> 125</div> 126<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 127<td align="left"></td> 128<td align="right"><div class="copyright-footer">Copyright © 2015 Abel Sinkovics<p> 129 Distributed under the Boost Software License, Version 1.0. (See accompanying 130 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>) 131 </p> 132</div></td> 133</tr></table> 134<hr> 135<div class="spirit-nav"> 136<a accesskey="p" href="performance.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="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 137</div> 138</body> 139</html> 140