char_generator.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2001-2011 Joel de Guzman
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(BOOST_SPIRIT_CHAR_GENERATOR_SEP_07_2009_0417PM)
  7. #define BOOST_SPIRIT_CHAR_GENERATOR_SEP_07_2009_0417PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/karma/domain.hpp>
  12. #include <boost/spirit/home/karma/generator.hpp>
  13. #include <boost/spirit/home/karma/detail/generate_to.hpp>
  14. #include <boost/spirit/home/karma/detail/extract_from.hpp>
  15. #include <boost/spirit/home/karma/meta_compiler.hpp>
  16. #include <boost/spirit/home/karma/delimit_out.hpp>
  17. #include <boost/spirit/home/support/unused.hpp>
  18. #include <boost/spirit/home/support/info.hpp>
  19. #include <boost/spirit/home/support/container.hpp>
  20. namespace boost { namespace spirit
  21. {
  22. ///////////////////////////////////////////////////////////////////////////
  23. // Enablers
  24. ///////////////////////////////////////////////////////////////////////////
  25. template <>
  26. struct use_operator<karma::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_generator_id)
  34. }
  35. template <typename T>
  36. struct is_char_generator : detail::has_char_generator_id<T> {};
  37. }}}
  38. namespace boost { namespace spirit { namespace karma
  39. {
  40. ///////////////////////////////////////////////////////////////////////////
  41. // The base char_parser
  42. ///////////////////////////////////////////////////////////////////////////
  43. template <typename Derived, typename CharEncoding, typename Tag
  44. , typename Char = typename CharEncoding::char_type, typename Attr = Char>
  45. struct char_generator : primitive_generator<Derived>
  46. {
  47. typedef CharEncoding char_encoding;
  48. typedef Tag tag;
  49. typedef Char char_type;
  50. struct char_generator_id;
  51. // if Attr is unused_type, Derived must supply its own attribute
  52. // metafunction
  53. template <typename Context, typename Unused>
  54. struct attribute
  55. {
  56. typedef Attr type;
  57. };
  58. template <
  59. typename OutputIterator, typename Context, typename Delimiter
  60. , typename Attribute>
  61. bool generate(OutputIterator& sink, Context& context, Delimiter const& d
  62. , Attribute const& attr) const
  63. {
  64. if (!traits::has_optional_value(attr))
  65. return false;
  66. Attr ch = Attr();
  67. if (!this->derived().test(traits::extract_from<Attr>(attr, context), ch, context))
  68. return false;
  69. return karma::detail::generate_to(sink, ch, char_encoding(), tag()) &&
  70. karma::delimit_out(sink, d); // always do post-delimiting
  71. }
  72. // Requirement: g.test(attr, ch, context) -> bool
  73. //
  74. // attr: associated attribute
  75. // ch: character to be generated (set by test())
  76. // context: enclosing rule context
  77. };
  78. ///////////////////////////////////////////////////////////////////////////
  79. // negated_char_generator handles ~cg expressions (cg is a char_generator)
  80. ///////////////////////////////////////////////////////////////////////////
  81. template <typename Positive>
  82. struct negated_char_generator
  83. : char_generator<negated_char_generator<Positive>
  84. , typename Positive::char_encoding, typename Positive::tag>
  85. {
  86. negated_char_generator(Positive const& positive)
  87. : positive(positive) {}
  88. template <typename Attribute, typename CharParam, typename Context>
  89. bool test(Attribute const& attr, CharParam& ch, Context& context) const
  90. {
  91. return !positive.test(attr, ch, context);
  92. }
  93. template <typename Context>
  94. info what(Context& context) const
  95. {
  96. return info("not", positive.what(context));
  97. }
  98. Positive positive;
  99. };
  100. ///////////////////////////////////////////////////////////////////////////
  101. // Generator generators: make_xxx function (objects)
  102. ///////////////////////////////////////////////////////////////////////////
  103. namespace detail
  104. {
  105. template <typename Positive>
  106. struct make_negated_char_generator
  107. {
  108. typedef negated_char_generator<Positive> result_type;
  109. result_type operator()(Positive const& positive) const
  110. {
  111. return result_type(positive);
  112. }
  113. };
  114. template <typename Positive>
  115. struct make_negated_char_generator<negated_char_generator<Positive> >
  116. {
  117. typedef Positive result_type;
  118. result_type operator()(negated_char_generator<Positive> const& ncg) const
  119. {
  120. return ncg.positive;
  121. }
  122. };
  123. }
  124. template <typename Elements, typename Modifiers>
  125. struct make_composite<proto::tag::complement, Elements, Modifiers>
  126. {
  127. typedef typename
  128. fusion::result_of::value_at_c<Elements, 0>::type
  129. subject;
  130. BOOST_SPIRIT_ASSERT_MSG((
  131. traits::is_char_generator<subject>::value
  132. ), subject_is_not_negatable, (subject));
  133. typedef typename
  134. detail::make_negated_char_generator<subject>::result_type
  135. result_type;
  136. result_type operator()(Elements const& elements, unused_type) const
  137. {
  138. return detail::make_negated_char_generator<subject>()(
  139. fusion::at_c<0>(elements));
  140. }
  141. };
  142. }}}
  143. #endif