delimit.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_KARMA_DELIMIT_MAR_02_2007_0217PM)
  6. #define BOOST_SPIRIT_KARMA_DELIMIT_MAR_02_2007_0217PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/karma/meta_compiler.hpp>
  11. #include <boost/spirit/home/karma/generator.hpp>
  12. #include <boost/spirit/home/karma/domain.hpp>
  13. #include <boost/spirit/home/karma/detail/unused_delimiter.hpp>
  14. #include <boost/spirit/home/karma/delimit_out.hpp>
  15. #include <boost/spirit/home/karma/auxiliary/lazy.hpp>
  16. #include <boost/spirit/home/karma/char/char.hpp>
  17. #include <boost/spirit/home/support/unused.hpp>
  18. #include <boost/spirit/home/support/common_terminals.hpp>
  19. #include <boost/spirit/home/support/has_semantic_action.hpp>
  20. #include <boost/spirit/home/support/handles_container.hpp>
  21. #include <boost/spirit/home/karma/detail/attributes.hpp>
  22. #include <boost/spirit/home/support/info.hpp>
  23. #include <boost/fusion/include/at.hpp>
  24. #include <boost/fusion/include/vector.hpp>
  25. namespace boost { namespace spirit
  26. {
  27. ///////////////////////////////////////////////////////////////////////////
  28. // Enablers
  29. ///////////////////////////////////////////////////////////////////////////
  30. template <>
  31. struct use_directive<karma::domain, tag::delimit> // enables delimit[]
  32. : mpl::true_ {};
  33. // enables delimit(d)[g], where d is a generator
  34. template <typename T>
  35. struct use_directive<karma::domain
  36. , terminal_ex<tag::delimit, fusion::vector1<T> > >
  37. : boost::spirit::traits::matches<karma::domain, T> {};
  38. // enables *lazy* delimit(d)[g]
  39. template <>
  40. struct use_lazy_directive<karma::domain, tag::delimit, 1>
  41. : mpl::true_ {};
  42. }}
  43. namespace boost { namespace spirit { namespace karma
  44. {
  45. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  46. using spirit::delimit;
  47. #endif
  48. using spirit::delimit_type;
  49. ///////////////////////////////////////////////////////////////////////////
  50. // The redelimit_generator generator is used for delimit[...] directives.
  51. ///////////////////////////////////////////////////////////////////////////
  52. template <typename Subject>
  53. struct redelimit_generator : unary_generator<redelimit_generator<Subject> >
  54. {
  55. typedef Subject subject_type;
  56. typedef typename subject_type::properties properties;
  57. template <typename Context, typename Iterator>
  58. struct attribute
  59. : traits::attribute_of<subject_type, Context, Iterator>
  60. {};
  61. redelimit_generator(Subject const& subject)
  62. : subject(subject) {}
  63. template <typename OutputIterator, typename Context, typename Delimiter
  64. , typename Attribute>
  65. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  66. , Attribute const& attr) const
  67. {
  68. // The delimit_space generator simply dispatches to the embedded
  69. // generator while supplying either the delimiter which has been
  70. // used before a surrounding verbatim[] directive or a single
  71. // space as the new delimiter to use (if no surrounding verbatim[]
  72. // was specified).
  73. return subject.generate(sink, ctx
  74. , detail::get_delimiter(d, compile<karma::domain>(' ')), attr);
  75. }
  76. template <typename Context>
  77. info what(Context& context) const
  78. {
  79. return info("delimit", subject.what(context));
  80. }
  81. Subject subject;
  82. };
  83. ///////////////////////////////////////////////////////////////////////////
  84. // The delimit_generator is used for delimit(d)[...] directives.
  85. ///////////////////////////////////////////////////////////////////////////
  86. template <typename Subject, typename Delimiter>
  87. struct delimit_generator
  88. : unary_generator<delimit_generator<Subject, Delimiter> >
  89. {
  90. typedef Subject subject_type;
  91. typedef Delimiter delimiter_type;
  92. typedef typename subject_type::properties properties;
  93. template <typename Context, typename Iterator>
  94. struct attribute
  95. : traits::attribute_of<subject_type, Context, Iterator>
  96. {};
  97. delimit_generator(Subject const& subject, Delimiter const& delimiter)
  98. : subject(subject), delimiter(delimiter) {}
  99. template <typename OutputIterator, typename Context
  100. , typename Delimiter_, typename Attribute>
  101. bool generate(OutputIterator& sink, Context& ctx, Delimiter_ const&
  102. , Attribute const& attr) const
  103. {
  104. // the delimit generator simply dispatches to the embedded
  105. // generator while supplying it's argument as the new delimiter
  106. // to use
  107. return subject.generate(sink, ctx, delimiter, attr);
  108. }
  109. template <typename Context>
  110. info what(Context& context) const
  111. {
  112. return info("delimit", subject.what(context));
  113. }
  114. Subject subject;
  115. Delimiter delimiter;
  116. };
  117. ///////////////////////////////////////////////////////////////////////////
  118. // Generator generators: make_xxx function (objects)
  119. ///////////////////////////////////////////////////////////////////////////
  120. template <typename Subject, typename Modifiers>
  121. struct make_directive<tag::delimit, Subject, Modifiers>
  122. {
  123. typedef redelimit_generator<Subject> result_type;
  124. result_type operator()(unused_type, Subject const& subject
  125. , unused_type) const
  126. {
  127. return result_type(subject);
  128. }
  129. };
  130. template <typename Delimiter, typename Subject, typename Modifiers>
  131. struct make_directive<
  132. terminal_ex<tag::delimit, fusion::vector1<Delimiter> >
  133. , Subject, Modifiers>
  134. {
  135. typedef typename
  136. result_of::compile<karma::domain, Delimiter, Modifiers>::type
  137. delimiter_type;
  138. typedef delimit_generator<Subject, delimiter_type> result_type;
  139. template <typename Terminal>
  140. result_type operator()(Terminal const& term, Subject const& subject
  141. , unused_type) const
  142. {
  143. return result_type(subject
  144. , compile<karma::domain>(fusion::at_c<0>(term.args)));
  145. }
  146. };
  147. }}}
  148. namespace boost { namespace spirit { namespace traits
  149. {
  150. ///////////////////////////////////////////////////////////////////////////
  151. template <typename Subject>
  152. struct has_semantic_action<karma::redelimit_generator<Subject> >
  153. : unary_has_semantic_action<Subject> {};
  154. template <typename Subject, typename Delimiter>
  155. struct has_semantic_action<karma::delimit_generator<Subject, Delimiter> >
  156. : unary_has_semantic_action<Subject> {};
  157. ///////////////////////////////////////////////////////////////////////////
  158. template <typename Subject, typename Attribute
  159. , typename Context, typename Iterator>
  160. struct handles_container<karma::redelimit_generator<Subject>, Attribute
  161. , Context, Iterator>
  162. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  163. template <typename Subject, typename Delimiter, typename Attribute
  164. , typename Context, typename Iterator>
  165. struct handles_container<karma::delimit_generator<Subject, Delimiter>
  166. , Attribute, Context, Iterator>
  167. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  168. }}}
  169. #endif