]>
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 #include <boost/spirit/include/qi_operator.hpp>
9 #include <boost/spirit/include/qi_char.hpp>
10 #include <boost/spirit/include/qi_string.hpp>
11 #include <boost/spirit/include/qi_numeric.hpp>
12 #include <boost/spirit/include/qi_auxiliary.hpp>
13 #include <boost/spirit/include/qi_directive.hpp>
14 #include <boost/spirit/include/qi_nonterminal.hpp>
15 #include <boost/spirit/include/qi_action.hpp>
16 #include <boost/phoenix/core.hpp>
17 #include <boost/phoenix/operator.hpp>
18 #include <boost/fusion/include/std_pair.hpp>
28 using spirit_test::test_attr
;
29 using spirit_test::test
;
31 using namespace boost::spirit::ascii
;
32 using namespace boost::spirit::qi::labels
;
33 using boost::spirit::qi::locals
;
34 using boost::spirit::qi::rule
;
35 using boost::spirit::qi::int_
;
36 using boost::spirit::qi::uint_
;
37 using boost::spirit::qi::fail
;
38 using boost::spirit::qi::on_error
;
39 using boost::spirit::qi::debug
;
40 using boost::spirit::qi::lit
;
42 namespace phx
= boost::phoenix
;
44 { // synth attribute value-init
47 rule
<char const*, char()> r
;
48 r
= alpha
[_val
+= _1
];
49 BOOST_TEST(test_attr("abcdef", +r
, s
));
50 BOOST_TEST(s
== "abcdef");
53 { // auto rules aliasing tests
56 rule
<char const*, char()> a
, b
;
60 BOOST_TEST(test("x", a
[phx::ref(ch
) = _1
]));
61 BOOST_TEST(ch
== 'x');
63 BOOST_TEST(test_attr("z", a
, ch
)); // attribute is given.
64 BOOST_TEST(ch
== 'z');
66 a
= b
; // test deduced auto rule behavior
70 BOOST_TEST(test("x", a
[phx::ref(ch
) = _1
]));
71 BOOST_TEST(ch
== 'x');
73 BOOST_TEST(test_attr("z", a
, ch
)); // attribute is given.
74 BOOST_TEST(ch
== 'z');
77 { // context (w/arg) tests
80 rule
<char const*, char(int)> a
; // 1 arg
81 a
= alpha
[_val
= _1
+ _r1
];
83 BOOST_TEST(test("x", a(phx::val(1))[phx::ref(ch
) = _1
]));
84 BOOST_TEST(ch
== 'x' + 1);
86 BOOST_TEST(test_attr("a", a(1), ch
)); // allow scalars as rule args too.
87 BOOST_TEST(ch
== 'a' + 1);
89 rule
<char const*, char(int, int)> b
; // 2 args
90 b
= alpha
[_val
= _1
+ _r1
+ _r2
];
91 BOOST_TEST(test_attr("a", b(1, 2), ch
));
92 BOOST_TEST(ch
== 'a' + 1 + 2);
95 { // context (w/ reference arg) tests
98 rule
<char const*, void(char&)> a
; // 1 arg (reference)
101 BOOST_TEST(test("x", a(phx::ref(ch
))));
102 BOOST_TEST(ch
== 'x');
105 { // context (w/locals) tests
107 rule
<char const*, locals
<char> > a
; // 1 local
108 a
= alpha
[_a
= _1
] >> char_(_a
);
109 BOOST_TEST(test("aa", a
));
110 BOOST_TEST(!test("ax", a
));
113 { // context (w/args and locals) tests
115 rule
<char const*, void(int), locals
<char> > a
; // 1 arg + 1 local
116 a
= alpha
[_a
= _1
+ _r1
] >> char_(_a
);
117 BOOST_TEST(test("ab", a(phx::val(1))));
118 BOOST_TEST(test("xy", a(phx::val(1))));
119 BOOST_TEST(!test("ax", a(phx::val(1))));
122 { // void() has unused type (void == unused_type)
124 std::pair
<int, char> attr
;
125 rule
<char const*, void()> r
;
127 BOOST_TEST(test_attr("123ax", int_
>> char_
>> r
, attr
));
128 BOOST_TEST(attr
.first
== 123);
129 BOOST_TEST(attr
.second
== 'a');
132 { // bug: test that injected attributes are ok
134 rule
<char const*, char(int) > r
;
137 r
= char_(_r1
)[_val
= _1
];
140 return boost::report_errors();