]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#optional] |
2 | [section optional] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class P, class Default = /* unspecified */> | |
7 | struct optional; | |
8 | ||
9 | This is a [link parser_combinator parser combinator]. | |
10 | ||
11 | [table Arguments | |
12 | [[Name] [Type]] | |
13 | [[`P`] [[link parser parser]]] | |
14 | [[`Default`] [[link metaprogramming_value template metaprogramming value]]] | |
15 | ] | |
16 | ||
17 | [h1 Description] | |
18 | ||
19 | It tries parsing the input with `P`. When `P` succeeds, the result of parsing is | |
20 | the result of `P`. Otherwise no characters are consumed and the result of | |
21 | parsing is `Default`. | |
22 | ||
23 | [h1 Header] | |
24 | ||
25 | #include <boost/metaparse/optional.hpp> | |
26 | ||
27 | [h1 Expression semantics] | |
28 | ||
29 | For any `p` [link parser parser] and `d` | |
30 | [link metaprogramming_value template metaprogramming value] | |
31 | ||
32 | optional<p, d> | |
33 | ||
34 | is equivalent to | |
35 | ||
36 | one_of<p, return_<d>> | |
37 | ||
38 | [h1 Example] | |
39 | ||
40 | #include <boost/metaparse/optional.hpp> | |
41 | #include <boost/metaparse/start.hpp> | |
42 | #include <boost/metaparse/int_.hpp> | |
43 | #include <boost/metaparse/middle_of.hpp> | |
44 | #include <boost/metaparse/sequence.hpp> | |
45 | #include <boost/metaparse/lit_c.hpp> | |
46 | #include <boost/metaparse/string.hpp> | |
47 | #include <boost/metaparse/get_result.hpp> | |
48 | ||
49 | #include <boost/mpl/int.hpp> | |
50 | #include <boost/mpl/equal.hpp> | |
51 | #include <boost/mpl/equal_to.hpp> | |
52 | #include <boost/mpl/vector_c.hpp> | |
53 | ||
54 | using namespace boost::metaparse; | |
55 | ||
56 | using complex_number = | |
57 | sequence< | |
58 | // Real | |
59 | int_, | |
60 | ||
61 | // Imaginary | |
62 | optional< | |
63 | middle_of<lit_c<'+'>, int_, lit_c<'i'>>, | |
64 | boost::mpl::int_<0> | |
65 | > | |
66 | > | |
67 | ; | |
68 | ||
69 | static_assert( | |
70 | boost::mpl::equal< | |
71 | boost::mpl::vector_c<int, 1, 0>, | |
72 | get_result< | |
73 | complex_number::apply<BOOST_METAPARSE_STRING("1"), start> | |
74 | >::type, | |
75 | ||
76 | boost::mpl::equal_to<boost::mpl::_, boost::mpl::_> | |
77 | >::type::value, | |
78 | "No imaginary" | |
79 | ); | |
80 | ||
81 | static_assert( | |
82 | boost::mpl::equal< | |
83 | boost::mpl::vector_c<int, 1, 0>, | |
84 | get_result< | |
85 | complex_number::apply<BOOST_METAPARSE_STRING("1+0i"), start> | |
86 | >::type, | |
87 | ||
88 | boost::mpl::equal_to<boost::mpl::_, boost::mpl::_> | |
89 | >::type::value, | |
90 | "0 as imaginary" | |
91 | ); | |
92 | ||
93 | static_assert( | |
94 | boost::mpl::equal< | |
95 | boost::mpl::vector_c<int, 0, 1>, | |
96 | get_result< | |
97 | complex_number::apply<BOOST_METAPARSE_STRING("0+1i"), start> | |
98 | >::type, | |
99 | ||
100 | boost::mpl::equal_to<boost::mpl::_, boost::mpl::_> | |
101 | >::type::value, | |
102 | "Non-null imaginary" | |
103 | ); | |
104 | ||
105 | [endsect] | |
106 |