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/spirit/home/x3.hpp>
8#include <cstring>
9#include <functional>
10
11#include "test.hpp"
12
13#ifdef _MSC_VER
14// bogus https://developercommunity.visualstudio.com/t/buggy-warning-c4709/471956
15# pragma warning(disable: 4709) // comma operator within array index expression
16#endif
17
18namespace x3 = boost::spirit::x3;
19
20int x = 0;
21
22auto fun1 =
23 [](auto& ctx)
24 {
25 x += x3::_attr(ctx);
26 }
27;
28
29struct fun_action
30{
31 template <typename Context>
32 void operator()(Context const& ctx) const
33 {
34 x += x3::_attr(ctx);
35 }
36};
37
38auto fail =
39 [](auto& ctx)
40 {
41 x3::_pass(ctx) = false;
42 }
43;
44
45struct setnext
46{
47 setnext(char& next) : next(next) {}
48
49 template <typename Context>
50 void operator()(Context const& ctx) const
51 {
52 next = x3::_attr(ctx);
53 }
54
55 char& next;
56};
57
58
59struct stationary : boost::noncopyable
60{
61 explicit stationary(int i) : val{i} {}
62 stationary& operator=(int i) { val = i; return *this; }
63
64 int val;
65};
66
67
68int main()
69{
70 using spirit_test::test;
71 using spirit_test::test_attr;
72
73 using x3::int_;
74
75 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(x3::int_type{}[std::true_type{}]);
76
77 {
78 char const *s1 = "{42}", *e1 = s1 + std::strlen(s: s1);
79 x3::parse(first&: s1, last: e1, p: '{' >> int_[fun1] >> '}');
80 }
81
82
83 {
84 char const *s1 = "{42}", *e1 = s1 + std::strlen(s: s1);
85 x3::parse(first&: s1, last: e1, p: '{' >> int_[fun_action()] >> '}');
86 }
87
88 {
89 using namespace std::placeholders;
90 char const *s1 = "{42}", *e1 = s1 + std::strlen(s: s1);
91 x3::parse(first&: s1, last: e1, p: '{' >> int_[std::bind(f: fun_action(), args: _1)] >> '}');
92 }
93
94 BOOST_TEST(x == (42*3));
95
96 {
97 std::string input("1234 6543");
98 char next = '\0';
99 BOOST_TEST(x3::phrase_parse(input.begin(), input.end(),
100 x3::int_[fail] | x3::digit[setnext(next)], x3::space));
101 BOOST_TEST(next == '1');
102 }
103
104 { // ensure no unneeded synthesization, copying and moving occurred
105 auto p = '{' >> int_ >> '}';
106
107 stationary st { 0 };
108 BOOST_TEST(test_attr("{42}", p[([]{})], st));
109 BOOST_TEST_EQ(st.val, 42);
110 }
111
112 return boost::report_errors();
113}
114

source code of boost/libs/spirit/test/x3/actions.cpp