PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/avr-cpp-libs/foreign/cpp/boost/spirit/home/classic/core/composite/actions.hpp

https://bitbucket.org/nazemnykh_anton/avr-cpp-libs
C++ Header | 132 lines | 50 code | 16 blank | 66 comment | 1 complexity | 78f611703149436ae777bccbec4af232 MD5 | raw file
  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. namespace boost { namespace spirit {
  13. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  14. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  15. #pragma warning(push)
  16. #pragma warning(disable:4512) //assignment operator could not be generated
  17. #endif
  18. ///////////////////////////////////////////////////////////////////////////
  19. //
  20. // action class
  21. //
  22. // The action class binds a parser with a user defined semantic
  23. // action. Instances of action are never created manually. Instead,
  24. // action objects are typically created indirectly through
  25. // expression templates of the form:
  26. //
  27. // p[f]
  28. //
  29. // where p is a parser and f is a function or functor. The semantic
  30. // action may be a function or a functor. When the parser is
  31. // successful, the actor calls the scanner's action_policy policy
  32. // (see scanner.hpp):
  33. //
  34. // scan.do_action(actor, attribute, first, last);
  35. //
  36. // passing in these information:
  37. //
  38. // actor: The action's function or functor
  39. // attribute: The match (returned by the parser) object's
  40. // attribute (see match.hpp)
  41. // first: Iterator pointing to the start of the matching
  42. // portion of the input
  43. // last: Iterator pointing to one past the end of the
  44. // matching portion of the input
  45. //
  46. // It is the responsibility of the scanner's action_policy policy to
  47. // dispatch the function or functor as it sees fit. The expected
  48. // function or functor signature depends on the parser being
  49. // wrapped. In general, if the attribute type of the parser being
  50. // wrapped is a nil_t, the function or functor expect the signature:
  51. //
  52. // void func(Iterator first, Iterator last); // functions
  53. //
  54. // struct ftor // functors
  55. // {
  56. // void func(Iterator first, Iterator last) const;
  57. // };
  58. //
  59. // where Iterator is the type of the iterator that is being used and
  60. // first and last are the iterators pointing to the matching portion
  61. // of the input.
  62. //
  63. // If the attribute type of the parser being wrapped is not a nil_t,
  64. // the function or functor usually expect the signature:
  65. //
  66. // void func(T val); // functions
  67. //
  68. // struct ftor // functors
  69. // {
  70. // void func(T val) const;
  71. // };
  72. //
  73. // where T is the attribute type and val is the attribute value
  74. // returned by the parser being wrapped.
  75. //
  76. ///////////////////////////////////////////////////////////////////////////
  77. template <typename ParserT, typename ActionT>
  78. class action : public unary<ParserT, parser<action<ParserT, ActionT> > >
  79. {
  80. public:
  81. typedef action<ParserT, ActionT> self_t;
  82. typedef action_parser_category parser_category_t;
  83. typedef unary<ParserT, parser<self_t> > base_t;
  84. typedef ActionT predicate_t;
  85. template <typename ScannerT>
  86. struct result
  87. {
  88. typedef typename parser_result<ParserT, ScannerT>::type type;
  89. };
  90. action(ParserT const& p, ActionT const& a)
  91. : base_t(p)
  92. , actor(a) {}
  93. template <typename ScannerT>
  94. typename parser_result<self_t, ScannerT>::type
  95. parse(ScannerT const& scan) const
  96. {
  97. typedef typename ScannerT::iterator_t iterator_t;
  98. typedef typename parser_result<self_t, ScannerT>::type result_t;
  99. scan.at_end(); // allow skipper to take effect
  100. iterator_t save = scan.first;
  101. result_t hit = this->subject().parse(scan);
  102. if (hit)
  103. {
  104. typename result_t::return_t val = hit.value();
  105. scan.do_action(actor, val, save, scan.first);
  106. }
  107. return hit;
  108. }
  109. ActionT const& predicate() const { return actor; }
  110. private:
  111. ActionT actor;
  112. };
  113. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  114. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  115. #endif