]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [/============================================================================== |
2 | Copyright (C) 2001-2011 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 | ||
28 | So, what does this give us? Well, we can simply pass in a `std::vector<double>` | |
29 | to our number list parser and it will happily churn out our result in our | |
30 | vector. For that to happen, we'll use a variation of the `phrase_parse` with | |
31 | an additional argument: the parser's attribute. With the following arguments | |
32 | passed to `phrase_parse` | |
33 | ||
34 | # An iterator pointing to the start of the input | |
35 | # An iterator pointing to one past the end of the input | |
36 | # The parser object | |
37 | # Another parser called the skip parser | |
38 | # The parser's attribute | |
39 | ||
40 | our parser now is further simplified to: | |
41 | ||
42 | [import ../../example/qi/num_list4.cpp] | |
43 | ||
44 | [tutorial_numlist4] | |
45 | ||
46 | The full cpp file for this example can be found here: [@../../example/qi/num_list4.cpp] | |
47 | ||
48 | [*Hey, no more actions!!!] Now we're entering the realm of attribute grammars. | |
49 | Cool eh? | |
50 | ||
51 | [endsect] |