compile.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // compile.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_COMPILE_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_STATIC_COMPILE_HPP_EAN_10_04_2005
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <boost/mpl/bool.hpp>
  14. #include <boost/iterator/iterator_traits.hpp>
  15. #include <boost/proto/core.hpp>
  16. #include <boost/xpressive/regex_traits.hpp>
  17. #include <boost/xpressive/detail/core/regex_impl.hpp>
  18. #include <boost/xpressive/detail/core/linker.hpp>
  19. #include <boost/xpressive/detail/core/optimize.hpp>
  20. #include <boost/xpressive/detail/core/adaptor.hpp>
  21. #include <boost/xpressive/detail/core/matcher/end_matcher.hpp>
  22. #include <boost/xpressive/detail/static/static.hpp>
  23. #include <boost/xpressive/detail/static/visitor.hpp>
  24. #include <boost/xpressive/detail/static/grammar.hpp>
  25. namespace boost { namespace xpressive { namespace detail
  26. {
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // static_compile_impl2
  29. template<typename Xpr, typename BidiIter, typename Traits>
  30. void static_compile_impl2(Xpr const &xpr, shared_ptr<regex_impl<BidiIter> > const &impl, Traits const &tr)
  31. {
  32. typedef typename iterator_value<BidiIter>::type char_type;
  33. impl->tracking_clear();
  34. impl->traits_ = new traits_holder<Traits>(tr);
  35. // "compile" the regex and wrap it in an xpression_adaptor.
  36. typedef xpression_visitor<BidiIter, mpl::false_, Traits> visitor_type;
  37. visitor_type visitor(tr, impl);
  38. intrusive_ptr<matchable_ex<BidiIter> const> adxpr = make_adaptor<matchable_ex<BidiIter> >(
  39. typename Grammar<char_type>::template impl<Xpr const &, end_xpression, visitor_type &>()(
  40. xpr
  41. , end_xpression()
  42. , visitor
  43. )
  44. );
  45. // Link and optimize the regex
  46. common_compile(adxpr, *impl, visitor.traits());
  47. // References changed, update dependencies.
  48. impl->tracking_update();
  49. }
  50. ///////////////////////////////////////////////////////////////////////////////
  51. // pattern for imbued regexes.
  52. struct XpressiveLocaleModifier
  53. : proto::binary_expr<
  54. modifier_tag
  55. , proto::terminal<locale_modifier<proto::_> >
  56. , proto::_
  57. >
  58. {};
  59. ///////////////////////////////////////////////////////////////////////////////
  60. // static_compile_impl1
  61. template<typename Xpr, typename BidiIter>
  62. typename disable_if<proto::matches<Xpr, XpressiveLocaleModifier> >::type
  63. static_compile_impl1(Xpr const &xpr, shared_ptr<regex_impl<BidiIter> > const &impl)
  64. {
  65. // use default traits
  66. typedef typename iterator_value<BidiIter>::type char_type;
  67. typedef typename default_regex_traits<char_type>::type traits_type;
  68. traits_type tr;
  69. static_compile_impl2(xpr, impl, tr);
  70. }
  71. ///////////////////////////////////////////////////////////////////////////////
  72. // static_compile_impl1
  73. template<typename Xpr, typename BidiIter>
  74. typename enable_if<proto::matches<Xpr, XpressiveLocaleModifier> >::type
  75. static_compile_impl1(Xpr const &xpr, shared_ptr<regex_impl<BidiIter> > const &impl)
  76. {
  77. // use specified traits
  78. typedef typename proto::result_of::value<typename proto::result_of::left<Xpr>::type>::type::locale_type locale_type;
  79. typedef typename regex_traits_type<locale_type, BidiIter>::type traits_type;
  80. static_compile_impl2(proto::right(xpr), impl, traits_type(proto::value(proto::left(xpr)).getloc()));
  81. }
  82. ///////////////////////////////////////////////////////////////////////////////
  83. // static_compile
  84. template<typename Xpr, typename BidiIter>
  85. void static_compile(Xpr const &xpr, shared_ptr<regex_impl<BidiIter> > const &impl)
  86. {
  87. static_compile_impl1(xpr, impl);
  88. }
  89. }}} // namespace boost::xpressive::detail
  90. #endif