action.hpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #if !defined(SPIRIT_LEX_ACTION_NOV_18_2007_0743PM)
  6. #define SPIRIT_LEX_ACTION_NOV_18_2007_0743PM
  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. private:
  46. // silence MSVC warning C4512: assignment operator could not be generated
  47. action& operator= (action const&);
  48. };
  49. }}}
  50. ///////////////////////////////////////////////////////////////////////////////
  51. namespace boost { namespace spirit
  52. {
  53. ///////////////////////////////////////////////////////////////////////////
  54. // Karma action meta-compiler
  55. template <>
  56. struct make_component<lex::domain, tag::action>
  57. {
  58. template <typename Sig>
  59. struct result;
  60. template <typename This, typename Elements, typename Modifiers>
  61. struct result<This(Elements, Modifiers)>
  62. {
  63. typedef typename
  64. remove_const<typename Elements::car_type>::type
  65. subject_type;
  66. typedef typename
  67. remove_const<typename Elements::cdr_type::car_type>::type
  68. action_type;
  69. typedef lex::action<subject_type, action_type> type;
  70. };
  71. template <typename Elements>
  72. typename result<make_component(Elements, unused_type)>::type
  73. operator()(Elements const& elements, unused_type) const
  74. {
  75. typename result<make_component(Elements, unused_type)>::type
  76. result(elements.car, elements.cdr.car);
  77. return result;
  78. }
  79. };
  80. }}
  81. #endif