plain_token.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #if !defined(BOOST_SPIRIT_LEX_PLAIN_TOKEN_NOV_11_2007_0451PM)
  6. #define BOOST_SPIRIT_LEX_PLAIN_TOKEN_NOV_11_2007_0451PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/support/info.hpp>
  11. #include <boost/spirit/home/qi/detail/attributes.hpp>
  12. #include <boost/spirit/home/support/common_terminals.hpp>
  13. #include <boost/spirit/home/support/handles_container.hpp>
  14. #include <boost/spirit/home/qi/skip_over.hpp>
  15. #include <boost/spirit/home/qi/domain.hpp>
  16. #include <boost/spirit/home/qi/parser.hpp>
  17. #include <boost/spirit/home/qi/meta_compiler.hpp>
  18. #include <boost/spirit/home/qi/detail/assign_to.hpp>
  19. #include <boost/range/iterator_range.hpp>
  20. #include <boost/fusion/include/vector.hpp>
  21. #include <boost/fusion/include/at.hpp>
  22. #include <boost/mpl/or.hpp>
  23. #include <boost/mpl/and.hpp>
  24. #include <boost/type_traits/is_integral.hpp>
  25. #include <boost/type_traits/is_enum.hpp>
  26. #include <boost/lexical_cast.hpp>
  27. namespace boost { namespace spirit
  28. {
  29. ///////////////////////////////////////////////////////////////////////////
  30. // Enablers
  31. ///////////////////////////////////////////////////////////////////////////
  32. // enables token
  33. template <>
  34. struct use_terminal<qi::domain, tag::token>
  35. : mpl::true_ {};
  36. // enables token(id)
  37. template <typename A0>
  38. struct use_terminal<qi::domain
  39. , terminal_ex<tag::token, fusion::vector1<A0> >
  40. > : mpl::or_<is_integral<A0>, is_enum<A0> > {};
  41. // enables token(idmin, idmax)
  42. template <typename A0, typename A1>
  43. struct use_terminal<qi::domain
  44. , terminal_ex<tag::token, fusion::vector2<A0, A1> >
  45. > : mpl::and_<
  46. mpl::or_<is_integral<A0>, is_enum<A0> >
  47. , mpl::or_<is_integral<A1>, is_enum<A1> >
  48. > {};
  49. // enables *lazy* token(id)
  50. template <>
  51. struct use_lazy_terminal<
  52. qi::domain, tag::token, 1
  53. > : mpl::true_ {};
  54. // enables *lazy* token(idmin, idmax)
  55. template <>
  56. struct use_lazy_terminal<
  57. qi::domain, tag::token, 2
  58. > : mpl::true_ {};
  59. }}
  60. namespace boost { namespace spirit { namespace qi
  61. {
  62. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  63. using spirit::token;
  64. #endif
  65. using spirit::token_type;
  66. ///////////////////////////////////////////////////////////////////////////
  67. template <typename TokenId>
  68. struct plain_token
  69. : primitive_parser<plain_token<TokenId> >
  70. {
  71. template <typename Context, typename Iterator>
  72. struct attribute
  73. {
  74. typedef typename Iterator::base_iterator_type iterator_type;
  75. typedef iterator_range<iterator_type> type;
  76. };
  77. plain_token(TokenId const& id)
  78. : id(id) {}
  79. template <typename Iterator, typename Context
  80. , typename Skipper, typename Attribute>
  81. bool parse(Iterator& first, Iterator const& last
  82. , Context& /*context*/, Skipper const& skipper
  83. , Attribute& attr) const
  84. {
  85. qi::skip_over(first, last, skipper); // always do a pre-skip
  86. if (first != last) {
  87. // simply match the token id with the id this component has
  88. // been initialized with
  89. typedef typename
  90. boost::detail::iterator_traits<Iterator>::value_type
  91. token_type;
  92. typedef typename token_type::id_type id_type;
  93. token_type const& t = *first;
  94. if (id_type(~0) == id_type(id) || id_type(id) == t.id()) {
  95. spirit::traits::assign_to(t, attr);
  96. ++first;
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. template <typename Context>
  103. info what(Context& /*context*/) const
  104. {
  105. return info("token",
  106. "token(" + boost::lexical_cast<utf8_string>(id) + ")");
  107. }
  108. TokenId id;
  109. };
  110. ///////////////////////////////////////////////////////////////////////////
  111. template <typename TokenId>
  112. struct plain_token_range
  113. : primitive_parser<plain_token_range<TokenId> >
  114. {
  115. template <typename Context, typename Iterator>
  116. struct attribute
  117. {
  118. typedef typename Iterator::base_iterator_type iterator_type;
  119. typedef iterator_range<iterator_type> type;
  120. };
  121. plain_token_range(TokenId const& idmin, TokenId const& idmax)
  122. : idmin(idmin), idmax(idmax) {}
  123. template <typename Iterator, typename Context
  124. , typename Skipper, typename Attribute>
  125. bool parse(Iterator& first, Iterator const& last
  126. , Context& /*context*/, Skipper const& skipper
  127. , Attribute& attr) const
  128. {
  129. qi::skip_over(first, last, skipper); // always do a pre-skip
  130. if (first != last) {
  131. // simply match the token id with the id this component has
  132. // been initialized with
  133. typedef typename
  134. boost::detail::iterator_traits<Iterator>::value_type
  135. token_type;
  136. typedef typename token_type::id_type id_type;
  137. token_type const& t = *first;
  138. if (id_type(idmin) >= t.id() && id_type(idmin) <= t.id())
  139. {
  140. spirit::traits::assign_to(t, attr);
  141. ++first;
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. template <typename Context>
  148. info what(Context& /*context*/) const
  149. {
  150. return info("token_range"
  151. , "token(" +
  152. boost::lexical_cast<utf8_string>(idmin) + ", " +
  153. boost::lexical_cast<utf8_string>(idmax) + ")"
  154. );
  155. return info("token_range");
  156. }
  157. TokenId idmin, idmax;
  158. };
  159. ///////////////////////////////////////////////////////////////////////////
  160. // Parser generators: make_xxx function (objects)
  161. ///////////////////////////////////////////////////////////////////////////
  162. template <typename Modifiers>
  163. struct make_primitive<tag::token, Modifiers>
  164. {
  165. typedef plain_token<std::size_t> result_type;
  166. result_type operator()(unused_type, unused_type) const
  167. {
  168. return result_type(std::size_t(~0));
  169. }
  170. };
  171. template <typename Modifiers, typename TokenId>
  172. struct make_primitive<terminal_ex<tag::token, fusion::vector1<TokenId> >
  173. , Modifiers>
  174. {
  175. typedef plain_token<TokenId> result_type;
  176. template <typename Terminal>
  177. result_type operator()(Terminal const& term, unused_type) const
  178. {
  179. return result_type(fusion::at_c<0>(term.args));
  180. }
  181. };
  182. template <typename Modifiers, typename TokenId>
  183. struct make_primitive<terminal_ex<tag::token, fusion::vector2<TokenId, TokenId> >
  184. , Modifiers>
  185. {
  186. typedef plain_token_range<TokenId> result_type;
  187. template <typename Terminal>
  188. result_type operator()(Terminal const& term, unused_type) const
  189. {
  190. return result_type(fusion::at_c<0>(term.args)
  191. , fusion::at_c<1>(term.args));
  192. }
  193. };
  194. }}}
  195. namespace boost { namespace spirit { namespace traits
  196. {
  197. ///////////////////////////////////////////////////////////////////////////
  198. template<typename Idtype, typename Attr, typename Context, typename Iterator>
  199. struct handles_container<qi::plain_token<Idtype>, Attr, Context, Iterator>
  200. : mpl::true_
  201. {};
  202. template<typename Idtype, typename Attr, typename Context, typename Iterator>
  203. struct handles_container<qi::plain_token_range<Idtype>, Attr, Context, Iterator>
  204. : mpl::true_
  205. {};
  206. }}}
  207. #endif