lazy.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. };
  127. ///////////////////////////////////////////////////////////////////////////
  128. template <typename Function, typename Subject, typename Modifiers>
  129. struct lazy_directive
  130. : unary_generator<lazy_directive<Function, Subject, Modifiers> >
  131. {
  132. typedef mpl::int_<generator_properties::all_properties> properties;
  133. typedef Subject subject_type;
  134. template <typename Context, typename Iterator>
  135. struct attribute
  136. {
  137. typedef typename
  138. boost::result_of<karma_modify(tag::lazy_eval, Modifiers)>::type
  139. modifier;
  140. typedef typename
  141. remove_reference<
  142. typename boost::result_of<Function(unused_type, Context)>::type
  143. >::type
  144. directive_expr_type;
  145. typedef typename
  146. proto::result_of::make_expr<
  147. proto::tag::subscript
  148. , directive_expr_type
  149. , Subject
  150. >::type
  151. expr_type;
  152. // If you got an error_invalid_expression error message here,
  153. // then the expression (expr_type) is not a valid spirit karma
  154. // expression.
  155. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, expr_type);
  156. typedef typename
  157. result_of::compile<karma::domain, expr_type, modifier>::type
  158. generator_type;
  159. typedef typename
  160. traits::attribute_of<generator_type, Context, Iterator>::type
  161. type;
  162. };
  163. lazy_directive(Function const& function, Subject const& subject
  164. , Modifiers const& modifiers)
  165. : function(function), subject(subject), modifiers(modifiers) {}
  166. template <typename OutputIterator, typename Context, typename Delimiter
  167. , typename Attribute>
  168. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  169. , Attribute const& attr) const
  170. {
  171. return detail::lazy_generate_impl_main(compile<karma::domain>(
  172. proto::make_expr<proto::tag::subscript>(
  173. function(unused, ctx), subject)
  174. , karma_modify()(tag::lazy_eval(), modifiers))
  175. , sink, ctx, d, attr);
  176. }
  177. template <typename Context>
  178. info what(Context& ctx) const
  179. {
  180. return info("lazy-directive"
  181. , compile<karma::domain>(
  182. proto::make_expr<proto::tag::subscript>(
  183. function(unused, ctx), subject)
  184. , karma_modify()(tag::lazy_eval(), modifiers))
  185. .what(ctx)
  186. );
  187. }
  188. Function function;
  189. Subject subject;
  190. Modifiers modifiers;
  191. };
  192. ///////////////////////////////////////////////////////////////////////////
  193. // Generator generators: make_xxx function (objects)
  194. ///////////////////////////////////////////////////////////////////////////
  195. template <typename Eval, typename Modifiers>
  196. struct make_primitive<phoenix::actor<Eval>, Modifiers>
  197. {
  198. typedef lazy_generator<phoenix::actor<Eval>, Modifiers> result_type;
  199. result_type operator()(phoenix::actor<Eval> const& f
  200. , Modifiers const& modifiers) const
  201. {
  202. return result_type(f, modifiers);
  203. }
  204. };
  205. template <typename Terminal, typename Actor, int Arity, typename Modifiers>
  206. struct make_primitive<lazy_terminal<Terminal, Actor, Arity>, Modifiers>
  207. {
  208. typedef lazy_generator<Actor, Modifiers> result_type;
  209. result_type operator()(
  210. lazy_terminal<Terminal, Actor, Arity> const& lt
  211. , Modifiers const& modifiers) const
  212. {
  213. return result_type(lt.actor, modifiers);
  214. }
  215. };
  216. template <
  217. typename Terminal, typename Actor, int Arity, typename Subject
  218. , typename Modifiers>
  219. struct make_directive<lazy_terminal<Terminal, Actor, Arity>
  220. , Subject, Modifiers>
  221. {
  222. typedef lazy_directive<Actor, Subject, Modifiers> result_type;
  223. result_type operator()(
  224. lazy_terminal<Terminal, Actor, Arity> const& lt
  225. , Subject const& subject, Modifiers const& modifiers) const
  226. {
  227. return result_type(lt.actor, subject, modifiers);
  228. }
  229. };
  230. }}}
  231. #endif