1/*=============================================================================
2 Copyright (c) 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 "test.hpp"
9
10namespace x3 = boost::spirit::x3;
11
12struct my_tag;
13
14struct my_rule_class
15{
16 template <typename Iterator, typename Exception, typename Context>
17 x3::error_handler_result
18 on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
19 {
20 x3::get<my_tag>(context)++;
21 return x3::error_handler_result::fail;
22 }
23
24 template <typename Iterator, typename Attribute, typename Context>
25 inline void
26 on_success(Iterator const&, Iterator const&, Attribute&, Context const& context)
27 {
28 x3::get<my_tag>(context)++;
29 }
30};
31
32int
33main()
34{
35 using spirit_test::test_attr;
36 using spirit_test::test;
37
38 using boost::spirit::x3::rule;
39 using boost::spirit::x3::int_;
40 using boost::spirit::x3::with;
41
42// read from a mutable field is not allowed on these compilers
43#if (!defined(_MSC_VER) || _MSC_VER >= 1910) && \
44 (!defined(__clang__) || __clang_major__ >= 7)
45 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(with<my_tag>(0)['x']);
46#endif
47 {
48 constexpr int i = 0;
49 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(with<my_tag>(i)['x']);
50 }
51
52 { // injecting data into the context in the grammar
53
54 int val = 0;
55 auto r = rule<my_rule_class, char const*>() =
56 '(' > int_ > ',' > int_ > ')'
57 ;
58
59 auto start =
60 with<my_tag>(val: std::ref(t&: val)) [ r ]
61 ;
62
63 BOOST_TEST(test("(123,456)", start));
64 BOOST_TEST(!test("(abc,def)", start));
65 BOOST_TEST(val == 2);
66 }
67
68 { // injecting non-const lvalue into the context
69 int val = 0;
70 auto const r = int_[([](auto& ctx){
71 x3::get<my_tag>(ctx) += x3::_attr(ctx);
72 })];
73 BOOST_TEST(test("123,456", with<my_tag>(val)[r % ',']));
74 BOOST_TEST(579 == val);
75 }
76
77 { // injecting rvalue into the context
78 auto const r1 = int_[([](auto& ctx){
79 x3::get<my_tag>(ctx) += x3::_attr(ctx);
80 })];
81 auto const r2 = rule<struct my_rvalue_rule_class, int>() =
82 x3::lit(ch: '(') >> (r1 % ',') >> x3::lit(ch: ')')[([](auto& ctx){
83 x3::_val(ctx) = x3::get<my_tag>(ctx);
84 })];
85 int attr = 0;
86 BOOST_TEST(test_attr("(1,2,3)", with<my_tag>(100)[r2], attr));
87 BOOST_TEST(106 == attr);
88 }
89
90 { // injecting const/non-const lvalue and rvalue into the context
91 struct functor {
92 int operator()(int& val) {
93 return val * 10; // non-const ref returns 10 * injected val
94 }
95 int operator()(int const& val) {
96 return val; // const ref returns injected val
97 }
98 };
99
100 auto f = [](auto& ctx){
101 x3::_val(ctx) = x3::_attr(ctx) + functor()(x3::get<my_tag>(ctx));
102 };
103 auto const r = rule<struct my_rule_class2, int>() = int_[f];
104
105 int attr = 0;
106 int const cval = 10;
107 BOOST_TEST(test_attr("5", with<my_tag>(cval)[r], attr));
108 BOOST_TEST(15 == attr); // x3::get returns const ref to cval
109
110 attr = 0;
111 int val = 10;
112 BOOST_TEST(test_attr("5", with<my_tag>(val)[r], attr));
113 BOOST_TEST(105 == attr); // x3::get returns ref to val
114
115 attr = 0;
116
117 BOOST_TEST(test_attr("5", with<my_tag>(10)[r], attr));
118 // x3::get returns ref to member variable of with_directive
119 BOOST_TEST(105 == attr);
120 }
121
122 return boost::report_errors();
123}
124

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