]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/any_parser.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / test / x3 / any_parser.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2015 Joel de Guzman
3 Copyright (c) 2013-2014 Agustin Berge
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
8
9 // this file deliberately contains non-ascii characters
10 // boostinspect:noascii
11
12 #include <boost/detail/lightweight_test.hpp>
13 #include <boost/spirit/home/x3.hpp>
14
15 #include <string>
16 #include <cstring>
17 #include <iostream>
18 #include "test.hpp"
19
20 int
21 main()
22 {
23 using spirit_test::test_attr;
24 using spirit_test::test;
25
26 using namespace boost::spirit::x3::ascii;
27 using boost::spirit::x3::any_parser;
28 using boost::spirit::x3::int_;
29 using boost::spirit::x3::make_context;
30 using boost::spirit::x3::lit;
31 using boost::spirit::x3::unused_type;
32 using boost::spirit::x3::phrase_parse;
33 using boost::spirit::x3::skip_flag;
34 using boost::spirit::x3::skipper_tag;
35 using boost::spirit::x3::_attr;
36
37 typedef char const* iterator_type;
38 typedef decltype(make_context<skipper_tag>(space)) context_type;
39 { // basic tests
40
41 auto a = lit('a');
42 auto b = lit('b');
43 auto c = lit('c');
44
45 {
46 any_parser<iterator_type> start =
47 *(a | b | c);
48
49 BOOST_TEST(test("abcabcacb", start));
50 }
51 }
52
53 { // basic tests w/ skipper
54
55 auto a = lit('a');
56 auto b = lit('b');
57 auto c = lit('c');
58
59 {
60 any_parser<iterator_type, unused_type, context_type> start =
61 *(a | b | c);
62
63 BOOST_TEST(test(" a b c a b c a c b ", start, space));
64 }
65 }
66
67 { // basic tests w/ skipper but no final post-skip
68
69 any_parser<iterator_type, unused_type, context_type> a = lit('a');
70 any_parser<iterator_type, unused_type, context_type> b = lit('b');
71 any_parser<iterator_type, unused_type, context_type> c = lit('c');
72
73 {
74 any_parser<iterator_type, unused_type, context_type> start = *(a | b) >> c;
75
76 char const *s1 = " a b a a b b a c ... "
77 , *const e1 = s1 + std::strlen(s1);
78 BOOST_TEST(phrase_parse(s1, e1, start, space, skip_flag::dont_post_skip)
79 && s1 == e1 - 5);
80 }
81 }
82
83 { // context tests
84
85 char ch;
86 any_parser<iterator_type, char> a = alpha;
87
88 // this semantic action requires both the context and attribute
89 //!!auto f = [&](auto&, char attr){ ch = attr; };
90 //!!BOOST_TEST(test("x", a[f]));
91 //!!BOOST_TEST(ch == 'x');
92
93 // the semantic action may have the context passed
94 auto f2 = [&](auto&){ ch = 'y'; };
95 BOOST_TEST(test("x", a[f2]));
96 BOOST_TEST(ch == 'y');
97
98 // the semantic action may optionally not have any arguments at all
99 auto f3 = [&]{ ch = 'z'; };
100 BOOST_TEST(test("x", a[f3]));
101 BOOST_TEST(ch == 'z');
102
103 BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
104 BOOST_TEST(ch == 'z');
105 }
106
107 { // auto rules tests
108
109 char ch = '\0';
110 any_parser<iterator_type, char> a = alpha;
111 auto f = [&](auto& ctx){ ch = _attr(ctx); };
112
113 BOOST_TEST(test("x", a[f]));
114 BOOST_TEST(ch == 'x');
115 ch = '\0';
116 BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
117 BOOST_TEST(ch == 'z');
118
119 ch = '\0';
120 BOOST_TEST(test("x", a[f]));
121 BOOST_TEST(ch == 'x');
122 ch = '\0';
123 BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
124 BOOST_TEST(ch == 'z');
125 }
126
127 { // auto rules tests: allow stl containers as attributes to
128 // sequences (in cases where attributes of the elements
129 // are convertible to the value_type of the container or if
130 // the element itself is an stl container with value_type
131 // that is convertible to the value_type of the attribute).
132
133 std::string s;
134 auto f = [&](auto& ctx){ s = _attr(ctx); };
135
136 {
137 any_parser<iterator_type, std::string> r
138 = char_ >> *(',' >> char_)
139 ;
140
141 BOOST_TEST(test("a,b,c,d,e,f", r[f]));
142 BOOST_TEST(s == "abcdef");
143 }
144
145 {
146 any_parser<iterator_type, std::string> r
147 = char_ >> *(',' >> char_);
148 s.clear();
149 BOOST_TEST(test("a,b,c,d,e,f", r[f]));
150 BOOST_TEST(s == "abcdef");
151 }
152
153 {
154 any_parser<iterator_type, std::string> r
155 = char_ >> char_ >> char_ >> char_ >> char_ >> char_;
156 s.clear();
157 BOOST_TEST(test("abcdef", r[f]));
158 BOOST_TEST(s == "abcdef");
159 }
160 }
161
162 return boost::report_errors();
163 }