test.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #if !defined(BOOST_SPIRIT_LEX_TEST_MAR_23_2007_0721PM)
  6. #define BOOST_SPIRIT_LEX_TEST_MAR_23_2007_0721PM
  7. #include <boost/variant.hpp>
  8. #include <boost/range/iterator_range.hpp>
  9. namespace spirit_test
  10. {
  11. ///////////////////////////////////////////////////////////////////////////
  12. struct display_type
  13. {
  14. template<typename T>
  15. void operator()(T const &) const
  16. {
  17. std::cout << typeid(T).name() << std::endl;
  18. }
  19. template<typename T>
  20. static void print()
  21. {
  22. std::cout << typeid(T).name() << std::endl;
  23. }
  24. };
  25. ///////////////////////////////////////////////////////////////////////////
  26. display_type const display = {};
  27. ///////////////////////////////////////////////////////////////////////////
  28. template <typename Iterator>
  29. inline boost::iterator_range<Iterator> const&
  30. get_iterpair(boost::iterator_range<Iterator> const& itp)
  31. {
  32. return itp;
  33. }
  34. template <typename Iterator, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  35. inline boost::iterator_range<Iterator> const&
  36. get_iterpair(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& v)
  37. {
  38. return boost::get<boost::iterator_range<Iterator> >(v);
  39. }
  40. ///////////////////////////////////////////////////////////////////////////
  41. template <typename Lexer, typename Char>
  42. inline bool
  43. test(Lexer& lex, Char const* input, std::size_t token_id = 0,
  44. Char const* state = NULL)
  45. {
  46. typedef typename Lexer::iterator_type iterator_type;
  47. typedef std::basic_string<Char> string_type;
  48. string_type str(input);
  49. typename string_type::iterator it = str.begin();
  50. iterator_type first = lex.begin(it, str.end());
  51. iterator_type last = lex.end();
  52. bool r = true;
  53. if (NULL != state) {
  54. std::size_t stateid = lex.map_state(state);
  55. r = r && (static_cast<unsigned>(~0) != stateid);
  56. first.set_state(stateid);
  57. }
  58. r = r && lex;
  59. r = r && first != last;
  60. if (token_id != 0)
  61. r = r && (*first).id() == token_id;
  62. else
  63. r = r && (*first).id() != 0;
  64. using namespace boost;
  65. typedef typename Lexer::iterator_type::base_iterator_type iterator;
  66. typedef iterator_range<iterator> iterpair_type;
  67. iterpair_type const& ip = get_iterpair<iterator>((*first).value());
  68. r = r && string_type(ip.begin(), ip.end()) == str;
  69. return r && first != last && ++first == last;
  70. }
  71. }
  72. #endif