]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#one_of] |
2 | [section one_of] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class P1, class P2, /* ... */, class Pn> | |
7 | struct one_of; | |
8 | ||
9 | This is a [link parser_combinator parser combinator]. | |
10 | ||
11 | [table Arguments | |
12 | [[Name] [Type]] | |
13 | [[`P1` .. `Pn`] [[link parser parser]s]] | |
14 | ] | |
15 | ||
16 | [h1 Description] | |
17 | ||
18 | It accepts an input when any of the `P1`, ... `Pn` parsers accept it. The result | |
19 | of parsing is the result of applying the first parser that accepts the input. | |
20 | The parsers are tried in order, therefore in case of ambiguous grammars the | |
21 | result of parsing depends on the order of the `P1` ... `Pn` parsers. | |
22 | ||
23 | The maximum number of accepted parsers is defined by the | |
24 | `BOOST_METAPARSE_LIMIT_ONE_OF_SIZE` macro. Its default value is `20`. | |
25 | ||
26 | [h1 Header] | |
27 | ||
28 | #include <boost/metaparse/one_of.hpp> | |
29 | ||
30 | [h1 Expression semantics] | |
31 | ||
32 | For any `p1`, ..., `pn` parsers, `s` compile-time string and `pos` source | |
33 | position | |
34 | ||
35 | one_of<p1, ..., pn>::apply<s, pos> | |
36 | ||
37 | is equivalent to | |
38 | ||
39 | pk::apply<s, pos> | |
40 | ||
41 | when there is a `k`, that `pi::apply<s, pos>::type` returns an error for every | |
42 | `i` in the range `[1..k)` and `pk::apply<s, pos>::type` doesn't return an error. | |
43 | ||
44 | The parser combinator returns an error when there is no such `k`. | |
45 | ||
46 | [h1 Example] | |
47 | ||
48 | #include <boost/metaparse/one_of.hpp> | |
49 | #include <boost/metaparse/lit_c.hpp> | |
50 | #include <boost/metaparse/start.hpp> | |
51 | #include <boost/metaparse/string.hpp> | |
52 | #include <boost/metaparse/get_result.hpp> | |
53 | #include <boost/metaparse/is_error.hpp> | |
54 | ||
55 | using namespace boost::metaparse; | |
56 | ||
57 | using whitespace = | |
58 | one_of<lit_c<' '>, lit_c<'\n'>, lit_c<'\r'>, lit_c<'\t'>, lit_c<'\v'>>; | |
59 | ||
60 | static_assert( | |
61 | get_result< | |
62 | whitespace::apply<BOOST_METAPARSE_STRING(" "), start> | |
63 | >::type::value == ' ', | |
64 | "the result of parsing should be the first character of the input" | |
65 | ); | |
66 | ||
67 | static_assert( | |
68 | is_error<whitespace::apply<BOOST_METAPARSE_STRING("x"), start>>::type::value, | |
69 | "it should return an error when the input does not begin with a whitespace" | |
70 | ); | |
71 | ||
72 | ||
73 | [endsect] | |
74 |