1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Expectation Directive (expect[])</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="../directive.html" title="Parser Directives"> 9<link rel="prev" href="as.html" title="Parser Directives Forcing Atomic Assignment (as<T>, as_string[], as_wstring[])"> 10<link rel="next" href="../nonterminal.html" title="Nonterminal Parsers"> 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="as.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../directive.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="../nonterminal.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.directive.expect"></a><a class="link" href="expect.html" title="Expectation Directive (expect[])">Expectation 28 Directive (<code class="computeroutput"><span class="identifier">expect</span><span class="special">[]</span></code>)</a> 29</h5></div></div></div> 30<h6> 31<a name="spirit.qi.reference.directive.expect.h0"></a> 32 <span class="phrase"><a name="spirit.qi.reference.directive.expect.description"></a></span><a class="link" href="expect.html#spirit.qi.reference.directive.expect.description">Description</a> 33 </h6> 34<p> 35 There are occasions in which it is expected that the input must match 36 a particular parser or the input is invalid. Such cases generally arise 37 after matching a portion of a grammar, such that the context is fully 38 known. In such a situation, failure to match should result in an exception. 39 For example, when parsing an e-mail address, a name, an "@" 40 and a domain name must be matched or the address is invalid. 41 </p> 42<p> 43 The expect directive requires that the argument parser matches the input 44 or an exception is emitted. Using on_error(), that exception can be handled 45 by calling a handler with the context at which the parsing failed can 46 be reported. 47 </p> 48<p> 49 The expect directive parses an operand parser expression which may be 50 a single parser or a complex parser expression like a sequence. 51 </p> 52<p> 53 Single parser: 54 </p> 55<pre class="programlisting"><span class="identifier">expect</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span> 56</pre> 57<p> 58 Parser expression: 59 </p> 60<pre class="programlisting"><span class="identifier">expect</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="special">...]</span> 61</pre> 62<p> 63 In the latter case while the plain <a class="link" href="../operator/sequence.html" title="Sequence Parser (a >> b)">Sequence</a> 64 simply returns a no-match (returns <code class="computeroutput"><span class="keyword">false</span></code>) 65 when one of the elements fail, the expect directive throws an <a class="link" href="../operator/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"><</span><span class="identifier">Iter</span><span class="special">></span></code> if any of the parsers (even the first 66 parser of a sequence) fails to match. 67 </p> 68<div class="note"><table border="0" summary="Note"> 69<tr> 70<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td> 71<th align="left">Note</th> 72</tr> 73<tr><td align="left" valign="top"><p> 74 Spirit provides two ways to handle expectation failures by throwing 75 an expectation exception. Use the <a class="link" href="../operator/expect.html" title="Expectation Operator (a > b)">Expectation 76 operator</a> if you do not need an exception to be thrown when the 77 first parser of a sequence fails. 78 </p></td></tr> 79</table></div> 80<h6> 81<a name="spirit.qi.reference.directive.expect.h1"></a> 82 <span class="phrase"><a name="spirit.qi.reference.directive.expect.header"></a></span><a class="link" href="expect.html#spirit.qi.reference.directive.expect.header">Header</a> 83 </h6> 84<pre class="programlisting"><span class="comment">// forwards to <boost/spirit/home/qi/directive/expect.hpp></span> 85<span class="preprocessor">#include</span> <span class="special"><</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">></span> 86</pre> 87<p> 88 Also, see <a class="link" href="../../../structure/include.html" title="Include">Include Structure</a>. 89 </p> 90<h6> 91<a name="spirit.qi.reference.directive.expect.h2"></a> 92 <span class="phrase"><a name="spirit.qi.reference.directive.expect.model_of"></a></span><a class="link" href="expect.html#spirit.qi.reference.directive.expect.model_of">Model 93 of</a> 94 </h6> 95<div class="blockquote"><blockquote class="blockquote"><p> 96 <a class="link" href="../parser_concepts/unaryparser.html" title="UnaryParser"><code class="computeroutput"><span class="identifier">UnaryParser</span></code></a> 97 </p></blockquote></div> 98<div class="variablelist"> 99<p class="title"><b>Notation</b></p> 100<dl class="variablelist"> 101<dt><span class="term"><code class="computeroutput"><span class="identifier">a</span></code></span></dt> 102<dd><p> 103 A <a class="link" href="../parser_concepts/parser.html" title="Parser"><code class="computeroutput"><span class="identifier">Parser</span></code></a> 104 </p></dd> 105<dt><span class="term"><code class="computeroutput"><span class="identifier">Iter</span></code></span></dt> 106<dd><p> 107 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 108 </p></dd> 109</dl> 110</div> 111<h6> 112<a name="spirit.qi.reference.directive.expect.h3"></a> 113 <span class="phrase"><a name="spirit.qi.reference.directive.expect.expectation_failure"></a></span><a class="link" href="expect.html#spirit.qi.reference.directive.expect.expectation_failure">Expectation 114 Failure</a> 115 </h6> 116<p> 117 When the operand parser fails to match an <code class="computeroutput"><span class="identifier">expectation_failure</span><span class="special"><</span><span class="identifier">Iter</span><span class="special">></span></code> is thrown: 118 </p> 119<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Iter</span><span class="special">></span> 120<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> 121<span class="special">{</span> 122 <span class="identifier">Iter</span> <span class="identifier">first</span><span class="special">;</span> <span class="comment">// [first, last) iterator pointing</span> 123 <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> 124 <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> 125<span class="special">};</span> 126</pre> 127<h6> 128<a name="spirit.qi.reference.directive.expect.h4"></a> 129 <span class="phrase"><a name="spirit.qi.reference.directive.expect.expression_semantics"></a></span><a class="link" href="expect.html#spirit.qi.reference.directive.expect.expression_semantics">Expression 130 Semantics</a> 131 </h6> 132<p> 133 Semantics of an expression is defined only where it differs from, or 134 is not defined in <a class="link" href="../parser_concepts/unaryparser.html" title="UnaryParser"><code class="computeroutput"><span class="identifier">UnaryParser</span></code></a>. 135 </p> 136<div class="informaltable"><table class="table"> 137<colgroup> 138<col> 139<col> 140</colgroup> 141<thead><tr> 142<th> 143 <p> 144 Expression 145 </p> 146 </th> 147<th> 148 <p> 149 Semantics 150 </p> 151 </th> 152</tr></thead> 153<tbody><tr> 154<td> 155 <p> 156 <code class="computeroutput"><span class="identifier">expect</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span></code> 157 </p> 158 </td> 159<td> 160 <p> 161 Match <code class="computeroutput"><span class="identifier">a</span></code>. If 162 <code class="computeroutput"><span class="identifier">a</span></code> fails, throw 163 an <code class="computeroutput"><span class="identifier">expectation_failure</span><span class="special"><</span><span class="identifier">Iter</span><span class="special">></span></code> 164 </p> 165 </td> 166</tr></tbody> 167</table></div> 168<h6> 169<a name="spirit.qi.reference.directive.expect.h5"></a> 170 <span class="phrase"><a name="spirit.qi.reference.directive.expect.attributes"></a></span><a class="link" href="expect.html#spirit.qi.reference.directive.expect.attributes">Attributes</a> 171 </h6> 172<p> 173 See <a class="link" href="../../quick_reference/compound_attribute_rules.html#spirit.qi.quick_reference.compound_attribute_rules.notation">Compound 174 Attribute Notation</a>. 175 </p> 176<div class="informaltable"><table class="table"> 177<colgroup> 178<col> 179<col> 180</colgroup> 181<thead><tr> 182<th> 183 <p> 184 Expression 185 </p> 186 </th> 187<th> 188 <p> 189 Attribute 190 </p> 191 </th> 192</tr></thead> 193<tbody><tr> 194<td> 195 <p> 196 <code class="computeroutput"><span class="identifier">expect</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span></code> 197 </p> 198 </td> 199<td> 200 <p> 201</p> 202<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">expect</span><span class="special">[</span><span class="identifier">a</span><span class="special">]:</span> <span class="identifier">A</span> 203<span class="identifier">a</span><span class="special">:</span> <span class="identifier">Unused</span> <span class="special">--></span> <span class="identifier">expect</span><span class="special">[</span><span class="identifier">a</span><span class="special">]</span> <span class="special">:</span> <span class="identifier">Unused</span></pre> 204<p> 205 </p> 206 </td> 207</tr></tbody> 208</table></div> 209<h6> 210<a name="spirit.qi.reference.directive.expect.h6"></a> 211 <span class="phrase"><a name="spirit.qi.reference.directive.expect.complexity"></a></span><a class="link" href="expect.html#spirit.qi.reference.directive.expect.complexity">Complexity</a> 212 </h6> 213<div class="blockquote"><blockquote class="blockquote"><p> 214 The overall complexity of the expectation parser is defined by the 215 complexity of it's argument parser. The complexity of the expect directive 216 itself is O(1). 217 </p></blockquote></div> 218<h6> 219<a name="spirit.qi.reference.directive.expect.h7"></a> 220 <span class="phrase"><a name="spirit.qi.reference.directive.expect.example"></a></span><a class="link" href="expect.html#spirit.qi.reference.directive.expect.example">Example</a> 221 </h6> 222<div class="note"><table border="0" summary="Note"> 223<tr> 224<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td> 225<th align="left">Note</th> 226</tr> 227<tr><td align="left" valign="top"><p> 228 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> 229 section. 230 </p></td></tr> 231</table></div> 232<p> 233 Some using declarations: 234 </p> 235<p> 236</p> 237<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> 238<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">expect</span><span class="special">;</span> 239<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> 240</pre> 241<p> 242 </p> 243<p> 244 The code below uses an expectation operator to throw an <a class="link" href="../operator/expect.html#spirit.qi.reference.operator.expect.expectation_failure"><code class="computeroutput"><span class="identifier">expectation_failure</span></code></a> with a deliberate 245 parsing error when <code class="computeroutput"><span class="string">"o"</span></code> 246 is expected and <code class="computeroutput"><span class="string">"x"</span></code> 247 is what is found in the input. The <code class="computeroutput"><span class="keyword">catch</span></code> 248 block prints the information related to the error. Note: This is low 249 level code that demonstrates the <span class="emphasis"><em>bare-metal</em></span>. Typically, 250 you use an Error Handler to deal with the error. 251 </p> 252<p> 253</p> 254<pre class="programlisting"><span class="keyword">try</span> 255<span class="special">{</span> 256 <span class="identifier">test_parser</span><span class="special">(</span><span class="string">"xi"</span><span class="special">,</span> <span class="identifier">expect</span><span class="special">[</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> 257<span class="special">}</span> 258<span class="keyword">catch</span> <span class="special">(</span><span class="identifier">expectation_failure</span><span class="special"><</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*></span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">x</span><span class="special">)</span> 259<span class="special">{</span> 260 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</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> 261 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"got: \""</span> <span class="special"><<</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"><<</span> <span class="char">'"'</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 262<span class="special">}</span> 263</pre> 264<p> 265 </p> 266<p> 267 The code above will print: 268 </p> 269<pre class="programlisting">expected: tag: literal-char, value: o 270got: "x" 271</pre> 272</div> 273<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 274<td align="left"></td> 275<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p> 276 Distributed under the Boost Software License, Version 1.0. (See accompanying 277 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>) 278 </p> 279</div></td> 280</tr></table> 281<hr> 282<div class="spirit-nav"> 283<a accesskey="p" href="as.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../directive.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="../nonterminal.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 284</div> 285</body> 286</html> 287