[29] | 1 | /*============================================================================= |
---|
| 2 | Copyright (c) 2001-2003 Joel de Guzman |
---|
| 3 | Copyright (c) 2003 Martin Wille |
---|
| 4 | http://spirit.sourceforge.net/ |
---|
| 5 | |
---|
| 6 | Use, modification and distribution is subject to the Boost Software |
---|
| 7 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
| 8 | http://www.boost.org/LICENSE_1_0.txt) |
---|
| 9 | =============================================================================*/ |
---|
| 10 | #include <iostream> |
---|
| 11 | #include <boost/detail/lightweight_test.hpp> |
---|
| 12 | #include <string> |
---|
| 13 | |
---|
| 14 | using namespace std; |
---|
| 15 | |
---|
| 16 | #include <boost/spirit/core.hpp> |
---|
| 17 | #include <boost/spirit/attribute/parametric.hpp> |
---|
| 18 | #include <boost/spirit/phoenix/primitives.hpp> |
---|
| 19 | #include <boost/spirit/phoenix/operators.hpp> |
---|
| 20 | using namespace boost::spirit; |
---|
| 21 | using namespace phoenix; |
---|
| 22 | |
---|
| 23 | #include <boost/detail/lightweight_test.hpp> |
---|
| 24 | |
---|
| 25 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 26 | // |
---|
| 27 | // Parametric tests |
---|
| 28 | // |
---|
| 29 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 30 | |
---|
| 31 | template <typename T> |
---|
| 32 | static unsigned |
---|
| 33 | length(T const *p) |
---|
| 34 | { |
---|
| 35 | unsigned result = 0; |
---|
| 36 | while (*p++) |
---|
| 37 | ++result; |
---|
| 38 | return result; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | template <typename T> |
---|
| 42 | bool |
---|
| 43 | is_equal(T const* a, T const* b) |
---|
| 44 | { |
---|
| 45 | while (*a && *b) |
---|
| 46 | if (*a++ != *b++) |
---|
| 47 | return false; |
---|
| 48 | return true; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | typedef rule< scanner<wchar_t const *> > wrule_t; |
---|
| 52 | |
---|
| 53 | void |
---|
| 54 | narrow_f_ch_p() |
---|
| 55 | { |
---|
| 56 | char ch; |
---|
| 57 | rule<> r = anychar_p[var(ch) = arg1] >> *f_ch_p(const_(ch)); |
---|
| 58 | parse_info<char const*> pi; |
---|
| 59 | |
---|
| 60 | pi = parse("aaaaaaaaa", r); |
---|
| 61 | BOOST_TEST(pi.hit); |
---|
| 62 | BOOST_TEST(pi.full); |
---|
| 63 | |
---|
| 64 | pi = parse("aaaaabaaa", r); |
---|
| 65 | BOOST_TEST(pi.hit); |
---|
| 66 | BOOST_TEST(!pi.full); |
---|
| 67 | BOOST_TEST(is_equal(pi.stop, "baaa")); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | void |
---|
| 71 | wide_f_ch_p() |
---|
| 72 | { |
---|
| 73 | wchar_t ch; |
---|
| 74 | wrule_t r = anychar_p[var(ch) = arg1] >> *f_ch_p(const_(ch)); |
---|
| 75 | parse_info<wchar_t const*> pi; |
---|
| 76 | |
---|
| 77 | pi = parse(L"aaaaaaaaa", r); |
---|
| 78 | BOOST_TEST(pi.hit); |
---|
| 79 | BOOST_TEST(pi.full); |
---|
| 80 | |
---|
| 81 | pi = parse(L"aaaaabaaa", r); |
---|
| 82 | BOOST_TEST(pi.hit); |
---|
| 83 | BOOST_TEST(!pi.full); |
---|
| 84 | BOOST_TEST(is_equal(pi.stop, L"baaa")); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | void |
---|
| 88 | narrow_f_range_p() |
---|
| 89 | { |
---|
| 90 | char from = 'a'; |
---|
| 91 | char to = 'z'; |
---|
| 92 | |
---|
| 93 | parse_info<char const*> pi; |
---|
| 94 | |
---|
| 95 | rule<> r2 = *f_range_p(const_(from), const_(to)); |
---|
| 96 | pi = parse("abcdefghijklmnopqrstuvwxyz", r2); |
---|
| 97 | BOOST_TEST(pi.hit); |
---|
| 98 | BOOST_TEST(pi.full); |
---|
| 99 | |
---|
| 100 | pi = parse("abcdefghijklmnopqrstuvwxyz123", r2); |
---|
| 101 | BOOST_TEST(pi.hit); |
---|
| 102 | BOOST_TEST(!pi.full); |
---|
| 103 | BOOST_TEST(is_equal(pi.stop, "123")); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void |
---|
| 107 | wide_f_range_p() |
---|
| 108 | { |
---|
| 109 | wchar_t from = L'a'; |
---|
| 110 | wchar_t to = L'z'; |
---|
| 111 | |
---|
| 112 | parse_info<wchar_t const*> pi; |
---|
| 113 | |
---|
| 114 | wrule_t r2 = *f_range_p(const_(from), const_(to)); |
---|
| 115 | pi = parse(L"abcdefghijklmnopqrstuvwxyz", r2); |
---|
| 116 | BOOST_TEST(pi.hit); |
---|
| 117 | BOOST_TEST(pi.full); |
---|
| 118 | |
---|
| 119 | pi = parse(L"abcdefghijklmnopqrstuvwxyz123", r2); |
---|
| 120 | BOOST_TEST(pi.hit); |
---|
| 121 | BOOST_TEST(!pi.full); |
---|
| 122 | BOOST_TEST(is_equal(pi.stop, L"123")); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | void |
---|
| 126 | narrow_f_str_p() |
---|
| 127 | { |
---|
| 128 | parse_info<char const*> pi; |
---|
| 129 | |
---|
| 130 | char const* start = "kim"; |
---|
| 131 | char const* end = start + length(start); |
---|
| 132 | rule<> r3 = +f_str_p(const_(start), const_(end)); |
---|
| 133 | |
---|
| 134 | pi = parse("kimkimkimkimkimkimkimkimkim", r3); |
---|
| 135 | BOOST_TEST(pi.hit); |
---|
| 136 | BOOST_TEST(pi.full); |
---|
| 137 | |
---|
| 138 | pi = parse("kimkimkimkimkimkimkimkimkimmama", r3); |
---|
| 139 | BOOST_TEST(pi.hit); |
---|
| 140 | BOOST_TEST(!pi.full); |
---|
| 141 | BOOST_TEST(is_equal(pi.stop, "mama")); |
---|
| 142 | |
---|
| 143 | pi = parse("joel", r3); |
---|
| 144 | BOOST_TEST(!pi.hit); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | void |
---|
| 148 | wide_f_str_p() |
---|
| 149 | { |
---|
| 150 | parse_info<wchar_t const*> pi; |
---|
| 151 | |
---|
| 152 | wchar_t const* start = L"kim"; |
---|
| 153 | wchar_t const* end = start + length(start); |
---|
| 154 | wrule_t r3 = +f_str_p(const_(start), const_(end)); |
---|
| 155 | |
---|
| 156 | pi = parse(L"kimkimkimkimkimkimkimkimkim", r3); |
---|
| 157 | BOOST_TEST(pi.hit); |
---|
| 158 | BOOST_TEST(pi.full); |
---|
| 159 | |
---|
| 160 | pi = parse(L"kimkimkimkimkimkimkimkimkimmama", r3); |
---|
| 161 | BOOST_TEST(pi.hit); |
---|
| 162 | BOOST_TEST(!pi.full); |
---|
| 163 | BOOST_TEST(is_equal(pi.stop, L"mama")); |
---|
| 164 | |
---|
| 165 | pi = parse(L"joel", r3); |
---|
| 166 | BOOST_TEST(!pi.hit); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 170 | // |
---|
| 171 | // test suite |
---|
| 172 | // |
---|
| 173 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 174 | int |
---|
| 175 | main() |
---|
| 176 | { |
---|
| 177 | narrow_f_ch_p(); |
---|
| 178 | wide_f_ch_p(); |
---|
| 179 | narrow_f_range_p(); |
---|
| 180 | wide_f_range_p(); |
---|
| 181 | narrow_f_str_p(); |
---|
| 182 | wide_f_str_p(); |
---|
| 183 | |
---|
| 184 | return boost::report_errors(); |
---|
| 185 | } |
---|
| 186 | |
---|