as_inverse.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // as_inverse.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XPRESSIVE_DETAIL_STATIC_TRANSFORMS_AS_INVERSE_HPP_EAN_04_05_2007
  8. #define BOOST_XPRESSIVE_DETAIL_STATIC_TRANSFORMS_AS_INVERSE_HPP_EAN_04_05_2007
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <boost/mpl/sizeof.hpp>
  14. #include <boost/mpl/not.hpp>
  15. #include <boost/xpressive/detail/detail_fwd.hpp>
  16. #include <boost/xpressive/detail/static/static.hpp>
  17. #include <boost/proto/core.hpp>
  18. #define UNCV(x) typename remove_const<x>::type
  19. #define UNREF(x) typename remove_reference<x>::type
  20. #define UNCVREF(x) UNCV(UNREF(x))
  21. namespace boost { namespace xpressive { namespace grammar_detail
  22. {
  23. template<typename T>
  24. struct inverter
  25. {
  26. typedef T type;
  27. static T call(T t)
  28. {
  29. t.inverse();
  30. return t;
  31. }
  32. };
  33. template<typename Traits, typename ICase, typename Not>
  34. struct inverter<detail::literal_matcher<Traits, ICase, Not> >
  35. {
  36. typedef detail::literal_matcher<Traits, ICase, typename mpl::not_<Not>::type> type;
  37. static type call(detail::literal_matcher<Traits, ICase, Not> t)
  38. {
  39. return type(t.ch_);
  40. }
  41. };
  42. template<typename Traits>
  43. struct inverter<detail::logical_newline_matcher<Traits> >
  44. {
  45. // ~_ln matches any one character that is not in the "newline" character class
  46. typedef detail::posix_charset_matcher<Traits> type;
  47. static type call(detail::logical_newline_matcher<Traits> t)
  48. {
  49. return type(t.newline(), true);
  50. }
  51. };
  52. template<typename Traits>
  53. struct inverter<detail::assert_word_matcher<detail::word_boundary<mpl::true_>, Traits> >
  54. {
  55. typedef detail::assert_word_matcher<detail::word_boundary<mpl::false_>, Traits> type;
  56. static type call(detail::assert_word_matcher<detail::word_boundary<mpl::true_>, Traits> t)
  57. {
  58. return type(t.word());
  59. }
  60. };
  61. struct as_inverse : proto::callable
  62. {
  63. template<typename Sig>
  64. struct result;
  65. template<typename This, typename Matcher>
  66. struct result<This(Matcher)>
  67. : inverter<UNCVREF(Matcher)>
  68. {};
  69. template<typename Matcher>
  70. typename inverter<Matcher>::type operator ()(Matcher const &matcher) const
  71. {
  72. return inverter<Matcher>::call(matcher);
  73. }
  74. };
  75. }}}
  76. #undef UNCV
  77. #undef UNREF
  78. #undef UNCVREF
  79. #endif