]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/qi/rule3.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 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/include/qi_operator.hpp>
13 #include <boost/spirit/include/qi_char.hpp>
14 #include <boost/spirit/include/qi_string.hpp>
15 #include <boost/spirit/include/qi_numeric.hpp>
16 #include <boost/spirit/include/qi_auxiliary.hpp>
17 #include <boost/spirit/include/qi_directive.hpp>
18 #include <boost/spirit/include/qi_nonterminal.hpp>
19 #include <boost/spirit/include/qi_action.hpp>
20 #include <boost/spirit/include/phoenix_core.hpp>
21 #include <boost/spirit/include/phoenix_operator.hpp>
22 #include <boost/spirit/include/phoenix_object.hpp>
23 #include <boost/spirit/include/phoenix_bind.hpp>
24 #include <boost/fusion/include/std_pair.hpp>
34 using spirit_test::test_attr
;
35 using spirit_test::test
;
37 using namespace boost::spirit::ascii
;
38 using namespace boost::spirit::qi::labels
;
39 using boost::spirit::qi::locals
;
40 using boost::spirit::qi::rule
;
41 using boost::spirit::qi::int_
;
42 using boost::spirit::qi::uint_
;
43 using boost::spirit::qi::fail
;
44 using boost::spirit::qi::on_error
;
45 using boost::spirit::qi::debug
;
46 using boost::spirit::qi::lit
;
48 namespace phx
= boost::phoenix
;
50 { // synth attribute value-init
53 rule
<char const*, char()> r
;
54 r
= alpha
[_val
+= _1
];
55 BOOST_TEST(test_attr("abcdef", +r
, s
));
56 BOOST_TEST(s
== "abcdef");
59 { // auto rules aliasing tests
62 rule
<char const*, char()> a
, b
;
66 BOOST_TEST(test("x", a
[phx::ref(ch
) = _1
]));
67 BOOST_TEST(ch
== 'x');
69 BOOST_TEST(test_attr("z", a
, ch
)); // attribute is given.
70 BOOST_TEST(ch
== 'z');
72 a
= b
; // test deduced auto rule behavior
76 BOOST_TEST(test("x", a
[phx::ref(ch
) = _1
]));
77 BOOST_TEST(ch
== 'x');
79 BOOST_TEST(test_attr("z", a
, ch
)); // attribute is given.
80 BOOST_TEST(ch
== 'z');
83 { // context (w/arg) tests
86 rule
<char const*, char(int)> a
; // 1 arg
87 a
= alpha
[_val
= _1
+ _r1
];
89 BOOST_TEST(test("x", a(phx::val(1))[phx::ref(ch
) = _1
]));
90 BOOST_TEST(ch
== 'x' + 1);
92 BOOST_TEST(test_attr("a", a(1), ch
)); // allow scalars as rule args too.
93 BOOST_TEST(ch
== 'a' + 1);
95 rule
<char const*, char(int, int)> b
; // 2 args
96 b
= alpha
[_val
= _1
+ _r1
+ _r2
];
97 BOOST_TEST(test_attr("a", b(1, 2), ch
));
98 BOOST_TEST(ch
== 'a' + 1 + 2);
101 { // context (w/ reference arg) tests
104 rule
<char const*, void(char&)> a
; // 1 arg (reference)
107 BOOST_TEST(test("x", a(phx::ref(ch
))));
108 BOOST_TEST(ch
== 'x');
111 { // context (w/locals) tests
113 rule
<char const*, locals
<char> > a
; // 1 local
114 a
= alpha
[_a
= _1
] >> char_(_a
);
115 BOOST_TEST(test("aa", a
));
116 BOOST_TEST(!test("ax", a
));
119 { // context (w/args and locals) tests
121 rule
<char const*, void(int), locals
<char> > a
; // 1 arg + 1 local
122 a
= alpha
[_a
= _1
+ _r1
] >> char_(_a
);
123 BOOST_TEST(test("ab", a(phx::val(1))));
124 BOOST_TEST(test("xy", a(phx::val(1))));
125 BOOST_TEST(!test("ax", a(phx::val(1))));
128 { // void() has unused type (void == unused_type)
130 std::pair
<int, char> attr
;
131 rule
<char const*, void()> r
;
133 BOOST_TEST(test_attr("123ax", int_
>> char_
>> r
, attr
));
134 BOOST_TEST(attr
.first
== 123);
135 BOOST_TEST(attr
.second
== 'a');
138 { // bug: test that injected attributes are ok
140 rule
<char const*, char(int) > r
;
143 r
= char_(_r1
)[_val
= _1
];
146 return boost::report_errors();