• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Number List Redux - list syntax</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="../tutorials.html" title="Tutorials">
9<link rel="prev" href="number_list___stuffing_numbers_into_a_std__vector.html" title="Number List - stuffing numbers into a std::vector">
10<link rel="next" href="number_list_attribute___one_more__with_style.html" title="Number List Attribute - one more, with style">
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="number_list___stuffing_numbers_into_a_std__vector.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.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="number_list_attribute___one_more__with_style.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h4 class="title">
27<a name="spirit.qi.tutorials.number_list_redux___list_syntax"></a><a class="link" href="number_list_redux___list_syntax.html" title="Number List Redux - list syntax">Number
28        List Redux - list syntax</a>
29</h4></div></div></div>
30<p>
31          So far, we've been using the syntax:
32        </p>
33<pre class="programlisting"><span class="identifier">double_</span> <span class="special">&gt;&gt;</span> <span class="special">*(</span><span class="char">','</span> <span class="special">&gt;&gt;</span> <span class="identifier">double_</span><span class="special">)</span>
34</pre>
35<p>
36          to parse a comma-delimited list of numbers. Such lists are common in parsing
37          and Spirit provides a simpler shortcut for them. The expression above can
38          be simplified to:
39        </p>
40<pre class="programlisting"><span class="identifier">double_</span> <span class="special">%</span> <span class="char">','</span>
41</pre>
42<p>
43          read as: a list of doubles separated by <code class="computeroutput"><span class="char">','</span></code>.
44        </p>
45<p>
46          This sample, again a variation of our previous example, demonstrates just
47          that:
48        </p>
49<p>
50</p>
51<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">&gt;</span>
52<span class="keyword">bool</span> <span class="identifier">parse_numbers</span><span class="special">(</span><span class="identifier">Iterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">Iterator</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;&amp;</span> <span class="identifier">v</span><span class="special">)</span>
53<span class="special">{</span>
54    <span class="keyword">using</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">double_</span><span class="special">;</span>
55    <span class="keyword">using</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">phrase_parse</span><span class="special">;</span>
56    <span class="keyword">using</span> <span class="identifier">qi</span><span class="special">::</span><span class="identifier">_1</span><span class="special">;</span>
57    <span class="keyword">using</span> <span class="identifier">ascii</span><span class="special">::</span><span class="identifier">space</span><span class="special">;</span>
58    <span class="keyword">using</span> <span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">push_back</span><span class="special">;</span>
59
60    <span class="keyword">bool</span> <span class="identifier">r</span> <span class="special">=</span> <span class="identifier">phrase_parse</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span>
61
62        <span class="comment">//  Begin grammar</span>
63        <span class="special">(</span>
64            <span class="identifier">double_</span><span class="special">[</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">v</span><span class="special">),</span> <span class="identifier">_1</span><span class="special">)]</span> <span class="special">%</span> <span class="char">','</span>
65        <span class="special">)</span>
66        <span class="special">,</span>
67        <span class="comment">//  End grammar</span>
68
69        <span class="identifier">space</span><span class="special">);</span>
70
71    <span class="keyword">if</span> <span class="special">(</span><span class="identifier">first</span> <span class="special">!=</span> <span class="identifier">last</span><span class="special">)</span> <span class="comment">// fail if we did not get a full match</span>
72        <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
73    <span class="keyword">return</span> <span class="identifier">r</span><span class="special">;</span>
74<span class="special">}</span>
75</pre>
76<p>
77        </p>
78<p>
79          The full cpp file for this example can be found here: <a href="../../../../../example/qi/num_list3.cpp" target="_top">../../example/qi/num_list3.cpp</a>
80        </p>
81</div>
82<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
83<td align="left"></td>
84<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
85        Distributed under the Boost Software License, Version 1.0. (See accompanying
86        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>)
87      </p>
88</div></td>
89</tr></table>
90<hr>
91<div class="spirit-nav">
92<a accesskey="p" href="number_list___stuffing_numbers_into_a_std__vector.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.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="number_list_attribute___one_more__with_style.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
93</div>
94</body>
95</html>
96