/Src/Dependencies/Boost/boost/spirit/home/lex/qi/state_switcher.hpp

http://hadesmem.googlecode.com/ · C++ Header · 270 lines · 191 code · 40 blank · 39 comment · 1 complexity · 32f6744b2a33677226cd9bc6ee257ffe MD5 · raw file

  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. private:
  134. // silence MSVC warning C4512: assignment operator could not be generated
  135. reset_state_on_exit& operator= (reset_state_on_exit const&);
  136. };
  137. }
  138. ///////////////////////////////////////////////////////////////////////////
  139. // Parser, which switches the state of the underlying lexer component
  140. // for the execution of the embedded sub-parser, switching the state back
  141. // afterwards. This parser gets used for the in_state(...)[p] construct.
  142. ///////////////////////////////////////////////////////////////////////////
  143. template <typename Subject, typename State>
  144. struct state_switcher_context
  145. : unary_parser<state_switcher_context<Subject, State> >
  146. {
  147. typedef Subject subject_type;
  148. typedef typename traits::char_type_of<State>::type char_type;
  149. typedef typename remove_const<char_type>::type non_const_char_type;
  150. template <typename Context, typename Iterator>
  151. struct attribute
  152. {
  153. typedef typename
  154. traits::attribute_of<subject_type, Context, Iterator>::type
  155. type;
  156. };
  157. state_switcher_context(Subject const& subject
  158. , typename add_reference<State>::type state)
  159. : subject(subject), state(state) {}
  160. // The following conversion constructors are needed to make the
  161. // in_state_switcher template usable
  162. template <typename String>
  163. state_switcher_context(
  164. state_switcher_context<Subject, String> const& rhs)
  165. : subject(rhs.subject), state(traits::get_c_string(rhs.state)) {}
  166. template <typename Iterator, typename Context
  167. , typename Skipper, typename Attribute>
  168. bool parse(Iterator& first, Iterator const& last
  169. , Context& context, Skipper const& skipper
  170. , Attribute& attr) const
  171. {
  172. qi::skip_over(first, last, skipper); // always do a pre-skip
  173. // the state has to be reset at exit in any case
  174. detail::reset_state_on_exit<Iterator> guard(first, state);
  175. return subject.parse(first, last, context, skipper, attr);
  176. }
  177. template <typename Context>
  178. info what(Context& context) const
  179. {
  180. return info("in_state", subject.what(context));
  181. }
  182. Subject subject;
  183. State state;
  184. private:
  185. // silence MSVC warning C4512: assignment operator could not be generated
  186. state_switcher_context& operator= (state_switcher_context const&);
  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