/src/contrib/boost/spirit/home/lex/lexer/lexertl/semantic_action_data.hpp

http://pythonocc.googlecode.com/ · C++ Header · 118 lines · 74 code · 20 blank · 24 comment · 8 complexity · cf8b5fc32948650710b20c882bc34309 MD5 · raw file

  1. // Copyright (c) 2001-2010 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(BOOST_SPIRIT_LEX_LEXER_SEMANTIC_ACTION_DATA_JUN_10_2009_0417PM)
  6. #define BOOST_SPIRIT_LEX_LEXER_SEMANTIC_ACTION_DATA_JUN_10_2009_0417PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/lex/lexer/pass_flags.hpp>
  11. #include <boost/mpl/bool.hpp>
  12. #include <boost/function.hpp>
  13. #include <vector>
  14. namespace boost { namespace spirit { namespace lex { namespace lexertl
  15. {
  16. namespace detail
  17. {
  18. ///////////////////////////////////////////////////////////////////////
  19. template <typename Iterator, typename SupportsState, typename Data>
  20. struct semantic_actions;
  21. // This specialization of semantic_actions will be used if the token
  22. // type (lexer definition) does not support states, which simplifies
  23. // the data structures used to store the semantic action function
  24. // objects.
  25. template <typename Iterator, typename Data>
  26. struct semantic_actions<Iterator, mpl::false_, Data>
  27. {
  28. typedef void functor_type(Iterator&, Iterator&
  29. , BOOST_SCOPED_ENUM(pass_flags)&, std::size_t&, Data&);
  30. typedef boost::function<functor_type> functor_wrapper_type;
  31. // add a semantic action function object
  32. template <typename Idtype, typename F>
  33. void add_action(Idtype unique_id, std::size_t, F act)
  34. {
  35. if (actions_.size() <= unique_id)
  36. actions_.resize(unique_id + 1);
  37. actions_[unique_id] = act;
  38. }
  39. // try to invoke a semantic action for the given token (unique_id)
  40. BOOST_SCOPED_ENUM(pass_flags) invoke_actions(std::size_t /*state*/
  41. , std::size_t& id, std::size_t unique_id, Iterator& end
  42. , Data& data) const
  43. {
  44. // if there is nothing to invoke, continue with 'match'
  45. if (unique_id >= actions_.size() || !actions_[unique_id])
  46. return pass_flags::pass_normal;
  47. // Note: all arguments might be changed by the invoked semantic
  48. // action
  49. BOOST_SCOPED_ENUM(pass_flags) match = pass_flags::pass_normal;
  50. actions_[unique_id](data.get_first(), end, match, id, data);
  51. return match;
  52. }
  53. std::vector<functor_wrapper_type> actions_;
  54. };
  55. // This specialization of semantic_actions will be used if the token
  56. // type (lexer definition) needs to support states, resulting in a more
  57. // complex data structure needed for storing the semantic action
  58. // function objects.
  59. template <typename Iterator, typename Data>
  60. struct semantic_actions<Iterator, mpl::true_, Data>
  61. {
  62. typedef void functor_type(Iterator&, Iterator&
  63. , BOOST_SCOPED_ENUM(pass_flags)&, std::size_t&, Data&);
  64. typedef boost::function<functor_type> functor_wrapper_type;
  65. // add a semantic action function object
  66. template <typename Idtype, typename F>
  67. void add_action(Idtype unique_id, std::size_t state, F act)
  68. {
  69. if (actions_.size() <= state)
  70. actions_.resize(state + 1);
  71. std::vector<functor_wrapper_type>& actions (actions_[state]);
  72. if (actions.size() <= unique_id)
  73. actions.resize(unique_id + 1);
  74. actions[unique_id] = act;
  75. }
  76. // try to invoke a semantic action for the given token (unique_id)
  77. BOOST_SCOPED_ENUM(pass_flags) invoke_actions(std::size_t state
  78. , std::size_t& id, std::size_t unique_id, Iterator& end
  79. , Data& data) const
  80. {
  81. // if there is no action defined for this state, return match
  82. if (state >= actions_.size())
  83. return pass_flags::pass_normal;
  84. // if there is nothing to invoke, continue with 'match'
  85. std::vector<functor_wrapper_type> const& actions = actions_[state];
  86. if (unique_id >= actions.size() || !actions[unique_id])
  87. return pass_flags::pass_normal;
  88. // Note: all arguments might be changed by the invoked semantic
  89. // action
  90. BOOST_SCOPED_ENUM(pass_flags) match = pass_flags::pass_normal;
  91. actions[unique_id](data.get_first(), end, match, id, data);
  92. return match;
  93. }
  94. std::vector<std::vector<functor_wrapper_type> > actions_;
  95. };
  96. }
  97. }}}}
  98. #endif