8951 lines
1.3 MiB
8951 lines
1.3 MiB
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
<html>
|
||
<head>
|
||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||
<title>Getting started with Boost.Metaparse</title>
|
||
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
|
||
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
|
||
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
|
||
<link rel="up" href="../metaparse.html" title="Chapter 24. Boost.Metaparse">
|
||
<link rel="prev" href="preface.html" title="Preface">
|
||
<link rel="next" href="user_manual.html" title="User manual">
|
||
</head>
|
||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||
<table cellpadding="2" width="100%"><tr>
|
||
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
|
||
<td align="center"><a href="../../../index.html">Home</a></td>
|
||
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
|
||
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
|
||
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
|
||
<td align="center"><a href="../../../more/index.htm">More</a></td>
|
||
</tr></table>
|
||
<hr>
|
||
<div class="spirit-nav">
|
||
<a accesskey="p" href="preface.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="user_manual.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
||
<a name="metaparse.getting_started_with_boost_metap"></a><a name="getting_started"></a><a class="link" href="getting_started_with_boost_metap.html" title="Getting started with Boost.Metaparse">Getting
|
||
started with Boost.Metaparse</a>
|
||
</h2></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.1_introduction">1.
|
||
Introduction</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.2_the_text_to_parse">2.
|
||
The text to parse</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser">3.
|
||
Creating a simple parser</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.4_parsing_simple_expressions">4.
|
||
Parsing simple expressions</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions">5.
|
||
Parsing longer expressions</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.6_adding_support_for_other_opera">6.
|
||
Adding support for other operators</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.7_dealing_with_precedence">7.
|
||
Dealing with precedence</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.8_dealing_with_associativity">8.
|
||
Dealing with associativity</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.9_dealing_with_unary_operators">9.
|
||
Dealing with unary operators</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.10_dealing_with_parens">10.
|
||
Dealing with parens</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input">11.
|
||
Dealing with invalid input</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.12_summary">12.
|
||
Summary</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.copy_paste_friendly_code_example">Copy-paste
|
||
friendly code examples</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.definitions_before_each_section">Definitions
|
||
before each section</a></span></dt>
|
||
</dl></div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.1_introduction"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.1_introduction" title="1. Introduction">1.
|
||
Introduction</a>
|
||
</h3></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.1_introduction.1_1_testing_environment">1.1.
|
||
Testing environment</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.1_introduction.1_2_using_a_regular_testing_envi">1.2.
|
||
Using a "regular" testing environment</a></span></dt>
|
||
</dl></div>
|
||
<p>
|
||
This tutorial shows you how to build a parser for a small calculator language
|
||
from the ground up. The goal is not to have a complete calculator, but to
|
||
show you the most common situations one can face while building a parser
|
||
using Metaparse. This tutorial assumes, that you have some template metaprogramming
|
||
experience.
|
||
</p>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.1_introduction.1_1_testing_environment"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.1_introduction.1_1_testing_environment" title="1.1. Testing environment">1.1.
|
||
Testing environment</a>
|
||
</h4></div></div></div>
|
||
<p>
|
||
While you are using Metaparse, you will be writing parsers turning an input
|
||
text into a type. These types can later be processed by further template
|
||
metaprograms. While you are working on your parsers, you'll probably want
|
||
to look at the result of parsing a test input. This tutorial assumes that
|
||
you can use <a href="http://metashell.org" target="_top">Metashell</a>. Since
|
||
the <a href="http://metashell.org/about/demo" target="_top">online demo</a> makes
|
||
the Boost headers available, you can use that in the tutorial as well.
|
||
</p>
|
||
<p>
|
||
If you install Metashell on your computer, make sure that you have the
|
||
Boost libraries and the <code class="computeroutput"><span class="identifier">getting_started</span></code>
|
||
example of Metaparse on the include path. For example, you can start Metashell
|
||
with the following arguments:
|
||
</p>
|
||
<pre class="programlisting"><span class="error">$</span> <span class="identifier">metashell</span> <span class="special">-</span><span class="identifier">I</span><span class="error">$</span><span class="identifier">BOOST_ROOT</span> <span class="special">-</span><span class="identifier">I</span><span class="error">$</span><span class="identifier">BOOST_ROOT</span><span class="special">/</span><span class="identifier">libs</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">example</span><span class="special">/</span><span class="identifier">getting_started</span>
|
||
</pre>
|
||
<p>
|
||
<code class="computeroutput"><span class="error">$</span><span class="identifier">BOOST_ROOT</span></code>
|
||
refers to the <span class="emphasis"><em>boost root directory</em></span> (where you have
|
||
checked out the Boost source code).
|
||
</p>
|
||
<p>
|
||
This tutorial is long and therefore you might want to make shorter or longer
|
||
breaks while reading it. To make it easy for you to stop at a certain point
|
||
and continue later (or to start in the middle if you are already familiar
|
||
with the basics) Metaparse has a <code class="computeroutput"><span class="identifier">getting_started</span></code>
|
||
directory in the <code class="computeroutput"><span class="identifier">example</span></code>s.
|
||
This contains the definitions for each section of this tutorial.
|
||
</p>
|
||
<p>
|
||
If you're about to start (or continue) this guide from section 5.2.1, you
|
||
can include <code class="computeroutput"><span class="number">5</span><span class="identifier">_2_1</span><span class="special">.</span><span class="identifier">hpp</span></code>.
|
||
This will define everything you need to start with that section.
|
||
</p>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
You have access to these headers in the online Metashell demo as well.
|
||
For example you can include the <code class="computeroutput"><span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">getting_started</span><span class="special">/</span><span class="number">5</span><span class="identifier">_2_1</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code> header to start from section 5.2.1.
|
||
</p></td></tr>
|
||
</table></div>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.1_introduction.1_2_using_a_regular_testing_envi"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.1_introduction.1_2_using_a_regular_testing_envi" title='1.2. Using a "regular" testing environment'>1.2.
|
||
Using a "regular" testing environment</a>
|
||
</h4></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.1_introduction.1_2_using_a_regular_testing_envi.1_2_1_enforcing_an_error_message">1.2.1.
|
||
Enforcing an error message or a warning containing the result of the metafunction
|
||
call</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.1_introduction.1_2_using_a_regular_testing_envi.1_2_2_displaying_the_result_of_t">1.2.2.
|
||
Displaying the result of the metafunction call at runtime</a></span></dt>
|
||
</dl></div>
|
||
<p>
|
||
If you have no access to Metashell or you prefer using your regular C++
|
||
development environment while processing this tutorial, this is also possible.
|
||
</p>
|
||
<p>
|
||
The tutorial (and usually experimenting with Metaparse) requires that you
|
||
evaluate different template metaprogramming expressions and check their
|
||
result, which is a type. Thus, to try the examples of this tutorial you
|
||
need a way to be able to display the result of evaluating a template metaprogram.
|
||
This section shows you two options.
|
||
</p>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h5 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.1_introduction.1_2_using_a_regular_testing_envi.1_2_1_enforcing_an_error_message"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.1_introduction.1_2_using_a_regular_testing_envi.1_2_1_enforcing_an_error_message" title="1.2.1. Enforcing an error message or a warning containing the result of the metafunction call">1.2.1.
|
||
Enforcing an error message or a warning containing the result of the metafunction
|
||
call</a>
|
||
</h5></div></div></div>
|
||
<p>
|
||
You can either use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">print</span></code>
|
||
or <code class="computeroutput"><span class="identifier">mpllibs</span><span class="special">::</span><span class="identifier">metamonad</span><span class="special">::</span><span class="identifier">fail_with_type</span></code> to enforce a warning
|
||
or an error message containing the result of a metaprogram evaluation.
|
||
For example to see what <a class="link" href="reference.html#BOOST_METAPARSE_STRING"><code class="computeroutput"><span class="identifier">BOOST_METAPARSE_STRING</span></code></a><code class="computeroutput"><span class="special">(</span><span class="string">"11 + 2"</span><span class="special">)</span></code> refers to, you can create a <code class="computeroutput"><span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span></code> with the following content:
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">print</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">print</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"11 + 2"</span><span class="special">)></span> <span class="identifier">x</span><span class="special">;</span>
|
||
</pre>
|
||
<p>
|
||
If you try to compile it, the compiler will display warnings containing
|
||
the type the expression <a class="link" href="reference.html#BOOST_METAPARSE_STRING"><code class="computeroutput"><span class="identifier">BOOST_METAPARSE_STRING</span></code></a><code class="computeroutput"><span class="special">(</span><span class="string">"11 + 2"</span><span class="special">)</span></code> constructs. To use this technique for
|
||
this tutorial, you need to add all the includes and definitions the tutorial
|
||
suggests typing in the shell to your <code class="computeroutput"><span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span></code>
|
||
file. When the shell suggests to try to call some metafunction (or you'd
|
||
like to try something out), you need to replace the template argument
|
||
of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">print</span></code> with the expression in question
|
||
and recompile the code.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h5 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.1_introduction.1_2_using_a_regular_testing_envi.1_2_2_displaying_the_result_of_t"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.1_introduction.1_2_using_a_regular_testing_envi.1_2_2_displaying_the_result_of_t" title="1.2.2. Displaying the result of the metafunction call at runtime">1.2.2.
|
||
Displaying the result of the metafunction call at runtime</a>
|
||
</h5></div></div></div>
|
||
<p>
|
||
You can also display the result of metaprograms at runtime. You can use
|
||
the <a href="http://boost.org/libs/type_index" target="_top">Boost.TypeIndex</a>
|
||
library to do this. For example to see what <a class="link" href="reference.html#BOOST_METAPARSE_STRING"><code class="computeroutput"><span class="identifier">BOOST_METAPARSE_STRING</span></code></a><code class="computeroutput"><span class="special">(</span><span class="string">"11 + 2"</span><span class="special">)</span></code> refers to, you can create a <code class="computeroutput"><span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span></code> with the following content:
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_index</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
|
||
|
||
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
|
||
<span class="special">{</span>
|
||
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span>
|
||
<span class="special"><<</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_id_with_cvr</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"11 + 2"</span><span class="special">)>()</span>
|
||
<span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
||
<span class="special">}</span>
|
||
</pre>
|
||
<p>
|
||
If you compile and run this code, it will display the type on the standard
|
||
output.
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.2_the_text_to_parse"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.2_the_text_to_parse" title="2. The text to parse">2.
|
||
The text to parse</a>
|
||
</h3></div></div></div>
|
||
<p>
|
||
With Metaparse you can create template metaprograms parsing an input text.
|
||
To pass the input text to the metaprograms, you need to represent them as
|
||
types. For example let's represent the text <code class="computeroutput"><span class="string">"Hello
|
||
world"</span></code> as a type. The most straightforward way of doing
|
||
it would be creating a variadic template class taking the characters of the
|
||
text as template arguments:
|
||
</p>
|
||
<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">char</span><span class="special">...</span> <span class="identifier">Cs</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">string</span><span class="special">;</span>
|
||
</pre>
|
||
<p>
|
||
The text <code class="computeroutput"><span class="string">"11 + 2"</span></code> can
|
||
be represented the following way:
|
||
</p>
|
||
<pre class="programlisting"><span class="identifier">string</span><span class="special"><</span><span class="char">'1'</span><span class="special">,</span> <span class="char">'1'</span><span class="special">,</span> <span class="char">' '</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="char">' '</span><span class="special">,</span> <span class="char">'2'</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
Metaparse provides this type for you. Run the following command in Metashell:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that the <code class="computeroutput"><span class="special">></span></code> character
|
||
at the beginning of the above code example is the prompt of Metashell.
|
||
It is added to the code examples as a hint to what you should run in Metashell
|
||
(or add to your test <code class="computeroutput"><span class="identifier">cpp</span></code>
|
||
file if you are using a regular development environment).
|
||
</p></td></tr>
|
||
</table></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that in the <a href="http://abel.web.elte.hu/shell/metashell.html" target="_top">online-demo</a>
|
||
of Metashell you can paste code into the shell by right-clicking on the
|
||
shell somewhere and choosing <span class="emphasis"><em>Paste from browser</em></span> in
|
||
the context menu.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
This will make this type available for you. Now you can try running the following
|
||
command:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">metaparse</span><span class="special">::</span><span class="identifier">string</span><span class="special"><</span><span class="char">'1'</span><span class="special">,</span> <span class="char">'1'</span><span class="special">,</span> <span class="char">' '</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="char">' '</span><span class="special">,</span> <span class="char">'2'</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
The shell will echo (almost) the same type back to you. The only difference
|
||
is that it is in a sub-namespace indicating the version of Metaparse being
|
||
used.
|
||
</p>
|
||
<p>
|
||
The nice thing about this representation is that metaprograms can easily
|
||
access the individual characters of the text. The not so nice thing about
|
||
this representation is that if you want to write the text <code class="computeroutput"><span class="string">"Hello
|
||
world"</span></code> in your source code, you have to type a lot.
|
||
</p>
|
||
<p>
|
||
Metaparse provides a macro that can turn a string literal into an instance
|
||
of <a class="link" href="reference.html#string"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">metaparse</span><span class="special">::</span><span class="identifier">string</span></code></a>.
|
||
This is the <a class="link" href="reference.html#BOOST_METAPARSE_STRING"><code class="computeroutput"><span class="identifier">BOOST_METAPARSE_STRING</span></code></a>
|
||
macro. You get it by including <code class="computeroutput"><span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code>.
|
||
Let's try it by running the following command in Metashell:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"11 + 2"</span><span class="special">)</span>
|
||
</pre>
|
||
<p>
|
||
You will get the same result as you got by instantiating <a class="link" href="reference.html#string"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">metaparse</span><span class="special">::</span><span class="identifier">string</span></code></a> yourself.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser" title="3. Creating a simple parser">3.
|
||
Creating a simple parser</a>
|
||
</h3></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser.3_1_dealing_with_invalid_input">3.1.
|
||
Dealing with invalid input</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser.3_2_dealing_with_input_containin">3.2.
|
||
Dealing with input containing more than what is needed</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser.3_3_accepting_optional_whitespac">3.3.
|
||
Accepting optional whitespaces at the end of the input</a></span></dt>
|
||
</dl></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined so
|
||
far <a class="link" href="getting_started_with_boost_metap.html#before_3">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Let's try creating a parser. We will start with creating a parser for something
|
||
simple: we will be parsing integer numbers, such as the text <code class="computeroutput"><span class="string">"13"</span></code>. You can think of this first
|
||
parsing exercise as a <span class="emphasis"><em>template metaprogramming string-to-int conversion</em></span>
|
||
because we expect to get the value <code class="computeroutput"><span class="number">13</span></code>
|
||
as the result of parsing.
|
||
</p>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
You know the difference between <code class="computeroutput"><span class="string">"13"</span></code>
|
||
and <code class="computeroutput"><span class="number">13</span></code> in C++. One of them
|
||
is a character array, the other one is an integral value. But what is the
|
||
difference between them in template metaprogramming? They are represented
|
||
by different types. For example <code class="computeroutput"><span class="string">"13"</span></code>
|
||
is represented by <a class="link" href="reference.html#string"><code class="computeroutput"><span class="identifier">string</span></code></a><code class="computeroutput"><span class="special"><</span><span class="char">'1'</span><span class="special">,</span>
|
||
<span class="char">'3'</span><span class="special">></span></code>
|
||
while <code class="computeroutput"><span class="number">13</span></code> is represented by
|
||
<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">integral_constant</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">13</span><span class="special">></span></code>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
To build a parser, we need to specify the grammar to use. Metaparse provides
|
||
building blocks (called parsers) we can use to do this and one of them is
|
||
the <a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a>
|
||
parser which does exactly what we need: it parses integers. To make it available,
|
||
we need to include it:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
Our grammar is simple: <a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a>.
|
||
(Don't worry, we'll parse more complicated languages later).
|
||
</p>
|
||
<p>
|
||
A parser is a <a class="link" href="reference.html#metafunction_class">template metafunction class</a>.
|
||
It can be used directly, but its interface is designed for completeness and
|
||
not for ease of use. Metaparse provides the <a class="link" href="reference.html#build_parser"><code class="computeroutput"><span class="identifier">build_parser</span></code></a> <a class="link" href="reference.html#metafunction">metafunction</a>
|
||
that adds a wrapper to parsers with a simple interface.
|
||
</p>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
In this tutorial, we will always be wrapping our parsers with this. We
|
||
will call these wrapped parsers parsers as well. If you are interested
|
||
in it, you can learn about the complete interface of parsers <a class="link" href="reference.html#parser">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Let's create a parser using <a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a>
|
||
and <a class="link" href="reference.html#build_parser"><code class="computeroutput"><span class="identifier">build_parser</span></code></a>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="special">></span> <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>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_0">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
First we need to include <code class="computeroutput"><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span></code> to
|
||
make <a class="link" href="reference.html#build_parser"><code class="computeroutput"><span class="identifier">build_parser</span></code></a>
|
||
available. Then we make our lives easier by running <code class="computeroutput"><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></code>. The third command defines the parser: we
|
||
need to instantiate the <a class="link" href="reference.html#build_parser"><code class="computeroutput"><span class="identifier">build_parser</span></code></a>
|
||
template class with our parser (<a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a>
|
||
in this case) as argument.
|
||
</p>
|
||
<p>
|
||
Now that we have a parser, let's parse some text with it (if you haven't
|
||
done it yet, include <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span></code>):
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser1</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"13"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">13</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<code class="computeroutput"><span class="identifier">exp_parser1</span></code> is a <a class="link" href="reference.html#metafunction_class">template
|
||
metafunction class</a> taking the input text as it's argument and it returns
|
||
the integral representation of the number in the string. Try it with different
|
||
numbers and see how it converts them.
|
||
</p>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser.3_1_dealing_with_invalid_input"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser.3_1_dealing_with_invalid_input" title="3.1. Dealing with invalid input">3.1.
|
||
Dealing with invalid input</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_3_1">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Have you tried parsing an invalid input? Something that is not a number,
|
||
such as:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser1</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"thirteen"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
</pre>
|
||
<p>
|
||
Well, <code class="computeroutput"><span class="string">"thirteen"</span></code>
|
||
<span class="emphasis"><em>is</em></span> a number, but our parser does not speak English,
|
||
so it is considered as invalid input. As a result of this, compilation
|
||
fails and you get a compilation error from Metashell.
|
||
</p>
|
||
<p>
|
||
In the <a href="../#dealing-with-invalid-input-1" target="_top">Dealing with invalid
|
||
input</a> section we will go into further details on error handling.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser.3_2_dealing_with_input_containin"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser.3_2_dealing_with_input_containin" title="3.2. Dealing with input containing more than what is needed">3.2.
|
||
Dealing with input containing more than what is needed</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_3_2">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Let's try to give the parser two numbers instead of one:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser1</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"11 13"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">11</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
You might be surprised by this: the parser did not return an error. It
|
||
parsed the first number, <code class="computeroutput"><span class="number">11</span></code>
|
||
and ignored <code class="computeroutput"><span class="number">13</span></code>. The way <a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a>
|
||
works is that it parses the number at the beginning of the input text and
|
||
ignores the rest of the input.
|
||
</p>
|
||
<p>
|
||
So <code class="computeroutput"><span class="identifier">exp_parser1</span></code> has a bug:
|
||
our little language consists of <span class="emphasis"><em>one</em></span> number, not a
|
||
<span class="emphasis"><em>list of numbers</em></span>. Let's fix our parser to treat more
|
||
than one numbers as an invalid input:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This gives us the <a class="link" href="reference.html#entire_input"><code class="computeroutput"><span class="identifier">entire_input</span></code></a>
|
||
template class. We can wrap <a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a>
|
||
with <a class="link" href="reference.html#entire_input"><code class="computeroutput"><span class="identifier">entire_input</span></code></a>
|
||
indicating that the number we parse with <a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a> should be the entire input.
|
||
Anything that comes after that is an error. So our parser is <a class="link" href="reference.html#entire_input"><code class="computeroutput"><span class="identifier">entire_input</span></code></a><code class="computeroutput"><span class="special"><</span></code><a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a><code class="computeroutput"><span class="special">></span></code> now. Let's wrap it with <a class="link" href="reference.html#build_parser"><code class="computeroutput"><span class="identifier">build_parser</span></code></a>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
</pre>
|
||
<p>
|
||
Let's try this new parser out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser2</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"13"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">13</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
It can still parse numbers. Let's try to give it two numbers:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser2</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"11 13"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
</pre>
|
||
<p>
|
||
This generates a compilation error, since the parser failed.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser.3_3_accepting_optional_whitespac"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.3_creating_a_simple_parser.3_3_accepting_optional_whitespac" title="3.3. Accepting optional whitespaces at the end of the input">3.3.
|
||
Accepting optional whitespaces at the end of the input</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_3_3">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Our parser became a bit too restrictive now. It doesn't allow <span class="emphasis"><em>anything</em></span>
|
||
after the number, not even whitespaces:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser2</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"11 "</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
</pre>
|
||
<p>
|
||
Let's allow whitespaces after the number:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This makes the <a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a>
|
||
template class available. It takes a parser as its argument and allows
|
||
optional whitespaces after that. Let's create a third parser allowing whitespaces
|
||
after the number:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
</pre>
|
||
<p>
|
||
We expect <a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a><code class="computeroutput"><span class="special"><</span></code><a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a><code class="computeroutput"><span class="special">></span></code> to be the entire input in this case.
|
||
We allow optional whitespaces after <a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a> but nothing else:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser3</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"11 "</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">11</span><span class="special">></span>
|
||
</pre>
|
||
</div>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.4_parsing_simple_expressions"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.4_parsing_simple_expressions" title="4. Parsing simple expressions">4.
|
||
Parsing simple expressions</a>
|
||
</h3></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.4_parsing_simple_expressions.4_1_tokenizer">4.1.
|
||
Tokenizer</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.4_parsing_simple_expressions.4_2_evaluating_the_expression">4.2.
|
||
Evaluating the expression</a></span></dt>
|
||
</dl></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined so
|
||
far <a class="link" href="getting_started_with_boost_metap.html#before_4">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
We can parse numbers. Let's try parsing something more complicated, such
|
||
as <code class="computeroutput"><span class="string">"11 + 2"</span></code>. This is
|
||
a number followed by a <code class="computeroutput"><span class="special">+</span></code> symbol
|
||
followed by another number. <a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a>
|
||
(or <a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a><code class="computeroutput"><span class="special"><</span></code><a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a><code class="computeroutput"><span class="special">></span></code>) implements the parser for one number.
|
||
</p>
|
||
<p>
|
||
First, let's write a parser for the <code class="computeroutput"><span class="special">+</span></code>
|
||
symbol. We can use the following:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This gives us <a class="link" href="reference.html#lit_c"><code class="computeroutput"><span class="identifier">lit_c</span></code></a>
|
||
which we can use to parse specific characters, such as <code class="computeroutput"><span class="special">+</span></code>.
|
||
The grammar parsing the <code class="computeroutput"><span class="special">+</span></code> character
|
||
can be represented by <a class="link" href="reference.html#lit_c"><code class="computeroutput"><span class="identifier">lit_c</span></code></a><code class="computeroutput"><span class="special"><</span><span class="char">'+'</span><span class="special">></span></code>.
|
||
To allow optional whitespaces after it, we should use <a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a><code class="computeroutput"><span class="special"><</span></code><a class="link" href="reference.html#lit_c"><code class="computeroutput"><span class="identifier">lit_c</span></code></a><code class="computeroutput"><span class="special"><</span><span class="char">'+'</span><span class="special">>></span></code>.
|
||
</p>
|
||
<p>
|
||
So to parse <code class="computeroutput"><span class="string">"11 + 2"</span></code>
|
||
we need the following sequence of parsers:
|
||
</p>
|
||
<pre class="programlisting"><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">></span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>></span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
Metaparse provides <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a>
|
||
for parsing the sequence of things:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
We can implement the parser for our expressions using <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a>:
|
||
</p>
|
||
<pre class="programlisting"><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>></span>
|
||
</pre>
|
||
<p>
|
||
Let's create a parser using it:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
</pre>
|
||
<p>
|
||
Try parsing a simple expression using it:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser4</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"11 + 2"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">v_item</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">v_item</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">></span>
|
||
<span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">v_item</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">11</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector0</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">na</span><span class="special">>,</span> <span class="number">0</span>
|
||
<span class="special">>,</span> <span class="number">0</span><span class="special">>,</span> <span class="number">0</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
What you get might look strange to you. It is a <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
from [Boost.MPL]( http://boost.org/libs/mpl). What you can see in the shell
|
||
is the way this vector is represented. Metashell offers [pretty printing](metashell.org/manual/getting_started#data-structures-of-boostmpl)
|
||
for <a href="http://boost.org/libs/mpl" target="_top">Boost.MPL</a> containers:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
After including this header, try parsing again:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser4</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"11 + 2"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">11</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">></span> <span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
What you get now looks more simple: this is a vector of three elements:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">11</span><span class="special">></span></code>
|
||
This is the result of parsing with <a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a><code class="computeroutput"><span class="special"><</span></code><a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a><code class="computeroutput"><span class="special">></span></code>.
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">></span></code> This
|
||
is the result of parsing with <a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a><code class="computeroutput"><span class="special"><</span></code><a class="link" href="reference.html#lit_c"><code class="computeroutput"><span class="identifier">lit_c</span></code></a><code class="computeroutput"><span class="special"><</span><span class="char">'+'</span><span class="special">>></span></code>.
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">></span> <span class="special">></span></code> This is the result of parsing with
|
||
<a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a><code class="computeroutput"><span class="special"><</span></code><a class="link" href="reference.html#int_"><code class="computeroutput"><span class="identifier">int_</span></code></a><code class="computeroutput"><span class="special">></span></code>.
|
||
</li>
|
||
</ul></div>
|
||
<p>
|
||
The result of parsing with a <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a>
|
||
is the <code class="computeroutput"><span class="identifier">vector</span></code> of the individual
|
||
parsing results.
|
||
</p>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.4_parsing_simple_expressions.4_1_tokenizer"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.4_parsing_simple_expressions.4_1_tokenizer" title="4.1. Tokenizer">4.1.
|
||
Tokenizer</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_4_1">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
You might have noticed that our parsers have no separate tokenizers. Tokenization
|
||
is part of the parsing process. However, it makes the code of the parsers
|
||
cleaner if we separate the two layers. The previous example has two types
|
||
of tokens:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
a number (eg. <code class="computeroutput"><span class="number">13</span></code>)
|
||
</li>
|
||
<li class="listitem">
|
||
a <code class="computeroutput"><span class="special">+</span></code> symbol
|
||
</li>
|
||
</ul></div>
|
||
<p>
|
||
In our last solution we parsed them by using the <a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a><code class="computeroutput"><span class="special"><</span></code><a href="../int_html" target="_top"><code class="computeroutput"><span class="identifier">int_</span></code></a><code class="computeroutput"><span class="special">></span></code> and <a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a><code class="computeroutput"><span class="special"><</span></code><a class="link" href="reference.html#lit_c"><code class="computeroutput"><span class="identifier">lit_c</span></code></a><code class="computeroutput"><span class="special"><</span><span class="char">'+'</span><span class="special">>></span></code>
|
||
parsers. Have you noticed a pattern? We wrap the parsers of the tokens
|
||
with <a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a><code class="computeroutput"><span class="special"><...></span></code>. It is not just syntactic sugar.
|
||
Our tokens might be followed (separated) by whitespaces, which can be ignored.
|
||
That is what <a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a><code class="computeroutput"><span class="special"><...></span></code> implements.
|
||
</p>
|
||
<p>
|
||
So let's make the implementation of <code class="computeroutput"><span class="identifier">exp_parser</span></code>
|
||
cleaner by separating the tokenization from the rest of the parser:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_1">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
These two definitions create type aliases for the parsers of our tokens.
|
||
For the compiler it doesn't matter if we use <code class="computeroutput"><span class="identifier">plus_token</span></code>
|
||
or <a class="link" href="reference.html#token"><code class="computeroutput"><span class="identifier">token</span></code></a><code class="computeroutput"><span class="special"><</span></code><a class="link" href="reference.html#lit_c"><code class="computeroutput"><span class="identifier">lit_c</span></code></a><code class="computeroutput"><span class="special"><</span><span class="char">'+'</span><span class="special">>></span></code>,
|
||
since they refer to the same type. But it makes the code of the parser
|
||
easier to understand.
|
||
</p>
|
||
<p>
|
||
We can now define our expression parser using these tokens:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
</pre>
|
||
<p>
|
||
We can use it the same way as <code class="computeroutput"><span class="identifier">exp_parser4</span></code>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser5</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"11 + 2"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">11</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">></span> <span class="special">></span>
|
||
</pre>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.4_parsing_simple_expressions.4_2_evaluating_the_expression"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.4_parsing_simple_expressions.4_2_evaluating_the_expression" title="4.2. Evaluating the expression">4.2.
|
||
Evaluating the expression</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_4_2">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
It would be nice if we could evaluate the expression as well. Instead of
|
||
returning a <code class="computeroutput"><span class="identifier">vector</span></code> as the
|
||
result of parsing, we should return the evaluated expression. For example
|
||
the result of parsing <code class="computeroutput"><span class="string">"11 + 2"</span></code>
|
||
should be <code class="computeroutput"><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">13</span><span class="special">></span></code>.
|
||
</p>
|
||
<p>
|
||
Metaparse provides <a class="link" href="reference.html#transform"><code class="computeroutput"><span class="identifier">transform</span></code></a>
|
||
which we can use to implement this:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This can be used to transform the result of a parser. For example we have
|
||
the <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">></span></code> parser which returns a <code class="computeroutput"><span class="identifier">vector</span></code>. We want to transform this <code class="computeroutput"><span class="identifier">vector</span></code> into a number, which is the result
|
||
of evaluating the expression. We need to pass <a class="link" href="reference.html#transform"><code class="computeroutput"><span class="identifier">transform</span></code></a> the <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><...></span></code>
|
||
parser and a function which turns the <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
into the result we need. First let's create this <a class="link" href="reference.html#metafunction">metafunction</a>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="special">></span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">{};</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_2">copy-paste friendly version</a>
|
||
</p>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that if the last character of your command is the <code class="computeroutput"><span class="special">\</span></code> character in Metashell, then the shell
|
||
assumes that you will continue typing the same command and waits for
|
||
that before evaluating your command. When Metashell is waiting for the
|
||
second (or third, or fourth, etc) line of a command, it uses a special
|
||
prompt, <code class="computeroutput"><span class="special">...></span></code>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
What it does is that using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span></code>
|
||
it takes the first (index 0) and the third (index 2) elements of the <code class="computeroutput"><span class="identifier">vector</span></code> that is the result of parsing
|
||
with <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><...></span></code> and adds them. We can try it
|
||
out with an example <code class="computeroutput"><span class="identifier">vector</span></code>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">eval_plus</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">11</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">13</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_3">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
We can use <code class="computeroutput"><span class="identifier">eval_plus</span></code> to
|
||
build a parser that evaluates the expression it parses:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">transform</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_4">copy-paste friendly version</a>
|
||
</p>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that we have to use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span></code>
|
||
to turn our <code class="computeroutput"><span class="identifier">eval_plus</span></code>
|
||
<a class="link" href="reference.html#metafunction">metafunction</a> into a <a class="link" href="reference.html#metafunction_class">metafunction
|
||
class</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
<a class="link" href="reference.html#transform"><code class="computeroutput"><span class="identifier">transform</span></code></a>
|
||
parses the input using <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">></span></code> and transforms the result of that using
|
||
<code class="computeroutput"><span class="identifier">eval_plus</span></code>. Let's try it
|
||
out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser6</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"11 + 2"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">13</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
We have created a simple expression parser. The following diagram shows
|
||
how it works:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag0.png" width="50%"></span>
|
||
</p>
|
||
<p>
|
||
The rounded boxes in the diagram are the parsers parsing the input, which
|
||
are functions (<a class="link" href="reference.html#metafunction_class">template metafunction
|
||
class</a>es). The arrows represent how the results are passed around
|
||
between these parsers (they are the return values of the function calls).
|
||
</p>
|
||
<p>
|
||
It uses <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a>
|
||
to parse the different elements (the first number, the <code class="computeroutput"><span class="special">+</span></code>
|
||
symbol and the second number) and builds a <code class="computeroutput"><span class="identifier">vector</span></code>.
|
||
The final result is calculated from that <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
by the <a class="link" href="reference.html#transform"><code class="computeroutput"><span class="identifier">transform</span></code></a>
|
||
parser.
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions" title="5. Parsing longer expressions">5.
|
||
Parsing longer expressions</a>
|
||
</h3></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_1_parsing_a_subexpression_repe">5.1.
|
||
Parsing a subexpression repeatedly</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres">5.2.
|
||
Evaluating the parsed expression</a></span></dt>
|
||
</dl></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined so
|
||
far <a class="link" href="getting_started_with_boost_metap.html#before_5">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
We can parse simple expressions adding two numbers together. But we can't
|
||
parse expressions adding three, four or maybe more numbers together. In this
|
||
section we will implement a parser for expressions adding lots of numbers
|
||
together.
|
||
</p>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_1_parsing_a_subexpression_repe"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_1_parsing_a_subexpression_repe" title="5.1. Parsing a subexpression repeatedly">5.1.
|
||
Parsing a subexpression repeatedly</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_5_1">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
We can't solve this problem with <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a>, since we don't know
|
||
how many numbers the input will have. We need a parser that:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
parses the first number
|
||
</li>
|
||
<li class="listitem">
|
||
keeps parsing <code class="computeroutput"><span class="special">+</span> <span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
elements until the end of the input
|
||
</li>
|
||
</ul></div>
|
||
<p>
|
||
Parsing the first number is something we can already do: the <code class="computeroutput"><span class="identifier">int_token</span></code> parser does it for us. Parsing
|
||
the <code class="computeroutput"><span class="special">+</span> <span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
elements is more tricky. Metaparse offers different tools for approaching
|
||
this. The most simple is <a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a>
|
||
needs a parser (which parses one <code class="computeroutput"><span class="special">+</span>
|
||
<span class="special"><</span><span class="identifier">number</span><span class="special">></span></code> element) and it keeps parsing the input
|
||
with it as long as it can. This will parse the entire input for us. Let's
|
||
create a parser for our expressions using it:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_5">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
We have a <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a>
|
||
with two elements:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
The first number (<code class="computeroutput"><span class="identifier">int_token</span></code>)
|
||
</li>
|
||
<li class="listitem">
|
||
The <code class="computeroutput"><span class="special">+</span> <span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
parts
|
||
</li>
|
||
</ul></div>
|
||
<p>
|
||
The second part is an <a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a>,
|
||
which parses the <code class="computeroutput"><span class="special">+</span> <span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
elements. One such element is parsed by <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span>
|
||
<span class="identifier">int_token</span><span class="special">></span></code>.
|
||
This is just a sequence of the <code class="computeroutput"><span class="special">+</span></code>
|
||
symbol and the number.
|
||
</p>
|
||
<p>
|
||
Let's try parsing an expression using this:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
</pre>
|
||
<p>
|
||
Here is a formatted version of the result which is easier to read:
|
||
</p>
|
||
<pre class="programlisting"><span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="comment">// The result of int_token</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">1</span><span class="special">>,</span>
|
||
|
||
<span class="comment">// The result of repeated< sequence<plus_token, int_token> ></span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">></span> <span class="special">>,</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">3</span><span class="special">></span> <span class="special">>,</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">4</span><span class="special">></span> <span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
The result is a <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
of two elements. The first element of this <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
is the result of parsing the input with <code class="computeroutput"><span class="identifier">int_token</span></code>,
|
||
the second element of this <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
is the result of parsing the input with <a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a><code class="computeroutput"><span class="special"><</span>
|
||
</code><a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span></code>. This second element is also a
|
||
<code class="computeroutput"><span class="identifier">vector</span></code>. Each element of
|
||
this <code class="computeroutput"><span class="identifier">vector</span></code> is the result
|
||
of parsing the input with <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">></span></code> once. Here is a diagram showing how
|
||
<code class="computeroutput"><span class="identifier">exp_parser7</span></code> parses the
|
||
input <code class="computeroutput"><span class="number">1</span> <span class="special">+</span>
|
||
<span class="number">2</span> <span class="special">+</span> <span class="number">3</span> <span class="special">+</span> <span class="number">4</span></code>:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag1.png" width="90%"></span>
|
||
</p>
|
||
<p>
|
||
The diagram shows that the <code class="computeroutput"><span class="special">+</span> <span class="special"><</span><span class="identifier">number</span><span class="special">></span></code> elements are parsed by <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span>
|
||
<span class="identifier">int_token</span><span class="special">></span></code>
|
||
elements and their results are collected by <a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a>, which constructs a
|
||
<code class="computeroutput"><span class="identifier">vector</span></code> of these results.
|
||
The value of the first <code class="computeroutput"><span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
and this <code class="computeroutput"><span class="identifier">vector</span></code> are placed
|
||
in another <code class="computeroutput"><span class="identifier">vector</span></code>, which
|
||
is the result of parsing.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres" title="5.2. Evaluating the parsed expression">5.2.
|
||
Evaluating the parsed expression</a>
|
||
</h4></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_1_learning_about_boost_mpl_f">5.2.1.
|
||
Learning about <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code></a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_2_evaluating_the_expression_">5.2.2.
|
||
Evaluating the expression using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code></a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_3_using_a_folding_parser_com">5.2.3.
|
||
Using a folding parser combinator</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_4_processing_the_initial_ele">5.2.4.
|
||
Processing the initial element with the folding parser combinator</a></span></dt>
|
||
</dl></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_5_2">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
The final result here is a pair of the first number and the <code class="computeroutput"><span class="identifier">vector</span></code> of the rest of the values. To
|
||
calculate the result we need to process that data structure. Let's give
|
||
the example output we have just parsed a name. This will make it easier
|
||
to test the code calculating the final result from this structure:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
</pre>
|
||
<p>
|
||
Now we can write a <a class="link" href="reference.html#metafunction">template metafunction</a>
|
||
turning this structure into the result of the calculation this structure
|
||
represents.
|
||
</p>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h5 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_1_learning_about_boost_mpl_f"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_1_learning_about_boost_mpl_f" title="5.2.1. Learning about boost::mpl::fold">5.2.1.
|
||
Learning about <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code></a>
|
||
</h5></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_5_2_1">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
We have a <code class="computeroutput"><span class="identifier">vector</span></code> containing
|
||
another <code class="computeroutput"><span class="identifier">vector</span></code>. Therefore,
|
||
we will need to be able to summarise the elements of different <code class="computeroutput"><span class="identifier">vector</span></code>s. We can use the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code> <a class="link" href="reference.html#metafunction">metafunction</a>
|
||
to do this:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
With this <a class="link" href="reference.html#metafunction">metafunction</a>, we can iterate
|
||
over a <code class="computeroutput"><span class="identifier">vector</span></code> of parsed
|
||
numbers and summarise them. We can provide it a <a class="link" href="reference.html#metafunction">metafunction</a>
|
||
taking two arguments: the sum we have so far and the next element of
|
||
the <code class="computeroutput"><span class="identifier">vector</span></code>. This <a class="link" href="reference.html#metafunction">metafunction</a> will be called for every element
|
||
of the <code class="computeroutput"><span class="identifier">vector</span></code>.
|
||
</p>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that this is very similar to the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">accumulate</span></code>
|
||
algorithm. <a href="http://boost.org/libs/mpl" target="_top">Boost.MPL</a>
|
||
provides <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">accumulate</span></code> as well, which is a synonym
|
||
for <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code>. This tutorial (and Metaparse)
|
||
uses the name <code class="computeroutput"><span class="identifier">fold</span></code>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Let's start with a simple case: a <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
of numbers. For example let's summarise the elements of the following
|
||
<code class="computeroutput"><span class="identifier">vector</span></code>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_6">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
We will write a <a class="link" href="reference.html#metafunction">template metafunction</a>,
|
||
<code class="computeroutput"><span class="identifier">sum_vector</span></code> for summarising
|
||
the elements of a <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
of numbers:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">Vector</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>::</span><span class="identifier">type</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">{};</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_7">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
This <a class="link" href="reference.html#metafunction">metafunction</a> takes the <code class="computeroutput"><span class="identifier">vector</span></code> to summarise the elements of
|
||
as its argument and uses <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code>
|
||
to calculate the sum. <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code>
|
||
takes three arguments:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
The container to summarise. This is <code class="computeroutput"><span class="identifier">Vector</span></code>.
|
||
</li>
|
||
<li class="listitem">
|
||
The starting value for <span class="emphasis"><em>the sum we have so far</em></span>.
|
||
Using <code class="computeroutput"><span class="number">0</span></code> means that we
|
||
want to start the sum from <code class="computeroutput"><span class="number">0</span></code>.
|
||
</li>
|
||
<li class="listitem">
|
||
The function to call in every iteration while looping over the container.
|
||
We are using a <a href="http://www.boost.org/libs/mpl/doc/refmanual/lambda-expression.html" target="_top">lambda
|
||
expression</a> in our example, which is the expression wrapped
|
||
by <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span></code>. This expression adds its
|
||
two arguments together using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span></code>.
|
||
The lambda expression refers to its arguments by <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span></code>
|
||
and <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span></code>.
|
||
</li>
|
||
</ul></div>
|
||
<p>
|
||
Let's try this <a class="link" href="reference.html#metafunction">metafunction</a> out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">sum_vector</span><span class="special"><</span><span class="identifier">vector_of_numbers</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">13</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
It works as expected. Here is a diagram showing how it works:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag2.png" width="50%"></span>
|
||
</p>
|
||
<p>
|
||
As the diagram shows, <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code>
|
||
evaluates the lambda expression for each element of the <code class="computeroutput"><span class="identifier">vector</span></code> and passes the result of the
|
||
previous evaluation to the next lambda expression invocation.
|
||
</p>
|
||
<p>
|
||
We have a <a class="link" href="reference.html#metafunction">metafunction</a> that can summarise
|
||
a <code class="computeroutput"><span class="identifier">vector</span></code> of numbers.
|
||
The result of parsing the <code class="computeroutput"><span class="special">+</span> <span class="special"><</span><span class="identifier">number</span><span class="special">></span></code> elements is a <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
of <code class="computeroutput"><span class="identifier">vector</span></code>s. As a recap,
|
||
here is <code class="computeroutput"><span class="identifier">temp_result</span></code>:
|
||
</p>
|
||
<pre class="programlisting"><span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="comment">// The result of int_token</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">1</span><span class="special">>,</span>
|
||
|
||
<span class="comment">// The result of repeated< sequence<plus_token, int_token> ></span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">></span> <span class="special">>,</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">3</span><span class="special">></span> <span class="special">>,</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">4</span><span class="special">></span> <span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
First let's summarise the result of <a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a><code class="computeroutput"><span class="special"><...></span></code>
|
||
using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code>. This is a <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
of <code class="computeroutput"><span class="identifier">vector</span></code>s, but that's
|
||
fine. <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code> doesn't care about what the elements
|
||
of the <code class="computeroutput"><span class="identifier">vector</span></code> are. They
|
||
can be numbers, <code class="computeroutput"><span class="identifier">vector</span></code>s
|
||
or something else as well. The function we use to add two numbers together
|
||
(which was a lambda expression in our previous example) gets these elements
|
||
as its argument and has to deal with them. So to summarise the elements
|
||
of the <code class="computeroutput"><span class="identifier">vector</span></code>s we get
|
||
as the result of parsing with <a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a><code class="computeroutput"><span class="special"><...></span></code>,
|
||
we need to write a <a class="link" href="reference.html#metafunction">metafunction</a> that
|
||
can deal with these elements. One such element is <code class="computeroutput"><span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="keyword">char</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">>></span></code>.
|
||
Here is a <a class="link" href="reference.html#metafunction">metafunction</a> that can be
|
||
used in a <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">Sum</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">{};</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_8">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
This function takes two arguments:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">Sum</span></code>, which is a number.
|
||
This is the summary of the already processed elements.
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">Item</span></code>, the next item
|
||
of the <code class="computeroutput"><span class="identifier">vector</span></code>. These
|
||
items are <code class="computeroutput"><span class="identifier">vector</span></code>s
|
||
of size two: the result of parsing the <code class="computeroutput"><span class="special">+</span></code>
|
||
symbol and the number.
|
||
</li>
|
||
</ul></div>
|
||
<p>
|
||
The <a class="link" href="reference.html#metafunction">metafunction</a> adds the sum we
|
||
have so far and the next number together using the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span></code>
|
||
<a class="link" href="reference.html#metafunction">metafunction</a>. To get the next number
|
||
out of <code class="computeroutput"><span class="identifier">Item</span></code>, it uses
|
||
<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span></code>. Let's try <code class="computeroutput"><span class="identifier">sum_items</span></code>
|
||
out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">sum_items</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">1</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">>></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">3</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_9">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
We have called <code class="computeroutput"><span class="identifier">sum_items</span></code>
|
||
with values from <code class="computeroutput"><span class="identifier">temp_result</span></code>
|
||
and saw that it works as expected: it added the partial sum (<code class="computeroutput"><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">1</span><span class="special">></span></code>)
|
||
to the next number (<code class="computeroutput"><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">></span></code>).
|
||
</p>
|
||
<p>
|
||
<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code> can summarise the list we get
|
||
as the result of parsing the <code class="computeroutput"><span class="special">+</span>
|
||
<span class="special"><</span><span class="identifier">number</span><span class="special">></span></code> elements of the input, so we need
|
||
to extract this list from <code class="computeroutput"><span class="identifier">temp_result</span></code>
|
||
first:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">temp_result</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
</pre>
|
||
<p>
|
||
Here is the formatted version of the result:
|
||
</p>
|
||
<pre class="programlisting"><span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">>>,</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">3</span><span class="special">>>,</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">4</span><span class="special">>></span>
|
||
<span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This is the second element of the <code class="computeroutput"><span class="identifier">temp_result</span></code>
|
||
vector (the first one is the value of the first <code class="computeroutput"><span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
element). Let's try fold out for this:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">temp_result</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> <span class="comment">/* The vector to summarise */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="comment">/* The value to start the sum from */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* The function to call in each iteration */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">9</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_10">copy-paste friendly version</a>
|
||
</p>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
We are using <code class="computeroutput"><span class="identifier">sum_items</span></code>
|
||
as the function to call in each iteration. We are passing a <a class="link" href="reference.html#metafunction">metafunction</a>
|
||
(<code class="computeroutput"><span class="identifier">sum_items</span></code>) to another
|
||
<a class="link" href="reference.html#metafunction">metafunction</a> (<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code>)
|
||
as an argument. To be able to do this, we need to turn it into a <a class="link" href="reference.html#metafunction_class">template metafunction class</a> using
|
||
<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span></code> (<code class="computeroutput"><span class="number">2</span></code>
|
||
means that it takes two arguments).
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
As we have seen, the result of this is the sum of the elements, which
|
||
was <code class="computeroutput"><span class="number">9</span></code> in our case. Here is
|
||
a diagram showing how <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code>
|
||
works:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag3.png" width="50%"></span>
|
||
</p>
|
||
<p>
|
||
It starts with the value <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">></span></code>
|
||
and adds the elements of the <code class="computeroutput"><span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span></code>
|
||
containing the parsing results one by one. The diagram shows how the
|
||
subresults are calculated and then used for further calculations.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h5 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_2_evaluating_the_expression_"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_2_evaluating_the_expression_" title="5.2.2. Evaluating the expression using boost::mpl::fold">5.2.2.
|
||
Evaluating the expression using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code></a>
|
||
</h5></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_5_2_2">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Let's use <code class="computeroutput"><span class="identifier">sum_items</span></code> with
|
||
<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code> to build the parser that summarises
|
||
the values coming from the <code class="computeroutput"><span class="special">+</span> <span class="special"><</span><span class="identifier">number</span><span class="special">></span></code> elements. We can extend the parser
|
||
we were using in <code class="computeroutput"><span class="identifier">exp_parser7</span></code>
|
||
by wrapping the <a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a><code class="computeroutput"><span class="special"><...></span></code> part with <a class="link" href="reference.html#transform"><code class="computeroutput"><span class="identifier">transform</span></code></a>, which transforms
|
||
the result of <a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a><code class="computeroutput"><span class="special"><...></span></code> with the folding expression
|
||
we have just created:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">transform</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="comment">/* The folding expression we have just created */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="comment">/* of the repeated<...> parser */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>::</span><span class="identifier">type</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_11">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
It uses <a class="link" href="reference.html#transform"><code class="computeroutput"><span class="identifier">transform</span></code></a>
|
||
to turn the result of the previous version of our parser into one that
|
||
summarises the <code class="computeroutput"><span class="special">+</span> <span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
elements. Let's try it out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser8</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">boost_</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">1</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">9</span><span class="special">></span> <span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This returns a pair of numbers as the result of parsing: the first number
|
||
and the sum of the rest. To get the value of the entire expression we
|
||
need to add these two numbers together. We can extend our parser to do
|
||
this final addition as well:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">transform</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="comment">/* What we had so far */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">int_token</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">transform</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>::</span><span class="identifier">type</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_12">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
<code class="computeroutput"><span class="identifier">exp_parser9</span></code> wraps the
|
||
parser we had so far (which gives us the two element <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
as the result) with <a class="link" href="reference.html#transform"><code class="computeroutput"><span class="identifier">transform</span></code></a>
|
||
to add the elements of that two element <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
together. Since that two element <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
is a <code class="computeroutput"><span class="identifier">vector</span></code> of numbers,
|
||
we can (re)use the <code class="computeroutput"><span class="identifier">sum_vector</span></code>
|
||
<a class="link" href="reference.html#metafunction">metafunction</a> for this. Let's try
|
||
it out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser9</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">10</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
It gives us the correct result, but it is very inefficient. Let's see
|
||
why:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag4.png" width="90%"></span>
|
||
</p>
|
||
<p>
|
||
There are two loops in this process:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
first <a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a>
|
||
loops over the input to parse all of the <code class="computeroutput"><span class="special">+</span>
|
||
<span class="special"><</span><span class="identifier">number</span><span class="special">></span></code> elements. It builds a <code class="computeroutput"><span class="identifier">vector</span></code> during this. (<code class="computeroutput"><span class="identifier">Loop</span> <span class="number">1</span></code>
|
||
on the diagram)
|
||
</li>
|
||
<li class="listitem">
|
||
then <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code> loops over this <code class="computeroutput"><span class="identifier">vector</span></code> to summarise the elements.
|
||
(<code class="computeroutput"><span class="identifier">Loop</span> <span class="number">2</span></code>
|
||
on the diagram)
|
||
</li>
|
||
</ul></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that we have been talking about <span class="emphasis"><em>loop</em></span>s while
|
||
there is no such thing as a loop in template metaprogramming. Loops
|
||
can be implemented using <span class="emphasis"><em>recursion</em></span>: every recursive
|
||
call is one iteration of the loop. The loop is stopped at the bottom
|
||
of the recursive chain.
|
||
</p></td></tr>
|
||
</table></div>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h5 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_3_using_a_folding_parser_com"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_3_using_a_folding_parser_com" title="5.2.3. Using a folding parser combinator">5.2.3.
|
||
Using a folding parser combinator</a>
|
||
</h5></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_5_2_3">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
It would be nice, if the two loops could be merged together and the temporary
|
||
<code class="computeroutput"><span class="identifier">vector</span></code> wouldn't have
|
||
to be built in the middle (don't forget: there is no such thing as a
|
||
<span class="emphasis"><em>garbage collector</em></span> for template metaprogramming.
|
||
Once you instantiate a template, it will be available until the end of
|
||
... the compilation).
|
||
</p>
|
||
<p>
|
||
Metaparse provides the <a class="link" href="reference.html#foldl"><code class="computeroutput"><span class="identifier">foldl</span></code></a>
|
||
parser combinator:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
It is almost the same as <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code>,
|
||
but instead of taking the <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
as its first argument, which was coming from the repeated application
|
||
of a parser (<a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">></span></code>) on the input, it takes the parser
|
||
itself. <a class="link" href="reference.html#foldl"><code class="computeroutput"><span class="identifier">foldl</span></code></a>
|
||
parses the input and calculates the summary on the fly. Here is how we
|
||
can write our parser using it:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">transform</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">int_token</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_13">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
Here are the formatted versions of <code class="computeroutput"><span class="identifier">exp_parser9</span></code>
|
||
and <code class="computeroutput"><span class="identifier">exp_parser10</span></code> side-by-side:
|
||
</p>
|
||
<pre class="programlisting"><span class="comment">// exp_parser9 exp_parser10</span>
|
||
|
||
<span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span> <span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">,</span>
|
||
|
||
|
||
<span class="identifier">transform</span><span class="special"><</span> <span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">></span>
|
||
|
||
|
||
<span class="special">>,</span> <span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">></span>
|
||
<span class="special">></span> <span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_14">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
In <code class="computeroutput"><span class="identifier">exp_parser10</span></code> the "_<a class="link" href="reference.html#repeated"><code class="computeroutput"><span class="identifier">repeated</span></code></a>
|
||
and then <a class="link" href="reference.html#transform"><code class="computeroutput"><span class="identifier">transform</span></code></a>
|
||
with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span></code>_" part (the middle block
|
||
of <code class="computeroutput"><span class="identifier">exp_parser9</span></code>) has been
|
||
replaced by one <a class="link" href="reference.html#foldl"><code class="computeroutput"><span class="identifier">foldl</span></code></a>
|
||
parser that does the same thing but without building a <code class="computeroutput"><span class="identifier">vector</span></code> in the middle. The same starting
|
||
value (<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">></span></code>)
|
||
and callback function (<code class="computeroutput"><span class="identifier">sum_items</span></code>)
|
||
could be used.
|
||
</p>
|
||
<p>
|
||
Here is a diagram showing how <code class="computeroutput"><span class="identifier">exp_parser10</span></code>
|
||
works:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag5.png" width="90%"></span>
|
||
</p>
|
||
<p>
|
||
In this case, the results of the <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span>
|
||
<span class="identifier">int_token</span><span class="special">></span></code>
|
||
parsers are passed directly to a folding algorithm without an intermediate
|
||
<code class="computeroutput"><span class="identifier">vector</span></code>. Here is a diagram
|
||
showing <code class="computeroutput"><span class="identifier">exp_parser9</span></code> and
|
||
<code class="computeroutput"><span class="identifier">exp_parser10</span></code> side-by-side
|
||
to make it easier to see the difference:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag6.png" width="90%"></span>
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h5 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_4_processing_the_initial_ele"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.5_parsing_longer_expressions.5_2_evaluating_the_parsed_expres.5_2_4_processing_the_initial_ele" title="5.2.4. Processing the initial element with the folding parser combinator">5.2.4.
|
||
Processing the initial element with the folding parser combinator</a>
|
||
</h5></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_5_2_4">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
This solution can still be improved. The <a class="link" href="reference.html#foldl"><code class="computeroutput"><span class="identifier">foldl</span></code></a> summarising the <code class="computeroutput"><span class="special">+</span> <span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
elements starts from <code class="computeroutput"><span class="number">0</span></code> and
|
||
once this is done, we add the value of the first <code class="computeroutput"><span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
of the input to it in the first iteration. It would be more straightforward
|
||
if <a class="link" href="reference.html#foldl"><code class="computeroutput"><span class="identifier">foldl</span></code></a>
|
||
could use the value of the first <code class="computeroutput"><span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
as the initial value of the "<span class="emphasis"><em>sum we have so far</em></span>".
|
||
Metaparse provides <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a> for
|
||
this:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a>
|
||
is almost the same as <a class="link" href="reference.html#foldl"><code class="computeroutput"><span class="identifier">foldl</span></code></a>.
|
||
The difference is that instead of taking a starting <span class="emphasis"><em>value</em></span>
|
||
for the sum it takes a <span class="emphasis"><em>parser</em></span>. First it parses the
|
||
input with this parser and uses the value it returns as the starting
|
||
value. Here is how we can implement our parser using it:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_15">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
This version of <code class="computeroutput"><span class="identifier">exp_parser</span></code>
|
||
uses <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a>.
|
||
This implementation is more compact than the earlier versions. There
|
||
is no <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a>
|
||
element in this: the first <code class="computeroutput"><span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
is parsed by <code class="computeroutput"><span class="identifier">int_token</span></code>
|
||
and its value is used as the initial value for the summary. Let's try
|
||
it out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser11</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">10</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
It returns the same result as the earlier version but works differently.
|
||
Here is a diagram showing how this implementation works:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag7.png" width="90%"></span>
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.6_adding_support_for_other_opera"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.6_adding_support_for_other_opera" title="6. Adding support for other operators">6.
|
||
Adding support for other operators</a>
|
||
</h3></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.6_adding_support_for_other_opera.6_1_parsing_expressions_containi">6.1.
|
||
Parsing expressions containing <code class="computeroutput"><span class="special">-</span></code>
|
||
operators</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.6_adding_support_for_other_opera.6_2_evaluating_expressions_conta">6.2.
|
||
Evaluating expressions containing <code class="computeroutput"><span class="special">-</span></code>
|
||
operators</a></span></dt>
|
||
</dl></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined so
|
||
far <a class="link" href="getting_started_with_boost_metap.html#before_6">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Our parsers now support expressions adding numbers together. In this section
|
||
we will add support for the <code class="computeroutput"><span class="special">-</span></code>
|
||
operator, so expressions like <code class="computeroutput"><span class="number">1</span> <span class="special">+</span> <span class="number">2</span> <span class="special">-</span>
|
||
<span class="number">3</span></code> can be evaluated.
|
||
</p>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.6_adding_support_for_other_opera.6_1_parsing_expressions_containi"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.6_adding_support_for_other_opera.6_1_parsing_expressions_containi" title="6.1. Parsing expressions containing - operators">6.1.
|
||
Parsing expressions containing <code class="computeroutput"><span class="special">-</span></code>
|
||
operators</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_6_1">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Currently we use the <code class="computeroutput"><span class="identifier">plus_token</span></code>
|
||
for parsing "the" operator, which has to be <code class="computeroutput"><span class="special">+</span></code>.
|
||
We can define a new token for parsing the <code class="computeroutput"><span class="special">-</span></code>
|
||
symbol:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
</pre>
|
||
<p>
|
||
We need to build a parser that accepts either a <code class="computeroutput"><span class="special">+</span></code>
|
||
or a <code class="computeroutput"><span class="special">-</span></code> symbol. This can be
|
||
implemented using <a class="link" href="reference.html#one_of"><code class="computeroutput"><span class="identifier">one_of</span></code></a>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="reference.html#one_of"><code class="computeroutput"><span class="identifier">one_of</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">></span></code> is a parser which accepts either a
|
||
<code class="computeroutput"><span class="special">+</span></code> (using <code class="computeroutput"><span class="identifier">plus_token</span></code>)
|
||
or a <code class="computeroutput"><span class="special">-</span></code> (using <code class="computeroutput"><span class="identifier">minus_token</span></code>) symbol. The result of parsing
|
||
is the result of the parser that succeeded.
|
||
</p>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
You can give any parser to <a class="link" href="reference.html#one_of"><code class="computeroutput"><span class="identifier">one_of</span></code></a>,
|
||
therefore it is possible that more than one of them can parse the input.
|
||
In those cases the order matters: <a class="link" href="reference.html#one_of"><code class="computeroutput"><span class="identifier">one_of</span></code></a> tries parsing the input
|
||
with the parsers from left to right and the first one that succeeds,
|
||
wins.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Using this, we can make our parser accept subtractions as well:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">int_token</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_16">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
It uses <a class="link" href="reference.html#one_of"><code class="computeroutput"><span class="identifier">one_of</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">></span></code> as the separator for the numbers. Let's
|
||
try it out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser12</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 - 3"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">6</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
The result is not correct. The reason for this is that <code class="computeroutput"><span class="identifier">sum_items</span></code>,
|
||
the function we summarise with ignores which operator was used and assumes
|
||
that it is always <code class="computeroutput"><span class="special">+</span></code>.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.6_adding_support_for_other_opera.6_2_evaluating_expressions_conta"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.6_adding_support_for_other_opera.6_2_evaluating_expressions_conta" title="6.2. Evaluating expressions containing - operators">6.2.
|
||
Evaluating expressions containing <code class="computeroutput"><span class="special">-</span></code>
|
||
operators</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_6_2">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
To fix the evaluation of expressions containing subtractions, we need to
|
||
fix the function we use for summarising. We need to write a version that
|
||
takes the operator being used into account.
|
||
</p>
|
||
<p>
|
||
First of all we will need the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span></code>
|
||
<a class="link" href="reference.html#metafunction">metafunction</a> for implementing subtraction:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
Let's write a helper metafunction that takes three arguments: the left
|
||
operand, the operator and the right operand:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
<span class="special">></span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
<span class="special">></span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_17">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
The first command declares the <code class="computeroutput"><span class="identifier">eval_binary_op</span></code>
|
||
metafunction. The first and third arguments are the left and right operands
|
||
and the second argument is the operator.
|
||
</p>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that it does not satisfy the expectations of a <a class="link" href="reference.html#metafunction">template
|
||
metafunction</a> since it takes the operator as a <code class="computeroutput"><span class="keyword">char</span></code>
|
||
and not as a <code class="computeroutput"><span class="keyword">class</span></code> (or
|
||
<code class="computeroutput"><span class="keyword">typename</span></code>) argument. For
|
||
simplicity, we will still call it a metafunction.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
The second and third commands define the operation for the cases when the
|
||
operator is <code class="computeroutput"><span class="special">+</span></code> and <code class="computeroutput"><span class="special">-</span></code>. When the <code class="computeroutput"><span class="identifier">eval_binary_op</span></code>
|
||
metafunction is called, the C++ compiler chooses one of the definitions
|
||
based on the operator. If you have functional programming experience this
|
||
approach (pattern matching) might be familiar to you. Let's try <code class="computeroutput"><span class="identifier">eval_binary_op</span></code> out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">11</span><span class="special">>,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">13</span><span class="special">></span>
|
||
<span class="special">></span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">13</span><span class="special">>,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">11</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_18">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
You might also try to use it with an operator it does not expect (yet).
|
||
For example <code class="computeroutput"><span class="char">'*'</span></code>. You will see
|
||
the C++ compiler complaining about that the requested version of the <code class="computeroutput"><span class="identifier">eval_binary_op</span></code> template has not been
|
||
defined. This solution can be extended and support for the <code class="computeroutput"><span class="char">'*'</span></code> operator can always be added later.
|
||
</p>
|
||
<p>
|
||
Let's write the <a class="link" href="reference.html#metafunction">metafunction</a> we can
|
||
use from the folding parser to evaluate the expressions using <code class="computeroutput"><span class="special">+</span></code> and <code class="computeroutput"><span class="special">-</span></code>
|
||
operators. This takes two arguments:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
The partial result we have evaluated so far. (This used to be the summary
|
||
we have evaluated so far, but we are making it a more general evaluation
|
||
now). This is the left operand, a number.
|
||
</li>
|
||
<li class="listitem">
|
||
The result of parsing <code class="computeroutput"><span class="special">(+|-)</span>
|
||
<span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>. This a <code class="computeroutput"><span class="identifier">vector</span></code>
|
||
containing two elements: a character representing the operator (<code class="computeroutput"><span class="special">+</span></code> or <code class="computeroutput"><span class="special">-</span></code>)
|
||
and the value of the <code class="computeroutput"><span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>.
|
||
The number is the right operand.
|
||
</li>
|
||
</ul></div>
|
||
<p>
|
||
Let's write the <a class="link" href="reference.html#metafunction">metafunction</a> <code class="computeroutput"><span class="identifier">binary_op</span></code> that takes these arguments
|
||
and calls <code class="computeroutput"><span class="identifier">eval_binary_op</span></code>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">eval_binary_op</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">S</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">{};</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_19">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
This <a class="link" href="reference.html#metafunction">metafunction</a> takes the operator
|
||
(the first element) and the right operand (the second element) from <code class="computeroutput"><span class="identifier">Item</span></code>. The operator is a class representing
|
||
a character, such as <code class="computeroutput"><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">></span></code>.
|
||
To get the character value out of it, one has to access its <code class="computeroutput"><span class="special">::</span><span class="identifier">value</span></code>.
|
||
For example <code class="computeroutput"><span class="identifier">mpl_</span><span class="special">::</span><span class="keyword">char</span><span class="special"><</span><span class="char">'+'</span><span class="special">>::</span><span class="identifier">value</span></code>
|
||
is <code class="computeroutput"><span class="char">'+'</span></code>. Since <code class="computeroutput"><span class="identifier">eval_binary_op</span></code>
|
||
takes this character value as its second argument, we had to pass <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span></code>
|
||
to it. Let's try it out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">binary_op</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">11</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>>>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">13</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
We passed it a number (<code class="computeroutput"><span class="number">11</span></code>)
|
||
and a <code class="computeroutput"><span class="identifier">vector</span></code> of a character
|
||
(<code class="computeroutput"><span class="special">+</span></code>) and another number (<code class="computeroutput"><span class="number">2</span></code>). It added the two numbers as expected.
|
||
Let's use this function as the third argument of <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">int_token</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_20">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
It uses <code class="computeroutput"><span class="identifier">binary_op</span></code> instead
|
||
of <code class="computeroutput"><span class="identifier">sum_items</span></code>. Let's try
|
||
it out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser13</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 - 3"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">0</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
It returns the correct result.
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.7_dealing_with_precedence"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.7_dealing_with_precedence" title="7. Dealing with precedence">7.
|
||
Dealing with precedence</a>
|
||
</h3></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.7_dealing_with_precedence.7_1_adding_support_for_the_opera">7.1.
|
||
Adding support for the <code class="computeroutput"><span class="special">*</span></code> operator</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.7_dealing_with_precedence.7_2_adding_support_for_precedenc">7.2.
|
||
Adding support for precedence of operators</a></span></dt>
|
||
</dl></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined so
|
||
far <a class="link" href="getting_started_with_boost_metap.html#before_7">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
We support addition and subtraction. Let's support multiplication as well.
|
||
</p>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.7_dealing_with_precedence.7_1_adding_support_for_the_opera"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.7_dealing_with_precedence.7_1_adding_support_for_the_opera" title="7.1. Adding support for the * operator">7.1.
|
||
Adding support for the <code class="computeroutput"><span class="special">*</span></code> operator</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_7_1">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
We can extend the solution we have built for addition and subtraction.
|
||
To do that, we need to add support for multiplication to <code class="computeroutput"><span class="identifier">eval_binary_op</span></code>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="special">></span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_21">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
We had to include <code class="computeroutput"><span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code> to get the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span></code>
|
||
<a class="link" href="reference.html#metafunction">metafunction</a> and then we could extend
|
||
<code class="computeroutput"><span class="identifier">eval_binary_op</span></code> to support
|
||
the <code class="computeroutput"><span class="special">*</span></code> operator as well. We
|
||
can try it out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">3</span><span class="special">>,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">4</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">12</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This works as expected. Let's create a token for parsing the <code class="computeroutput"><span class="special">*</span></code> symbol:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
</pre>
|
||
<p>
|
||
Now we can extend our parser to accept the <code class="computeroutput"><span class="special">*</span></code>
|
||
symbol as an operator:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">int_token</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_22">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
This version accepts either a <code class="computeroutput"><span class="special">+</span></code>,
|
||
a <code class="computeroutput"><span class="special">-</span></code> or a <code class="computeroutput"><span class="special">*</span></code>
|
||
symbol as the operator. Let's try this out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser14</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"2 * 3"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">6</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This works as expected. Let's try another, slightly more complicated expression:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser14</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 * 3"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">9</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This returns a wrong result. The value of this expression should be <code class="computeroutput"><span class="number">7</span></code>, not <code class="computeroutput"><span class="number">9</span></code>.
|
||
The problem with this is that our current implementation does not take
|
||
operator precedence into account. It treats this expression as <code class="computeroutput"><span class="special">(</span><span class="number">1</span> <span class="special">+</span>
|
||
<span class="number">2</span><span class="special">)</span> <span class="special">*</span> <span class="number">3</span></code> while
|
||
we expect it to be <code class="computeroutput"><span class="number">1</span> <span class="special">+</span>
|
||
<span class="special">(</span><span class="number">2</span> <span class="special">*</span> <span class="number">3</span><span class="special">)</span></code>
|
||
since addition has higher precedence than multiplication.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.7_dealing_with_precedence.7_2_adding_support_for_precedenc"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.7_dealing_with_precedence.7_2_adding_support_for_precedenc" title="7.2. Adding support for precedence of operators">7.2.
|
||
Adding support for precedence of operators</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_7_2">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Let's make it possible for different operators to have different precedence.
|
||
To do this, we define a new parser for parsing expressions containing only
|
||
<code class="computeroutput"><span class="special">*</span></code> operators (that is the operator
|
||
with the lowest precedence):
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
</pre>
|
||
<p>
|
||
<code class="computeroutput"><span class="identifier">mult_exp1</span></code> can parse expressions
|
||
containing only <code class="computeroutput"><span class="special">*</span></code> operator.
|
||
For example <code class="computeroutput"><span class="number">3</span> <span class="special">*</span>
|
||
<span class="number">2</span></code> or <code class="computeroutput"><span class="number">6</span>
|
||
<span class="special">*</span> <span class="number">7</span> <span class="special">*</span> <span class="number">8</span></code>. Now
|
||
we can create a parser supporting only the <code class="computeroutput"><span class="special">+</span></code>
|
||
and <code class="computeroutput"><span class="special">-</span></code> operators but instead
|
||
of separating <span class="emphasis"><em>numbers</em></span> with these operators we will
|
||
separate <span class="emphasis"><em>expressions containing only <code class="computeroutput"><span class="special">*</span></code>
|
||
operators</em></span>. This means that the expression <code class="computeroutput"><span class="number">1</span>
|
||
<span class="special">*</span> <span class="number">2</span> <span class="special">+</span> <span class="number">3</span> <span class="special">*</span>
|
||
<span class="number">4</span></code> is interpreted as the expressions
|
||
<code class="computeroutput"><span class="number">1</span> <span class="special">*</span>
|
||
<span class="number">2</span></code> and <code class="computeroutput"><span class="number">3</span>
|
||
<span class="special">*</span> <span class="number">4</span></code>
|
||
separated by a <code class="computeroutput"><span class="special">+</span></code> operator.
|
||
A number (eg. <code class="computeroutput"><span class="number">13</span></code>) is the special
|
||
case of an <span class="emphasis"><em>expression containing only <code class="computeroutput"><span class="special">*</span></code>
|
||
operators</em></span>.
|
||
</p>
|
||
<p>
|
||
Here is the parser implementing this:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mult_exp1</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_23">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
Note that this is almost the same as <code class="computeroutput"><span class="identifier">exp_parser13</span></code>.
|
||
The only difference is that it uses <code class="computeroutput"><span class="identifier">mult_exp1</span></code>
|
||
everywhere, where <code class="computeroutput"><span class="identifier">exp_parser13</span></code>
|
||
was using <code class="computeroutput"><span class="identifier">int_token</span></code>. Let's
|
||
try it out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser15</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 * 3"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">7</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This takes the precedence rules into account. The following diagram shows
|
||
how it works:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag8.png" width="80%"></span>
|
||
</p>
|
||
<p>
|
||
Subexpressions using <code class="computeroutput"><span class="special">*</span></code> operators
|
||
only are evaluated (by <code class="computeroutput"><span class="identifier">mult_exp1</span></code>)
|
||
and treated as single units while interpreting expressions using <code class="computeroutput"><span class="special">+</span></code> and <code class="computeroutput"><span class="special">-</span></code>
|
||
operators. Numbers not surrounded by <code class="computeroutput"><span class="special">*</span></code>
|
||
operators are treated also as operators using <code class="computeroutput"><span class="special">*</span></code>
|
||
only (containing no operations but a number).
|
||
</p>
|
||
<p>
|
||
If you need more layers (eg. introducing the <code class="computeroutput"><span class="special">^</span></code>
|
||
operator) you can extend this solution with further layers. The order of
|
||
the layers determine the precedence of the operators.
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.8_dealing_with_associativity"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.8_dealing_with_associativity" title="8. Dealing with associativity">8.
|
||
Dealing with associativity</a>
|
||
</h3></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.8_dealing_with_associativity.8_1_understanding_the_current_im">8.1.
|
||
Understanding the current implementation</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.8_dealing_with_associativity.8_2_folding_in_reverse_order">8.2.
|
||
Folding in reverse order</a></span></dt>
|
||
</dl></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined so
|
||
far <a class="link" href="getting_started_with_boost_metap.html#before_8">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Let's add division to our calculator language. Since it has the same precedence
|
||
as multiplication, it should be added to that layer:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="special">></span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">int_token</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mult_exp2</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_24">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
We have to include <code class="computeroutput"><span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code>
|
||
to get a <a class="link" href="reference.html#metafunction">metafunction</a> for doing a division.
|
||
We need to extend the <code class="computeroutput"><span class="identifier">eval_binary_op</span></code>
|
||
<a class="link" href="reference.html#metafunction">metafunction</a> to support division as well.
|
||
We had to introduce a new token, <code class="computeroutput"><span class="identifier">divides_token</span></code>
|
||
that can parse the <code class="computeroutput"><span class="special">/</span></code> symbol.
|
||
</p>
|
||
<p>
|
||
We have extended <code class="computeroutput"><span class="identifier">mult_exp1</span></code>
|
||
to accept either a <code class="computeroutput"><span class="identifier">times_token</span></code>
|
||
or a <code class="computeroutput"><span class="identifier">divides_token</span></code> as the
|
||
operator. This extended parser is called <code class="computeroutput"><span class="identifier">mult_exp2</span></code>.
|
||
</p>
|
||
<p>
|
||
We have written a new parser, <code class="computeroutput"><span class="identifier">exp_parser16</span></code>
|
||
which is the same as <code class="computeroutput"><span class="identifier">exp_parser15</span></code>
|
||
but uses <code class="computeroutput"><span class="identifier">mult_exp2</span></code> instead
|
||
of <code class="computeroutput"><span class="identifier">mult_exp1</span></code>. This can parse
|
||
expressions using division as well (and this new operator has the right precedence).
|
||
Let's try it out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser16</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"8 / 4"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This works as expected. But what should be the value of <code class="computeroutput"><span class="number">8</span>
|
||
<span class="special">/</span> <span class="number">4</span> <span class="special">/</span> <span class="number">2</span></code>? The answer
|
||
can be either <code class="computeroutput"><span class="number">1</span></code> or <code class="computeroutput"><span class="number">4</span></code> depending on the associativity of the division
|
||
operator. If it is left associative, then this expressions is interpreted
|
||
as <code class="computeroutput"><span class="special">(</span><span class="number">8</span>
|
||
<span class="special">/</span> <span class="number">4</span><span class="special">)</span> <span class="special">/</span> <span class="number">2</span></code>
|
||
and the result is <code class="computeroutput"><span class="number">1</span></code>. If it is
|
||
right associative, this expression is interpreted as <code class="computeroutput"><span class="number">8</span>
|
||
<span class="special">/</span> <span class="special">(</span><span class="number">4</span> <span class="special">/</span> <span class="number">2</span><span class="special">)</span></code> and the result is <code class="computeroutput"><span class="number">4</span></code>.
|
||
</p>
|
||
<p>
|
||
Try to guess which result our current implementation gives before trying
|
||
it out. Once you have verified the current behaviour, continue reading.
|
||
</p>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.8_dealing_with_associativity.8_1_understanding_the_current_im"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.8_dealing_with_associativity.8_1_understanding_the_current_im" title="8.1. Understanding the current implementation">8.1.
|
||
Understanding the current implementation</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_8_1">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Here is a diagram showing how our current parser processes the expression
|
||
<code class="computeroutput"><span class="number">8</span> <span class="special">/</span>
|
||
<span class="number">4</span> <span class="special">/</span> <span class="number">2</span></code>:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag8.png" width="70%"></span>
|
||
</p>
|
||
<p>
|
||
It takes the first number, <code class="computeroutput"><span class="number">8</span></code>,
|
||
divides it by the second one, <code class="computeroutput"><span class="number">4</span></code>
|
||
and then it divides the result with the third one, <code class="computeroutput"><span class="number">2</span></code>.
|
||
This means, that in our current implementation, division is left associative:
|
||
<code class="computeroutput"><span class="number">8</span> <span class="special">/</span>
|
||
<span class="number">4</span> <span class="special">/</span> <span class="number">2</span></code> means <code class="computeroutput"><span class="special">(</span><span class="number">8</span> <span class="special">/</span> <span class="number">4</span><span class="special">)</span> <span class="special">/</span> <span class="number">2</span></code>.
|
||
</p>
|
||
<p>
|
||
Another thing to note is that the initial value is <code class="computeroutput"><span class="number">8</span></code>
|
||
and the list of values <a class="link" href="reference.html#foldl"><code class="computeroutput"><span class="identifier">foldl</span></code></a>
|
||
iterates over is "<code class="computeroutput"><span class="special">/</span> <span class="number">4</span></code>", "<code class="computeroutput"><span class="special">/</span>
|
||
<span class="number">2</span></code>".
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.8_dealing_with_associativity.8_2_folding_in_reverse_order"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.8_dealing_with_associativity.8_2_folding_in_reverse_order" title="8.2. Folding in reverse order">8.2.
|
||
Folding in reverse order</a>
|
||
</h4></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined
|
||
so far <a class="link" href="getting_started_with_boost_metap.html#before_8_2">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
<a class="link" href="reference.html#foldl"><code class="computeroutput"><span class="identifier">foldl</span></code></a>
|
||
applies a parser repeatedly and iterates over the parsing results from
|
||
<span class="emphasis"><em>left</em></span> to right. (This is where the <code class="computeroutput"><span class="identifier">l</span></code>
|
||
in the name comes from). Metaparse provides another folding parser combinator,
|
||
<a class="link" href="reference.html#foldr"><code class="computeroutput"><span class="identifier">foldr</span></code></a>.
|
||
It applies a parser on the input as well but it iterates from <span class="emphasis"><em>right</em></span>
|
||
to left over the results.
|
||
</p>
|
||
<p>
|
||
Similarly to <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a>,
|
||
Metaparse provides <a class="link" href="reference.html#foldr_start_with_parser"><code class="computeroutput"><span class="identifier">foldr_start_with_parser</span></code></a> as well.
|
||
A major difference between the two (<a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a> and <a class="link" href="reference.html#foldr_start_with_parser"><code class="computeroutput"><span class="identifier">foldr_start_with</span><span class="special">-</span><span class="identifier">parser</span></code></a>)
|
||
solutions is that while <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a> treats
|
||
the <span class="emphasis"><em>first</em></span> number as a special one, <a class="link" href="reference.html#foldr_start_with_parser"><code class="computeroutput"><span class="identifier">foldr_start_with_parser</span></code></a> treats
|
||
the <span class="emphasis"><em>last</em></span> number as a special one. This might sound
|
||
strange, but think about it: if you want to summarise the elements from
|
||
right to left, your starting value should be the last element, not the
|
||
first one, as the first one is the one you visit last.
|
||
</p>
|
||
<p>
|
||
Due to the above difference <a class="link" href="reference.html#foldr_start_with_parser"><code class="computeroutput"><span class="identifier">foldr_start_with_parser</span></code></a> is not
|
||
a drop-in replacement of <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a>. While
|
||
the list of values <a class="link" href="reference.html#foldl"><code class="computeroutput"><span class="identifier">foldl</span></code></a>
|
||
was iterating over is "<code class="computeroutput"><span class="number">8</span></code>",
|
||
"<code class="computeroutput"><span class="special">/</span> <span class="number">4</span></code>",
|
||
"<code class="computeroutput"><span class="special">/</span> <span class="number">2</span></code>",
|
||
the list of values <a class="link" href="reference.html#foldr"><code class="computeroutput"><span class="identifier">foldlr</span></code></a>
|
||
has to iterate over is "<code class="computeroutput"><span class="number">2</span></code>",
|
||
"<code class="computeroutput"><span class="number">4</span> <span class="special">/</span></code>",
|
||
"<code class="computeroutput"><span class="number">8</span> <span class="special">/</span></code>".
|
||
</p>
|
||
<p>
|
||
This means that the function we use to <span class="emphasis"><em>"add"</em></span>
|
||
a new value to the already evaluated part of the expression (this has been
|
||
<code class="computeroutput"><span class="identifier">binary_op</span></code> so far) has to
|
||
be prepared for taking the next operator and operand in a reverse order
|
||
(eg. by taking "<code class="computeroutput"><span class="number">4</span> <span class="special">/</span></code>"
|
||
instead of "<code class="computeroutput"><span class="special">/</span> <span class="number">4</span></code>").
|
||
We write another <a class="link" href="reference.html#metafunction">metafunction</a> for this
|
||
purpose:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="keyword">struct</span> <span class="identifier">reverse_binary_op</span> <span class="special">:</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">eval_binary_op</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">S</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">{};</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_25">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
There are multiple differences between <code class="computeroutput"><span class="identifier">binary_op</span></code>
|
||
and <code class="computeroutput"><span class="identifier">reverse_binary_op</span></code>:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
The <code class="computeroutput"><span class="identifier">Item</span></code> argument,
|
||
which is a <code class="computeroutput"><span class="identifier">vector</span></code> is
|
||
expected to be <code class="computeroutput"><span class="special">[</span><span class="keyword">operator</span><span class="special">,</span> <span class="identifier">operand</span><span class="special">]</span></code> in <code class="computeroutput"><span class="identifier">binary_op</span></code>
|
||
and <code class="computeroutput"><span class="special">[</span><span class="identifier">operand</span><span class="special">,</span> <span class="keyword">operator</span><span class="special">]</span></code> in <code class="computeroutput"><span class="identifier">reverse_binary_op</span></code>.
|
||
</li>
|
||
<li class="listitem">
|
||
Both versions use <code class="computeroutput"><span class="identifier">eval_binary_op</span></code>
|
||
to evaluate the subexpression, but <code class="computeroutput"><span class="identifier">binary_op</span></code>
|
||
treats <code class="computeroutput"><span class="identifier">S</span></code>, the value
|
||
representing the already evaluated part of the expression as the left
|
||
operand, while <code class="computeroutput"><span class="identifier">reverse_binary_op</span></code>
|
||
treats it as the right operand. This is because in the first case we
|
||
are going from left to right while in the second case we are going
|
||
from right to left.
|
||
</li>
|
||
</ul></div>
|
||
<p>
|
||
We need to include <a class="link" href="reference.html#foldr_start_with_parser"><code class="computeroutput"><span class="identifier">foldr_start_with_parser</span></code></a>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldr_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
We can rewrite <code class="computeroutput"><span class="identifier">mult_exp</span></code>
|
||
using <a class="link" href="reference.html#foldr_start_with_parser"><code class="computeroutput"><span class="identifier">foldr_start_with_parser</span></code></a>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">mult_exp3</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldr_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>>,</span> <span class="comment">/* The parser applied repeatedly */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The parser parsing the last number */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">reverse_binary_op</span><span class="special">></span> <span class="comment">/* The function called for every result */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="comment">/* of applying the above parser */</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_26">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
It is almost the same as <code class="computeroutput"><span class="identifier">mult_exp2</span></code>,
|
||
but ...
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
... the parser applied repeatedly parses <code class="computeroutput"><span class="special"><</span><span class="identifier">number</span><span class="special">></span>
|
||
<span class="special"><</span><span class="keyword">operator</span><span class="special">></span></code> elements instead of <code class="computeroutput"><span class="special"><</span><span class="keyword">operator</span><span class="special">></span> <span class="special"><</span><span class="identifier">number</span><span class="special">></span></code>
|
||
elements (what <code class="computeroutput"><span class="identifier">mult_exp2</span></code>
|
||
did).
|
||
</li>
|
||
<li class="listitem">
|
||
... this version uses <code class="computeroutput"><span class="identifier">reverse_binary_op</span></code>
|
||
instead of <code class="computeroutput"><span class="identifier">binary_op</span></code>
|
||
as the function that is called for every result of applying the above
|
||
parser.
|
||
</li>
|
||
</ul></div>
|
||
<p>
|
||
We can create a new version of <code class="computeroutput"><span class="identifier">exp_parser</span></code>
|
||
that uses <code class="computeroutput"><span class="identifier">mult_exp3</span></code> instead
|
||
of <code class="computeroutput"><span class="identifier">mult_exp2</span></code>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser17</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mult_exp3</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_27">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
The only difference between <code class="computeroutput"><span class="identifier">exp_parser17</span></code>
|
||
and the previous version, <code class="computeroutput"><span class="identifier">exp_parser16</span></code>
|
||
is that it uses the updated version of <code class="computeroutput"><span class="identifier">mult_exp</span></code>.
|
||
Let's try this parser out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser17</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"8 / 4 / 2"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">4</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This version of the parser gives <span class="emphasis"><em>the other</em></span> possible
|
||
result. The one you get when division is right associative, which means
|
||
that the above expression is evaluated as <code class="computeroutput"><span class="number">8</span>
|
||
<span class="special">/</span> <span class="special">(</span><span class="number">4</span> <span class="special">/</span> <span class="number">2</span><span class="special">)</span></code>. Here is a diagram showing how the <a class="link" href="reference.html#foldr_start_with_parser"><code class="computeroutput"><span class="identifier">foldr_start_with_parser</span></code></a>-based
|
||
solution works:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag10.png" width="70%"></span>
|
||
</p>
|
||
<p>
|
||
To make it easier to compare the two solutions, here is a diagram showing
|
||
the two approaches side-by-side:
|
||
</p>
|
||
<p>
|
||
<span class="inlinemediaobject"><img src="../images/metaparse/tutorial_diag11.png" width="100%"></span>
|
||
</p>
|
||
<p>
|
||
As we have seen, the associativity of the operators can be controlled by
|
||
choosing between folding solutions. The folding solutions going from left
|
||
to right implement left associativity, while the solutions going from right
|
||
to left implement right associativity.
|
||
</p>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that folding solutions going from left to right is implemented in
|
||
a more efficient way than folding from right to left. Therefore when
|
||
both solutions can be used you should prefer folding from left to right.
|
||
</p></td></tr>
|
||
</table></div>
|
||
</div>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.9_dealing_with_unary_operators"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.9_dealing_with_unary_operators" title="9. Dealing with unary operators">9.
|
||
Dealing with unary operators</a>
|
||
</h3></div></div></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that you can find everything that has been included and defined so
|
||
far <a class="link" href="getting_started_with_boost_metap.html#before_9">here</a>.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Our calculator language provides no direct support for negative numbers.
|
||
To get a negative number, we need to do a subtraction. For example to get
|
||
the number <code class="computeroutput"><span class="special">-</span><span class="number">13</span></code>
|
||
we need to evaluate the expression <code class="computeroutput"><span class="number">0</span>
|
||
<span class="special">-</span> <span class="number">13</span></code>.
|
||
</p>
|
||
<p>
|
||
We will implement <code class="computeroutput"><span class="special">-</span></code> as a unary
|
||
operator. Therefore the expression <code class="computeroutput"><span class="special">-</span><span class="number">13</span></code> won't be a <span class="emphasis"><em>negative number</em></span>.
|
||
It will be the unary <code class="computeroutput"><span class="special">-</span></code> operator
|
||
applied on the number <code class="computeroutput"><span class="number">13</span></code>.
|
||
</p>
|
||
<p>
|
||
Since <code class="computeroutput"><span class="special">-</span></code> is an operator, it might
|
||
be used multiple times. So the expression <code class="computeroutput"><span class="special">---</span><span class="number">13</span></code> is also valid and gives the same result
|
||
as <code class="computeroutput"><span class="special">-</span><span class="number">13</span></code>.
|
||
This means that any number of <code class="computeroutput"><span class="special">-</span></code>
|
||
symbols are valid before a number.
|
||
</p>
|
||
<p>
|
||
Our parser can be extended to support the unary <code class="computeroutput"><span class="special">-</span></code>
|
||
operator by adding a new layer to the list of precedence layers. This should
|
||
have the lowest precedence, which means that we should use this new layer
|
||
where we have been using <code class="computeroutput"><span class="identifier">int_token</span></code>.
|
||
Let's write a new parser:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">negate</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">unary_exp1</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldr_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">int_token</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_28">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
We had to include <code class="computeroutput"><span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">negate</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code>
|
||
to get a <a class="link" href="reference.html#metafunction">metafunction</a> we can negate a
|
||
value with.
|
||
</p>
|
||
<p>
|
||
<code class="computeroutput"><span class="identifier">unary_exp1</span></code> is implemented
|
||
with right to left folding: it starts from the number (parsed by <code class="computeroutput"><span class="identifier">int_token</span></code>) and processes the <code class="computeroutput"><span class="special">-</span></code> symbols one by one. The function to be called
|
||
for each <code class="computeroutput"><span class="special">-</span></code> symbol is a lambda
|
||
expression that negates the number. So the number is negated for every <code class="computeroutput"><span class="special">-</span></code> symbol.
|
||
</p>
|
||
<p>
|
||
We can implement a new version of <code class="computeroutput"><span class="identifier">mult_exp</span></code>
|
||
and <code class="computeroutput"><span class="identifier">exp_parser</span></code>. They are
|
||
the same as <code class="computeroutput"><span class="identifier">mult_exp2</span></code> and
|
||
<code class="computeroutput"><span class="identifier">exp_parser16</span></code>. The only difference
|
||
is that they (directly only <code class="computeroutput"><span class="identifier">exp_parser18</span></code>)
|
||
use <code class="computeroutput"><span class="identifier">unary_exp1</span></code> instead of
|
||
<code class="computeroutput"><span class="identifier">int_token</span></code>.
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">mult_exp4</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp1</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">unary_exp1</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser18</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mult_exp4</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_29">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
Let's try these new parsers out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser18</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"---13"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="special">-</span><span class="number">13</span><span class="special">></span>
|
||
<span class="special">></span> <span class="identifier">exp_parser18</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"13"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">13</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_30">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
It can deal with negative numbers correctly.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.10_dealing_with_parens"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.10_dealing_with_parens" title="10. Dealing with parens">10.
|
||
Dealing with parens</a>
|
||
</h3></div></div></div>
|
||
<p>
|
||
Our parsers already support the precedence of the different operators. Let's
|
||
add support for parens as well, so users can override the precedence rules
|
||
when they need to.
|
||
</p>
|
||
<p>
|
||
We can add a new parser for parsing (and evaluating) expressions in parens.
|
||
First we introduce tokens for parsing the <code class="computeroutput"><span class="special">(</span></code>
|
||
and <code class="computeroutput"><span class="special">)</span></code> symbols:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">lparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'('</span><span class="special">>>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">rparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">')'</span><span class="special">>>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_31">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
A paren can contain an expression with any operators in it, so we add a parser
|
||
for parsing (and evaluating) an expression containing operators of the highest
|
||
precedence:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">plus_exp1</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mult_exp4</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_32">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
This was just a refactoring of our last parser for the calculator language.
|
||
We can build the parser for our calculator language by using <a class="link" href="reference.html#build_parser"><code class="computeroutput"><span class="identifier">build_parser</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">plus_exp1</span><span class="special">></span></code>
|
||
now. Let's write a parser for a paren expression:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">paren_exp1</span> <span class="special">=</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
This definition parses a left paren, then a complete expression followed
|
||
by a right paren. The result of parsing a paren expression is a <code class="computeroutput"><span class="identifier">vector</span></code> of three elements: the <code class="computeroutput"><span class="special">(</span></code> character, the value of the expression and
|
||
the <code class="computeroutput"><span class="special">)</span></code> character. We only need
|
||
the value of the expression, which is the middle element. We could wrap the
|
||
whole thing with a <a class="link" href="reference.html#transform"><code class="computeroutput"><span class="identifier">transform</span></code></a>
|
||
that gets the middle element and throws the rest away, but we don't need
|
||
to. This is such a common pattern, that Metaparse provides <a class="link" href="reference.html#middle_of"><code class="computeroutput"><span class="identifier">middle_of</span></code></a> for this:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">middle_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">paren_exp2</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_33">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
This implementation is almost the same as <code class="computeroutput"><span class="identifier">paren_exp1</span></code>.
|
||
The difference is that the result of parsing will be the value of the wrapped
|
||
expression (the result of the <code class="computeroutput"><span class="identifier">plus_exp1</span></code>
|
||
parser).
|
||
</p>
|
||
<p>
|
||
Let's define a parser for a primary expression which is either a number or
|
||
an expression in parens:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">primary_exp1</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
This parser accepts either a number using <code class="computeroutput"><span class="identifier">int_token</span></code>
|
||
or an expression in parens using <code class="computeroutput"><span class="identifier">paren_exp1</span></code>.
|
||
</p>
|
||
<p>
|
||
Everywhere, where one can write a number (parsed by <code class="computeroutput"><span class="identifier">int_token</span></code>),
|
||
one can write a complete expression in parens as well. Our current parser
|
||
implementation parses <code class="computeroutput"><span class="identifier">int_token</span></code>s
|
||
in <code class="computeroutput"><span class="identifier">unary_exp</span></code>, therefore we
|
||
need to change that to use <code class="computeroutput"><span class="identifier">primary_exp</span></code>
|
||
instead of <code class="computeroutput"><span class="identifier">int_token</span></code>.
|
||
</p>
|
||
<p>
|
||
There is a problem here: this makes the definitions of our parsers <span class="emphasis"><em>recursive</em></span>.
|
||
Think about it:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">plus_exp</span></code> uses <code class="computeroutput"><span class="identifier">mult_exp</span></code>
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">mult_exp</span></code> uses <code class="computeroutput"><span class="identifier">unary_exp</span></code>
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">unary_exp</span></code> uses <code class="computeroutput"><span class="identifier">primary_exp</span></code>
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">primary_exp</span></code> uses <code class="computeroutput"><span class="identifier">paren_exp</span></code>
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">paren_exp</span></code> uses <code class="computeroutput"><span class="identifier">plus_exp</span></code>
|
||
</li>
|
||
</ul></div>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Since we are versioning the different parser implementations in Metashell
|
||
(<code class="computeroutput"><span class="identifier">paren_exp1</span></code>, <code class="computeroutput"><span class="identifier">paren_exp2</span></code>, etc) you might try to define
|
||
these recursive parsers and it might seem to work for the first time. In
|
||
that case, when you later try creating a parser as part of a library (save
|
||
your Metashell environment to a file or re-implement the important/successful
|
||
elements) you face this issue.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
We have been using type aliases (<code class="computeroutput"><span class="keyword">typedef</span></code>
|
||
and <code class="computeroutput"><span class="keyword">using</span></code>) for defining the
|
||
parsers. We can do it as long as their definition is not recursive. We can
|
||
not refer to a type alias until we have defined it and type aliases can not
|
||
be forward declared, so we can't find a point in the recursive cycle where
|
||
we could start defining things.
|
||
</p>
|
||
<p>
|
||
A solution for this is making one of the parsers a new class instead of a
|
||
type alias. Classes can be forward declared, therefore we can declare the
|
||
class, implement the rest of the parsers as they can refer to that class
|
||
and then define the class at the end.
|
||
</p>
|
||
<p>
|
||
Let's make <code class="computeroutput"><span class="identifier">plus_exp</span></code> a class.
|
||
So as a first step, let's forward declare it:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">plus_exp2</span><span class="special">;</span>
|
||
</pre>
|
||
<p>
|
||
Now we can write the rest of the parsers and they can refer to <code class="computeroutput"><span class="identifier">plus_exp2</span></code>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">paren_exp3</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp2</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">primary_exp2</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">unary_exp2</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldr_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">primary_exp2</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">mult_exp5</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp2</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">unary_exp2</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_34">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
There is nothing new in the definition of these parsers. They build up the
|
||
hierarchy we have worked out in the earlier sections of this tutorial. The
|
||
only element missing is <code class="computeroutput"><span class="identifier">plus_exp2</span></code>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">plus_exp2</span> <span class="special">:</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp5</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mult_exp5</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">{};</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_35">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
This definition makes use of inheritance instead of type aliasing. Now we
|
||
can write the parser for the calculator that supports parens as well:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser19</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp2</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
Let's try this parser out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser19</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"(1 + 2) * 3"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">9</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
Our parser accepts and can deal with parens in the expressions.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input"></a><a name="dealing_with_invalid_input"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input" title="11. Dealing with invalid input">11.
|
||
Dealing with invalid input</a>
|
||
</h3></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_1_improving_the_error_message">11.1.
|
||
Improving the error messages</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_2_defining_custom_errors">11.2.
|
||
Defining custom errors</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_3_missing_closing_parens">11.3.
|
||
Missing closing parens</a></span></dt>
|
||
</dl></div>
|
||
<p>
|
||
So far we have been focusing on parsing valid user input. However, users
|
||
of our parsers will make mistakes and we should help them finding the source
|
||
of the problem. And we should make this process not too painful.
|
||
</p>
|
||
<p>
|
||
The major difficulty in error reporting is that we have no direct way of
|
||
showing error messages to the user. The parsers are template metaprograms.
|
||
When they detect that the input is invalid, they can make the compilation
|
||
fail and the compiler (running the metaprogram) display an error message.
|
||
What we can do is making those error messages short and contain all information
|
||
about the parsing error. We should make it easy to find this information
|
||
in whatever the compiler displays.
|
||
</p>
|
||
<p>
|
||
So let's try to parse some invalid expression and let's see what happens:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser19</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"hello"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
</pre>
|
||
<p>
|
||
You will get a lot (if you have seen error messages coming from template
|
||
metaprograms you know: this is <span class="emphasis"><em>not</em></span> a lot.) of error
|
||
messages. Take a closer look. It contains this:
|
||
</p>
|
||
<pre class="programlisting"><span class="identifier">x__________________PARSING_FAILED__________________x</span><span class="special"><</span>
|
||
<span class="number">1</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">metaparse</span><span class="special">::</span><span class="identifier">v1</span><span class="special">::</span><span class="identifier">error</span><span class="special">::</span><span class="identifier">literal_expected</span><span class="special"><</span><span class="char">'('</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
You can see a formatted version above. There are no line breaks in the real
|
||
output. This is relatively easy to spot (thanks to the <code class="computeroutput"><span class="identifier">____________</span></code>
|
||
part) and contains answers to the main questions one has when parsing fails:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
<span class="emphasis"><em>where</em></span> is the error? It is column <code class="computeroutput"><span class="number">1</span></code>
|
||
in line <code class="computeroutput"><span class="number">1</span></code> (inside <a class="link" href="reference.html#BOOST_METAPARSE_STRING"><code class="computeroutput"><span class="identifier">BOOST_METAPARSE_STRING</span></code></a>). This
|
||
is the <code class="computeroutput"><span class="number">1</span><span class="special">,</span>
|
||
<span class="number">1</span></code> part.
|
||
</li>
|
||
<li class="listitem">
|
||
<span class="emphasis"><em>what</em></span> is the problem? <code class="computeroutput"><span class="identifier">literal_expected</span><span class="special"><</span><span class="char">'('</span><span class="special">></span></code>.
|
||
This is a bit misleading, as it contains only a part of the problem.
|
||
An open paren is not the only acceptable token here, a number would also
|
||
be fine. This misleading error message is <span class="emphasis"><em>our</em></span> fault:
|
||
<span class="emphasis"><em>we</em></span> (the parser authors) need to make the parsing
|
||
errors more descriptive.
|
||
</li>
|
||
</ul></div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_1_improving_the_error_message"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_1_improving_the_error_message" title="11.1. Improving the error messages">11.1.
|
||
Improving the error messages</a>
|
||
</h4></div></div></div>
|
||
<p>
|
||
So how can we improve the error messages? Let's look at what went wrong
|
||
in the previous case:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
The input was <code class="computeroutput"><span class="identifier">hello</span></code>.
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">plus_exp2</span></code> tried to
|
||
parse it.
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">plus_exp2</span></code> tried to
|
||
parse it using <code class="computeroutput"><span class="identifier">mult_exp5</span></code>
|
||
(assuming that this is the initial <code class="computeroutput"><span class="identifier">mult_exp</span></code>
|
||
in the list of <code class="computeroutput"><span class="special">+</span></code> / <code class="computeroutput"><span class="special">-</span></code> separated <code class="computeroutput"><span class="identifier">mult_exp</span></code>s).
|
||
</li>
|
||
<li class="listitem">
|
||
so <code class="computeroutput"><span class="identifier">mult_exp5</span></code> tried
|
||
to parse it.
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">mult_exp5</span></code> tried to
|
||
parse it using <code class="computeroutput"><span class="identifier">unary_exp2</span></code>
|
||
(assuming that this is the initial <code class="computeroutput"><span class="identifier">unary_exp</span></code>
|
||
in the list of <code class="computeroutput"><span class="special">*</span></code> / <code class="computeroutput"><span class="special">/</span></code> separated <code class="computeroutput"><span class="identifier">unary_exp</span></code>s).
|
||
</li>
|
||
<li class="listitem">
|
||
so <code class="computeroutput"><span class="identifier">unary_exp2</span></code> tried
|
||
to parse it.
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">unary_exp2</span></code> parsed all
|
||
of the <code class="computeroutput"><span class="special">-</span></code> symbols using
|
||
<code class="computeroutput"><span class="identifier">minus_token</span></code>. There
|
||
were none of them (the input started with an <code class="computeroutput"><span class="identifier">h</span></code>
|
||
character).
|
||
</li>
|
||
<li class="listitem">
|
||
so <code class="computeroutput"><span class="identifier">unary_exp2</span></code> tried
|
||
to parse it using <code class="computeroutput"><span class="identifier">primary_exp2</span></code>.
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">primary_exp2</span></code> is: <a class="link" href="reference.html#one_of"><code class="computeroutput"><span class="identifier">one_of</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">></span></code>. It tried parsing the input with
|
||
<code class="computeroutput"><span class="identifier">int_token</span></code> (which failed)
|
||
and then with <code class="computeroutput"><span class="identifier">paren_exp2</span></code>
|
||
(which failed as well). So <a class="link" href="reference.html#one_of"><code class="computeroutput"><span class="identifier">one_of</span></code></a>
|
||
could not parse the input with any of the choices and therefore it
|
||
failed as well. In such situations <code class="computeroutput"><span class="identifier">one_of</span></code>
|
||
checks which parser made the most progress (consumed the most characters
|
||
of the input) before failing and assumes, that that is the parser the
|
||
user intended to use, thus it returns the error message coming from
|
||
that parser. In this example none of the parsers could make any progress,
|
||
in which case <code class="computeroutput"><span class="identifier">one_of</span></code>
|
||
returns the error coming from the last parser in the list. This was
|
||
<code class="computeroutput"><span class="identifier">paren_exp2</span></code>, and it
|
||
expects the expression to start with an open paren. This is where the
|
||
error message came from. The rest of the layers did not change or improve
|
||
this error message so this was the error message displayed to the user.
|
||
</li>
|
||
</ul></div>
|
||
<p>
|
||
We, the parser authors know: we expect a primary expression there. When
|
||
<a class="link" href="reference.html#one_of"><code class="computeroutput"><span class="identifier">one_of</span></code></a>
|
||
fails, it means that none was found.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_2_defining_custom_errors"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_2_defining_custom_errors" title="11.2. Defining custom errors">11.2.
|
||
Defining custom errors</a>
|
||
</h4></div></div></div>
|
||
<p>
|
||
To be able to return custom error messages (like <code class="computeroutput"><span class="identifier">missing_primary_expression</span></code>)
|
||
to the user, we need to define those error messages first. The error messages
|
||
are represented by classes with some requirements:
|
||
</p>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||
<li class="listitem">
|
||
It should have a static method called <code class="computeroutput"><span class="identifier">get_value</span><span class="special">()</span></code> returning a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>
|
||
containing the description of the error.
|
||
</li>
|
||
<li class="listitem">
|
||
It should be a <a class="link" href="reference.html#metaprogramming_value">template metaprogramming
|
||
value</a>.
|
||
</li>
|
||
</ul></div>
|
||
<p>
|
||
These classes are called <a class="link" href="reference.html#parsing_error_message">parsing
|
||
error message</a>s. To make it easy to implement such classes and to
|
||
make it difficult (if not impossible) to forget to fulfill a requirement,
|
||
Metaparse provides a macro for defining these classes. To get this macro,
|
||
include the following header:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">define_error</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
Let's define the <a class="link" href="reference.html#parsing_error_message">parsing error message</a>:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">BOOST_METAPARSE_DEFINE_ERROR</span><span class="special">(</span><span class="identifier">missing_primary_expression</span><span class="special">,</span> <span class="string">"Missing primary expression"</span><span class="special">);</span>
|
||
</pre>
|
||
<p>
|
||
This defines a class called <code class="computeroutput"><span class="identifier">missing_primary_expression</span></code>
|
||
representing this error message. What we need to do is making our parser
|
||
return this error message when <a class="link" href="reference.html#one_of"><code class="computeroutput"><span class="identifier">one_of</span></code></a>
|
||
fails.
|
||
</p>
|
||
<p>
|
||
Let's define <code class="computeroutput"><span class="identifier">plus_exp</span></code> and
|
||
<code class="computeroutput"><span class="identifier">paren_exp</span></code> first. Their
|
||
definition does not change:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">plus_exp3</span><span class="special">;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">paren_exp4</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp3</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_36">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
When the input contains no number (parsed by <code class="computeroutput"><span class="identifier">int_token</span></code>)
|
||
and no paren expression (parsed by <code class="computeroutput"><span class="identifier">paren_exp4</span></code>),
|
||
we should return the <code class="computeroutput"><span class="identifier">missing_primary_expression</span></code>
|
||
error message. We can do it by adding a third parser to <code class="computeroutput"><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp4</span><span class="special">,</span> <span class="special">...></span></code>
|
||
which always fails with this error message. Metaparse provides <a class="link" href="reference.html#fail"><code class="computeroutput"><span class="identifier">fail</span></code></a> for this:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">fail</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
Now we can define the <code class="computeroutput"><span class="identifier">primary_exp</span></code>
|
||
parser using it:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">primary_exp3</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp4</span><span class="special">,</span> <span class="identifier">fail</span><span class="special"><</span><span class="identifier">missing_primary_expression</span><span class="special">>>;</span>
|
||
</pre>
|
||
<p>
|
||
It adds <a class="link" href="reference.html#fail"><code class="computeroutput"><span class="identifier">fail</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">missing_primary_expression</span><span class="special">></span></code> to <code class="computeroutput"><span class="identifier">one_of</span></code>
|
||
as the last element. Therefore if none of the "real" cases parse
|
||
the input <span class="emphasis"><em>and</em></span> none of them makes any progress before
|
||
failing, the error message will be <code class="computeroutput"><span class="identifier">missing_primary_expression</span></code>.
|
||
</p>
|
||
<p>
|
||
We need to define the rest of the parsers. Their definition is the same
|
||
as before:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">using</span> <span class="identifier">unary_exp3</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldr_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">primary_exp3</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">mult_exp6</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp3</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">unary_exp3</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
<span class="special">></span> <span class="keyword">struct</span> <span class="identifier">plus_exp3</span> <span class="special">:</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mult_exp6</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">{};</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser20</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp3</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_37">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
We can try to give our new parser an invalid input:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser20</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"hello"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
<span class="special">.....</span> <span class="identifier">x__________________PARSING_FAILED__________________x</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">missing_primary_expression</span><span class="special">></span> <span class="special">....</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
</pre>
|
||
<p>
|
||
The error message is now more specific to the calculator language. This
|
||
covers only one case, where the error messages can be improved. Other cases
|
||
(eg. missing closing parens, missing operators, etc) can be covered in
|
||
a similar way.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h4 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_3_missing_closing_parens"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_3_missing_closing_parens" title="11.3. Missing closing parens">11.3.
|
||
Missing closing parens</a>
|
||
</h4></div></div></div>
|
||
<div class="toc"><dl class="toc">
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_3_missing_closing_parens.11_3_1_simplifying_the_parser">11.3.1.
|
||
Simplifying the parser</a></span></dt>
|
||
<dt><span class="section"><a href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_3_missing_closing_parens.11_3_2_using_foldl_reject_incomp">11.3.2.
|
||
Using <code class="computeroutput"><span class="identifier">foldl_reject_incomplete_start_with_parser</span></code>
|
||
at other places as well</a></span></dt>
|
||
</dl></div>
|
||
<p>
|
||
Missing closing parens are common errors. Let's see how our parsers report
|
||
them:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser20</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"(1+2"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
<span class="special">.....</span> <span class="identifier">x__________________PARSING_FAILED__________________x</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">5</span><span class="special">,</span> <span class="identifier">unpaired</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">literal_expected</span><span class="special"><</span><span class="char">')'</span><span class="special">>>></span> <span class="special">....</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
</pre>
|
||
<p>
|
||
The parser could detect that there is a missing paren and the error report
|
||
points to the open paren which is not closed. This looks great, but we
|
||
are not done yet. Let's try a slightly more complex input:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser20</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"0+(1+2"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">0</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
This is getting strange now. We parse the <code class="computeroutput"><span class="special">+</span>
|
||
<span class="special"><</span><span class="identifier">mult_exp</span><span class="special">></span></code> elements using <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a> (see the
|
||
definition of <code class="computeroutput"><span class="identifier">plus_exp3</span></code>).
|
||
<a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a>
|
||
parses the input as long as it can and stops when it fails to parse it.
|
||
In the above input, it parses <code class="computeroutput"><span class="number">0</span></code>
|
||
as the initial element and then it tries to parse the first <code class="computeroutput"><span class="special">+</span> <span class="special"><</span><span class="identifier">mult_exp</span><span class="special">></span></code> element. But parsing the <code class="computeroutput"><span class="special"><</span><span class="identifier">mult_exp</span><span class="special">></span></code> part fails because of the missing closing
|
||
paren. So <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a>
|
||
stops and ignores this failing part of the input.
|
||
</p>
|
||
<p>
|
||
The result of the above is that we parse only the <code class="computeroutput"><span class="number">0</span></code>
|
||
part of the input, ignore the "garbage" at the end and assume
|
||
that the value of the expression is <code class="computeroutput"><span class="number">0</span></code>.
|
||
This could be fixed by using <a class="link" href="reference.html#entire_input"><code class="computeroutput"><span class="identifier">entire_input</span></code></a>. Our parser would
|
||
reject the input (because of the "garbage" at the end), but the
|
||
error message would not be useful. So we take a different approach.
|
||
</p>
|
||
<p>
|
||
When <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a>
|
||
stops, we should check if there is an extra broken <code class="computeroutput"><span class="special">+</span>
|
||
<span class="special"><</span><span class="identifier">mult_exp</span><span class="special">></span></code> there or not. When there is, we should
|
||
report what is wrong with that broken <code class="computeroutput"><span class="special">+</span>
|
||
<span class="special"><</span><span class="identifier">mult_exp</span><span class="special">></span></code> (eg. a missing closing paren). Metaparse
|
||
provides <a class="link" href="reference.html#fail_at_first_char_expected"><code class="computeroutput"><span class="identifier">fail_at_first_char_expected</span></code></a>
|
||
to implement such validations. <a class="link" href="reference.html#fail_at_first_char_expected"><code class="computeroutput"><span class="identifier">fail_at_first_char_expected</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">parser</span><span class="special">></span></code> checks how <code class="computeroutput"><span class="identifier">parser</span></code>
|
||
fails to parse the input: when it fails right at the first character,
|
||
<a class="link" href="reference.html#fail_at_first_char_expected"><code class="computeroutput"><span class="identifier">fail_at_first_char_expected</span></code></a>
|
||
assumes that there is no garbage and accepts the input. When <code class="computeroutput"><span class="identifier">parser</span></code> consumes characters from the input
|
||
before failing, <a class="link" href="reference.html#fail_at_first_char_expected"><code class="computeroutput"><span class="identifier">fail_at_first_char_expected</span></code></a> assumes
|
||
that there is a broken expression and propagates the error. It can be used
|
||
the following way:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">fail_at_first_char_expected</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">first_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="special">></span> <span class="keyword">struct</span> <span class="identifier">plus_exp4</span> <span class="special">:</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">first_of</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mult_exp6</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">fail_at_first_char_expected</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">{};</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser21</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp4</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_38">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
<a class="link" href="reference.html#first_of"><code class="computeroutput"><span class="identifier">first_of</span></code></a>
|
||
is similar to <a class="link" href="reference.html#middle_of"><code class="computeroutput"><span class="identifier">middle_of</span></code></a>,
|
||
but keeps the result of the first element, not the middle one. We use it
|
||
to keep the "real" result (the result of <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a>) and to
|
||
throw the dummy result coming from <a class="link" href="reference.html#fail_at_first_char_expected"><code class="computeroutput"><span class="identifier">fail_at_first_char_expected</span></code></a> away
|
||
when there is no broken expression at the end. <a class="link" href="reference.html#first_of"><code class="computeroutput"><span class="identifier">first_of</span></code></a> propagates any error
|
||
coming from <a class="link" href="reference.html#fail_at_first_char_expected"><code class="computeroutput"><span class="identifier">fail_at_first_char_expected</span></code></a>.
|
||
</p>
|
||
<p>
|
||
Let's try this new expression parser out with a missing closing paren:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser21</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"0+(1+2"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
<span class="special">.....</span> <span class="identifier">x__________________PARSING_FAILED__________________x</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">7</span><span class="special">,</span> <span class="identifier">unpaired</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="identifier">literal_expected</span><span class="special"><</span><span class="char">')'</span><span class="special">>>></span> <span class="special">....</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
</pre>
|
||
<p>
|
||
This works as expected now: it tells us that there is a missing paren and
|
||
it points us the open paren which is not closed.
|
||
</p>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h5 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_3_missing_closing_parens.11_3_1_simplifying_the_parser"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_3_missing_closing_parens.11_3_1_simplifying_the_parser" title="11.3.1. Simplifying the parser">11.3.1.
|
||
Simplifying the parser</a>
|
||
</h5></div></div></div>
|
||
<p>
|
||
Our parser provides useful error messages for missing closing parens,
|
||
however, the implementation of the parser (<code class="computeroutput"><span class="identifier">plus_exp4</span></code>)
|
||
is long and repetitive: it contains the parser for the repeated element
|
||
(<a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><</span></code><a class="link" href="reference.html#one_of"><code class="computeroutput"><span class="identifier">one_of</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span>
|
||
<span class="identifier">minus_token</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp6</span><span class="special">></span></code>)
|
||
twice, and that is not ideal.
|
||
</p>
|
||
<p>
|
||
<code class="computeroutput"><span class="identifier">plus_exp4</span></code> uses <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a>
|
||
to implement repetition. Metaparse provides <a class="link" href="reference.html#foldl_reject_incomplete_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_reject_incomplete_start_with_parser</span></code></a>
|
||
which does the same we did with <a class="link" href="reference.html#first_of"><code class="computeroutput"><span class="identifier">first_of</span></code></a>, <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a> and
|
||
<a class="link" href="reference.html#fail_at_first_char_expected"><code class="computeroutput"><span class="identifier">fail_at_first_char_expected</span></code></a>
|
||
together:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="special">#</span><span class="identifier">include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="special">></span> <span class="keyword">struct</span> <span class="identifier">plus_exp5</span> <span class="special">:</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mult_exp6</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">{};</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser22</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp5</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_39">copy-paste friendly version</a>
|
||
</p>
|
||
<p>
|
||
It parses the input using <a class="link" href="reference.html#sequence"><code class="computeroutput"><span class="identifier">sequence</span></code></a><code class="computeroutput"><span class="special"><</span></code><a class="link" href="reference.html#one_of"><code class="computeroutput"><span class="identifier">one_of</span></code></a><code class="computeroutput"><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span>
|
||
<span class="identifier">minus_token</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp6</span><span class="special">></span></code>)
|
||
repeatedly. When it fails, <a class="link" href="reference.html#foldl_reject_incomplete_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_reject_incomplete_start_with_parser</span></code></a>
|
||
checks if it consumed any character before failing (the same as what
|
||
<a class="link" href="reference.html#fail_at_first_char_expected"><code class="computeroutput"><span class="identifier">fail_at_first_char_expected</span></code></a>
|
||
does), and if yes, then <a class="link" href="reference.html#foldl_reject_incomplete_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_reject_incomplete_start_with_parser</span></code></a>
|
||
fails.
|
||
</p>
|
||
<p>
|
||
This makes the implementation of the repetition with advanced error reporting
|
||
simpler. Let's try it out:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser22</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"0+(1+2"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
<span class="special">.....</span> <span class="identifier">x__________________PARSING_FAILED__________________x</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">7</span><span class="special">,</span> <span class="identifier">unpaired</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="identifier">literal_expected</span><span class="special"><</span><span class="char">')'</span><span class="special">>>></span> <span class="special">....</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
</pre>
|
||
<p>
|
||
Note that other folding parsers have their <code class="computeroutput"><span class="identifier">f</span></code>
|
||
versions as well (eg. <a class="link" href="reference.html#foldr_reject_incomplete"><code class="computeroutput"><span class="identifier">foldr_reject_incomplete</span></code></a>, <a class="link" href="reference.html#foldl_reject_incomplete1"><code class="computeroutput"><span class="identifier">foldl_reject_incomplete1</span></code></a>,
|
||
etc).
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h5 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_3_missing_closing_parens.11_3_2_using_foldl_reject_incomp"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.11_dealing_with_invalid_input.11_3_missing_closing_parens.11_3_2_using_foldl_reject_incomp" title="11.3.2. Using foldl_reject_incomplete_start_with_parser at other places as well">11.3.2.
|
||
Using <code class="computeroutput"><span class="identifier">foldl_reject_incomplete_start_with_parser</span></code>
|
||
at other places as well</a>
|
||
</h5></div></div></div>
|
||
<p>
|
||
We have replaced one <a class="link" href="reference.html#foldl_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_start_with_parser</span></code></a> with
|
||
<a class="link" href="reference.html#foldl_reject_incomplete_start_with_parser"><code class="computeroutput"><span class="identifier">foldl_reject_incomplete_start_with_parser</span></code></a>.
|
||
Other layers (<code class="computeroutput"><span class="identifier">mult_exp</span></code>,
|
||
<code class="computeroutput"><span class="identifier">unary_exp</span></code>, etc) use folding
|
||
as well. Let's use it at all layers:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">plus_exp6</span><span class="special">;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">paren_exp5</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp6</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">primary_exp4</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp5</span><span class="special">,</span> <span class="identifier">fail</span><span class="special"><</span><span class="identifier">missing_primary_expression</span><span class="special">>>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">unary_exp4</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldr_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">primary_exp4</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">mult_exp7</span> <span class="special">=</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp4</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">unary_exp4</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">>;</span>
|
||
<span class="special">></span> <span class="keyword">struct</span> <span class="identifier">plus_exp6</span> <span class="special">:</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special"><</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp7</span><span class="special">>,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">mult_exp7</span><span class="special">,</span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">\</span>
|
||
<span class="special">...></span> <span class="special">></span> <span class="special">{};</span>
|
||
<span class="special">></span> <span class="keyword">using</span> <span class="identifier">exp_parser23</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp6</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a class="link" href="getting_started_with_boost_metap.html#getting_started_40">copy-paste friendly version</a>
|
||
</p>
|
||
<div class="note"><table border="0" summary="Note">
|
||
<tr>
|
||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
|
||
<th align="left">Note</th>
|
||
</tr>
|
||
<tr><td align="left" valign="top"><p>
|
||
Note that <code class="computeroutput"><span class="identifier">unary_exp4</span></code>
|
||
uses <a class="link" href="reference.html#foldr_start_with_parser"><code class="computeroutput"><span class="identifier">foldr_start_with_parser</span></code></a>
|
||
instead of <code class="computeroutput"><span class="identifier">foldr_reject_incomplete_start_with_parser</span></code>.
|
||
The reason behind it is that there is no <code class="computeroutput"><span class="identifier">foldr_reject_incomplete_start_with_parser</span></code>.
|
||
<a class="link" href="reference.html#foldr_start_with_parser"><code class="computeroutput"><span class="identifier">foldr_start_with_parser</span></code></a>
|
||
applies the <code class="computeroutput"><span class="identifier">primary_exp4</span></code>
|
||
parser when <code class="computeroutput"><span class="identifier">minus_token</span></code>
|
||
does not accept the input any more. Therefore, it is supposed to catch
|
||
the errors of incomplete expressions after the repetition.
|
||
</p></td></tr>
|
||
</table></div>
|
||
<p>
|
||
Let's try different invalid expressions:
|
||
</p>
|
||
<pre class="programlisting"><span class="special">></span> <span class="identifier">exp_parser23</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1+(2*"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
<span class="special">.....</span> <span class="identifier">x__________________PARSING_FAILED__________________x</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">6</span><span class="special">,</span> <span class="identifier">missing_primary_expression</span><span class="special">></span> <span class="special">....</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
|
||
<span class="special">></span> <span class="identifier">exp_parser23</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1+(2*3"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
<span class="special">.....</span> <span class="identifier">x__________________PARSING_FAILED__________________x</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">7</span><span class="special">,</span> <span class="identifier">unpaired</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="identifier">literal_expected</span><span class="special"><</span><span class="char">')'</span><span class="special">>>></span> <span class="special">....</span>
|
||
<span class="special"><<</span> <span class="identifier">compilation</span> <span class="identifier">error</span> <span class="special">>></span>
|
||
</pre>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.12_summary"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.12_summary" title="12. Summary">12.
|
||
Summary</a>
|
||
</h3></div></div></div>
|
||
<p>
|
||
This tutorial showed you how to build a parser for a calculator language.
|
||
Now that you understand how to do this, you should be able to use the same
|
||
techniques and building blocks presented here to build a parser for your
|
||
own language. You should start building the parser and once you face a problem
|
||
(eg. you need to add parens or you need better error messages) you can always
|
||
return to this tutorial and read the section showing you how to deal with
|
||
those situations.
|
||
</p>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.copy_paste_friendly_code_example"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.copy_paste_friendly_code_example" title="Copy-paste friendly code examples">Copy-paste
|
||
friendly code examples</a>
|
||
</h3></div></div></div>
|
||
<a name="getting_started_0"></a><pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<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>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_1"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
</pre>
|
||
<a name="getting_started_2"></a><pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">></span> <span class="special">{};</span>
|
||
</pre>
|
||
<a name="getting_started_3"></a><pre class="programlisting"><span class="identifier">eval_plus</span><span class="special"><</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">11</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">></span> <span class="special">>>::</span><span class="identifier">type</span>
|
||
</pre>
|
||
<a name="getting_started_4"></a><pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">transform</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_5"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span> <span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_6"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_7"></a><pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span> <span class="identifier">Vector</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span> <span class="special">>::</span><span class="identifier">type</span> <span class="special">></span> <span class="special">{};</span>
|
||
</pre>
|
||
<a name="getting_started_8"></a><pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">></span> <span class="special">{};</span>
|
||
</pre>
|
||
<a name="getting_started_9"></a><pre class="programlisting"><span class="identifier">sum_items</span><span class="special"><</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">1</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">char_</span><span class="special"><</span><span class="char">'+'</span><span class="special">>,</span> <span class="identifier">mpl_</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">2</span><span class="special">>></span> <span class="special">>::</span><span class="identifier">type</span>
|
||
</pre>
|
||
<a name="getting_started_10"></a><pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">temp_result</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> <span class="comment">/* The vector to summarise */</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="comment">/* The value to start the sum from */</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* The function to call in each iteration */</span> <span class="special">>::</span><span class="identifier">type</span>
|
||
</pre>
|
||
<a name="getting_started_11"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span> <span class="identifier">transform</span><span class="special"><</span> <span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span> <span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span> <span class="comment">/* The folding expression we have just created */</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span> <span class="comment">/* of the repeated<...> parser */</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="special">></span> <span class="special">>::</span><span class="identifier">type</span> <span class="special">></span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_12"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">transform</span><span class="special"><</span> <span class="comment">/* What we had so far */</span> <span class="identifier">sequence</span><span class="special"><</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">transform</span><span class="special"><</span> <span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="special">></span> <span class="special">>::</span><span class="identifier">type</span> <span class="special">></span> <span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_13"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">transform</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">foldl</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="special">></span> <span class="special">>,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_14"></a><pre class="programlisting"><span class="special">></span>
|
||
</pre>
|
||
<a name="getting_started_15"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_16"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_17"></a><pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
</pre>
|
||
<a name="getting_started_18"></a><pre class="programlisting"><span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">11</span><span class="special">>,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">13</span><span class="special">>,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
</pre>
|
||
<a name="getting_started_19"></a><pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span> <span class="identifier">eval_binary_op</span><span class="special"><</span> <span class="identifier">S</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">></span> <span class="special">{};</span>
|
||
</pre>
|
||
<a name="getting_started_20"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_21"></a><pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
</pre>
|
||
<a name="getting_started_22"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_23"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_24"></a><pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_25"></a><pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">reverse_binary_op</span> <span class="special">:</span> <span class="identifier">eval_binary_op</span><span class="special"><</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span> <span class="identifier">S</span> <span class="special">></span> <span class="special">{};</span>
|
||
</pre>
|
||
<a name="getting_started_26"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">mult_exp3</span> <span class="special">=</span> <span class="identifier">foldr_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>>,</span> <span class="comment">/* The parser applied repeatedly */</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The parser parsing the last number */</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">reverse_binary_op</span><span class="special">></span> <span class="comment">/* The function called for every result */</span> <span class="comment">/* of applying the above parser */</span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_27"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">exp_parser17</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_28"></a><pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">negate</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp1</span> <span class="special">=</span> <span class="identifier">foldr_start_with_parser</span><span class="special"><</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_29"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">mult_exp4</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp1</span><span class="special">>,</span> <span class="identifier">unary_exp1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser18</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_30"></a><pre class="programlisting"><span class="identifier">exp_parser18</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"---13"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
<span class="identifier">exp_parser18</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"13"</span><span class="special">)>::</span><span class="identifier">type</span>
|
||
</pre>
|
||
<a name="getting_started_31"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">lparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'('</span><span class="special">>>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">rparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">')'</span><span class="special">>>;</span>
|
||
</pre>
|
||
<a name="getting_started_32"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">plus_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_33"></a><pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">middle_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp2</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_34"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">paren_exp3</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp2</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp2</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp2</span> <span class="special">=</span> <span class="identifier">foldr_start_with_parser</span><span class="special"><</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">primary_exp2</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span> <span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp5</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp2</span><span class="special">>,</span> <span class="identifier">unary_exp2</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_35"></a><pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">plus_exp2</span> <span class="special">:</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp5</span><span class="special">>,</span> <span class="identifier">mult_exp5</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">></span> <span class="special">{};</span>
|
||
</pre>
|
||
<a name="getting_started_36"></a><pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">plus_exp3</span><span class="special">;</span>
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp4</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp3</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_37"></a><pre class="programlisting"><span class="keyword">using</span> <span class="identifier">unary_exp3</span> <span class="special">=</span> <span class="identifier">foldr_start_with_parser</span><span class="special"><</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">primary_exp3</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span> <span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp6</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp3</span><span class="special">>,</span> <span class="identifier">unary_exp3</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">>;</span>
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp3</span> <span class="special">:</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">></span> <span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser20</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp3</span><span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_38"></a><pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">fail_at_first_char_expected</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">first_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp4</span> <span class="special">:</span> <span class="identifier">first_of</span><span class="special"><</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">>,</span> <span class="identifier">fail_at_first_char_expected</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">></span> <span class="special">></span> <span class="special">></span> <span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser21</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp4</span><span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_39"></a><pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp5</span> <span class="special">:</span> <span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">></span> <span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser22</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp5</span><span class="special">>;</span>
|
||
</pre>
|
||
<a name="getting_started_40"></a><pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">plus_exp6</span><span class="special">;</span>
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp5</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp6</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp4</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp5</span><span class="special">,</span> <span class="identifier">fail</span><span class="special"><</span><span class="identifier">missing_primary_expression</span><span class="special">>>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp4</span> <span class="special">=</span> <span class="identifier">foldr_start_with_parser</span><span class="special"><</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">primary_exp4</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span> <span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp7</span> <span class="special">=</span> <span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp4</span><span class="special">>,</span> <span class="identifier">unary_exp4</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">>;</span>
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp6</span> <span class="special">:</span> <span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special"><</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp7</span><span class="special">>,</span> <span class="identifier">mult_exp7</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span> <span class="special">></span> <span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser23</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp6</span><span class="special">>;</span>
|
||
</pre>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h3 class="title">
|
||
<a name="metaparse.getting_started_with_boost_metap.definitions_before_each_section"></a><a class="link" href="getting_started_with_boost_metap.html#metaparse.getting_started_with_boost_metap.definitions_before_each_section" title="Definitions before each section">Definitions
|
||
before each section</a>
|
||
</h3></div></div></div>
|
||
<p>
|
||
<a name="before_3"></a><span class="emphasis"><em>Definitions before section 3.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_3_1"></a><span class="emphasis"><em>Definitions before section 3.1.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_3_2"></a><span class="emphasis"><em>Definitions before section 3.2.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_3_3"></a><span class="emphasis"><em>Definitions before section 3.3.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_4"></a><span class="emphasis"><em>Definitions before section 4.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_4_1"></a><span class="emphasis"><em>Definitions before section 4.1.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_4_2"></a><span class="emphasis"><em>Definitions before section 4.2.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_5"></a><span class="emphasis"><em>Definitions before section 5.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_5_1"></a><span class="emphasis"><em>Definitions before section 5.1.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_5_2"></a><span class="emphasis"><em>Definitions before section 5.2.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_5_2_1"></a><span class="emphasis"><em>Definitions before section 5.2.1.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_5_2_2"></a><span class="emphasis"><em>Definitions before section 5.2.2.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_5_2_3"></a><span class="emphasis"><em>Definitions before section 5.2.3.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_5_2_4"></a><span class="emphasis"><em>Definitions before section 5.2.4.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_6"></a><span class="emphasis"><em>Definitions before section 6.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_6_1"></a><span class="emphasis"><em>Definitions before section 6.1.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_6_2"></a><span class="emphasis"><em>Definitions before section 6.2.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_7"></a><span class="emphasis"><em>Definitions before section 7.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_7_1"></a><span class="emphasis"><em>Definitions before section 7.1.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_7_2"></a><span class="emphasis"><em>Definitions before section 7.2.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_8"></a><span class="emphasis"><em>Definitions before section 8.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_8_1"></a><span class="emphasis"><em>Definitions before section 8.1.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_8_2"></a><span class="emphasis"><em>Definitions before section 8.2.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_9"></a><span class="emphasis"><em>Definitions before section 9.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">reverse_binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="identifier">S</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldr_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>>,</span> <span class="comment">/* The parser applied repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The parser parsing the last number */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">reverse_binary_op</span><span class="special">></span> <span class="comment">/* The function called for every result */</span>
|
||
<span class="comment">/* of applying the above parser */</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser17</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_10"></a><span class="emphasis"><em>Definitions before section 10.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">reverse_binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="identifier">S</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldr_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>>,</span> <span class="comment">/* The parser applied repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The parser parsing the last number */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">reverse_binary_op</span><span class="special">></span> <span class="comment">/* The function called for every result */</span>
|
||
<span class="comment">/* of applying the above parser */</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser17</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">negate</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp4</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser18</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_11"></a><span class="emphasis"><em>Definitions before section 11.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">reverse_binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="identifier">S</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldr_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>>,</span> <span class="comment">/* The parser applied repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The parser parsing the last number */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">reverse_binary_op</span><span class="special">></span> <span class="comment">/* The function called for every result */</span>
|
||
<span class="comment">/* of applying the above parser */</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser17</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">negate</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp4</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser18</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">lparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'('</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">rparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">')'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp1</span> <span class="special">=</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">middle_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp2</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp1</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp3</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp2</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp2</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp5</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp5</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp5</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser19</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp2</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_11_1"></a><span class="emphasis"><em>Definitions before section 11.1.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">reverse_binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="identifier">S</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldr_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>>,</span> <span class="comment">/* The parser applied repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The parser parsing the last number */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">reverse_binary_op</span><span class="special">></span> <span class="comment">/* The function called for every result */</span>
|
||
<span class="comment">/* of applying the above parser */</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser17</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">negate</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp4</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser18</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">lparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'('</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">rparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">')'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp1</span> <span class="special">=</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">middle_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp2</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp1</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp3</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp2</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp2</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp5</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp5</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp5</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser19</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp2</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_11_2"></a><span class="emphasis"><em>Definitions before section 11.2.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">reverse_binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="identifier">S</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldr_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>>,</span> <span class="comment">/* The parser applied repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The parser parsing the last number */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">reverse_binary_op</span><span class="special">></span> <span class="comment">/* The function called for every result */</span>
|
||
<span class="comment">/* of applying the above parser */</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser17</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">negate</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp4</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser18</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">lparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'('</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">rparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">')'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp1</span> <span class="special">=</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">middle_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp2</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp1</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp3</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp2</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp2</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp5</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp5</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp5</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser19</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp2</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_11_3"></a><span class="emphasis"><em>Definitions before section 11.3.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">reverse_binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="identifier">S</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldr_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>>,</span> <span class="comment">/* The parser applied repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The parser parsing the last number */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">reverse_binary_op</span><span class="special">></span> <span class="comment">/* The function called for every result */</span>
|
||
<span class="comment">/* of applying the above parser */</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser17</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">negate</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp4</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser18</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">lparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'('</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">rparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">')'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp1</span> <span class="special">=</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">middle_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp2</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp1</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp3</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp2</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp2</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp5</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp5</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp5</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser19</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp2</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">define_error</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="identifier">BOOST_METAPARSE_DEFINE_ERROR</span><span class="special">(</span><span class="identifier">missing_primary_expression</span><span class="special">,</span> <span class="string">"Missing primary expression"</span><span class="special">);</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp3</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp4</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp3</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">fail</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp3</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp4</span><span class="special">,</span> <span class="identifier">fail</span><span class="special"><</span><span class="identifier">missing_primary_expression</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp6</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp3</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp6</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser20</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp3</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_11_3_1"></a><span class="emphasis"><em>Definitions before section 11.3.1.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">reverse_binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="identifier">S</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldr_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>>,</span> <span class="comment">/* The parser applied repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The parser parsing the last number */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">reverse_binary_op</span><span class="special">></span> <span class="comment">/* The function called for every result */</span>
|
||
<span class="comment">/* of applying the above parser */</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser17</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">negate</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp4</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser18</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">lparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'('</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">rparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">')'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp1</span> <span class="special">=</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">middle_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp2</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp1</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp3</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp2</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp2</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp5</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp5</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp5</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser19</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp2</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">define_error</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="identifier">BOOST_METAPARSE_DEFINE_ERROR</span><span class="special">(</span><span class="identifier">missing_primary_expression</span><span class="special">,</span> <span class="string">"Missing primary expression"</span><span class="special">);</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp3</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp4</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp3</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">fail</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp3</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp4</span><span class="special">,</span> <span class="identifier">fail</span><span class="special"><</span><span class="identifier">missing_primary_expression</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp6</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp3</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp6</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser20</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp3</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">fail_at_first_char_expected</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">first_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp4</span> <span class="special">:</span>
|
||
<span class="identifier">first_of</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp6</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">fail_at_first_char_expected</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser21</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp4</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_11_3_2"></a><span class="emphasis"><em>Definitions before section 11.3.2.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">reverse_binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="identifier">S</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldr_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>>,</span> <span class="comment">/* The parser applied repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The parser parsing the last number */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">reverse_binary_op</span><span class="special">></span> <span class="comment">/* The function called for every result */</span>
|
||
<span class="comment">/* of applying the above parser */</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser17</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">negate</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp4</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser18</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">lparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'('</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">rparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">')'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp1</span> <span class="special">=</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">middle_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp2</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp1</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp3</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp2</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp2</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp5</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp5</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp5</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser19</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp2</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">define_error</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="identifier">BOOST_METAPARSE_DEFINE_ERROR</span><span class="special">(</span><span class="identifier">missing_primary_expression</span><span class="special">,</span> <span class="string">"Missing primary expression"</span><span class="special">);</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp3</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp4</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp3</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">fail</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp3</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp4</span><span class="special">,</span> <span class="identifier">fail</span><span class="special"><</span><span class="identifier">missing_primary_expression</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp6</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp3</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp6</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser20</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp3</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">fail_at_first_char_expected</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">first_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp4</span> <span class="special">:</span>
|
||
<span class="identifier">first_of</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp6</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">fail_at_first_char_expected</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser21</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp4</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp5</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp6</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser22</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp5</span><span class="special">>;</span>
|
||
</pre>
|
||
<p>
|
||
<a name="before_12"></a><span class="emphasis"><em>Definitions before section 12.</em></span>
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">int_</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">build_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<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>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser1</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">entire_input</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser2</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">token</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser3</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">entire_input</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">lit_c</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser4</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>,</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">metashell</span><span class="special">/</span><span class="identifier">formatter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">int_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">int_</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'+'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser5</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">transform</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">plus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">at</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">eval_plus</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Vector</span><span class="special">,</span> <span class="number">2</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">quote</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser6</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">eval_plus</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser7</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The first <number> */</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>></span> <span class="comment">/* The "+ <number>" elements */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">temp_result</span> <span class="special">=</span> <span class="identifier">exp_parser7</span><span class="special">::</span><span class="identifier">apply</span><span class="special"><</span><span class="identifier">BOOST_METAPARSE_STRING</span><span class="special">(</span><span class="string">"1 + 2 + 3 + 4"</span><span class="special">)>::</span><span class="identifier">type</span><span class="special">;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">fold</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">vector_of_numbers</span> <span class="special">=</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">2</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">5</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">6</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Vector</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_vector</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">Vector</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_2</span><span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Sum</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">sum_items</span> <span class="special">:</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span>
|
||
<span class="identifier">Sum</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser8</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* parse the first <number> */</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span> <span class="comment">/* parse the "+ <number>" elements */</span>
|
||
<span class="comment">/* lambda expression summarising the "+ <number>" elements using fold */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="comment">/* The folding expression we have just created */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span> <span class="comment">/* the argument of the lambda expression, the result */</span>
|
||
<span class="comment">/* of the repeated<...> parser */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser9</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="comment">/* What we had so far */</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">repeated</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">fold</span><span class="special"><</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">></span> <span class="comment">/* summarise the vector of numbers */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser10</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">transform</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">foldl</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">int_</span><span class="special"><</span><span class="number">0</span><span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote1</span><span class="special"><</span><span class="identifier">sum_vector</span><span class="special">>></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser11</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="comment">/* apply this parser repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* use this parser to get the initial value */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span> <span class="comment">/* use this function to add a new value to the summary */</span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">minus_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'-'</span><span class="special">>>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">one_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser12</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">sum_items</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">minus</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">Op</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special">;</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'+'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">plus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'-'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">minus</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="identifier">S</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser13</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">times</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'*'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">times</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">times_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'*'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser14</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">,</span> <span class="identifier">times_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp1</span> <span class="special">=</span> <span class="identifier">foldl_start_with_parser</span><span class="special"><</span><span class="identifier">sequence</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">int_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser15</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">divides</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">L</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">eval_binary_op</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="char">'/'</span><span class="special">,</span> <span class="identifier">R</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">divides</span><span class="special"><</span><span class="identifier">L</span><span class="special">,</span> <span class="identifier">R</span><span class="special">>::</span><span class="identifier">type</span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">divides_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'/'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">int_token</span><span class="special">>,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser16</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Item</span><span class="special">></span>
|
||
<span class="keyword">struct</span> <span class="identifier">reverse_binary_op</span> <span class="special">:</span>
|
||
<span class="identifier">eval_binary_op</span><span class="special"><</span>
|
||
<span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">0</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">at_c</span><span class="special"><</span><span class="identifier">Item</span><span class="special">,</span> <span class="number">1</span><span class="special">>::</span><span class="identifier">type</span><span class="special">::</span><span class="identifier">value</span><span class="special">,</span>
|
||
<span class="identifier">S</span>
|
||
<span class="special">></span>
|
||
<span class="special">{};</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldr_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>>,</span> <span class="comment">/* The parser applied repeatedly */</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span> <span class="comment">/* The parser parsing the last number */</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">reverse_binary_op</span><span class="special">></span> <span class="comment">/* The function called for every result */</span>
|
||
<span class="comment">/* of applying the above parser */</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser17</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">negate</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">int_token</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp4</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp1</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp1</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser18</span> <span class="special">=</span>
|
||
<span class="identifier">build_parser</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">>;</span>
|
||
<span class="keyword">using</span> <span class="identifier">lparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">'('</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">rparen_token</span> <span class="special">=</span> <span class="identifier">token</span><span class="special"><</span><span class="identifier">lit_c</span><span class="special"><</span><span class="char">')'</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">plus_exp1</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp1</span> <span class="special">=</span> <span class="identifier">sequence</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">middle_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp2</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp1</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp1</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp3</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp2</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp2</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp2</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp2</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp5</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp2</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp2</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp2</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp5</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp5</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser19</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp2</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">define_error</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="identifier">BOOST_METAPARSE_DEFINE_ERROR</span><span class="special">(</span><span class="identifier">missing_primary_expression</span><span class="special">,</span> <span class="string">"Missing primary expression"</span><span class="special">);</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp3</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp4</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp3</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">fail</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp3</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp4</span><span class="special">,</span> <span class="identifier">fail</span><span class="special"><</span><span class="identifier">missing_primary_expression</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp3</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp6</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp3</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp3</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp3</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp6</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser20</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp3</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">fail_at_first_char_expected</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">first_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp4</span> <span class="special">:</span>
|
||
<span class="identifier">first_of</span><span class="special"><</span>
|
||
<span class="identifier">foldl_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp6</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>,</span>
|
||
<span class="identifier">fail_at_first_char_expected</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">></span>
|
||
<span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser21</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp4</span><span class="special">>;</span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">metaparse</span><span class="special">/</span><span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp5</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp6</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp6</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser22</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp5</span><span class="special">>;</span>
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp6</span><span class="special">;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">paren_exp5</span> <span class="special">=</span> <span class="identifier">middle_of</span><span class="special"><</span><span class="identifier">lparen_token</span><span class="special">,</span> <span class="identifier">plus_exp6</span><span class="special">,</span> <span class="identifier">rparen_token</span><span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">primary_exp4</span> <span class="special">=</span> <span class="identifier">one_of</span><span class="special"><</span><span class="identifier">int_token</span><span class="special">,</span> <span class="identifier">paren_exp5</span><span class="special">,</span> <span class="identifier">fail</span><span class="special"><</span><span class="identifier">missing_primary_expression</span><span class="special">>>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">unary_exp4</span> <span class="special">=</span>
|
||
<span class="identifier">foldr_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">minus_token</span><span class="special">,</span>
|
||
<span class="identifier">primary_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">lambda</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">negate</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">_1</span><span class="special">>>::</span><span class="identifier">type</span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">mult_exp7</span> <span class="special">=</span>
|
||
<span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">times_token</span><span class="special">,</span> <span class="identifier">divides_token</span><span class="special">>,</span> <span class="identifier">unary_exp4</span><span class="special">>,</span>
|
||
<span class="identifier">unary_exp4</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">>;</span>
|
||
|
||
<span class="keyword">struct</span> <span class="identifier">plus_exp6</span> <span class="special">:</span>
|
||
<span class="identifier">foldl_reject_incomplete_start_with_parser</span><span class="special"><</span>
|
||
<span class="identifier">sequence</span><span class="special"><</span><span class="identifier">one_of</span><span class="special"><</span><span class="identifier">plus_token</span><span class="special">,</span> <span class="identifier">minus_token</span><span class="special">>,</span> <span class="identifier">mult_exp7</span><span class="special">>,</span>
|
||
<span class="identifier">mult_exp7</span><span class="special">,</span>
|
||
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">quote2</span><span class="special"><</span><span class="identifier">binary_op</span><span class="special">></span>
|
||
<span class="special">></span> <span class="special">{};</span>
|
||
|
||
<span class="keyword">using</span> <span class="identifier">exp_parser23</span> <span class="special">=</span> <span class="identifier">build_parser</span><span class="special"><</span><span class="identifier">plus_exp6</span><span class="special">>;</span>
|
||
</pre>
|
||
</div>
|
||
</div>
|
||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||
<td align="left"></td>
|
||
<td align="right"><div class="copyright-footer">Copyright © 2015 Abel Sinkovics<p>
|
||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||
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>)
|
||
</p>
|
||
</div></td>
|
||
</tr></table>
|
||
<hr>
|
||
<div class="spirit-nav">
|
||
<a accesskey="p" href="preface.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="user_manual.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
|
||
</div>
|
||
</body>
|
||
</html>
|