plain_tokenid_mask.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_MASK_JUN_03_2011_0929PM)
  6. #define BOOST_SPIRIT_LEX_PLAIN_TOKENID_MASK_JUN_03_2011_0929PM
  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 tokenid_mask(id)
  33. template <typename A0>
  34. struct use_terminal<qi::domain
  35. , terminal_ex<tag::tokenid_mask, fusion::vector1<A0> >
  36. > : mpl::or_<is_integral<A0>, is_enum<A0> > {};
  37. // enables *lazy* tokenid_mask(id)
  38. template <>
  39. struct use_lazy_terminal<
  40. qi::domain, tag::tokenid_mask, 1
  41. > : mpl::true_ {};
  42. }}
  43. namespace boost { namespace spirit { namespace qi
  44. {
  45. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  46. using spirit::tokenid_mask;
  47. #endif
  48. using spirit::tokenid_mask_type;
  49. ///////////////////////////////////////////////////////////////////////////
  50. // The plain_tokenid represents a simple token defined by the lexer inside
  51. // a Qi grammar. The difference to plain_token is that it exposes the
  52. // matched token id instead of the iterator_range of the matched input.
  53. // Additionally it applies the given mask to the matched token id.
  54. template <typename Mask>
  55. struct plain_tokenid_mask
  56. : primitive_parser<plain_tokenid_mask<Mask> >
  57. {
  58. template <typename Context, typename Iterator>
  59. struct attribute
  60. {
  61. typedef Mask type;
  62. };
  63. plain_tokenid_mask(Mask const& mask)
  64. : mask(mask) {}
  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 mask 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 ((t.id() & mask) == id_type(mask))
  81. {
  82. spirit::traits::assign_to(t.id(), attr);
  83. ++first;
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. template <typename Context>
  90. info what(Context& /*context*/) const
  91. {
  92. return info("tokenid_mask",
  93. "tokenid_mask(" + boost::lexical_cast<utf8_string>(mask) + ")");
  94. }
  95. Mask mask;
  96. };
  97. ///////////////////////////////////////////////////////////////////////////
  98. // Parser generators: make_xxx function (objects)
  99. ///////////////////////////////////////////////////////////////////////////
  100. template <typename Modifiers, typename Mask>
  101. struct make_primitive<terminal_ex<tag::tokenid_mask, fusion::vector1<Mask> >
  102. , Modifiers>
  103. {
  104. typedef plain_tokenid_mask<Mask> result_type;
  105. template <typename Terminal>
  106. result_type operator()(Terminal const& term, unused_type) const
  107. {
  108. return result_type(fusion::at_c<0>(term.args));
  109. }
  110. };
  111. }}}
  112. namespace boost { namespace spirit { namespace traits
  113. {
  114. ///////////////////////////////////////////////////////////////////////////
  115. template<typename Mask, typename Attr, typename Context, typename Iterator>
  116. struct handles_container<qi::plain_tokenid_mask<Mask>, Attr, Context, Iterator>
  117. : mpl::true_
  118. {};
  119. }}}
  120. #endif