eps.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_EPS_APRIL_21_2007_0246PM)
  6. #define BOOST_SPIRIT_KARMA_EPS_APRIL_21_2007_0246PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/support/common_terminals.hpp>
  11. #include <boost/spirit/home/support/info.hpp>
  12. #include <boost/spirit/home/karma/domain.hpp>
  13. #include <boost/spirit/home/karma/meta_compiler.hpp>
  14. #include <boost/spirit/home/karma/delimit_out.hpp>
  15. #include <boost/spirit/home/support/unused.hpp>
  16. #include <boost/fusion/include/at.hpp>
  17. namespace boost { namespace spirit
  18. {
  19. ///////////////////////////////////////////////////////////////////////////
  20. // Enablers
  21. ///////////////////////////////////////////////////////////////////////////
  22. // enables eps
  23. template <>
  24. struct use_terminal<karma::domain, tag::eps>
  25. : mpl::true_ {};
  26. // enables eps(bool-condition)
  27. template <typename A0>
  28. struct use_terminal<karma::domain
  29. , terminal_ex<tag::eps, fusion::vector1<A0> > >
  30. : is_convertible<A0, bool> {};
  31. // enables lazy eps(f)
  32. template <>
  33. struct use_lazy_terminal<karma::domain, tag::eps, 1>
  34. : mpl::true_ {};
  35. }}
  36. ///////////////////////////////////////////////////////////////////////////////
  37. namespace boost { namespace spirit { namespace karma
  38. {
  39. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  40. using boost::spirit::eps;
  41. #endif
  42. using boost::spirit::eps_type;
  43. struct eps_generator : primitive_generator<eps_generator>
  44. {
  45. template <typename Context, typename Unused>
  46. struct attribute
  47. {
  48. typedef unused_type type;
  49. };
  50. template <
  51. typename OutputIterator, typename Context, typename Delimiter
  52. , typename Attribute>
  53. static bool generate(OutputIterator& sink, Context&, Delimiter const& d
  54. , Attribute const& /*attr*/)
  55. {
  56. return karma::delimit_out(sink, d); // always do post-delimiting
  57. }
  58. template <typename Context>
  59. info what(Context const& /*context*/) const
  60. {
  61. return info("eps");
  62. }
  63. };
  64. struct semantic_predicate : primitive_generator<semantic_predicate>
  65. {
  66. template <typename Context, typename Unused>
  67. struct attribute
  68. {
  69. typedef unused_type type;
  70. };
  71. semantic_predicate(bool predicate)
  72. : predicate_(predicate)
  73. {}
  74. template <
  75. typename OutputIterator, typename Context, typename Delimiter
  76. , typename Attribute>
  77. bool generate(OutputIterator& sink, Context&, Delimiter const& d
  78. , Attribute const& /*attr*/) const
  79. {
  80. // only do post-delimiting when predicate is true
  81. return predicate_ && karma::delimit_out(sink, d);
  82. }
  83. template <typename Context>
  84. info what(Context const& /*context*/) const
  85. {
  86. return info("semantic-predicate");
  87. }
  88. bool predicate_;
  89. };
  90. ///////////////////////////////////////////////////////////////////////////
  91. // Generator generators: make_xxx function (objects)
  92. ///////////////////////////////////////////////////////////////////////////
  93. template <typename Modifiers>
  94. struct make_primitive<tag::eps, Modifiers>
  95. {
  96. typedef eps_generator result_type;
  97. result_type operator()(unused_type, unused_type) const
  98. {
  99. return result_type();
  100. }
  101. };
  102. template <typename Modifiers, typename A0>
  103. struct make_primitive<
  104. terminal_ex<tag::eps, fusion::vector1<A0> >
  105. , Modifiers>
  106. {
  107. typedef semantic_predicate result_type;
  108. template <typename Terminal>
  109. result_type operator()(Terminal const& term, unused_type) const
  110. {
  111. return result_type(fusion::at_c<0>(term.args));
  112. }
  113. };
  114. }}}
  115. #endif