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