plain_raw_token.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_RAW_TOKEN_JUN_03_2011_0853PM)
  6. #define BOOST_SPIRIT_LEX_PLAIN_RAW_TOKEN_JUN_03_2011_0853PM
  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/type_traits/is_integral.hpp>
  23. #include <boost/type_traits/is_enum.hpp>
  24. #include <iterator> // for std::iterator_traits
  25. #include <sstream>
  26. namespace boost { namespace spirit
  27. {
  28. ///////////////////////////////////////////////////////////////////////////
  29. // Enablers
  30. ///////////////////////////////////////////////////////////////////////////
  31. // enables raw_token
  32. template <>
  33. struct use_terminal<qi::domain, tag::raw_token>
  34. : mpl::true_ {};
  35. // enables raw_token(id)
  36. template <typename A0>
  37. struct use_terminal<qi::domain
  38. , terminal_ex<tag::raw_token, fusion::vector1<A0> >
  39. > : mpl::or_<is_integral<A0>, is_enum<A0> > {};
  40. // enables *lazy* raw_token(id)
  41. template <>
  42. struct use_lazy_terminal<
  43. qi::domain, tag::raw_token, 1
  44. > : mpl::true_ {};
  45. }}
  46. namespace boost { namespace spirit { namespace qi
  47. {
  48. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  49. using spirit::raw_token;
  50. #endif
  51. using spirit::raw_token_type;
  52. ///////////////////////////////////////////////////////////////////////////
  53. template <typename TokenId>
  54. struct plain_raw_token
  55. : primitive_parser<plain_raw_token<TokenId> >
  56. {
  57. template <typename Context, typename Iterator>
  58. struct attribute
  59. {
  60. typedef unused_type type;
  61. };
  62. plain_raw_token(TokenId const& id)
  63. : id(id) {}
  64. template <typename Iterator, typename Context
  65. , typename Skipper, typename Attribute>
  66. bool parse(Iterator& first, Iterator const& last
  67. , Context& /*context*/, Skipper const& skipper
  68. , Attribute& attr) const
  69. {
  70. qi::skip_over(first, last, skipper); // always do a pre-skip
  71. if (first != last) {
  72. // simply match the token id with the id this component has
  73. // been initialized with
  74. typedef typename
  75. std::iterator_traits<Iterator>::value_type
  76. token_type;
  77. typedef typename token_type::id_type id_type;
  78. token_type const& t = *first;
  79. if (id_type(~0) == id_type(id) || id_type(id) == t.id()) {
  80. spirit::traits::assign_to(t, attr);
  81. ++first;
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. template <typename Context>
  88. info what(Context& /*context*/) const
  89. {
  90. std::stringstream ss;
  91. ss << "raw_token(" << id << ")";
  92. return info("raw_token", ss.str());
  93. }
  94. TokenId id;
  95. };
  96. ///////////////////////////////////////////////////////////////////////////
  97. // Parser generators: make_xxx function (objects)
  98. ///////////////////////////////////////////////////////////////////////////
  99. template <typename Modifiers>
  100. struct make_primitive<tag::raw_token, Modifiers>
  101. {
  102. typedef plain_raw_token<std::size_t> result_type;
  103. result_type operator()(unused_type, unused_type) const
  104. {
  105. return result_type(std::size_t(~0));
  106. }
  107. };
  108. template <typename Modifiers, typename TokenId>
  109. struct make_primitive<terminal_ex<tag::raw_token, fusion::vector1<TokenId> >
  110. , Modifiers>
  111. {
  112. typedef plain_raw_token<TokenId> result_type;
  113. template <typename Terminal>
  114. result_type operator()(Terminal const& term, unused_type) const
  115. {
  116. return result_type(fusion::at_c<0>(term.args));
  117. }
  118. };
  119. }}}
  120. namespace boost { namespace spirit { namespace traits
  121. {
  122. ///////////////////////////////////////////////////////////////////////////
  123. template<typename Idtype, typename Attr, typename Context, typename Iterator>
  124. struct handles_container<qi::plain_raw_token<Idtype>, Attr, Context, Iterator>
  125. : mpl::true_
  126. {};
  127. }}}
  128. #endif