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