]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/qi/rule3.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / test / qi / rule3.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 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/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>
25
26 #include <string>
27 #include <cstring>
28 #include <iostream>
29 #include "test.hpp"
30
31 int
32 main()
33 {
34 using spirit_test::test_attr;
35 using spirit_test::test;
36
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;
47
48 namespace phx = boost::phoenix;
49
50 { // synth attribute value-init
51
52 std::string s;
53 rule<char const*, char()> r;
54 r = alpha[_val += _1];
55 BOOST_TEST(test_attr("abcdef", +r, s));
56 BOOST_TEST(s == "abcdef");
57 }
58
59 { // auto rules aliasing tests
60
61 char ch = '\0';
62 rule<char const*, char()> a, b;
63 a %= b;
64 b %= alpha;
65
66 BOOST_TEST(test("x", a[phx::ref(ch) = _1]));
67 BOOST_TEST(ch == 'x');
68 ch = '\0';
69 BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
70 BOOST_TEST(ch == 'z');
71
72 a = b; // test deduced auto rule behavior
73 b = alpha;
74
75 ch = '\0';
76 BOOST_TEST(test("x", a[phx::ref(ch) = _1]));
77 BOOST_TEST(ch == 'x');
78 ch = '\0';
79 BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
80 BOOST_TEST(ch == 'z');
81 }
82
83 { // context (w/arg) tests
84
85 char ch;
86 rule<char const*, char(int)> a; // 1 arg
87 a = alpha[_val = _1 + _r1];
88
89 BOOST_TEST(test("x", a(phx::val(1))[phx::ref(ch) = _1]));
90 BOOST_TEST(ch == 'x' + 1);
91
92 BOOST_TEST(test_attr("a", a(1), ch)); // allow scalars as rule args too.
93 BOOST_TEST(ch == 'a' + 1);
94
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);
99 }
100
101 { // context (w/ reference arg) tests
102
103 char ch;
104 rule<char const*, void(char&)> a; // 1 arg (reference)
105 a = alpha[_r1 = _1];
106
107 BOOST_TEST(test("x", a(phx::ref(ch))));
108 BOOST_TEST(ch == 'x');
109 }
110
111 { // context (w/locals) tests
112
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));
117 }
118
119 { // context (w/args and locals) tests
120
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))));
126 }
127
128 { // void() has unused type (void == unused_type)
129
130 std::pair<int, char> attr;
131 rule<char const*, void()> r;
132 r = char_;
133 BOOST_TEST(test_attr("123ax", int_ >> char_ >> r, attr));
134 BOOST_TEST(attr.first == 123);
135 BOOST_TEST(attr.second == 'a');
136 }
137
138 { // bug: test that injected attributes are ok
139
140 rule<char const*, char(int) > r;
141
142 // problem code:
143 r = char_(_r1)[_val = _1];
144 }
145
146 return boost::report_errors();
147 }
148