]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#one_char] |
2 | [section one_char] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | struct one_char; | |
7 | ||
8 | This is a [link parser parser]. | |
9 | ||
10 | [h1 Description] | |
11 | ||
12 | `one_char` accepts one character. The result of parsing is the accepted | |
13 | character. It fails for empty input. | |
14 | ||
15 | [h1 Header] | |
16 | ||
17 | #include <boost/metaparse/one_char.hpp> | |
18 | ||
19 | [h1 Expression semantics] | |
20 | ||
21 | For any `s` non-empty compile-time string and `pos` source position the | |
22 | following are equivalent | |
23 | ||
24 | get_result<one_char::apply<s, pos>> | |
25 | ||
26 | boost::mpl::front<s> | |
27 | ||
28 | get_remaining<one_char::apply<s, pos>> | |
29 | ||
30 | boost::mpl::pop_front<s> | |
31 | ||
32 | The value of `get_position<one_char::apply<s, pos>>` depends on the first | |
33 | character of the sequence and the value of the one parsed before that (which is | |
34 | stored in the source position). `one_char` updates the source position's `col` | |
35 | and `line` values based on the new line characters. It detects Linux (`\n`), | |
36 | Windows (`\r\n`) and Mac (`\r`) line endings. | |
37 | ||
38 | `one_char::apply<BOOST_METAPARSE_STRING(""), pos>` returns an error for every | |
39 | `pos` source position. | |
40 | ||
41 | [h1 Example] | |
42 | ||
43 | #include <boost/metaparse/one_char.hpp> | |
44 | #include <boost/metaparse/start.hpp> | |
45 | #include <boost/metaparse/string.hpp> | |
46 | #include <boost/metaparse/get_result.hpp> | |
47 | #include <boost/metaparse/get_remaining.hpp> | |
48 | #include <boost/metaparse/is_error.hpp> | |
49 | ||
50 | #include <type_traits> | |
51 | ||
52 | using namespace boost::metaparse; | |
53 | ||
54 | static_assert( | |
55 | get_result< | |
56 | one_char::apply<BOOST_METAPARSE_STRING("foo"), start> | |
57 | >::type::value == 'f', | |
58 | "the result of parsing should be the first character of the input" | |
59 | ); | |
60 | ||
61 | static_assert( | |
62 | std::is_same< | |
63 | BOOST_METAPARSE_STRING("oo"), | |
64 | get_remaining<one_char::apply<BOOST_METAPARSE_STRING("foo"), start>>::type | |
65 | >::type::value, | |
66 | "one_char should consume the first character of the input" | |
67 | ); | |
68 | ||
69 | static_assert( | |
70 | is_error<one_char::apply<BOOST_METAPARSE_STRING(""), start>>::type::value, | |
71 | "it should return an error for empty input" | |
72 | ); | |
73 | ||
74 | [endsect] | |
75 |