rule.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // Copyright (c) 2001-2011 Joel de Guzman
  2. // Copyright (c) 2001-2011 Hartmut Kaiser
  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_RULE_MAR_05_2007_0455PM)
  7. #define BOOST_SPIRIT_KARMA_RULE_MAR_05_2007_0455PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/assert.hpp>
  12. #include <boost/config.hpp>
  13. #include <boost/function.hpp>
  14. #include <boost/mpl/vector.hpp>
  15. #include <boost/type_traits/add_const.hpp>
  16. #include <boost/type_traits/add_reference.hpp>
  17. #include <boost/type_traits/is_same.hpp>
  18. #include <boost/fusion/include/vector.hpp>
  19. #include <boost/fusion/include/size.hpp>
  20. #include <boost/fusion/include/make_vector.hpp>
  21. #include <boost/fusion/include/cons.hpp>
  22. #include <boost/fusion/include/as_list.hpp>
  23. #include <boost/fusion/include/as_vector.hpp>
  24. #include <boost/spirit/home/support/unused.hpp>
  25. #include <boost/spirit/home/support/argument.hpp>
  26. #include <boost/spirit/home/support/context.hpp>
  27. #include <boost/spirit/home/support/info.hpp>
  28. #include <boost/spirit/home/karma/delimit_out.hpp>
  29. #include <boost/spirit/home/karma/detail/attributes.hpp>
  30. #include <boost/spirit/home/support/nonterminal/extract_param.hpp>
  31. #include <boost/spirit/home/support/nonterminal/locals.hpp>
  32. #include <boost/spirit/home/karma/reference.hpp>
  33. #include <boost/spirit/home/karma/detail/output_iterator.hpp>
  34. #include <boost/spirit/home/karma/nonterminal/nonterminal_fwd.hpp>
  35. #include <boost/spirit/home/karma/nonterminal/detail/generator_binder.hpp>
  36. #include <boost/spirit/home/karma/nonterminal/detail/parameterized.hpp>
  37. #if defined(BOOST_MSVC)
  38. # pragma warning(push)
  39. # pragma warning(disable: 4355) // 'this' : used in base member initializer list warning
  40. #endif
  41. namespace boost { namespace spirit { namespace karma
  42. {
  43. BOOST_PP_REPEAT(SPIRIT_ATTRIBUTES_LIMIT, SPIRIT_USING_ATTRIBUTE, _)
  44. using spirit::_pass_type;
  45. using spirit::_val_type;
  46. using spirit::_a_type;
  47. using spirit::_b_type;
  48. using spirit::_c_type;
  49. using spirit::_d_type;
  50. using spirit::_e_type;
  51. using spirit::_f_type;
  52. using spirit::_g_type;
  53. using spirit::_h_type;
  54. using spirit::_i_type;
  55. using spirit::_j_type;
  56. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  57. using spirit::_pass;
  58. using spirit::_val;
  59. using spirit::_a;
  60. using spirit::_b;
  61. using spirit::_c;
  62. using spirit::_d;
  63. using spirit::_e;
  64. using spirit::_f;
  65. using spirit::_g;
  66. using spirit::_h;
  67. using spirit::_i;
  68. using spirit::_j;
  69. #endif
  70. using spirit::info;
  71. using spirit::locals;
  72. template <
  73. typename OutputIterator, typename T1, typename T2, typename T3
  74. , typename T4>
  75. struct rule
  76. : proto::extends<
  77. typename proto::terminal<
  78. reference<rule<OutputIterator, T1, T2, T3, T4> const>
  79. >::type
  80. , rule<OutputIterator, T1, T2, T3, T4>
  81. >
  82. , generator<rule<OutputIterator, T1, T2, T3, T4> >
  83. {
  84. typedef mpl::int_<generator_properties::all_properties> properties;
  85. typedef OutputIterator iterator_type;
  86. typedef rule<OutputIterator, T1, T2, T3, T4> this_type;
  87. typedef reference<this_type const> reference_;
  88. typedef typename proto::terminal<reference_>::type terminal;
  89. typedef proto::extends<terminal, this_type> base_type;
  90. typedef mpl::vector<T1, T2, T3, T4> template_params;
  91. // the output iterator is always wrapped by karma
  92. typedef detail::output_iterator<OutputIterator, properties>
  93. output_iterator;
  94. // locals_type is a sequence of types to be used as local variables
  95. typedef typename
  96. spirit::detail::extract_locals<template_params>::type
  97. locals_type;
  98. // The delimiter-generator type
  99. typedef typename
  100. spirit::detail::extract_component<
  101. karma::domain, template_params>::type
  102. delimiter_type;
  103. // The rule's signature
  104. typedef typename
  105. spirit::detail::extract_sig<template_params>::type
  106. sig_type;
  107. // The rule's encoding type
  108. typedef typename
  109. spirit::detail::extract_encoding<template_params>::type
  110. encoding_type;
  111. // This is the rule's attribute type
  112. typedef typename
  113. spirit::detail::attr_from_sig<sig_type>::type
  114. attr_type;
  115. typedef typename add_reference<
  116. typename add_const<attr_type>::type>::type
  117. attr_reference_type;
  118. // parameter_types is a sequence of types passed as parameters to the rule
  119. typedef typename
  120. spirit::detail::params_from_sig<sig_type>::type
  121. parameter_types;
  122. static size_t const params_size =
  123. fusion::result_of::size<parameter_types>::type::value;
  124. // the context passed to the right hand side of a rule contains
  125. // the attribute and the parameters for this particular rule invocation
  126. typedef context<
  127. fusion::cons<attr_reference_type, parameter_types>
  128. , locals_type>
  129. context_type;
  130. typedef function<
  131. bool(output_iterator&, context_type&, delimiter_type const&)>
  132. function_type;
  133. typedef typename
  134. mpl::if_<
  135. is_same<encoding_type, unused_type>
  136. , unused_type
  137. , tag::char_code<tag::encoding, encoding_type>
  138. >::type
  139. encoding_modifier_type;
  140. explicit rule(std::string const& name_ = "unnamed-rule")
  141. : base_type(terminal::make(reference_(*this)))
  142. , name_(name_)
  143. {
  144. }
  145. rule(rule const& rhs)
  146. : base_type(terminal::make(reference_(*this)))
  147. , name_(rhs.name_)
  148. , f(rhs.f)
  149. {
  150. }
  151. template <typename Auto, typename Expr>
  152. static void define(rule& lhs, Expr const& expr, mpl::false_)
  153. {
  154. // Report invalid expression error as early as possible.
  155. // If you got an error_invalid_expression error message here,
  156. // then the expression (expr) is not a valid spirit karma expression.
  157. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr);
  158. }
  159. template <typename Auto, typename Expr>
  160. static void define(rule& lhs, Expr const& expr, mpl::true_)
  161. {
  162. lhs.f = detail::bind_generator<Auto>(
  163. compile<karma::domain>(expr, encoding_modifier_type()));
  164. }
  165. template <typename Expr>
  166. rule (Expr const& expr, std::string const& name_ = "unnamed-rule")
  167. : base_type(terminal::make(reference_(*this)))
  168. , name_(name_)
  169. {
  170. define<mpl::false_>(*this, expr, traits::matches<karma::domain, Expr>());
  171. }
  172. rule& operator=(rule const& rhs)
  173. {
  174. // The following assertion fires when you try to initialize a rule
  175. // from an uninitialized one. Did you mean to refer to the right
  176. // hand side rule instead of assigning from it? In this case you
  177. // should write lhs = rhs.alias();
  178. BOOST_ASSERT(rhs.f && "Did you mean rhs.alias() instead of rhs?");
  179. f = rhs.f;
  180. name_ = rhs.name_;
  181. return *this;
  182. }
  183. std::string const& name() const
  184. {
  185. return name_;
  186. }
  187. void name(std::string const& str)
  188. {
  189. name_ = str;
  190. }
  191. template <typename Expr>
  192. rule& operator=(Expr const& expr)
  193. {
  194. define<mpl::false_>(*this, expr, traits::matches<karma::domain, Expr>());
  195. return *this;
  196. }
  197. // VC7.1 has problems to resolve 'rule' without explicit template parameters
  198. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1400)
  199. // g++ 3.3 barfs if this is a member function :(
  200. template <typename Expr>
  201. friend rule& operator%=(rule& r, Expr const& expr)
  202. {
  203. define<mpl::true_>(r, expr, traits::matches<karma::domain, Expr>());
  204. return r;
  205. }
  206. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  207. // non-const version needed to suppress proto's %= kicking in
  208. template <typename Expr>
  209. friend rule& operator%=(rule& r, Expr& expr)
  210. {
  211. return r %= static_cast<Expr const&>(expr);
  212. }
  213. #else
  214. // for rvalue references
  215. template <typename Expr>
  216. friend rule& operator%=(rule& r, Expr&& expr)
  217. {
  218. define<mpl::true_>(r, expr, traits::matches<karma::domain, Expr>());
  219. return r;
  220. }
  221. #endif
  222. #else
  223. // both friend functions have to be defined out of class as VC7.1
  224. // will complain otherwise
  225. template <typename OutputIterator_, typename T1_, typename T2_
  226. , typename T3_, typename T4_, typename Expr>
  227. friend rule<OutputIterator_, T1_, T2_, T3_, T4_>& operator%=(
  228. rule<OutputIterator_, T1_, T2_, T3_, T4_>&r, Expr const& expr);
  229. // non-const version needed to suppress proto's %= kicking in
  230. template <typename OutputIterator_, typename T1_, typename T2_
  231. , typename T3_, typename T4_, typename Expr>
  232. friend rule<OutputIterator_, T1_, T2_, T3_, T4_>& operator%=(
  233. rule<OutputIterator_, T1_, T2_, T3_, T4_>& r, Expr& expr);
  234. #endif
  235. template <typename Context, typename Unused>
  236. struct attribute
  237. {
  238. typedef attr_type type;
  239. };
  240. template <typename Context, typename Delimiter, typename Attribute>
  241. bool generate(output_iterator& sink, Context&, Delimiter const& delim
  242. , Attribute const& attr) const
  243. {
  244. if (f)
  245. {
  246. // Create an attribute if none is supplied.
  247. typedef traits::make_attribute<attr_type, Attribute>
  248. make_attribute;
  249. typedef traits::transform_attribute<
  250. typename make_attribute::type, attr_type, domain>
  251. transform;
  252. typename transform::type attr_ =
  253. traits::pre_transform<domain, attr_type>(
  254. make_attribute::call(attr));
  255. // If you are seeing a compilation error here, you are probably
  256. // trying to use a rule or a grammar which has inherited
  257. // attributes, without passing values for them.
  258. context_type context(attr_);
  259. // If you are seeing a compilation error here stating that the
  260. // third parameter can't be converted to a karma::reference
  261. // then you are probably trying to use a rule or a grammar with
  262. // an incompatible delimiter type.
  263. if (f(sink, context, delim))
  264. {
  265. // do a post-delimit if this is an implied verbatim
  266. if (is_same<delimiter_type, unused_type>::value)
  267. karma::delimit_out(sink, delim);
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. template <typename Context, typename Delimiter, typename Attribute
  274. , typename Params>
  275. bool generate(output_iterator& sink, Context& caller_context
  276. , Delimiter const& delim, Attribute const& attr
  277. , Params const& params) const
  278. {
  279. if (f)
  280. {
  281. // Create an attribute if none is supplied.
  282. typedef traits::make_attribute<attr_type, Attribute>
  283. make_attribute;
  284. typedef traits::transform_attribute<
  285. typename make_attribute::type, attr_type, domain>
  286. transform;
  287. typename transform::type attr_ =
  288. traits::pre_transform<domain, attr_type>(
  289. make_attribute::call(attr));
  290. // If you are seeing a compilation error here, you are probably
  291. // trying to use a rule or a grammar which has inherited
  292. // attributes, passing values of incompatible types for them.
  293. context_type context(attr_, params, caller_context);
  294. // If you are seeing a compilation error here stating that the
  295. // third parameter can't be converted to a karma::reference
  296. // then you are probably trying to use a rule or a grammar with
  297. // an incompatible delimiter type.
  298. if (f(sink, context, delim))
  299. {
  300. // do a post-delimit if this is an implied verbatim
  301. if (is_same<delimiter_type, unused_type>::value)
  302. karma::delimit_out(sink, delim);
  303. return true;
  304. }
  305. }
  306. return false;
  307. }
  308. template <typename Context>
  309. info what(Context& /*context*/) const
  310. {
  311. return info(name_);
  312. }
  313. reference_ alias() const
  314. {
  315. return reference_(*this);
  316. }
  317. typename proto::terminal<this_type>::type copy() const
  318. {
  319. typename proto::terminal<this_type>::type result = {*this};
  320. return result;
  321. }
  322. // bring in the operator() overloads
  323. rule const& get_parameterized_subject() const { return *this; }
  324. typedef rule parameterized_subject_type;
  325. #include <boost/spirit/home/karma/nonterminal/detail/fcall.hpp>
  326. std::string name_;
  327. function_type f;
  328. };
  329. #if BOOST_WORKAROUND(BOOST_MSVC, < 1400)
  330. template <typename OutputIterator_, typename T1_, typename T2_
  331. , typename T3_, typename T4_, typename Expr>
  332. rule<OutputIterator_, T1_, T2_, T3_, T4_>& operator%=(
  333. rule<OutputIterator_, T1_, T2_, T3_, T4_>&r, Expr const& expr)
  334. {
  335. // Report invalid expression error as early as possible.
  336. // If you got an error_invalid_expression error message here, then
  337. // the expression (expr) is not a valid spirit karma expression.
  338. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr);
  339. typedef typename
  340. rule<OutputIterator_, T1_, T2_, T3_, T4_>::encoding_modifier_type
  341. encoding_modifier_type;
  342. r.f = detail::bind_generator<mpl::true_>(
  343. compile<karma::domain>(expr, encoding_modifier_type()));
  344. return r;
  345. }
  346. // non-const version needed to suppress proto's %= kicking in
  347. template <typename OutputIterator_, typename T1_, typename T2_
  348. , typename T3_, typename T4_, typename Expr>
  349. rule<OutputIterator_, T1_, T2_, T3_, T4_>& operator%=(
  350. rule<OutputIterator_, T1_, T2_, T3_, T4_>& r, Expr& expr)
  351. {
  352. return r %= static_cast<Expr const&>(expr);
  353. }
  354. #endif
  355. }}}
  356. namespace boost { namespace spirit { namespace traits
  357. {
  358. namespace detail
  359. {
  360. template <typename RuleAttribute, typename Attribute>
  361. struct nonterminal_handles_container
  362. : mpl::and_<
  363. traits::is_container<RuleAttribute>
  364. , is_convertible<Attribute, RuleAttribute> >
  365. {};
  366. }
  367. ///////////////////////////////////////////////////////////////////////////
  368. template <
  369. typename IteratorA, typename IteratorB, typename Attribute
  370. , typename Context, typename T1, typename T2, typename T3, typename T4>
  371. struct handles_container<
  372. karma::rule<IteratorA, T1, T2, T3, T4>, Attribute, Context
  373. , IteratorB>
  374. : detail::nonterminal_handles_container<
  375. typename attribute_of<
  376. karma::rule<IteratorA, T1, T2, T3, T4>
  377. , Context, IteratorB
  378. >::type, Attribute>
  379. {};
  380. }}}
  381. #if defined(BOOST_MSVC)
  382. # pragma warning(pop)
  383. #endif
  384. #endif