• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Expectation Operator (a &gt; b)</title>
5<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../../index.html" title="Spirit 2.5.8">
8<link rel="up" href="../operator.html" title="Parser Operators">
9<link rel="prev" href="difference.html" title="Difference Parser (a - b)">
10<link rel="next" href="kleene.html" title="Kleene Parser (*a)">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="difference.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../operator.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="kleene.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h5 class="title">
27<a name="spirit.qi.reference.operator.expect"></a><a class="link" href="expect.html" title="Expectation Operator (a &gt; b)">Expectation
28          Operator (<code class="computeroutput"><span class="identifier">a</span> <span class="special">&gt;</span>
29          <span class="identifier">b</span></code>)</a>
30</h5></div></div></div>
31<h6>
32<a name="spirit.qi.reference.operator.expect.h0"></a>
33            <span class="phrase"><a name="spirit.qi.reference.operator.expect.description"></a></span><a class="link" href="expect.html#spirit.qi.reference.operator.expect.description">Description</a>
34          </h6>
35<p>
36            There are occasions in which it is expected that the input must match
37            a particular parser or the input is invalid. Such cases generally arise
38            after matching a portion of a grammar, such that the context is fully
39            known. In such a situation, failure to match should result in an exception.
40            For example, when parsing an e-mail address, after matching a name and
41            "@" there must be a domain name or the address is invalid.
42          </p>
43<p>
44            The expectation operator (&gt;) requires that the following parser match
45            the input or an exception is emitted. Using on_error(), that exception
46            can be handled by calling a handler with the context at which the parsing
47            failed can be reported.
48          </p>
49<p>
50            By contrast, the follows operator (&gt;&gt;) does not require that the
51            following parser match the input, which allows for backtracking or simply
52            returning false from the parse() function with no exceptions.
53          </p>
54<p>
55            Like the <a class="link" href="sequence.html" title="Sequence Parser (a &gt;&gt; b)">Sequence</a>,
56            the expectation operator, <code class="computeroutput"><span class="identifier">a</span>
57            <span class="special">&gt;</span> <span class="identifier">b</span></code>,
58            parses two or more operands (<code class="computeroutput"><span class="identifier">a</span></code>,
59            <code class="computeroutput"><span class="identifier">b</span></code>, ... etc.), in sequence:
60          </p>
61<pre class="programlisting"><span class="identifier">a</span> <span class="special">&gt;</span> <span class="identifier">b</span> <span class="special">&gt;</span> <span class="special">...</span>
62</pre>
63<p>
64            However, while the plain <a class="link" href="sequence.html" title="Sequence Parser (a &gt;&gt; b)">Sequence</a>
65            simply returns a no-match (returns <code class="computeroutput"><span class="keyword">false</span></code>)
66            when one of the elements fail, the expectation: <code class="computeroutput"><span class="special">&gt;</span></code>
67            operator throws an <a class="link" href="expect.html#spirit.qi.reference.operator.expect.expectation_failure"><code class="computeroutput"><span class="identifier">expectation_failure</span></code></a><code class="computeroutput"><span class="special">&lt;</span><span class="identifier">Iter</span><span class="special">&gt;</span></code> when the second or succeeding operands
68            (all operands except the first) fail to match.
69          </p>
70<h6>
71<a name="spirit.qi.reference.operator.expect.h1"></a>
72            <span class="phrase"><a name="spirit.qi.reference.operator.expect.header"></a></span><a class="link" href="expect.html#spirit.qi.reference.operator.expect.header">Header</a>
73          </h6>
74<pre class="programlisting"><span class="comment">// forwards to &lt;boost/spirit/home/qi/operator/expect.hpp&gt;</span>
75<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">spirit</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">qi_expect</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
76</pre>
77<p>
78            Also, see <a class="link" href="../../../structure/include.html" title="Include">Include Structure</a>.
79          </p>
80<h6>
81<a name="spirit.qi.reference.operator.expect.h2"></a>
82            <span class="phrase"><a name="spirit.qi.reference.operator.expect.model_of"></a></span><a class="link" href="expect.html#spirit.qi.reference.operator.expect.model_of">Model
83            of</a>
84          </h6>
85<div class="blockquote"><blockquote class="blockquote"><p>
86              <a class="link" href="../parser_concepts/naryparser.html" title="NaryParser"><code class="computeroutput"><span class="identifier">NaryParser</span></code></a>
87            </p></blockquote></div>
88<div class="variablelist">
89<p class="title"><b>Notation</b></p>
90<dl class="variablelist">
91<dt><span class="term"><code class="computeroutput"><span class="identifier">a</span></code>, <code class="computeroutput"><span class="identifier">b</span></code></span></dt>
92<dd><p>
93                  A <a class="link" href="../parser_concepts/parser.html" title="Parser"><code class="computeroutput"><span class="identifier">Parser</span></code></a>
94                </p></dd>
95<dt><span class="term"><code class="computeroutput"><span class="identifier">Iter</span></code></span></dt>
96<dd><p>
97                  A <a href="http://en.cppreference.com/w/cpp/named_req/ForwardIterator" target="_top"><code class="computeroutput"><span class="identifier">ForwardIterator</span></code></a> type
98                </p></dd>
99</dl>
100</div>
101<h6>
102<a name="spirit.qi.reference.operator.expect.h3"></a>
103            <span class="phrase"><a name="spirit.qi.reference.operator.expect.expectation_failure"></a></span><a class="link" href="expect.html#spirit.qi.reference.operator.expect.expectation_failure">Expectation
104            Failure</a>
105          </h6>
106<p>
107            When any operand, except the first, fail to match an <code class="computeroutput"><span class="identifier">expectation_failure</span><span class="special">&lt;</span><span class="identifier">Iter</span><span class="special">&gt;</span></code> is thrown:
108          </p>
109<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Iter</span><span class="special">&gt;</span>
110<span class="keyword">struct</span> <span class="identifier">expectation_failure</span> <span class="special">:</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span>
111<span class="special">{</span>
112    <span class="identifier">Iter</span> <span class="identifier">first</span><span class="special">;</span>           <span class="comment">// [first, last) iterator pointing</span>
113    <span class="identifier">Iter</span> <span class="identifier">last</span><span class="special">;</span>            <span class="comment">// to the error position in the input.</span>
114    <code class="computeroutput"><span class="identifier">info</span></code> <span class="identifier">what_</span><span class="special">;</span>       <span class="comment">// Information about the nature of the error.</span>
115<span class="special">};</span>
116</pre>
117<h6>
118<a name="spirit.qi.reference.operator.expect.h4"></a>
119            <span class="phrase"><a name="spirit.qi.reference.operator.expect.expression_semantics"></a></span><a class="link" href="expect.html#spirit.qi.reference.operator.expect.expression_semantics">Expression
120            Semantics</a>
121          </h6>
122<p>
123            Semantics of an expression is defined only where it differs from, or
124            is not defined in <a class="link" href="../parser_concepts/naryparser.html" title="NaryParser"><code class="computeroutput"><span class="identifier">NaryParser</span></code></a>.
125          </p>
126<div class="informaltable"><table class="table">
127<colgroup>
128<col>
129<col>
130</colgroup>
131<thead><tr>
132<th>
133                    <p>
134                      Expression
135                    </p>
136                  </th>
137<th>
138                    <p>
139                      Semantics
140                    </p>
141                  </th>
142</tr></thead>
143<tbody><tr>
144<td>
145                    <p>
146                      <code class="computeroutput"><span class="identifier">a</span> <span class="special">&gt;</span>
147                      <span class="identifier">b</span></code>
148                    </p>
149                  </td>
150<td>
151                    <p>
152                      Match <code class="computeroutput"><span class="identifier">a</span></code> followed
153                      by <code class="computeroutput"><span class="identifier">b</span></code>. If <code class="computeroutput"><span class="identifier">a</span></code> fails, no-match. If <code class="computeroutput"><span class="identifier">b</span></code> fails, throw an <code class="computeroutput"><span class="identifier">expectation_failure</span><span class="special">&lt;</span><span class="identifier">Iter</span><span class="special">&gt;</span></code>
154                    </p>
155                  </td>
156</tr></tbody>
157</table></div>
158<div class="note"><table border="0" summary="Note">
159<tr>
160<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
161<th align="left">Note</th>
162</tr>
163<tr><td align="left" valign="top"><p>
164              If you need an exception to be thrown if <code class="computeroutput"><span class="identifier">a</span></code>
165              fails, use the <a class="link" href="../directive/expect.html" title="Expectation Directive (expect[])"><code class="computeroutput"><span class="identifier">expect</span></code></a> directive instead of
166              the expectation operator.
167            </p></td></tr>
168</table></div>
169<h6>
170<a name="spirit.qi.reference.operator.expect.h5"></a>
171            <span class="phrase"><a name="spirit.qi.reference.operator.expect.attributes"></a></span><a class="link" href="expect.html#spirit.qi.reference.operator.expect.attributes">Attributes</a>
172          </h6>
173<p>
174            See <a class="link" href="../../quick_reference/compound_attribute_rules.html#spirit.qi.quick_reference.compound_attribute_rules.notation">Compound
175            Attribute Notation</a>.
176          </p>
177<div class="informaltable"><table class="table">
178<colgroup>
179<col>
180<col>
181</colgroup>
182<thead><tr>
183<th>
184                    <p>
185                      Expression
186                    </p>
187                  </th>
188<th>
189                    <p>
190                      Attribute
191                    </p>
192                  </th>
193</tr></thead>
194<tbody><tr>
195<td>
196                    <p>
197                      <code class="computeroutput"><span class="identifier">a</span> <span class="special">&gt;</span>
198                      <span class="identifier">b</span></code>
199                    </p>
200                  </td>
201<td>
202                    <p>
203</p>
204<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">B</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">&gt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span>
205<span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">Unused</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">&gt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">A</span>
206<span class="identifier">a</span><span class="special">:</span> <span class="identifier">Unused</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">B</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">&gt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">B</span>
207<span class="identifier">a</span><span class="special">:</span> <span class="identifier">Unused</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">Unused</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">&gt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">Unused</span>
208
209<span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">A</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">&gt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span>
210<span class="identifier">a</span><span class="special">:</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">A</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">&gt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span>
211<span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">&gt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span>
212<span class="identifier">a</span><span class="special">:</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">&gt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span></pre>
213<p>
214                    </p>
215                  </td>
216</tr></tbody>
217</table></div>
218<h6>
219<a name="spirit.qi.reference.operator.expect.h6"></a>
220            <span class="phrase"><a name="spirit.qi.reference.operator.expect.complexity"></a></span><a class="link" href="expect.html#spirit.qi.reference.operator.expect.complexity">Complexity</a>
221          </h6>
222<div class="blockquote"><blockquote class="blockquote"><p>
223              The overall complexity of the expectation parser is defined by the
224              sum of the complexities of its elements. The complexity of the expectation
225              operator itself is O(N), where N is the number of elements in the sequence.
226            </p></blockquote></div>
227<h6>
228<a name="spirit.qi.reference.operator.expect.h7"></a>
229            <span class="phrase"><a name="spirit.qi.reference.operator.expect.example"></a></span><a class="link" href="expect.html#spirit.qi.reference.operator.expect.example">Example</a>
230          </h6>
231<div class="note"><table border="0" summary="Note">
232<tr>
233<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
234<th align="left">Note</th>
235</tr>
236<tr><td align="left" valign="top"><p>
237              The test harness for the example(s) below is presented in the <a class="link" href="../basics.html#spirit.qi.reference.basics.examples">Basics Examples</a>
238              section.
239            </p></td></tr>
240</table></div>
241<p>
242            Some using declarations:
243          </p>
244<p>
245</p>
246<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">ascii</span><span class="special">::</span><span class="identifier">char_</span><span class="special">;</span>
247<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">expectation_failure</span><span class="special">;</span>
248</pre>
249<p>
250          </p>
251<p>
252            The code below uses an expectation operator to throw an <a class="link" href="expect.html#spirit.qi.reference.operator.expect.expectation_failure"><code class="computeroutput"><span class="identifier">expectation_failure</span></code></a> with a deliberate
253            parsing error when <code class="computeroutput"><span class="string">"o"</span></code>
254            is expected and <code class="computeroutput"><span class="string">"i"</span></code>
255            is what is found in the input. The <code class="computeroutput"><span class="keyword">catch</span></code>
256            block prints the information related to the error. Note: This is low
257            level code that demonstrates the <span class="emphasis"><em>bare-metal</em></span>. Typically,
258            you use an Error Handler to deal with the error.
259          </p>
260<p>
261</p>
262<pre class="programlisting"><span class="keyword">try</span>
263<span class="special">{</span>
264    <span class="identifier">test_parser</span><span class="special">(</span><span class="string">"xi"</span><span class="special">,</span> <span class="identifier">char_</span><span class="special">(</span><span class="char">'x'</span><span class="special">)</span> <span class="special">&gt;</span> <span class="identifier">char_</span><span class="special">(</span><span class="char">'o'</span><span class="special">));</span> <span class="comment">// should throw an exception</span>
265<span class="special">}</span>
266<span class="keyword">catch</span> <span class="special">(</span><span class="identifier">expectation_failure</span><span class="special">&lt;</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">)</span>
267<span class="special">{</span>
268    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"expected: "</span><span class="special">;</span> <span class="identifier">print_info</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">what_</span><span class="special">);</span>
269    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"got: \""</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">last</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="char">'"'</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
270<span class="special">}</span>
271</pre>
272<p>
273          </p>
274<p>
275            The code above will print:
276          </p>
277<pre class="programlisting">expected: tag: literal-char, value: o
278got: "i"
279</pre>
280</div>
281<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
282<td align="left"></td>
283<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
284        Distributed under the Boost Software License, Version 1.0. (See accompanying
285        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>)
286      </p>
287</div></td>
288</tr></table>
289<hr>
290<div class="spirit-nav">
291<a accesskey="p" href="difference.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../operator.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="kleene.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
292</div>
293</body>
294</html>
295