]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/raw.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / spirit / test / x3 / raw.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #include <boost/spirit/home/x3.hpp>
8 #include <boost/fusion/include/std_pair.hpp>
9
10 #include <iostream>
11 #include <string>
12 #include "test.hpp"
13
14 using boost::spirit::x3::rule;
15
16 rule<class direct_rule, int> direct_rule = "direct_rule";
17 rule<class indirect_rule, int> indirect_rule = "indirect_rule";
18
19 auto const direct_rule_def = boost::spirit::x3::int_;
20 auto const indirect_rule_def = direct_rule;
21
22 BOOST_SPIRIT_DEFINE(direct_rule, indirect_rule)
23
24 int main()
25 {
26 using spirit_test::test;
27 using spirit_test::test_attr;
28 using namespace boost::spirit::x3::ascii;
29 using boost::spirit::x3::raw;
30 using boost::spirit::x3::eps;
31 using boost::spirit::x3::lit;
32 using boost::spirit::x3::_attr;
33 using boost::spirit::x3::parse;
34 using boost::spirit::x3::int_;
35 using boost::spirit::x3::char_;
36
37 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(raw['x']);
38
39 {
40 boost::iterator_range<char const*> range;
41 std::string str;
42 BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], range)));
43 BOOST_TEST((std::string(range.begin(), range.end()) == "spirit_test_123"));
44 BOOST_TEST((test_attr(" spirit", raw[*alpha], range, space)));
45 BOOST_TEST((range.size() == 6));
46 }
47
48 {
49 std::string str;
50 BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], str)));
51 BOOST_TEST((str == "spirit_test_123"));
52
53 str.clear();
54 BOOST_TEST((test_attr("x123", alpha >> raw[+alnum], str)))
55 && BOOST_TEST_EQ(str, "x123");
56 }
57
58 {
59 boost::iterator_range<char const*> range;
60 BOOST_TEST((test("x", raw[alpha])));
61 BOOST_TEST((test_attr("x", raw[alpha], range)));
62 BOOST_TEST((test_attr("x", raw[alpha] >> eps, range)));
63 }
64
65 {
66 boost::iterator_range<char const*> range;
67 BOOST_TEST((test("x", raw[alpha][ ([&](auto& ctx){ range = _attr(ctx); }) ])));
68 BOOST_TEST(range.size() == 1 && *range.begin() == 'x');
69 }
70
71 {
72 boost::iterator_range<char const*> range;
73 BOOST_TEST((test("x123x", lit('x') >> raw[+digit] >> lit('x'))));
74 BOOST_TEST((test_attr("x123x", lit('x') >> raw[+digit] >> lit('x'), range)));
75 BOOST_TEST((std::string(range.begin(), range.end()) == "123"));
76 }
77
78 {
79 using range = boost::iterator_range<std::string::iterator>;
80 boost::variant<int, range> attr;
81
82 std::string str("test");
83 parse(str.begin(), str.end(), (int_ | raw[*char_]), attr);
84
85 auto rng = boost::get<range>(attr);
86 BOOST_TEST(std::string(rng.begin(), rng.end()) == "test");
87 }
88
89 {
90 std::vector<boost::iterator_range<std::string::iterator>> attr;
91 std::string str("123abcd");
92 parse(str.begin(), str.end()
93 , (raw[int_] >> raw[*char_])
94 , attr
95 );
96 BOOST_TEST(attr.size() == 2);
97 BOOST_TEST(std::string(attr[0].begin(), attr[0].end()) == "123");
98 BOOST_TEST(std::string(attr[1].begin(), attr[1].end()) == "abcd");
99 }
100
101 {
102 std::pair<int, boost::iterator_range<std::string::iterator>> attr;
103 std::string str("123abcd");
104 parse(str.begin(), str.end()
105 , (int_ >> raw[*char_])
106 , attr
107 );
108 BOOST_TEST(attr.first == 123);
109 BOOST_TEST(std::string(attr.second.begin(), attr.second.end()) == "abcd");
110 }
111
112 {
113 // test with simple rule
114 boost::iterator_range<char const*> range;
115 BOOST_TEST((test_attr("123", raw[direct_rule], range)));
116 BOOST_TEST((std::string(range.begin(), range.end()) == "123"));
117 }
118
119 {
120 // test with complex rule
121 boost::iterator_range<char const*> range;
122 BOOST_TEST((test_attr("123", raw[indirect_rule], range)));
123 BOOST_TEST((std::string(range.begin(), range.end()) == "123"));
124 }
125
126 return boost::report_errors();
127 }