rule.hpp 16 KB

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