char_generator.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include <boost/proto/operators.hpp>
  21. #include <boost/proto/tags.hpp>
  22. namespace boost { namespace spirit
  23. {
  24. ///////////////////////////////////////////////////////////////////////////
  25. // Enablers
  26. ///////////////////////////////////////////////////////////////////////////
  27. template <>
  28. struct use_operator<karma::domain, proto::tag::complement> // enables ~
  29. : mpl::true_ {};
  30. }}
  31. namespace boost { namespace spirit { namespace traits // classification
  32. {
  33. namespace detail
  34. {
  35. BOOST_MPL_HAS_XXX_TRAIT_DEF(char_generator_id)
  36. }
  37. template <typename T>
  38. struct is_char_generator : detail::has_char_generator_id<T> {};
  39. }}}
  40. namespace boost { namespace spirit { namespace karma
  41. {
  42. ///////////////////////////////////////////////////////////////////////////
  43. // The base char_parser
  44. ///////////////////////////////////////////////////////////////////////////
  45. template <typename Derived, typename CharEncoding, typename Tag
  46. , typename Char = typename CharEncoding::char_type, typename Attr = Char>
  47. struct char_generator : primitive_generator<Derived>
  48. {
  49. typedef CharEncoding char_encoding;
  50. typedef Tag tag;
  51. typedef Char char_type;
  52. struct char_generator_id;
  53. // if Attr is unused_type, Derived must supply its own attribute
  54. // metafunction
  55. template <typename Context, typename Unused>
  56. struct attribute
  57. {
  58. typedef Attr type;
  59. };
  60. template <
  61. typename OutputIterator, typename Context, typename Delimiter
  62. , typename Attribute>
  63. bool generate(OutputIterator& sink, Context& context, Delimiter const& d
  64. , Attribute const& attr) const
  65. {
  66. if (!traits::has_optional_value(attr))
  67. return false;
  68. Attr ch = Attr();
  69. if (!this->derived().test(traits::extract_from<Attr>(attr, context), ch, context))
  70. return false;
  71. return karma::detail::generate_to(sink, ch, char_encoding(), tag()) &&
  72. karma::delimit_out(sink, d); // always do post-delimiting
  73. }
  74. // Requirement: g.test(attr, ch, context) -> bool
  75. //
  76. // attr: associated attribute
  77. // ch: character to be generated (set by test())
  78. // context: enclosing rule context
  79. };
  80. ///////////////////////////////////////////////////////////////////////////
  81. // negated_char_generator handles ~cg expressions (cg is a char_generator)
  82. ///////////////////////////////////////////////////////////////////////////
  83. template <typename Positive>
  84. struct negated_char_generator
  85. : char_generator<negated_char_generator<Positive>
  86. , typename Positive::char_encoding, typename Positive::tag>
  87. {
  88. negated_char_generator(Positive const& positive)
  89. : positive(positive) {}
  90. template <typename Attribute, typename CharParam, typename Context>
  91. bool test(Attribute const& attr, CharParam& ch, Context& context) const
  92. {
  93. return !positive.test(attr, ch, context);
  94. }
  95. template <typename Context>
  96. info what(Context& context) const
  97. {
  98. return info("not", positive.what(context));
  99. }
  100. Positive positive;
  101. };
  102. ///////////////////////////////////////////////////////////////////////////
  103. // Generator generators: make_xxx function (objects)
  104. ///////////////////////////////////////////////////////////////////////////
  105. namespace detail
  106. {
  107. template <typename Positive>
  108. struct make_negated_char_generator
  109. {
  110. typedef negated_char_generator<Positive> result_type;
  111. result_type operator()(Positive const& positive) const
  112. {
  113. return result_type(positive);
  114. }
  115. };
  116. template <typename Positive>
  117. struct make_negated_char_generator<negated_char_generator<Positive> >
  118. {
  119. typedef Positive result_type;
  120. result_type operator()(negated_char_generator<Positive> const& ncg) const
  121. {
  122. return ncg.positive;
  123. }
  124. };
  125. }
  126. template <typename Elements, typename Modifiers>
  127. struct make_composite<proto::tag::complement, Elements, Modifiers>
  128. {
  129. typedef typename
  130. fusion::result_of::value_at_c<Elements, 0>::type
  131. subject;
  132. BOOST_SPIRIT_ASSERT_MSG((
  133. traits::is_char_generator<subject>::value
  134. ), subject_is_not_negatable, (subject));
  135. typedef typename
  136. detail::make_negated_char_generator<subject>::result_type
  137. result_type;
  138. result_type operator()(Elements const& elements, unused_type) const
  139. {
  140. return detail::make_negated_char_generator<subject>()(
  141. fusion::at_c<0>(elements));
  142. }
  143. };
  144. }}}
  145. #endif