state_switcher.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2010 Bryce Lelbach
  3. //
  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. #if !defined(BOOST_SPIRIT_LEX_STATE_SWITCHER_SEP_23_2007_0714PM)
  7. #define BOOST_SPIRIT_LEX_STATE_SWITCHER_SEP_23_2007_0714PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/support/info.hpp>
  12. #include <boost/spirit/home/qi/detail/attributes.hpp>
  13. #include <boost/spirit/home/support/common_terminals.hpp>
  14. #include <boost/spirit/home/support/string_traits.hpp>
  15. #include <boost/spirit/home/support/has_semantic_action.hpp>
  16. #include <boost/spirit/home/support/handles_container.hpp>
  17. #include <boost/spirit/home/qi/skip_over.hpp>
  18. #include <boost/spirit/home/qi/domain.hpp>
  19. #include <boost/spirit/home/qi/parser.hpp>
  20. #include <boost/spirit/home/qi/meta_compiler.hpp>
  21. #include <boost/mpl/print.hpp>
  22. namespace boost { namespace spirit
  23. {
  24. ///////////////////////////////////////////////////////////////////////////
  25. // Enablers
  26. ///////////////////////////////////////////////////////////////////////////
  27. // enables set_state(s)
  28. template <typename A0>
  29. struct use_terminal<qi::domain
  30. , terminal_ex<tag::set_state, fusion::vector1<A0> >
  31. > : traits::is_string<A0> {};
  32. // enables *lazy* set_state(s)
  33. template <>
  34. struct use_lazy_terminal<
  35. qi::domain, tag::set_state, 1
  36. > : mpl::true_ {};
  37. // enables in_state(s)[p]
  38. template <typename A0>
  39. struct use_directive<qi::domain
  40. , terminal_ex<tag::in_state, fusion::vector1<A0> >
  41. > : traits::is_string<A0> {};
  42. // enables *lazy* in_state(s)[p]
  43. template <>
  44. struct use_lazy_directive<
  45. qi::domain, tag::in_state, 1
  46. > : mpl::true_ {};
  47. }}
  48. namespace boost { namespace spirit { namespace qi
  49. {
  50. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  51. using spirit::set_state;
  52. using spirit::in_state;
  53. #endif
  54. using spirit::set_state_type;
  55. using spirit::in_state_type;
  56. ///////////////////////////////////////////////////////////////////////////
  57. namespace detail
  58. {
  59. template <typename Iterator>
  60. inline std::size_t
  61. set_lexer_state(Iterator& it, std::size_t state)
  62. {
  63. return it.set_state(state);
  64. }
  65. template <typename Iterator, typename Char>
  66. inline std::size_t
  67. set_lexer_state(Iterator& it, Char const* statename)
  68. {
  69. std::size_t state = it.map_state(statename);
  70. // If the following assertion fires you probably used the
  71. // set_state(...) or in_state(...)[...] lexer state switcher with
  72. // a lexer state name unknown to the lexer (no token definitions
  73. // have been associated with this lexer state).
  74. BOOST_ASSERT(std::size_t(~0) != state);
  75. return it.set_state(state);
  76. }
  77. }
  78. ///////////////////////////////////////////////////////////////////////////
  79. // Parser switching the state of the underlying lexer component.
  80. // This parser gets used for the set_state(...) construct.
  81. ///////////////////////////////////////////////////////////////////////////
  82. template <typename State>
  83. struct state_switcher
  84. : primitive_parser<state_switcher<State> >
  85. {
  86. typedef typename
  87. remove_const<typename traits::char_type_of<State>::type>::type
  88. char_type;
  89. typedef std::basic_string<char_type> string_type;
  90. template <typename Context, typename Iterator>
  91. struct attribute
  92. {
  93. typedef unused_type type;
  94. };
  95. state_switcher(char_type const* state)
  96. : state(state) {}
  97. template <typename Iterator, typename Context
  98. , typename Skipper, typename Attribute>
  99. bool parse(Iterator& first, Iterator const& last
  100. , Context& /*context*/, Skipper const& skipper
  101. , Attribute& /*attr*/) const
  102. {
  103. qi::skip_over(first, last, skipper); // always do a pre-skip
  104. // just switch the state and return success
  105. detail::set_lexer_state(first, state.c_str());
  106. return true;
  107. }
  108. template <typename Context>
  109. info what(Context& /*context*/) const
  110. {
  111. return info("set_state");
  112. }
  113. string_type state;
  114. };
  115. ///////////////////////////////////////////////////////////////////////////
  116. namespace detail
  117. {
  118. template <typename Iterator>
  119. struct reset_state_on_exit
  120. {
  121. template <typename State>
  122. reset_state_on_exit(Iterator& it_, State state_)
  123. : it(it_)
  124. , state(set_lexer_state(it_, traits::get_c_string(state_)))
  125. {}
  126. ~reset_state_on_exit()
  127. {
  128. // reset the state of the underlying lexer instance
  129. set_lexer_state(it, state);
  130. }
  131. Iterator& it;
  132. std::size_t state;
  133. // silence MSVC warning C4512: assignment operator could not be generated
  134. BOOST_DELETED_FUNCTION(reset_state_on_exit& operator= (reset_state_on_exit const&))
  135. };
  136. }
  137. ///////////////////////////////////////////////////////////////////////////
  138. // Parser, which switches the state of the underlying lexer component
  139. // for the execution of the embedded sub-parser, switching the state back
  140. // afterwards. This parser gets used for the in_state(...)[p] construct.
  141. ///////////////////////////////////////////////////////////////////////////
  142. template <typename Subject, typename State>
  143. struct state_switcher_context
  144. : unary_parser<state_switcher_context<Subject, State> >
  145. {
  146. typedef Subject subject_type;
  147. typedef typename traits::char_type_of<State>::type char_type;
  148. typedef typename remove_const<char_type>::type non_const_char_type;
  149. template <typename Context, typename Iterator>
  150. struct attribute
  151. {
  152. typedef typename
  153. traits::attribute_of<subject_type, Context, Iterator>::type
  154. type;
  155. };
  156. state_switcher_context(Subject const& subject
  157. , typename add_reference<State>::type state)
  158. : subject(subject), state(state) {}
  159. // The following conversion constructors are needed to make the
  160. // in_state_switcher template usable
  161. template <typename String>
  162. state_switcher_context(
  163. state_switcher_context<Subject, String> const& rhs)
  164. : subject(rhs.subject), state(traits::get_c_string(rhs.state)) {}
  165. template <typename Iterator, typename Context
  166. , typename Skipper, typename Attribute>
  167. bool parse(Iterator& first, Iterator const& last
  168. , Context& context, Skipper const& skipper
  169. , Attribute& attr) const
  170. {
  171. qi::skip_over(first, last, skipper); // always do a pre-skip
  172. // the state has to be reset at exit in any case
  173. detail::reset_state_on_exit<Iterator> guard(first, state);
  174. return subject.parse(first, last, context, skipper, attr);
  175. }
  176. template <typename Context>
  177. info what(Context& context) const
  178. {
  179. return info("in_state", subject.what(context));
  180. }
  181. Subject subject;
  182. State state;
  183. // silence MSVC warning C4512: assignment operator could not be generated
  184. BOOST_DELETED_FUNCTION(state_switcher_context& operator= (state_switcher_context const&))
  185. };
  186. ///////////////////////////////////////////////////////////////////////////
  187. // Parser generators: make_xxx function (objects)
  188. ///////////////////////////////////////////////////////////////////////////
  189. template <typename Modifiers, typename State>
  190. struct make_primitive<terminal_ex<tag::set_state, fusion::vector1<State> >
  191. , Modifiers, typename enable_if<traits::is_string<State> >::type>
  192. {
  193. typedef typename add_const<State>::type const_string;
  194. typedef state_switcher<const_string> result_type;
  195. template <typename Terminal>
  196. result_type operator()(Terminal const& term, unused_type) const
  197. {
  198. return result_type(traits::get_c_string(fusion::at_c<0>(term.args)));
  199. }
  200. };
  201. template <typename State, typename Subject, typename Modifiers>
  202. struct make_directive<terminal_ex<tag::in_state, fusion::vector1<State> >
  203. , Subject, Modifiers>
  204. {
  205. typedef typename add_const<State>::type const_string;
  206. typedef state_switcher_context<Subject, const_string> result_type;
  207. template <typename Terminal>
  208. result_type operator()(Terminal const& term, Subject const& subject
  209. , unused_type) const
  210. {
  211. return result_type(subject, fusion::at_c<0>(term.args));
  212. }
  213. };
  214. }}}
  215. namespace boost { namespace spirit { namespace traits
  216. {
  217. ///////////////////////////////////////////////////////////////////////////
  218. template <typename Subject, typename State>
  219. struct has_semantic_action<qi::state_switcher_context<Subject, State> >
  220. : unary_has_semantic_action<Subject> {};
  221. ///////////////////////////////////////////////////////////////////////////
  222. template <typename Subject, typename State, typename Attribute
  223. , typename Context, typename Iterator>
  224. struct handles_container<qi::state_switcher_context<Subject, State>
  225. , Attribute, Context, Iterator>
  226. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  227. }}}
  228. #endif