| 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 | #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> |
| 19 | |
| 20 | #include <string> |
| 21 | #include <cstring> |
| 22 | #include <iostream> |
| 23 | #include "test.hpp" |
| 24 | |
| 25 | int |
| 26 | main() |
| 27 | { |
| 28 | using spirit_test::test_attr; |
| 29 | using spirit_test::test; |
| 30 | |
| 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; |
| 41 | |
| 42 | namespace phx = boost::phoenix; |
| 43 | |
| 44 | { // synth attribute value-init |
| 45 | |
| 46 | std::string s; |
| 47 | rule<char const*, char()> r; |
| 48 | r = alpha[_val += _1]; |
| 49 | BOOST_TEST(test_attr("abcdef" , +r, s)); |
| 50 | BOOST_TEST(s == "abcdef" ); |
| 51 | } |
| 52 | |
| 53 | { // auto rules aliasing tests |
| 54 | |
| 55 | char ch = '\0'; |
| 56 | rule<char const*, char()> a, b; |
| 57 | a %= b; |
| 58 | b %= alpha; |
| 59 | |
| 60 | BOOST_TEST(test("x" , a[phx::ref(ch) = _1])); |
| 61 | BOOST_TEST(ch == 'x'); |
| 62 | ch = '\0'; |
| 63 | BOOST_TEST(test_attr("z" , a, ch)); // attribute is given. |
| 64 | BOOST_TEST(ch == 'z'); |
| 65 | |
| 66 | a = b; // test deduced auto rule behavior |
| 67 | b = alpha; |
| 68 | |
| 69 | ch = '\0'; |
| 70 | BOOST_TEST(test("x" , a[phx::ref(ch) = _1])); |
| 71 | BOOST_TEST(ch == 'x'); |
| 72 | ch = '\0'; |
| 73 | BOOST_TEST(test_attr("z" , a, ch)); // attribute is given. |
| 74 | BOOST_TEST(ch == 'z'); |
| 75 | } |
| 76 | |
| 77 | { // context (w/arg) tests |
| 78 | |
| 79 | char ch; |
| 80 | rule<char const*, char(int)> a; // 1 arg |
| 81 | a = alpha[_val = _1 + _r1]; |
| 82 | |
| 83 | BOOST_TEST(test("x" , a(phx::val(1))[phx::ref(ch) = _1])); |
| 84 | BOOST_TEST(ch == 'x' + 1); |
| 85 | |
| 86 | BOOST_TEST(test_attr("a" , a(1), ch)); // allow scalars as rule args too. |
| 87 | BOOST_TEST(ch == 'a' + 1); |
| 88 | |
| 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); |
| 93 | } |
| 94 | |
| 95 | { // context (w/ reference arg) tests |
| 96 | |
| 97 | char ch; |
| 98 | rule<char const*, void(char&)> a; // 1 arg (reference) |
| 99 | a = alpha[_r1 = _1]; |
| 100 | |
| 101 | BOOST_TEST(test("x" , a(phx::ref(ch)))); |
| 102 | BOOST_TEST(ch == 'x'); |
| 103 | } |
| 104 | |
| 105 | { // context (w/locals) tests |
| 106 | |
| 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)); |
| 111 | } |
| 112 | |
| 113 | { // context (w/args and locals) tests |
| 114 | |
| 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)))); |
| 120 | } |
| 121 | |
| 122 | { // void() has unused type (void == unused_type) |
| 123 | |
| 124 | std::pair<int, char> attr; |
| 125 | rule<char const*, void()> r; |
| 126 | r = char_; |
| 127 | BOOST_TEST(test_attr("123ax" , int_ >> char_ >> r, attr)); |
| 128 | BOOST_TEST(attr.first == 123); |
| 129 | BOOST_TEST(attr.second == 'a'); |
| 130 | } |
| 131 | |
| 132 | { // bug: test that injected attributes are ok |
| 133 | |
| 134 | rule<char const*, char(int) > r; |
| 135 | |
| 136 | // problem code: |
| 137 | r = char_(_r1)[_val = _1]; |
| 138 | } |
| 139 | |
| 140 | return boost::report_errors(); |
| 141 | } |
| 142 | |
| 143 | |