plain_raw_token.hpp 4.8 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/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/type_traits/is_integral.hpp>
  24. #include <boost/type_traits/is_enum.hpp>
  25. #include <boost/lexical_cast.hpp>
  26. #include <iterator> // for std::iterator_traits
  27. namespace boost { namespace spirit
  28. {
  29. ///////////////////////////////////////////////////////////////////////////
  30. // Enablers
  31. ///////////////////////////////////////////////////////////////////////////
  32. // enables raw_token
  33. template <>
  34. struct use_terminal<qi::domain, tag::raw_token>
  35. : mpl::true_ {};
  36. // enables raw_token(id)
  37. template <typename A0>
  38. struct use_terminal<qi::domain
  39. , terminal_ex<tag::raw_token, fusion::vector1<A0> >
  40. > : mpl::or_<is_integral<A0>, is_enum<A0> > {};
  41. // enables *lazy* raw_token(id)
  42. template <>
  43. struct use_lazy_terminal<
  44. qi::domain, tag::raw_token, 1
  45. > : mpl::true_ {};
  46. }}
  47. namespace boost { namespace spirit { namespace qi
  48. {
  49. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  50. using spirit::raw_token;
  51. #endif
  52. using spirit::raw_token_type;
  53. ///////////////////////////////////////////////////////////////////////////
  54. template <typename TokenId>
  55. struct plain_raw_token
  56. : primitive_parser<plain_raw_token<TokenId> >
  57. {
  58. template <typename Context, typename Iterator>
  59. struct attribute
  60. {
  61. typedef unused_type type;
  62. };
  63. plain_raw_token(TokenId const& id)
  64. : id(id) {}
  65. template <typename Iterator, typename Context
  66. , typename Skipper, typename Attribute>
  67. bool parse(Iterator& first, Iterator const& last
  68. , Context& /*context*/, Skipper const& skipper
  69. , Attribute& attr) const
  70. {
  71. qi::skip_over(first, last, skipper); // always do a pre-skip
  72. if (first != last) {
  73. // simply match the token id with the id this component has
  74. // been initialized with
  75. typedef typename
  76. std::iterator_traits<Iterator>::value_type
  77. token_type;
  78. typedef typename token_type::id_type id_type;
  79. token_type const& t = *first;
  80. if (id_type(~0) == id_type(id) || id_type(id) == t.id()) {
  81. spirit::traits::assign_to(t, attr);
  82. ++first;
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. template <typename Context>
  89. info what(Context& /*context*/) const
  90. {
  91. return info("raw_token",
  92. "raw_token(" + boost::lexical_cast<utf8_string>(id) + ")");
  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