]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/actions.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / test / x3 / actions.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2015 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/detail/lightweight_test.hpp>
8 #include <boost/spirit/home/x3.hpp>
9 #include <cstring>
10 #include <functional>
11
12 namespace x3 = boost::spirit::x3;
13
14 int x = 0;
15
16 auto fun1 =
17 [](auto& ctx)
18 {
19 x += x3::_attr(ctx);
20 }
21 ;
22
23 struct fun_action
24 {
25 template <typename Context>
26 void operator()(Context const& ctx) const
27 {
28 x += x3::_attr(ctx);
29 }
30 };
31
32 auto fail =
33 [](auto& ctx)
34 {
35 x3::_pass(ctx) = false;
36 }
37 ;
38
39 struct setnext
40 {
41 setnext(char& next) : next(next) {}
42
43 template <typename Context>
44 void operator()(Context const& ctx) const
45 {
46 next = x3::_attr(ctx);
47 }
48
49 char& next;
50 };
51
52 int main()
53 {
54 using x3::int_;
55
56 {
57 char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
58 x3::parse(s1, e1, '{' >> int_[fun1] >> '}');
59 }
60
61
62 {
63 char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
64 x3::parse(s1, e1, '{' >> int_[fun_action()] >> '}');
65 }
66
67 {
68 using namespace std::placeholders;
69 char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
70 x3::parse(s1, e1, '{' >> int_[std::bind(fun_action(), _1)] >> '}');
71 }
72
73 BOOST_TEST(x == (42*3));
74
75 {
76 std::string input("1234 6543");
77 char next = '\0';
78 BOOST_TEST(x3::phrase_parse(input.begin(), input.end(),
79 x3::int_[fail] | x3::digit[setnext(next)], x3::space));
80 BOOST_TEST(next == '1');
81 }
82
83 return boost::report_errors();
84 }