char_parser.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  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. ==============================================================================*/
  6. #if !defined(BOOST_SPIRIT_CHAR_PARSER_APR_16_2006_0906AM)
  7. #define BOOST_SPIRIT_CHAR_PARSER_APR_16_2006_0906AM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/domain.hpp>
  12. #include <boost/spirit/home/qi/parser.hpp>
  13. #include <boost/spirit/home/qi/detail/assign_to.hpp>
  14. #include <boost/spirit/home/qi/meta_compiler.hpp>
  15. #include <boost/spirit/home/qi/skip_over.hpp>
  16. #include <boost/spirit/home/support/unused.hpp>
  17. #include <boost/spirit/home/support/info.hpp>
  18. namespace boost { namespace spirit
  19. {
  20. ///////////////////////////////////////////////////////////////////////////
  21. // Enablers
  22. ///////////////////////////////////////////////////////////////////////////
  23. template <>
  24. struct use_operator<qi::domain, proto::tag::complement> // enables ~
  25. : mpl::true_ {};
  26. }}
  27. namespace boost { namespace spirit { namespace traits // classification
  28. {
  29. namespace detail
  30. {
  31. BOOST_MPL_HAS_XXX_TRAIT_DEF(char_parser_id)
  32. }
  33. template <typename T>
  34. struct is_char_parser : detail::has_char_parser_id<T> {};
  35. }}}
  36. namespace boost { namespace spirit { namespace qi
  37. {
  38. ///////////////////////////////////////////////////////////////////////////
  39. // The base char_parser
  40. ///////////////////////////////////////////////////////////////////////////
  41. template <typename Derived, typename Char, typename Attr = Char>
  42. struct char_parser : primitive_parser<Derived>
  43. {
  44. typedef Char char_type;
  45. struct char_parser_id;
  46. // if Attr is unused_type, Derived must supply its own attribute
  47. // metafunction
  48. template <typename Context, typename Iterator>
  49. struct attribute
  50. {
  51. typedef Attr type;
  52. };
  53. template <typename Iterator, typename Context, typename Skipper, typename Attribute>
  54. bool parse(Iterator& first, Iterator const& last
  55. , Context& context, Skipper const& skipper, Attribute& attr_) const
  56. {
  57. qi::skip_over(first, last, skipper);
  58. if (first != last && this->derived().test(*first, context))
  59. {
  60. spirit::traits::assign_to(*first, attr_);
  61. ++first;
  62. return true;
  63. }
  64. return false;
  65. }
  66. // Requirement: p.test(ch, context) -> bool
  67. //
  68. // ch: character being parsed
  69. // context: enclosing rule context
  70. };
  71. ///////////////////////////////////////////////////////////////////////////
  72. // negated_char_parser handles ~cp expressions (cp is a char_parser)
  73. ///////////////////////////////////////////////////////////////////////////
  74. template <typename Positive>
  75. struct negated_char_parser :
  76. char_parser<negated_char_parser<Positive>, typename Positive::char_type>
  77. {
  78. negated_char_parser(Positive const& positive_)
  79. : positive(positive_) {}
  80. template <typename CharParam, typename Context>
  81. bool test(CharParam ch, Context& context) const
  82. {
  83. return !positive.test(ch, context);
  84. }
  85. template <typename Context>
  86. info what(Context& context) const
  87. {
  88. return info("not", positive.what(context));
  89. }
  90. Positive positive;
  91. };
  92. ///////////////////////////////////////////////////////////////////////////
  93. // Parser generators: make_xxx function (objects)
  94. ///////////////////////////////////////////////////////////////////////////
  95. namespace detail
  96. {
  97. template <typename Positive>
  98. struct make_negated_char_parser
  99. {
  100. typedef negated_char_parser<Positive> result_type;
  101. result_type operator()(Positive const& positive) const
  102. {
  103. return result_type(positive);
  104. }
  105. };
  106. template <typename Positive>
  107. struct make_negated_char_parser<negated_char_parser<Positive> >
  108. {
  109. typedef Positive result_type;
  110. result_type operator()(negated_char_parser<Positive> const& ncp) const
  111. {
  112. return ncp.positive;
  113. }
  114. };
  115. }
  116. template <typename Elements, typename Modifiers>
  117. struct make_composite<proto::tag::complement, Elements, Modifiers>
  118. {
  119. typedef typename
  120. fusion::result_of::value_at_c<Elements, 0>::type
  121. subject;
  122. BOOST_SPIRIT_ASSERT_MSG((
  123. traits::is_char_parser<subject>::value
  124. ), subject_is_not_negatable, (subject));
  125. typedef typename
  126. detail::make_negated_char_parser<subject>::result_type
  127. result_type;
  128. result_type operator()(Elements const& elements, unused_type) const
  129. {
  130. return detail::make_negated_char_parser<subject>()(
  131. fusion::at_c<0>(elements));
  132. }
  133. };
  134. }}}
  135. #endif