summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/test/x3/rule3.cpp
blob: 420146c0458ec1ee0a6acae781fd0f9bd9dc804a (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*=============================================================================
    Copyright (c) 2001-2012 Joel de Guzman

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/

#include <boost/detail/lightweight_test.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/std_pair.hpp>

#include <string>
#include <cstring>
#include <iostream>
#include "test.hpp"

using boost::spirit::x3::_val;

struct f
{
    template <typename Context>
    void operator()(Context const& ctx) const
    {
        _val(ctx) += _attr(ctx);
    }
};


struct stationary : boost::noncopyable
{
    explicit stationary(int i) : val{i} {}
    stationary& operator=(int i) { val = i; return *this; }

    int val;
};


namespace check_stationary {

boost::spirit::x3::rule<class a_r, stationary> const a;
boost::spirit::x3::rule<class b_r, stationary> const b;

auto const a_def = '{' >> boost::spirit::x3::int_ >> '}';
auto const b_def = a;

BOOST_SPIRIT_DEFINE(a, b)

}


int main()
{
    using spirit_test::test_attr;
    using spirit_test::test;

    using namespace boost::spirit::x3::ascii;
    using boost::spirit::x3::rule;
    using boost::spirit::x3::lit;
    using boost::spirit::x3::eps;
    using boost::spirit::x3::unused_type;


    { // synth attribute value-init

        std::string s;
        typedef rule<class r, std::string> rule_type;

        auto rdef = rule_type()
            = alpha                 [f()]
            ;

        BOOST_TEST(test_attr("abcdef", +rdef, s));
        BOOST_TEST(s == "abcdef");
    }

    { // synth attribute value-init

        std::string s;
        typedef rule<class r, std::string> rule_type;

        auto rdef = rule_type() =
            alpha /
               [](auto& ctx)
               {
                  _val(ctx) += _attr(ctx);
               }
            ;

        BOOST_TEST(test_attr("abcdef", +rdef, s));
        BOOST_TEST(s == "abcdef");
    }

    {
        auto r = rule<class r, int>{} = eps[([] (auto& ctx) {
            using boost::spirit::x3::_val;
            static_assert(std::is_same<std::decay_t<decltype(_val(ctx))>, unused_type>::value,
                "Attribute must not be synthesized");
        })];
        BOOST_TEST(test("", r));
    }

    { // ensure no unneded synthesization, copying and moving occured
        stationary st { 0 };
        BOOST_TEST(test_attr("{42}", check_stationary::b, st));
        BOOST_TEST_EQ(st.val, 42);
    }

    return boost::report_errors();
}