actions.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #ifndef BOOST_SPIRIT_ACTIONS_HPP
  8. #define BOOST_SPIRIT_ACTIONS_HPP
  9. #include <boost/spirit/home/classic/namespace.hpp>
  10. #include <boost/spirit/home/classic/core/parser.hpp>
  11. #include <boost/spirit/home/classic/core/composite/composite.hpp>
  12. #include <boost/core/ignore_unused.hpp>
  13. namespace boost { namespace spirit {
  14. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  15. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  16. #pragma warning(push)
  17. #pragma warning(disable:4512) //assignment operator could not be generated
  18. #endif
  19. ///////////////////////////////////////////////////////////////////////////
  20. //
  21. // action class
  22. //
  23. // The action class binds a parser with a user defined semantic
  24. // action. Instances of action are never created manually. Instead,
  25. // action objects are typically created indirectly through
  26. // expression templates of the form:
  27. //
  28. // p[f]
  29. //
  30. // where p is a parser and f is a function or functor. The semantic
  31. // action may be a function or a functor. When the parser is
  32. // successful, the actor calls the scanner's action_policy policy
  33. // (see scanner.hpp):
  34. //
  35. // scan.do_action(actor, attribute, first, last);
  36. //
  37. // passing in these information:
  38. //
  39. // actor: The action's function or functor
  40. // attribute: The match (returned by the parser) object's
  41. // attribute (see match.hpp)
  42. // first: Iterator pointing to the start of the matching
  43. // portion of the input
  44. // last: Iterator pointing to one past the end of the
  45. // matching portion of the input
  46. //
  47. // It is the responsibility of the scanner's action_policy policy to
  48. // dispatch the function or functor as it sees fit. The expected
  49. // function or functor signature depends on the parser being
  50. // wrapped. In general, if the attribute type of the parser being
  51. // wrapped is a nil_t, the function or functor expect the signature:
  52. //
  53. // void func(Iterator first, Iterator last); // functions
  54. //
  55. // struct ftor // functors
  56. // {
  57. // void func(Iterator first, Iterator last) const;
  58. // };
  59. //
  60. // where Iterator is the type of the iterator that is being used and
  61. // first and last are the iterators pointing to the matching portion
  62. // of the input.
  63. //
  64. // If the attribute type of the parser being wrapped is not a nil_t,
  65. // the function or functor usually expect the signature:
  66. //
  67. // void func(T val); // functions
  68. //
  69. // struct ftor // functors
  70. // {
  71. // void func(T val) const;
  72. // };
  73. //
  74. // where T is the attribute type and val is the attribute value
  75. // returned by the parser being wrapped.
  76. //
  77. ///////////////////////////////////////////////////////////////////////////
  78. template <typename ParserT, typename ActionT>
  79. class action : public unary<ParserT, parser<action<ParserT, ActionT> > >
  80. {
  81. public:
  82. typedef action<ParserT, ActionT> self_t;
  83. typedef action_parser_category parser_category_t;
  84. typedef unary<ParserT, parser<self_t> > base_t;
  85. typedef ActionT predicate_t;
  86. template <typename ScannerT>
  87. struct result
  88. {
  89. typedef typename parser_result<ParserT, ScannerT>::type type;
  90. };
  91. action(ParserT const& p, ActionT const& a)
  92. : base_t(p)
  93. , actor(a) {}
  94. template <typename ScannerT>
  95. typename parser_result<self_t, ScannerT>::type
  96. parse(ScannerT const& scan) const
  97. {
  98. typedef typename ScannerT::iterator_t iterator_t;
  99. typedef typename parser_result<self_t, ScannerT>::type result_t;
  100. ignore_unused(scan.at_end()); // allow skipper to take effect
  101. iterator_t save = scan.first;
  102. result_t hit = this->subject().parse(scan);
  103. if (hit)
  104. {
  105. typename result_t::return_t val = hit.value();
  106. scan.do_action(actor, val, save, scan.first);
  107. }
  108. return hit;
  109. }
  110. ActionT const& predicate() const { return actor; }
  111. private:
  112. ActionT actor;
  113. };
  114. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  115. #pragma warning(pop)
  116. #endif
  117. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  118. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  119. #endif