success_handler.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  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. ==============================================================================*/
  6. #if !defined(BOOST_SPIRIT_SUCCESS_HANDLER_FEBRUARY_25_2011_1051AM)
  7. #define BOOST_SPIRIT_SUCCESS_HANDLER_FEBRUARY_25_2011_1051AM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/nonterminal/rule.hpp>
  12. #include <boost/function.hpp>
  13. namespace boost { namespace spirit { namespace qi
  14. {
  15. template <
  16. typename Iterator, typename Context
  17. , typename Skipper, typename F
  18. >
  19. struct success_handler
  20. {
  21. typedef function<
  22. bool(Iterator& first, Iterator const& last
  23. , Context& context
  24. , Skipper const& skipper
  25. )>
  26. function_type;
  27. success_handler(function_type subject_, F f_)
  28. : subject(subject_)
  29. , f(f_)
  30. {
  31. }
  32. bool operator()(
  33. Iterator& first, Iterator const& last
  34. , Context& context, Skipper const& skipper) const
  35. {
  36. Iterator i = first;
  37. bool r = subject(i, last, context, skipper);
  38. if (r)
  39. {
  40. typedef
  41. fusion::vector<
  42. Iterator&
  43. , Iterator const&
  44. , Iterator const&>
  45. params;
  46. skip_over(first, last, skipper);
  47. params args(first, last, i);
  48. f(args, context);
  49. first = i;
  50. }
  51. return r;
  52. }
  53. function_type subject;
  54. F f;
  55. };
  56. template <
  57. typename Iterator, typename T0, typename T1, typename T2
  58. , typename F>
  59. void on_success(rule<Iterator, T0, T1, T2>& r, F f)
  60. {
  61. typedef rule<Iterator, T0, T1, T2> rule_type;
  62. typedef
  63. success_handler<
  64. Iterator
  65. , typename rule_type::context_type
  66. , typename rule_type::skipper_type
  67. , F>
  68. success_handler;
  69. r.f = success_handler(r.f, f);
  70. }
  71. }}}
  72. #endif