]>
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 Attribute - one more, with style] | |
10 | ||
11 | You've seen that the `double_` parser has a `double` attribute. All parsers have | |
12 | an attribute, even complex parsers. Those that are composed from primitives | |
13 | using operators, like the list parser, also have an attribute. It so happens that | |
14 | the attribute of a list parser: | |
15 | ||
16 | p % d | |
17 | ||
18 | is a `std::vector` of the attribute of `p`. So, for our parser: | |
19 | ||
20 | ||
21 | double_ % ',' | |
22 | ||
23 | we'll have an attribute of: | |
24 | ||
25 | std::vector<double> | |
26 | ||
27 | So, what does this give us? Well, we can simply pass in a `std::vector<double>` | |
28 | to our number list parser and it will happily churn out our result in our | |
29 | vector. For that to happen, we'll use a variation of the `phrase_parse` with | |
30 | an additional argument: the parser's attribute. With the following arguments | |
31 | passed to `phrase_parse` | |
32 | ||
33 | # An iterator pointing to the start of the input | |
34 | # An iterator pointing to one past the end of the input | |
35 | # The parser object | |
36 | # Another parser called the skip parser | |
37 | # The parser's attribute | |
38 | ||
39 | Our parser now is further simplified to: | |
40 | ||
41 | template <typename Iterator> | |
42 | bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v) | |
43 | { | |
44 | using x3::double_; | |
45 | using x3::phrase_parse; | |
46 | using x3::_attr; | |
47 | using ascii::space; | |
48 | ||
49 | bool r = phrase_parse(first, last, | |
50 | ||
51 | // Begin grammar | |
52 | ( | |
53 | double_ % ',' | |
54 | ) | |
55 | , | |
56 | // End grammar | |
57 | ||
58 | space, v); | |
59 | ||
60 | if (first != last) // fail if we did not get a full match | |
61 | return false; | |
62 | return r; | |
63 | } | |
64 | ||
65 | The full cpp file for this example can be found here: [@../../../example/x3/num_list/num_list4.cpp] | |
66 | ||
67 | [*Hey, no more actions!!!] Now we're entering the realm of attribute grammars. | |
68 | Cool eh? | |
69 | ||
70 | [endsect] |