]>
git.proxmox.com Git - ceph.git/blob - 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
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 ==============================================================================*/
9 // this file deliberately contains non-ascii characters
10 // boostinspect:noascii
12 #include <boost/detail/lightweight_test.hpp>
13 #include <boost/spirit/home/x3.hpp>
23 using spirit_test::test_attr
;
24 using spirit_test::test
;
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
;
37 typedef char const* iterator_type
;
38 typedef decltype(make_context
<skipper_tag
>(space
)) context_type
;
46 any_parser
<iterator_type
> start
=
49 BOOST_TEST(test("abcabcacb", start
));
53 { // basic tests w/ skipper
60 any_parser
<iterator_type
, unused_type
, context_type
> start
=
63 BOOST_TEST(test(" a b c a b c a c b ", start
, space
));
67 { // basic tests w/ skipper but no final post-skip
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');
74 any_parser
<iterator_type
, unused_type
, context_type
> start
= *(a
| b
) >> c
;
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
)
86 any_parser
<iterator_type
, char> a
= alpha
;
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');
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');
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');
103 BOOST_TEST(test_attr("z", a
, ch
)); // attribute is given.
104 BOOST_TEST(ch
== 'z');
107 { // auto rules tests
110 any_parser
<iterator_type
, char> a
= alpha
;
111 auto f
= [&](auto& ctx
){ ch
= _attr(ctx
); };
113 BOOST_TEST(test("x", a
[f
]));
114 BOOST_TEST(ch
== 'x');
116 BOOST_TEST(test_attr("z", a
, ch
)); // attribute is given.
117 BOOST_TEST(ch
== 'z');
120 BOOST_TEST(test("x", a
[f
]));
121 BOOST_TEST(ch
== 'x');
123 BOOST_TEST(test_attr("z", a
, ch
)); // attribute is given.
124 BOOST_TEST(ch
== 'z');
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).
134 auto f
= [&](auto& ctx
){ s
= _attr(ctx
); };
137 any_parser
<iterator_type
, std::string
> r
138 = char_
>> *(',' >> char_
)
141 BOOST_TEST(test("a,b,c,d,e,f", r
[f
]));
142 BOOST_TEST(s
== "abcdef");
146 any_parser
<iterator_type
, std::string
> r
147 = char_
>> *(',' >> char_
);
149 BOOST_TEST(test("a,b,c,d,e,f", r
[f
]));
150 BOOST_TEST(s
== "abcdef");
154 any_parser
<iterator_type
, std::string
> r
155 = char_
>> char_
>> char_
>> char_
>> char_
>> char_
;
157 BOOST_TEST(test("abcdef", r
[f
]));
158 BOOST_TEST(s
== "abcdef");
162 return boost::report_errors();