Blame view

3rdparty/boost_1_81_0/libs/metaparse/doc/except.qbk 2.03 KB
73ef4ff3   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  [#except]
  [section except]
  
  [h1 Synopsis]
  
    template <class P, class Result, class ErrorMsg>
    struct except;
  
  This is a [link parser_combinator parser combinator].
  
  [table Arguments
    [[Name]       [Type]]
    [[`P`]        [[link parser parser]]]
    [[`Result`]   [[link metaprogramming_value template metaprogramming value]]]
    [[`ErrorMsg`] [[link parsing_error_message parsing error message]]]
  ]
  
  [h1 Description]
  
  `except` accepts the input when `P` rejects it and the result of parsing is the
  `Result` argument. When `P` accepts the input, `except` rejects it and the
  reason is `ErrorMsg`.
  
  [h1 Header]
  
    #include <boost/metaparse/except.hpp>
  
  [h1 Expression semantics]
  
  For any `p` parser, `c` class, `msg` parsing error message, `s` compile-time
  string and `pos` source position the following are equivalent
  
    get_result<except<p, c, msg>, s, pos>::type
    c
  
    get_remaining<except<p, c, msg>, s, pos>::type
    s
  
    get_position<except<p, c, msg>, s, pos>::type
    pos
  
  when `p` rejects the input. The result of the parser is an error with the error
  message `msg` otherwise.
  
  [h1 Example]
  
    #include <boost/metaparse/except.hpp>
    #include <boost/metaparse/int_.hpp>
    #include <boost/metaparse/string.hpp>
    #include <boost/metaparse/start.hpp>
    #include <boost/metaparse/get_result.hpp>
    #include <boost/metaparse/get_message.hpp>
    #include <boost/metaparse/define_error.hpp>
    
    #include <type_traits>
    
    using namespace boost::metaparse;
    
    BOOST_METAPARSE_DEFINE_ERROR(
      number_is_not_allowed,
      "numbers are not allowed here"
    );
    
    using except_int =
      except<int_, std::integral_constant<int, 1>, number_is_not_allowed>;
    
    static_assert(
      get_result<
        except_int::apply<BOOST_METAPARSE_STRING("foo"), start>
      >::type::value == 1,
      "it should accept the input when it is not an integer"
    );
    
    static_assert(
      std::is_same<
        number_is_not_allowed,
        get_message<except_int::apply<BOOST_METAPARSE_STRING("13"), start>>::type
      >::type::value,
      "it should reject the input when it is an integer"
    );
  
  [endsect]