char_parser.hpp 5.2 KB

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