action.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. };
  46. }}}
  47. ///////////////////////////////////////////////////////////////////////////////
  48. namespace boost { namespace spirit
  49. {
  50. ///////////////////////////////////////////////////////////////////////////
  51. // Karma action meta-compiler
  52. template <>
  53. struct make_component<lex::domain, tag::action>
  54. {
  55. template <typename Sig>
  56. struct result;
  57. template <typename This, typename Elements, typename Modifiers>
  58. struct result<This(Elements, Modifiers)>
  59. {
  60. typedef typename
  61. remove_const<typename Elements::car_type>::type
  62. subject_type;
  63. typedef typename
  64. remove_const<typename Elements::cdr_type::car_type>::type
  65. action_type;
  66. typedef lex::action<subject_type, action_type> type;
  67. };
  68. template <typename Elements>
  69. typename result<make_component(Elements, unused_type)>::type
  70. operator()(Elements const& elements, unused_type) const
  71. {
  72. typename result<make_component(Elements, unused_type)>::type
  73. result(elements.car, elements.cdr.car);
  74. return result;
  75. }
  76. };
  77. }}
  78. #endif