flow_control.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // flow_control.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_CORE_FLOW_CONTROL_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_CORE_FLOW_CONTROL_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/xpressive/detail/detail_fwd.hpp>
  14. #include <boost/xpressive/detail/core/regex_impl.hpp>
  15. #include <boost/xpressive/detail/core/state.hpp>
  16. #include <boost/xpressive/detail/utility/ignore_unused.hpp>
  17. namespace boost { namespace xpressive { namespace detail
  18. {
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // push_context_match
  21. //
  22. template<typename BidiIter>
  23. inline bool push_context_match
  24. (
  25. regex_impl<BidiIter> const &impl
  26. , match_state<BidiIter> &state
  27. , matchable<BidiIter> const &next
  28. )
  29. {
  30. // avoid infinite recursion
  31. // BUGBUG this only catches direct infinite recursion, like sregex::compile("(?R)"), but
  32. // not indirect infinite recursion where two rules invoke each other recursively.
  33. if(state.is_active_regex(impl) && state.cur_ == state.sub_match(0).begin_)
  34. {
  35. return next.match(state);
  36. }
  37. // save state
  38. match_context<BidiIter> context = state.push_context(impl, next, context);
  39. detail::ignore_unused(context);
  40. // match the nested regex and uninitialize the match context
  41. // (reclaims the sub_match objects if necessary)
  42. return state.pop_context(impl, impl.xpr_->match(state));
  43. }
  44. ///////////////////////////////////////////////////////////////////////////////
  45. // pop_context_match
  46. //
  47. template<typename BidiIter>
  48. inline bool pop_context_match(match_state<BidiIter> &state)
  49. {
  50. // save state
  51. // BUGBUG nested regex could have changed state.traits_
  52. match_context<BidiIter> &context(*state.context_.prev_context_);
  53. state.swap_context(context);
  54. // Finished matching the nested regex; now match the rest of the enclosing regex
  55. bool success = context.next_ptr_->match(state);
  56. // restore state
  57. state.swap_context(context);
  58. return success;
  59. }
  60. }}} // namespace boost::xpressive::detail
  61. #endif