9
3

verbatim.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_VERBATIM_MAR_02_2007_0303PM)
  6. #define BOOST_SPIRIT_KARMA_VERBATIM_MAR_02_2007_0303PM
  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. namespace boost { namespace spirit
  23. {
  24. ///////////////////////////////////////////////////////////////////////////
  25. // Enablers
  26. ///////////////////////////////////////////////////////////////////////////
  27. template <>
  28. struct use_directive<karma::domain, tag::verbatim> // enables verbatim[]
  29. : mpl::true_ {};
  30. }}
  31. namespace boost { namespace spirit { namespace karma
  32. {
  33. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  34. using spirit::verbatim;
  35. #endif
  36. using spirit::verbatim_type;
  37. ///////////////////////////////////////////////////////////////////////////
  38. // The verbatim generator is used for verbatim[...] directives.
  39. ///////////////////////////////////////////////////////////////////////////
  40. template <typename Subject>
  41. struct verbatim_generator : unary_generator<verbatim_generator<Subject> >
  42. {
  43. typedef Subject subject_type;
  44. typedef typename subject_type::properties properties;
  45. template <typename Context, typename Iterator>
  46. struct attribute
  47. : traits::attribute_of<subject_type, Context, Iterator>
  48. {};
  49. verbatim_generator(Subject const& subject)
  50. : subject(subject) {}
  51. template <typename OutputIterator, typename Context, typename Delimiter
  52. , typename Attribute>
  53. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  54. , Attribute const& attr) const
  55. {
  56. // the verbatim generator simply dispatches to the embedded
  57. // generator while supplying unused_delimiter as the new delimiter
  58. // to avoid delimiting down the generator stream
  59. typedef detail::unused_delimiter<Delimiter> unused_delimiter;
  60. return subject.generate(sink, ctx, unused_delimiter(d), attr) &&
  61. karma::delimit_out(sink, d); // always do post-delimiting
  62. }
  63. template <typename Context>
  64. info what(Context& context) const
  65. {
  66. return info("verbatim", subject.what(context));
  67. }
  68. Subject subject;
  69. };
  70. ///////////////////////////////////////////////////////////////////////////
  71. // Generator generators: make_xxx function (objects)
  72. ///////////////////////////////////////////////////////////////////////////
  73. template <typename Subject, typename Modifiers>
  74. struct make_directive<tag::verbatim, Subject, Modifiers>
  75. {
  76. typedef verbatim_generator<Subject> result_type;
  77. result_type operator()(unused_type, Subject const& subject, unused_type) const
  78. {
  79. return result_type(subject);
  80. }
  81. };
  82. }}}
  83. namespace boost { namespace spirit { namespace traits
  84. {
  85. ///////////////////////////////////////////////////////////////////////////
  86. template <typename Subject>
  87. struct has_semantic_action<karma::verbatim_generator<Subject> >
  88. : unary_has_semantic_action<Subject> {};
  89. ///////////////////////////////////////////////////////////////////////////
  90. template <typename Subject, typename Attribute, typename Context
  91. , typename Iterator>
  92. struct handles_container<karma::verbatim_generator<Subject>, Attribute
  93. , Context, Iterator>
  94. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  95. }}}
  96. #endif