]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [/============================================================================== |
2 | Copyright (C) 2001-2015 Joel de Guzman | |
3 | Copyright (C) 2001-2011 Hartmut Kaiser | |
4 | ||
5 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
6 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
7 | ===============================================================================/] | |
8 | ||
9 | [section Number List Redux - list syntax] | |
10 | ||
11 | ||
12 | So far, we've been using the syntax: | |
13 | ||
14 | double_ >> *(',' >> double_) | |
15 | ||
16 | to parse a comma-delimited list of numbers. Such lists are common in parsing and | |
17 | Spirit provides a simpler shortcut for them. The expression above can be | |
18 | simplified to: | |
19 | ||
20 | double_ % ',' | |
21 | ||
22 | read as: a list of doubles separated by `','`. | |
23 | ||
24 | This sample, again a variation of our previous example, demonstrates just that: | |
25 | ||
26 | template <typename Iterator> | |
27 | bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v) | |
28 | { | |
29 | using x3::double_; | |
30 | using x3::phrase_parse; | |
31 | using x3::_attr; | |
32 | using ascii::space; | |
33 | ||
34 | auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); }; | |
35 | ||
36 | bool r = phrase_parse(first, last, | |
37 | ||
38 | // Begin grammar | |
39 | ( | |
40 | double_[push_back] % ',' | |
41 | ) | |
42 | , | |
43 | // End grammar | |
44 | ||
45 | space); | |
46 | ||
47 | if (first != last) // fail if we did not get a full match | |
48 | return false; | |
49 | return r; | |
50 | } | |
51 | ||
52 | The full cpp file for this example can be found here: [@../../../example/x3/num_list/num_list3.cpp] | |
53 | ||
54 | [endsect] |