1 /*============================================================================= 2 Copyright (c) 2001-2013 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/home/x3/core/parse.hpp> 11 #include <boost/utility/string_view.hpp> 12 #include <iostream> 13 14 namespace spirit_test 15 { 16 template <typename Char, typename Parser> test(Char const * in,Parser const & p,bool full_match=true)17 bool test(Char const* in, Parser const& p, bool full_match = true) 18 { 19 Char const* last = in; 20 while (*last) 21 last++; 22 return boost::spirit::x3::parse(in, last, p) 23 && (!full_match || (in == last)); 24 } 25 26 template <typename Char, typename Parser> test(boost::basic_string_view<Char,std::char_traits<Char>> in,Parser const & p,bool full_match=true)27 bool test(boost::basic_string_view<Char, std::char_traits<Char>> in, 28 Parser const& p, bool full_match = true) 29 { 30 auto const last = in.end(); 31 auto pos = in.begin(); 32 33 return boost::spirit::x3::parse(pos, last, p) && (!full_match || (pos == last)); 34 } 35 36 template <typename Char, typename Parser, typename Skipper> test(Char const * in,Parser const & p,Skipper const & s,bool full_match=true)37 bool test(Char const* in, Parser const& p 38 , Skipper const& s, bool full_match = true) 39 { 40 Char const* last = in; 41 while (*last) 42 last++; 43 return boost::spirit::x3::phrase_parse(in, last, p, s) 44 && (!full_match || (in == last)); 45 } 46 47 template <typename Char, typename Parser> test_failure(Char const * in,Parser const & p)48 bool test_failure(Char const* in, Parser const& p) 49 { 50 Char const * const start = in; 51 Char const* last = in; 52 while (*last) 53 last++; 54 55 return !boost::spirit::x3::parse(in, last, p) && (in == start); 56 } 57 58 template <typename Char, typename Parser> test_failure(boost::basic_string_view<Char,std::char_traits<Char>> const in,Parser const & p)59 bool test_failure(boost::basic_string_view<Char, std::char_traits<Char>> const in, 60 Parser const& p) 61 { 62 auto pos = in.begin(); 63 return !boost::spirit::x3::parse(pos, in.end(), p) && (pos == in.begin()); 64 } 65 66 template <typename Char, typename Parser, typename Attr> test_attr(Char const * in,Parser const & p,Attr & attr,bool full_match=true)67 bool test_attr(Char const* in, Parser const& p 68 , Attr& attr, bool full_match = true) 69 { 70 Char const* last = in; 71 while (*last) 72 last++; 73 return boost::spirit::x3::parse(in, last, p, attr) 74 && (!full_match || (in == last)); 75 } 76 77 template <typename Char, typename Parser, typename Attr, typename Skipper> test_attr(Char const * in,Parser const & p,Attr & attr,Skipper const & s,bool full_match=true)78 bool test_attr(Char const* in, Parser const& p 79 , Attr& attr, Skipper const& s, bool full_match = true) 80 { 81 Char const* last = in; 82 while (*last) 83 last++; 84 return boost::spirit::x3::phrase_parse(in, last, p, s, attr) 85 && (!full_match || (in == last)); 86 } 87 88 template <typename Char, typename Parser> binary_test(Char const * in,std::size_t size,Parser const & p,bool full_match=true)89 bool binary_test(Char const* in, std::size_t size, Parser const& p, 90 bool full_match = true) 91 { 92 Char const* last = in + size; 93 return boost::spirit::x3::parse(in, last, p) 94 && (!full_match || (in == last)); 95 } 96 97 template <typename Char, typename Parser, typename Skipper> binary_test(Char const * in,std::size_t size,Parser const & p,Skipper const & s,bool full_match=true)98 bool binary_test(Char const* in, std::size_t size, Parser const& p, 99 Skipper const& s, bool full_match = true) 100 { 101 Char const* last = in + size; 102 return boost::spirit::x3::phrase_parse(in, last, p, s) 103 && (!full_match || (in == last)); 104 } 105 106 template <typename Char, typename Parser, typename Attr> binary_test_attr(Char const * in,std::size_t size,Parser const & p,Attr & attr,bool full_match=true)107 bool binary_test_attr(Char const* in, std::size_t size, Parser const& p, 108 Attr& attr, bool full_match = true) 109 { 110 Char const* last = in + size; 111 return boost::spirit::x3::parse(in, last, p, attr) 112 && (!full_match || (in == last)); 113 } 114 115 template <typename Char, typename Parser, typename Attr, typename Skipper> binary_test_attr(Char const * in,std::size_t size,Parser const & p,Attr & attr,Skipper const & s,bool full_match=true)116 bool binary_test_attr(Char const* in, std::size_t size, Parser const& p, 117 Attr& attr, Skipper const& s, bool full_match = true) 118 { 119 Char const* last = in + size; 120 return boost::spirit::x3::phrase_parse(in, last, p, s, attr) 121 && (!full_match || (in == last)); 122 } 123 124 125 template <typename... T> always_true(T &&...)126 constexpr bool always_true(T&&...) { return true; } 127 128 template <typename Parser> test_ctors(Parser const & p)129 constexpr bool test_ctors(Parser const& p) 130 { 131 return always_true( 132 static_cast<Parser>(static_cast<Parser&&>( // test move ctor 133 static_cast<Parser>(p)))); // test copy ctor 134 } 135 } 136 137 # define BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(...) \ 138 static_assert(::spirit_test::test_ctors(__VA_ARGS__), "") 139 140 #endif 141