]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/rule2.cpp
1 /*=============================================================================
2 Copyright (c) 2001-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 =============================================================================*/
8 // this file deliberately contains non-ascii characters
9 // boostinspect:noascii
11 #include <boost/detail/lightweight_test.hpp>
12 #include <boost/spirit/home/x3.hpp>
22 using spirit_test::test_attr
;
23 using spirit_test::test
;
25 using namespace boost::spirit::x3::ascii
;
26 using boost::spirit::x3::rule
;
27 using boost::spirit::x3::int_
;
28 using boost::spirit::x3::lit
;
29 using boost::spirit::x3::unused_type
;
30 using boost::spirit::x3::_attr
;
35 auto a
= rule
<class a
, char>() = alpha
;
37 // this semantic action requires the context
38 auto f
= [&](auto& ctx
){ ch
= _attr(ctx
); };
39 BOOST_TEST(test("x", a
[f
]));
40 BOOST_TEST(ch
== 'x');
42 // this semantic action requires the (unused) context
43 auto f2
= [&](auto&){ ch
= 'y'; };
44 BOOST_TEST(test("x", a
[f2
]));
45 BOOST_TEST(ch
== 'y');
47 // the semantic action may optionally not have any arguments at all
48 auto f3
= [&]{ ch
= 'z'; };
49 BOOST_TEST(test("x", a
[f3
]));
50 BOOST_TEST(ch
== 'z');
52 BOOST_TEST(test_attr("z", a
, ch
)); // attribute is given.
53 BOOST_TEST(ch
== 'z');
59 auto a
= rule
<class a
, char>() = alpha
;
60 auto f
= [&](auto& ctx
){ ch
= _attr(ctx
); };
62 BOOST_TEST(test("x", a
[f
]));
63 BOOST_TEST(ch
== 'x');
65 BOOST_TEST(test_attr("z", a
, ch
)); // attribute is given.
66 BOOST_TEST(ch
== 'z');
69 BOOST_TEST(test("x", a
[f
]));
70 BOOST_TEST(ch
== 'x');
72 BOOST_TEST(test_attr("z", a
, ch
)); // attribute is given.
73 BOOST_TEST(ch
== 'z');
76 { // auto rules tests: allow stl containers as attributes to
77 // sequences (in cases where attributes of the elements
78 // are convertible to the value_type of the container or if
79 // the element itself is an stl container with value_type
80 // that is convertible to the value_type of the attribute).
83 auto f
= [&](auto& ctx
){ s
= _attr(ctx
); };
86 auto r
= rule
<class r
, std::string
>()
87 = char_
>> *(',' >> char_
)
90 BOOST_TEST(test("a,b,c,d,e,f", r
[f
]));
91 BOOST_TEST(s
== "abcdef");
95 auto r
= rule
<class r
, std::string
>()
96 = char_
>> *(',' >> char_
);
98 BOOST_TEST(test("a,b,c,d,e,f", r
[f
]));
99 BOOST_TEST(s
== "abcdef");
103 auto r
= rule
<class r
, std::string
>()
104 = char_
>> char_
>> char_
>> char_
>> char_
>> char_
;
106 BOOST_TEST(test("abcdef", r
[f
]));
107 BOOST_TEST(s
== "abcdef");
111 return boost::report_errors();