Blame view

3rdparty/boost_1_81_0/libs/metaparse/doc/repeated.qbk 2.01 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
83
84
85
86
87
  [#repeated]
  [section repeated]
  
  [h1 Synopsis]
  
    template <class P>
    struct repeated;
  
  This is a [link parser_combinator parser combinator].
  
  [table Arguments
    [[Name] [Type]]
    [[`P`]  [[link parser parser]]]
  ]
  
  [h1 Description]
  
  It applies `P` on the input string repeatedly as long as `P` accepts the input.
  The result of parsing is a sequence of the results of the individual
  applications of `P`.
  
  When `P` rejects the input for the first time, `repeated` still accepts the
  input and the result of parsing is an empty sequence.
  
  Here is a diagram showing how `repeated` works by example:
  
    using int_token = token<int_>;
  
  [$images/metaparse/repeated_diag1.png [width 70%]]
  
  Further details can be found in the [link repetition Repetition] section
  of the [link manual User Manual].
  
  [h1 Header]
  
    #include <boost/metaparse/repeated.hpp>
  
  [h1 Expression semantics]
  
  For any `p` parser the following are equivalent:
  
    repeated<p>
    
    foldl<
      p,
      /* unspecified empty sequence */,
      boost::mpl::push_back<_2, _1>
    >
  
  [h1 Example]
  
    #include <boost/metaparse/repeated.hpp>
    #include <boost/metaparse/digit_val.hpp>
    #include <boost/metaparse/start.hpp>
    #include <boost/metaparse/string.hpp>
    #include <boost/metaparse/get_result.hpp>
    
    #include <boost/mpl/equal.hpp>
    #include <boost/mpl/vector.hpp>
    #include <boost/mpl/int.hpp>
    
    using namespace boost::metaparse;
    
    using digits = repeated<digit_val>;
    
    static_assert(
      boost::mpl::equal<
        get_result<digits::apply<BOOST_METAPARSE_STRING("1234"), start>>::type,
        boost::mpl::vector<
          boost::mpl::int_<1>,
          boost::mpl::int_<2>,
          boost::mpl::int_<3>,
          boost::mpl::int_<4>
        >
      >::type::value,
      "the result of parsing should be the list of digit values"
    );
    
    static_assert(
      boost::mpl::equal<
        get_result<digits::apply<BOOST_METAPARSE_STRING("x"), start>>::type,
        boost::mpl::vector<>
      >::type::value,
      "repeated should accept the input when it can't parse anything with digit_val"
    );
  
  [endsect]