action.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. //
  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. #ifndef BOOST_SPIRIT_LEX_LEXER_ACTION_HPP
  6. #define BOOST_SPIRIT_LEX_LEXER_ACTION_HPP
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/lex/meta_compiler.hpp>
  11. #include <boost/spirit/home/lex/lexer_type.hpp>
  12. #include <boost/spirit/home/lex/argument.hpp>
  13. #include <boost/spirit/home/lex/lexer/support_functions.hpp>
  14. #include <boost/mpl/if.hpp>
  15. #include <boost/type_traits/remove_const.hpp>
  16. #include <boost/type_traits/is_same.hpp>
  17. ///////////////////////////////////////////////////////////////////////////////
  18. namespace boost { namespace spirit { namespace lex
  19. {
  20. ///////////////////////////////////////////////////////////////////////////
  21. template <typename Subject, typename Action>
  22. struct action : unary_lexer<action<Subject, Action> >
  23. {
  24. action(Subject const& subject, Action f)
  25. : subject(subject), f(f) {}
  26. template <typename LexerDef, typename String>
  27. void collect(LexerDef& lexdef, String const& state
  28. , String const& targetstate) const
  29. {
  30. // collect the token definition information for the token_def
  31. // this action is attached to
  32. subject.collect(lexdef, state, targetstate);
  33. }
  34. template <typename LexerDef>
  35. void add_actions(LexerDef& lexdef) const
  36. {
  37. // call to add all actions attached further down the hierarchy
  38. subject.add_actions(lexdef);
  39. // retrieve the id of the associated token_def and register the
  40. // given semantic action with the lexer instance
  41. lexdef.add_action(subject.unique_id(), subject.state(), f);
  42. }
  43. Subject subject;
  44. Action f;
  45. // silence MSVC warning C4512: assignment operator could not be generated
  46. BOOST_DELETED_FUNCTION(action& operator= (action const&))
  47. };
  48. }}}
  49. ///////////////////////////////////////////////////////////////////////////////
  50. namespace boost { namespace spirit
  51. {
  52. ///////////////////////////////////////////////////////////////////////////
  53. // Karma action meta-compiler
  54. template <>
  55. struct make_component<lex::domain, tag::action>
  56. {
  57. template <typename Sig>
  58. struct result;
  59. template <typename This, typename Elements, typename Modifiers>
  60. struct result<This(Elements, Modifiers)>
  61. {
  62. typedef typename
  63. remove_const<typename Elements::car_type>::type
  64. subject_type;
  65. typedef typename
  66. remove_const<typename Elements::cdr_type::car_type>::type
  67. action_type;
  68. typedef lex::action<subject_type, action_type> type;
  69. };
  70. template <typename Elements>
  71. typename result<make_component(Elements, unused_type)>::type
  72. operator()(Elements const& elements, unused_type) const
  73. {
  74. typename result<make_component(Elements, unused_type)>::type
  75. result(elements.car, elements.cdr.car);
  76. return result;
  77. }
  78. };
  79. }}
  80. #endif