• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Optional Parser (-a)</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="not_predicate.html" title="Not-Predicate Parser (!a)">
10<link rel="next" href="permutation.html" title="Permutation Parser (a ^ b)">
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="not_predicate.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="permutation.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.optional"></a><a class="link" href="optional.html" title="Optional Parser (-a)">Optional Parser
28          (<code class="computeroutput"><span class="special">-</span><span class="identifier">a</span></code>)</a>
29</h5></div></div></div>
30<h6>
31<a name="spirit.qi.reference.operator.optional.h0"></a>
32            <span class="phrase"><a name="spirit.qi.reference.operator.optional.description"></a></span><a class="link" href="optional.html#spirit.qi.reference.operator.optional.description">Description</a>
33          </h6>
34<p>
35            The optional operator, <code class="computeroutput"><span class="special">-</span><span class="identifier">a</span></code>, is a unary operator that matches
36            its operand zero or one time.
37          </p>
38<h6>
39<a name="spirit.qi.reference.operator.optional.h1"></a>
40            <span class="phrase"><a name="spirit.qi.reference.operator.optional.header"></a></span><a class="link" href="optional.html#spirit.qi.reference.operator.optional.header">Header</a>
41          </h6>
42<pre class="programlisting"><span class="comment">// forwards to &lt;boost/spirit/home/qi/operator/optional.hpp&gt;</span>
43<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_optional</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
44</pre>
45<p>
46            Also, see <a class="link" href="../../../structure/include.html" title="Include">Include Structure</a>.
47          </p>
48<h6>
49<a name="spirit.qi.reference.operator.optional.h2"></a>
50            <span class="phrase"><a name="spirit.qi.reference.operator.optional.model_of"></a></span><a class="link" href="optional.html#spirit.qi.reference.operator.optional.model_of">Model
51            of</a>
52          </h6>
53<div class="blockquote"><blockquote class="blockquote"><p>
54              <a class="link" href="../parser_concepts/unaryparser.html" title="UnaryParser"><code class="computeroutput"><span class="identifier">UnaryParser</span></code></a>
55            </p></blockquote></div>
56<div class="variablelist">
57<p class="title"><b>Notation</b></p>
58<dl class="variablelist">
59<dt><span class="term"><code class="computeroutput"><span class="identifier">a</span></code></span></dt>
60<dd><p>
61                  A <a class="link" href="../parser_concepts/parser.html" title="Parser"><code class="computeroutput"><span class="identifier">Parser</span></code></a>
62                </p></dd>
63</dl>
64</div>
65<h6>
66<a name="spirit.qi.reference.operator.optional.h3"></a>
67            <span class="phrase"><a name="spirit.qi.reference.operator.optional.expression_semantics"></a></span><a class="link" href="optional.html#spirit.qi.reference.operator.optional.expression_semantics">Expression
68            Semantics</a>
69          </h6>
70<p>
71            Semantics of an expression is defined only where it differs from, or
72            is not defined in <a class="link" href="../parser_concepts/unaryparser.html" title="UnaryParser"><code class="computeroutput"><span class="identifier">UnaryParser</span></code></a>.
73          </p>
74<div class="informaltable"><table class="table">
75<colgroup>
76<col>
77<col>
78</colgroup>
79<thead><tr>
80<th>
81                    <p>
82                      Expression
83                    </p>
84                  </th>
85<th>
86                    <p>
87                      Semantics
88                    </p>
89                  </th>
90</tr></thead>
91<tbody><tr>
92<td>
93                    <p>
94                      <code class="computeroutput"><span class="special">-</span><span class="identifier">a</span></code>
95                    </p>
96                  </td>
97<td>
98                    <p>
99                      Match <code class="computeroutput"><span class="identifier">a</span></code> zero
100                      or one time.
101                    </p>
102                  </td>
103</tr></tbody>
104</table></div>
105<h6>
106<a name="spirit.qi.reference.operator.optional.h4"></a>
107            <span class="phrase"><a name="spirit.qi.reference.operator.optional.attributes"></a></span><a class="link" href="optional.html#spirit.qi.reference.operator.optional.attributes">Attributes</a>
108          </h6>
109<p>
110            See <a class="link" href="../../quick_reference/compound_attribute_rules.html#spirit.qi.quick_reference.compound_attribute_rules.notation">Compound
111            Attribute Notation</a>.
112          </p>
113<div class="informaltable"><table class="table">
114<colgroup>
115<col>
116<col>
117</colgroup>
118<thead><tr>
119<th>
120                    <p>
121                      Expression
122                    </p>
123                  </th>
124<th>
125                    <p>
126                      Attribute
127                    </p>
128                  </th>
129</tr></thead>
130<tbody><tr>
131<td>
132                    <p>
133                      <code class="computeroutput"><span class="special">-</span><span class="identifier">a</span></code>
134                    </p>
135                  </td>
136<td>
137                    <p>
138</p>
139<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">--&gt;</span> <span class="special">-</span><span class="identifier">a</span><span class="special">:</span> <span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span>
140<span class="identifier">a</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">:</span> <span class="identifier">Unused</span></pre>
141<p>
142                    </p>
143                  </td>
144</tr></tbody>
145</table></div>
146<h6>
147<a name="spirit.qi.reference.operator.optional.h5"></a>
148            <span class="phrase"><a name="spirit.qi.reference.operator.optional.complexity"></a></span><a class="link" href="optional.html#spirit.qi.reference.operator.optional.complexity">Complexity</a>
149          </h6>
150<div class="blockquote"><blockquote class="blockquote"><p>
151              The complexity is defined by the complexity of the operand, <code class="computeroutput"><span class="identifier">a</span></code>
152            </p></blockquote></div>
153<h6>
154<a name="spirit.qi.reference.operator.optional.h6"></a>
155            <span class="phrase"><a name="spirit.qi.reference.operator.optional.example"></a></span><a class="link" href="optional.html#spirit.qi.reference.operator.optional.example">Example</a>
156          </h6>
157<div class="note"><table border="0" summary="Note">
158<tr>
159<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
160<th align="left">Note</th>
161</tr>
162<tr><td align="left" valign="top"><p>
163              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>
164              section.
165            </p></td></tr>
166</table></div>
167<p>
168            Some using declarations:
169          </p>
170<p>
171</p>
172<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>
173<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">lexeme</span><span class="special">;</span>
174<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">int_</span><span class="special">;</span>
175<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">vector</span><span class="special">;</span>
176<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">at_c</span><span class="special">;</span>
177<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">;</span>
178</pre>
179<p>
180          </p>
181<p>
182            Parse a person info with name (in quotes) optional age <a href="#ftn.spirit.qi.reference.operator.optional.f0" class="footnote" name="spirit.qi.reference.operator.optional.f0"><sup class="footnote">[9]</sup></a> and optional sex, all separated by comma.
183          </p>
184<p>
185</p>
186<pre class="programlisting"><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;,</span> <span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">attr</span><span class="special">;</span>
187
188<span class="identifier">test_phrase_parser_attr</span><span class="special">(</span>
189    <span class="string">"\"James Bond\", M"</span>
190  <span class="special">,</span> <span class="identifier">lexeme</span><span class="special">[</span><span class="char">'"'</span> <span class="special">&gt;&gt;</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">&gt;&gt;</span> <span class="char">'"'</span><span class="special">]</span>    <span class="comment">// name</span>
191        <span class="special">&gt;&gt;</span> <span class="special">-(</span><span class="char">','</span> <span class="special">&gt;&gt;</span> <span class="identifier">int_</span><span class="special">)</span>                   <span class="comment">// optional age</span>
192        <span class="special">&gt;&gt;</span> <span class="special">-(</span><span class="char">','</span> <span class="special">&gt;&gt;</span> <span class="identifier">char_</span><span class="special">)</span>                  <span class="comment">// optional sex</span>
193  <span class="special">,</span> <span class="identifier">attr</span><span class="special">);</span>
194
195<span class="comment">// Should print: James Bond,M</span>
196<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">at_c</span><span class="special">&lt;</span><span class="number">0</span><span class="special">&gt;(</span><span class="identifier">attr</span><span class="special">);</span>                 <span class="comment">// print name</span>
197<span class="keyword">if</span> <span class="special">(</span><span class="identifier">at_c</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;(</span><span class="identifier">attr</span><span class="special">))</span>                          <span class="comment">// print optional age</span>
198    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="char">','</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">at_c</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;(</span><span class="identifier">attr</span><span class="special">);</span>
199<span class="keyword">if</span> <span class="special">(</span><span class="identifier">at_c</span><span class="special">&lt;</span><span class="number">2</span><span class="special">&gt;(</span><span class="identifier">attr</span><span class="special">))</span>                          <span class="comment">// print optional sex</span>
200    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="char">','</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">at_c</span><span class="special">&lt;</span><span class="number">2</span><span class="special">&gt;(</span><span class="identifier">attr</span><span class="special">);</span>
201<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</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>
202</pre>
203<p>
204          </p>
205<div class="footnotes">
206<br><hr style="width:100; text-align:left;margin-left: 0">
207<div id="ftn.spirit.qi.reference.operator.optional.f0" class="footnote"><p><a href="#spirit.qi.reference.operator.optional.f0" class="para"><sup class="para">[9] </sup></a>
208              James Bond is shy about his age :-)
209            </p></div>
210</div>
211</div>
212<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
213<td align="left"></td>
214<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
215        Distributed under the Boost Software License, Version 1.0. (See accompanying
216        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>)
217      </p>
218</div></td>
219</tr></table>
220<hr>
221<div class="spirit-nav">
222<a accesskey="p" href="not_predicate.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="permutation.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
223</div>
224</body>
225</html>
226