plain_token.hpp 7.8 KB

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