lazy.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_KARMA_LAZY_MARCH_27_2007_1231PM)
  7. #define BOOST_SPIRIT_KARMA_LAZY_MARCH_27_2007_1231PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/karma/domain.hpp>
  12. #include <boost/spirit/home/karma/delimit_out.hpp>
  13. #include <boost/spirit/home/karma/meta_compiler.hpp>
  14. #include <boost/spirit/home/karma/detail/attributes.hpp>
  15. #include <boost/spirit/home/support/unused.hpp>
  16. #include <boost/spirit/home/support/info.hpp>
  17. #include <boost/spirit/home/support/lazy.hpp>
  18. #include <boost/fusion/include/at.hpp>
  19. #include <boost/utility/result_of.hpp>
  20. #include <boost/proto/make_expr.hpp>
  21. #include <boost/type_traits/remove_reference.hpp>
  22. #include <boost/mpl/not.hpp>
  23. namespace boost { namespace phoenix
  24. {
  25. template <typename Expr>
  26. struct actor;
  27. }}
  28. namespace boost { namespace spirit
  29. {
  30. ///////////////////////////////////////////////////////////////////////////
  31. // Enablers
  32. ///////////////////////////////////////////////////////////////////////////
  33. template <typename Eval>
  34. struct use_terminal<karma::domain, phoenix::actor<Eval> > // enables phoenix actors
  35. : mpl::true_ {};
  36. // forward declaration
  37. template <typename Terminal, typename Actor, int Arity>
  38. struct lazy_terminal;
  39. }}
  40. namespace boost { namespace spirit { namespace karma
  41. {
  42. using spirit::lazy;
  43. typedef modify<karma::domain> karma_modify;
  44. namespace detail
  45. {
  46. template <typename Generator, typename OutputIterator, typename Context
  47. , typename Delimiter, typename Attribute>
  48. bool lazy_generate_impl(Generator const& g, OutputIterator& sink
  49. , Context& context, Delimiter const& delim
  50. , Attribute const& attr, mpl::false_)
  51. {
  52. return g.generate(sink, context, delim, attr);
  53. }
  54. template <typename Generator, typename OutputIterator, typename Context
  55. , typename Delimiter, typename Attribute>
  56. bool lazy_generate_impl(Generator const& g, OutputIterator& sink
  57. , Context& context, Delimiter const& delim
  58. , Attribute const& /* attr */, mpl::true_)
  59. {
  60. // If DeducedAuto is false (semantic actions is present), the
  61. // component's attribute is unused.
  62. return g.generate(sink, context, delim, unused);
  63. }
  64. template <typename Generator, typename OutputIterator, typename Context
  65. , typename Delimiter, typename Attribute>
  66. bool lazy_generate_impl_main(Generator const& g, OutputIterator& sink
  67. , Context& context, Delimiter const& delim, Attribute const& attr)
  68. {
  69. // If DeducedAuto is true (no semantic action), we pass the parser's
  70. // attribute on to the component.
  71. typedef typename traits::has_semantic_action<Generator>::type auto_rule;
  72. return lazy_generate_impl(g, sink, context, delim, attr, auto_rule());
  73. }
  74. }
  75. template <typename Function, typename Modifiers>
  76. struct lazy_generator : generator<lazy_generator<Function, Modifiers> >
  77. {
  78. typedef mpl::int_<generator_properties::all_properties> properties;
  79. template <typename Context, typename Iterator>
  80. struct attribute
  81. {
  82. typedef typename
  83. boost::result_of<karma_modify(tag::lazy_eval, Modifiers)>::type
  84. modifier;
  85. typedef typename
  86. remove_reference<
  87. typename boost::result_of<Function(unused_type, Context)>::type
  88. >::type
  89. expr_type;
  90. // If you got an error_invalid_expression error message here,
  91. // then the expression (expr_type) is not a valid spirit karma
  92. // expression.
  93. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, expr_type);
  94. typedef typename
  95. result_of::compile<karma::domain, expr_type, modifier>::type
  96. generator_type;
  97. typedef typename
  98. traits::attribute_of<generator_type, Context, Iterator>::type
  99. type;
  100. };
  101. lazy_generator(Function const& func, Modifiers const& modifiers)
  102. : func(func), modifiers(modifiers) {}
  103. template <
  104. typename OutputIterator, typename Context,
  105. typename Delimiter, typename Attribute
  106. >
  107. bool generate(OutputIterator& sink, Context& context,
  108. Delimiter const& d, Attribute const& attr) const
  109. {
  110. return detail::lazy_generate_impl_main(
  111. compile<karma::domain>(func(unused, context)
  112. , karma_modify()(tag::lazy_eval(), modifiers))
  113. , sink, context, d, attr);
  114. }
  115. template <typename Context>
  116. info what(Context& context) const
  117. {
  118. return info("lazy"
  119. , compile<karma::domain>(func(unused, context)
  120. , karma_modify()(tag::lazy_eval(), modifiers))
  121. .what(context)
  122. );
  123. }
  124. Function func;
  125. Modifiers modifiers;
  126. // silence MSVC warning C4512: assignment operator could not be generated
  127. BOOST_DELETED_FUNCTION(lazy_generator& operator= (lazy_generator const&))
  128. };
  129. ///////////////////////////////////////////////////////////////////////////
  130. template <typename Function, typename Subject, typename Modifiers>
  131. struct lazy_directive
  132. : unary_generator<lazy_directive<Function, Subject, Modifiers> >
  133. {
  134. typedef mpl::int_<generator_properties::all_properties> properties;
  135. typedef Subject subject_type;
  136. template <typename Context, typename Iterator>
  137. struct attribute
  138. {
  139. typedef typename
  140. boost::result_of<karma_modify(tag::lazy_eval, Modifiers)>::type
  141. modifier;
  142. typedef typename
  143. remove_reference<
  144. typename boost::result_of<Function(unused_type, Context)>::type
  145. >::type
  146. directive_expr_type;
  147. typedef typename
  148. proto::result_of::make_expr<
  149. proto::tag::subscript
  150. , directive_expr_type
  151. , Subject
  152. >::type
  153. expr_type;
  154. // If you got an error_invalid_expression error message here,
  155. // then the expression (expr_type) is not a valid spirit karma
  156. // expression.
  157. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, expr_type);
  158. typedef typename
  159. result_of::compile<karma::domain, expr_type, modifier>::type
  160. generator_type;
  161. typedef typename
  162. traits::attribute_of<generator_type, Context, Iterator>::type
  163. type;
  164. };
  165. lazy_directive(Function const& function, Subject const& subject
  166. , Modifiers const& modifiers)
  167. : function(function), subject(subject), modifiers(modifiers) {}
  168. template <typename OutputIterator, typename Context, typename Delimiter
  169. , typename Attribute>
  170. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  171. , Attribute const& attr) const
  172. {
  173. return detail::lazy_generate_impl_main(compile<karma::domain>(
  174. proto::make_expr<proto::tag::subscript>(
  175. function(unused, ctx), subject)
  176. , karma_modify()(tag::lazy_eval(), modifiers))
  177. , sink, ctx, d, attr);
  178. }
  179. template <typename Context>
  180. info what(Context& ctx) const
  181. {
  182. return info("lazy-directive"
  183. , compile<karma::domain>(
  184. proto::make_expr<proto::tag::subscript>(
  185. function(unused, ctx), subject)
  186. , karma_modify()(tag::lazy_eval(), modifiers))
  187. .what(ctx)
  188. );
  189. }
  190. Function function;
  191. Subject subject;
  192. Modifiers modifiers;
  193. };
  194. ///////////////////////////////////////////////////////////////////////////
  195. // Generator generators: make_xxx function (objects)
  196. ///////////////////////////////////////////////////////////////////////////
  197. template <typename Eval, typename Modifiers>
  198. struct make_primitive<phoenix::actor<Eval>, Modifiers>
  199. {
  200. typedef lazy_generator<phoenix::actor<Eval>, Modifiers> result_type;
  201. result_type operator()(phoenix::actor<Eval> const& f
  202. , Modifiers const& modifiers) const
  203. {
  204. return result_type(f, modifiers);
  205. }
  206. };
  207. template <typename Terminal, typename Actor, int Arity, typename Modifiers>
  208. struct make_primitive<lazy_terminal<Terminal, Actor, Arity>, Modifiers>
  209. {
  210. typedef lazy_generator<Actor, Modifiers> result_type;
  211. result_type operator()(
  212. lazy_terminal<Terminal, Actor, Arity> const& lt
  213. , Modifiers const& modifiers) const
  214. {
  215. return result_type(lt.actor, modifiers);
  216. }
  217. };
  218. template <
  219. typename Terminal, typename Actor, int Arity, typename Subject
  220. , typename Modifiers>
  221. struct make_directive<lazy_terminal<Terminal, Actor, Arity>
  222. , Subject, Modifiers>
  223. {
  224. typedef lazy_directive<Actor, Subject, Modifiers> result_type;
  225. result_type operator()(
  226. lazy_terminal<Terminal, Actor, Arity> const& lt
  227. , Subject const& subject, Modifiers const& modifiers) const
  228. {
  229. return result_type(lt.actor, subject, modifiers);
  230. }
  231. };
  232. }}}
  233. #endif