]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2012 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 | ||
7c673cae FG |
8 | #include <boost/detail/lightweight_test.hpp> |
9 | #include <boost/spirit/home/x3.hpp> | |
10 | #include <boost/fusion/include/std_pair.hpp> | |
11 | ||
f67539c2 | 12 | #include <boost/variant.hpp> |
7c673cae FG |
13 | #include <string> |
14 | #include <cstring> | |
15 | #include <iostream> | |
16 | #include "test.hpp" | |
17 | ||
18 | using boost::spirit::x3::_val; | |
19 | ||
20 | struct f | |
21 | { | |
22 | template <typename Context> | |
23 | void operator()(Context const& ctx) const | |
24 | { | |
25 | _val(ctx) += _attr(ctx); | |
26 | } | |
27 | }; | |
28 | ||
92f5a8d4 TL |
29 | |
30 | struct stationary : boost::noncopyable | |
31 | { | |
32 | explicit stationary(int i) : val{i} {} | |
33 | stationary& operator=(int i) { val = i; return *this; } | |
34 | ||
35 | int val; | |
36 | }; | |
37 | ||
38 | ||
39 | namespace check_stationary { | |
40 | ||
41 | boost::spirit::x3::rule<class a_r, stationary> const a; | |
42 | boost::spirit::x3::rule<class b_r, stationary> const b; | |
43 | ||
44 | auto const a_def = '{' >> boost::spirit::x3::int_ >> '}'; | |
45 | auto const b_def = a; | |
46 | ||
47 | BOOST_SPIRIT_DEFINE(a, b) | |
48 | ||
49 | } | |
50 | ||
f67539c2 TL |
51 | namespace check_recursive { |
52 | ||
53 | using node_t = boost::make_recursive_variant< | |
54 | int, | |
55 | std::vector<boost::recursive_variant_> | |
56 | >::type; | |
57 | ||
58 | boost::spirit::x3::rule<class grammar_r, node_t> const grammar; | |
59 | ||
60 | auto const grammar_def = '[' >> grammar % ',' >> ']' | boost::spirit::x3::int_; | |
61 | ||
62 | BOOST_SPIRIT_DEFINE(grammar) | |
63 | ||
64 | } | |
65 | ||
92f5a8d4 | 66 | |
7c673cae FG |
67 | int main() |
68 | { | |
69 | using spirit_test::test_attr; | |
70 | using spirit_test::test; | |
71 | ||
72 | using namespace boost::spirit::x3::ascii; | |
73 | using boost::spirit::x3::rule; | |
7c673cae | 74 | using boost::spirit::x3::lit; |
92f5a8d4 TL |
75 | using boost::spirit::x3::eps; |
76 | using boost::spirit::x3::unused_type; | |
7c673cae FG |
77 | |
78 | ||
79 | { // synth attribute value-init | |
80 | ||
81 | std::string s; | |
82 | typedef rule<class r, std::string> rule_type; | |
83 | ||
84 | auto rdef = rule_type() | |
85 | = alpha [f()] | |
86 | ; | |
87 | ||
88 | BOOST_TEST(test_attr("abcdef", +rdef, s)); | |
89 | BOOST_TEST(s == "abcdef"); | |
90 | } | |
91 | ||
92 | { // synth attribute value-init | |
93 | ||
94 | std::string s; | |
95 | typedef rule<class r, std::string> rule_type; | |
96 | ||
97 | auto rdef = rule_type() = | |
98 | alpha / | |
99 | [](auto& ctx) | |
100 | { | |
101 | _val(ctx) += _attr(ctx); | |
102 | } | |
103 | ; | |
104 | ||
105 | BOOST_TEST(test_attr("abcdef", +rdef, s)); | |
106 | BOOST_TEST(s == "abcdef"); | |
107 | } | |
108 | ||
92f5a8d4 TL |
109 | { |
110 | auto r = rule<class r, int>{} = eps[([] (auto& ctx) { | |
111 | using boost::spirit::x3::_val; | |
112 | static_assert(std::is_same<std::decay_t<decltype(_val(ctx))>, unused_type>::value, | |
113 | "Attribute must not be synthesized"); | |
114 | })]; | |
115 | BOOST_TEST(test("", r)); | |
116 | } | |
117 | ||
f67539c2 | 118 | { // ensure no unneeded synthesization, copying and moving occurred |
92f5a8d4 TL |
119 | stationary st { 0 }; |
120 | BOOST_TEST(test_attr("{42}", check_stationary::b, st)); | |
121 | BOOST_TEST_EQ(st.val, 42); | |
122 | } | |
123 | ||
f67539c2 TL |
124 | { |
125 | using namespace check_recursive; | |
126 | node_t v; | |
127 | BOOST_TEST(test_attr("[4,2]", grammar, v)); | |
128 | BOOST_TEST((node_t{std::vector<node_t>{{4}, {2}}} == v)); | |
129 | } | |
130 | ||
7c673cae FG |
131 | return boost::report_errors(); |
132 | } |