/Src/Dependencies/Boost/boost/spirit/home/classic/dynamic/impl/conditions.ipp

http://hadesmem.googlecode.com/ · C++ Header · 104 lines · 56 code · 19 blank · 29 comment · 2 complexity · 5ce405064d4cb21a1cb51475c7c8e02f MD5 · raw file

  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Martin Wille
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #ifndef BOOST_SPIRIT_CONDITIONS_IPP
  9. #define BOOST_SPIRIT_CONDITIONS_IPP
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include <boost/spirit/home/classic/meta/parser_traits.hpp>
  12. #include <boost/spirit/home/classic/core/composite/epsilon.hpp>
  13. ///////////////////////////////////////////////////////////////////////////////
  14. namespace boost { namespace spirit {
  15. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  16. namespace impl {
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //
  19. // condition evaluation
  20. //
  21. ///////////////////////////////////////////////////////////////////////////////
  22. //////////////////////////////////
  23. // condition_parser_selector, decides which parser to use for a condition
  24. // If the template argument is a parser then that parser is used.
  25. // If the template argument is a functor then a condition parser using
  26. // the functor is chosen
  27. template <typename T> struct embed_t_accessor
  28. {
  29. typedef typename T::embed_t type;
  30. };
  31. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  32. template <> struct embed_t_accessor<int>
  33. {
  34. typedef int type;
  35. };
  36. #endif
  37. template <typename ConditionT>
  38. struct condition_parser_selector
  39. {
  40. typedef
  41. typename mpl::if_<
  42. is_parser<ConditionT>,
  43. ConditionT,
  44. condition_parser<ConditionT>
  45. >::type
  46. type;
  47. typedef typename embed_t_accessor<type>::type embed_t;
  48. };
  49. //////////////////////////////////
  50. // condition_evaluator, uses a parser to check wether a condition is met
  51. // takes a parser or a functor that can be evaluated in boolean context
  52. // as template parameter.
  53. // JDG 4-15-03 refactored
  54. template <typename ConditionT>
  55. struct condition_evaluator
  56. {
  57. typedef condition_parser_selector<ConditionT> selector_t;
  58. typedef typename selector_t::type selected_t;
  59. typedef typename selector_t::embed_t cond_embed_t;
  60. typedef typename boost::call_traits<cond_embed_t>::param_type
  61. param_t;
  62. condition_evaluator(param_t s) : cond(s) {}
  63. /////////////////////////////
  64. // evaluate, checks wether condition is met
  65. // returns length of a match or a negative number for no-match
  66. template <typename ScannerT>
  67. std::ptrdiff_t
  68. evaluate(ScannerT const &scan) const
  69. {
  70. typedef typename ScannerT::iterator_t iterator_t;
  71. typedef typename parser_result<selected_t, ScannerT>::type cres_t;
  72. iterator_t save(scan.first);
  73. cres_t result = cond.parse(scan);
  74. if (!result) // reset the position if evaluation
  75. scan.first = save; // fails.
  76. return result.length();
  77. }
  78. cond_embed_t cond;
  79. };
  80. ///////////////////////////////////////////////////////////////////////////////
  81. } // namespace impl
  82. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  83. }} // namespace boost::spirit
  84. #endif