9
3

delimit.hpp 7.3 KB

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