]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/raw.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
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>
14 using boost::spirit::x3::rule
;
16 rule
<class direct_rule
, int> direct_rule
= "direct_rule";
17 rule
<class indirect_rule
, int> indirect_rule
= "indirect_rule";
19 auto const direct_rule_def
= boost::spirit::x3::int_
;
20 auto const indirect_rule_def
= direct_rule
;
22 BOOST_SPIRIT_DEFINE(direct_rule
, indirect_rule
)
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_
;
37 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(raw
['x']);
40 boost::iterator_range
<char const*> range
;
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));
50 BOOST_TEST((test_attr("spirit_test_123", raw
[alpha
>> *(alnum
| '_')], str
)));
51 BOOST_TEST((str
== "spirit_test_123"));
54 BOOST_TEST((test_attr("x123", alpha
>> raw
[+alnum
], str
)))
55 && BOOST_TEST_EQ(str
, "x123");
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
)));
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');
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"));
79 using range
= boost::iterator_range
<std::string::iterator
>;
80 boost::variant
<int, range
> attr
;
82 std::string
str("test");
83 parse(str
.begin(), str
.end(), (int_
| raw
[*char_
]), attr
);
85 auto rng
= boost::get
<range
>(attr
);
86 BOOST_TEST(std::string(rng
.begin(), rng
.end()) == "test");
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_
])
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");
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_
])
108 BOOST_TEST(attr
.first
== 123);
109 BOOST_TEST(std::string(attr
.second
.begin(), attr
.second
.end()) == "abcd");
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"));
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"));
126 return boost::report_errors();