| 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 | #if !defined(BOOST_SPIRIT_TEST_FEBRUARY_01_2007_0605PM) |
| 8 | #define BOOST_SPIRIT_TEST_FEBRUARY_01_2007_0605PM |
| 9 | |
| 10 | #include <boost/spirit/include/qi_parse.hpp> |
| 11 | #include <boost/spirit/include/qi_what.hpp> |
| 12 | |
| 13 | #include <boost/core/lightweight_test.hpp> |
| 14 | #include <boost/variant/apply_visitor.hpp> |
| 15 | #include <iostream> |
| 16 | |
| 17 | namespace spirit_test |
| 18 | { |
| 19 | template <typename Char, typename Parser> |
| 20 | bool test(Char const* in, Parser const& p, bool full_match = true) |
| 21 | { |
| 22 | // we don't care about the result of the "what" function. |
| 23 | // we only care that all parsers have it: |
| 24 | boost::spirit::qi::what(p); |
| 25 | |
| 26 | Char const* last = in; |
| 27 | while (*last) |
| 28 | last++; |
| 29 | return boost::spirit::qi::parse(in, last, p) |
| 30 | && (!full_match || (in == last)); |
| 31 | } |
| 32 | |
| 33 | template <typename Char, typename Parser, typename Skipper> |
| 34 | bool test(Char const* in, Parser const& p |
| 35 | , Skipper const& s, bool full_match = true) |
| 36 | { |
| 37 | // we don't care about the result of the "what" function. |
| 38 | // we only care that all parsers have it: |
| 39 | boost::spirit::qi::what(p); |
| 40 | |
| 41 | Char const* last = in; |
| 42 | while (*last) |
| 43 | last++; |
| 44 | return boost::spirit::qi::phrase_parse(in, last, p, s) |
| 45 | && (!full_match || (in == last)); |
| 46 | } |
| 47 | |
| 48 | template <typename Char, typename Parser> |
| 49 | bool binary_test(Char const* in, std::size_t size, Parser const& p, |
| 50 | bool full_match = true) |
| 51 | { |
| 52 | // we don't care about the result of the "what" function. |
| 53 | // we only care that all parsers have it: |
| 54 | boost::spirit::qi::what(p); |
| 55 | |
| 56 | Char const* last = in + size; |
| 57 | return boost::spirit::qi::parse(in, last, p) |
| 58 | && (!full_match || (in == last)); |
| 59 | } |
| 60 | |
| 61 | template <typename Char, typename Parser, typename Skipper> |
| 62 | bool binary_test(Char const* in, std::size_t size, Parser const& p, |
| 63 | Skipper const& s, bool full_match = true) |
| 64 | { |
| 65 | // we don't care about the result of the "what" function. |
| 66 | // we only care that all parsers have it: |
| 67 | boost::spirit::qi::what(p); |
| 68 | |
| 69 | Char const* last = in + size; |
| 70 | return boost::spirit::qi::phrase_parse(in, last, p, s) |
| 71 | && (!full_match || (in == last)); |
| 72 | } |
| 73 | |
| 74 | template <typename Char, typename Parser, typename Attr> |
| 75 | bool test_attr(Char const* in, Parser const& p |
| 76 | , Attr& attr, bool full_match = true) |
| 77 | { |
| 78 | // we don't care about the result of the "what" function. |
| 79 | // we only care that all parsers have it: |
| 80 | boost::spirit::qi::what(p); |
| 81 | |
| 82 | Char const* last = in; |
| 83 | while (*last) |
| 84 | last++; |
| 85 | return boost::spirit::qi::parse(in, last, p, attr) |
| 86 | && (!full_match || (in == last)); |
| 87 | } |
| 88 | |
| 89 | template <typename Char, typename Parser, typename Attr, typename Skipper> |
| 90 | bool test_attr(Char const* in, Parser const& p |
| 91 | , Attr& attr, Skipper const& s, bool full_match = true) |
| 92 | { |
| 93 | // we don't care about the result of the "what" function. |
| 94 | // we only care that all parsers have it: |
| 95 | boost::spirit::qi::what(p); |
| 96 | |
| 97 | Char const* last = in; |
| 98 | while (*last) |
| 99 | last++; |
| 100 | return boost::spirit::qi::phrase_parse(in, last, p, s, attr) |
| 101 | && (!full_match || (in == last)); |
| 102 | } |
| 103 | |
| 104 | template <typename Char, typename Parser, typename Attr> |
| 105 | bool binary_test_attr(Char const* in, std::size_t size, Parser const& p, |
| 106 | Attr& attr, bool full_match = true) |
| 107 | { |
| 108 | // we don't care about the result of the "what" function. |
| 109 | // we only care that all parsers have it: |
| 110 | boost::spirit::qi::what(p); |
| 111 | |
| 112 | Char const* last = in + size; |
| 113 | return boost::spirit::qi::parse(in, last, p, attr) |
| 114 | && (!full_match || (in == last)); |
| 115 | } |
| 116 | |
| 117 | template <typename Char, typename Parser, typename Attr, typename Skipper> |
| 118 | bool binary_test_attr(Char const* in, std::size_t size, Parser const& p, |
| 119 | Attr& attr, Skipper const& s, bool full_match = true) |
| 120 | { |
| 121 | // we don't care about the result of the "what" function. |
| 122 | // we only care that all parsers have it: |
| 123 | boost::spirit::qi::what(p); |
| 124 | |
| 125 | Char const* last = in + size; |
| 126 | return boost::spirit::qi::phrase_parse(in, last, p, s, attr) |
| 127 | && (!full_match || (in == last)); |
| 128 | } |
| 129 | |
| 130 | struct printer |
| 131 | { |
| 132 | typedef boost::spirit::utf8_string string; |
| 133 | |
| 134 | void element(string const& tag, string const& value, int depth) const |
| 135 | { |
| 136 | for (int i = 0; i < (depth*4); ++i) // indent to depth |
| 137 | std::cout << ' '; |
| 138 | |
| 139 | std::cout << "tag: " << tag; |
| 140 | if (value != "" ) |
| 141 | std::cout << ", value: " << value; |
| 142 | std::cout << std::endl; |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | void print_info(boost::spirit::info const& what) |
| 147 | { |
| 148 | using boost::spirit::basic_info_walker; |
| 149 | |
| 150 | printer pr; |
| 151 | basic_info_walker<printer> walker(pr, what.tag, 0); |
| 152 | boost::apply_visitor(visitor&: walker, visitable: what.value); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #endif |
| 157 | |