]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/rule2.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / test / x3 / rule2.cpp
1 /*=============================================================================
2 Copyright (c) 2001-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
8 // this file deliberately contains non-ascii characters
9 // boostinspect:noascii
10
11 #include <boost/detail/lightweight_test.hpp>
12 #include <boost/spirit/home/x3.hpp>
13
14 #include <string>
15 #include <cstring>
16 #include <iostream>
17 #include "test.hpp"
18
19 int
20 main()
21 {
22 using spirit_test::test_attr;
23 using spirit_test::test;
24
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;
31
32 { // context tests
33
34 char ch;
35 auto a = rule<class a, char>() = alpha;
36
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');
41
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');
46
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');
51
52 BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
53 BOOST_TEST(ch == 'z');
54 }
55
56 { // auto rules tests
57
58 char ch = '\0';
59 auto a = rule<class a, char>() = alpha;
60 auto f = [&](auto& ctx){ ch = _attr(ctx); };
61
62 BOOST_TEST(test("x", a[f]));
63 BOOST_TEST(ch == 'x');
64 ch = '\0';
65 BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
66 BOOST_TEST(ch == 'z');
67
68 ch = '\0';
69 BOOST_TEST(test("x", a[f]));
70 BOOST_TEST(ch == 'x');
71 ch = '\0';
72 BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
73 BOOST_TEST(ch == 'z');
74 }
75
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).
81
82 std::string s;
83 auto f = [&](auto& ctx){ s = _attr(ctx); };
84
85 {
86 auto r = rule<class r, std::string>()
87 = char_ >> *(',' >> char_)
88 ;
89
90 BOOST_TEST(test("a,b,c,d,e,f", r[f]));
91 BOOST_TEST(s == "abcdef");
92 }
93
94 {
95 auto r = rule<class r, std::string>()
96 = char_ >> *(',' >> char_);
97 s.clear();
98 BOOST_TEST(test("a,b,c,d,e,f", r[f]));
99 BOOST_TEST(s == "abcdef");
100 }
101
102 {
103 auto r = rule<class r, std::string>()
104 = char_ >> char_ >> char_ >> char_ >> char_ >> char_;
105 s.clear();
106 BOOST_TEST(test("abcdef", r[f]));
107 BOOST_TEST(s == "abcdef");
108 }
109 }
110
111 return boost::report_errors();
112 }