]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/with.cpp
1 /*=============================================================================
2 Copyright (c) 2015 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/detail/lightweight_test.hpp>
8 #include <boost/spirit/home/x3.hpp>
11 namespace x3
= boost::spirit::x3
;
17 template <typename Iterator
, typename Exception
, typename Context
>
18 x3::error_handler_result
19 on_error(Iterator
&, Iterator
const&, Exception
const& x
, Context
const& context
)
21 x3::get
<my_tag
>(context
)++;
22 return x3::error_handler_result::fail
;
25 template <typename Iterator
, typename Attribute
, typename Context
>
27 on_success(Iterator
const&, Iterator
const&, Attribute
&, Context
const& context
)
29 x3::get
<my_tag
>(context
)++;
36 using spirit_test::test_attr
;
37 using spirit_test::test
;
39 using boost::spirit::x3::rule
;
40 using boost::spirit::x3::int_
;
41 using boost::spirit::x3::with
;
43 { // injecting data into the context in the grammar
46 auto r
= rule
<my_rule_class
, char const*>() =
47 '(' > int_
> ',' > int_
> ')'
51 with
<my_tag
>(std::ref(val
)) [ r
]
54 BOOST_TEST(test("(123,456)", start
));
55 BOOST_TEST(!test("(abc,def)", start
));
59 { // injecting non-const lvalue into the context
61 auto const r
= int_
[([](auto& ctx
){
62 x3::get
<my_tag
>(ctx
) += x3::_attr(ctx
);
64 BOOST_TEST(test("123,456", with
<my_tag
>(val
)[r
% ',']));
65 BOOST_TEST(579 == val
);
68 { // injecting rvalue into the context
69 auto const r1
= int_
[([](auto& ctx
){
70 x3::get
<my_tag
>(ctx
) += x3::_attr(ctx
);
72 auto const r2
= rule
<struct my_rvalue_rule_class
, int>() =
73 x3::lit('(') >> (r1
% ',') >> x3::lit(')')[([](auto& ctx
){
74 x3::_val(ctx
) = x3::get
<my_tag
>(ctx
);
77 BOOST_TEST(test_attr("(1,2,3)", with
<my_tag
>(100)[r2
], attr
));
78 BOOST_TEST(106 == attr
);
81 { // injecting const/non-const lvalue and rvalue into the context
83 int operator()(int& val
) {
84 return val
* 10; // non-const ref returns 10 * injected val
86 int operator()(int const& val
) {
87 return val
; // const ref returns injected val
91 auto f
= [](auto& ctx
){
92 x3::_val(ctx
) = x3::_attr(ctx
) + functor()(x3::get
<my_tag
>(ctx
));
94 auto const r
= rule
<struct my_rule_class2
, int>() = int_
[f
];
98 BOOST_TEST(test_attr("5", with
<my_tag
>(cval
)[r
], attr
));
99 BOOST_TEST(15 == attr
); // x3::get returns const ref to cval
103 BOOST_TEST(test_attr("5", with
<my_tag
>(val
)[r
], attr
));
104 BOOST_TEST(105 == attr
); // x3::get returns ref to val
108 BOOST_TEST(test_attr("5", with
<my_tag
>(10)[r
], attr
));
109 // x3::get returns ref to member variable of with_directive
110 BOOST_TEST(105 == attr
);
113 return boost::report_errors();