rule.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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::transform_attribute<
  249. Attribute const, attr_type, domain>
  250. transform;
  251. typename transform::type attr_ = transform::pre(attr);
  252. // If you are seeing a compilation error here, you are probably
  253. // trying to use a rule or a grammar which has inherited
  254. // attributes, without passing values for them.
  255. context_type context(attr_);
  256. // If you are seeing a compilation error here stating that the
  257. // third parameter can't be converted to a karma::reference
  258. // then you are probably trying to use a rule or a grammar with
  259. // an incompatible delimiter type.
  260. if (f(sink, context, delim))
  261. {
  262. // do a post-delimit if this is an implied verbatim
  263. if (is_same<delimiter_type, unused_type>::value)
  264. karma::delimit_out(sink, delim);
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. template <typename Context, typename Delimiter, typename Attribute
  271. , typename Params>
  272. bool generate(output_iterator& sink, Context& caller_context
  273. , Delimiter const& delim, Attribute const& attr
  274. , Params const& params) const
  275. {
  276. if (f)
  277. {
  278. // Create an attribute if none is supplied.
  279. typedef traits::transform_attribute<
  280. Attribute const, attr_type, domain>
  281. transform;
  282. typename transform::type attr_ = transform::pre(attr);
  283. // If you are seeing a compilation error here, you are probably
  284. // trying to use a rule or a grammar which has inherited
  285. // attributes, passing values of incompatible types for them.
  286. context_type context(attr_, params, caller_context);
  287. // If you are seeing a compilation error here stating that the
  288. // third parameter can't be converted to a karma::reference
  289. // then you are probably trying to use a rule or a grammar with
  290. // an incompatible delimiter type.
  291. if (f(sink, context, delim))
  292. {
  293. // do a post-delimit if this is an implied verbatim
  294. if (is_same<delimiter_type, unused_type>::value)
  295. karma::delimit_out(sink, delim);
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. template <typename Context>
  302. info what(Context& /*context*/) const
  303. {
  304. return info(name_);
  305. }
  306. reference_ alias() const
  307. {
  308. return reference_(*this);
  309. }
  310. typename proto::terminal<this_type>::type copy() const
  311. {
  312. typename proto::terminal<this_type>::type result = {*this};
  313. return result;
  314. }
  315. // bring in the operator() overloads
  316. rule const& get_parameterized_subject() const { return *this; }
  317. typedef rule parameterized_subject_type;
  318. #include <boost/spirit/home/karma/nonterminal/detail/fcall.hpp>
  319. std::string name_;
  320. function_type f;
  321. };
  322. #if BOOST_WORKAROUND(BOOST_MSVC, < 1400)
  323. template <typename OutputIterator_, typename T1_, typename T2_
  324. , typename T3_, typename T4_, typename Expr>
  325. rule<OutputIterator_, T1_, T2_, T3_, T4_>& operator%=(
  326. rule<OutputIterator_, T1_, T2_, T3_, T4_>&r, Expr const& expr)
  327. {
  328. // Report invalid expression error as early as possible.
  329. // If you got an error_invalid_expression error message here, then
  330. // the expression (expr) is not a valid spirit karma expression.
  331. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr);
  332. typedef typename
  333. rule<OutputIterator_, T1_, T2_, T3_, T4_>::encoding_modifier_type
  334. encoding_modifier_type;
  335. r.f = detail::bind_generator<mpl::true_>(
  336. compile<karma::domain>(expr, encoding_modifier_type()));
  337. return r;
  338. }
  339. // non-const version needed to suppress proto's %= kicking in
  340. template <typename OutputIterator_, typename T1_, typename T2_
  341. , typename T3_, typename T4_, typename Expr>
  342. rule<OutputIterator_, T1_, T2_, T3_, T4_>& operator%=(
  343. rule<OutputIterator_, T1_, T2_, T3_, T4_>& r, Expr& expr)
  344. {
  345. return r %= static_cast<Expr const&>(expr);
  346. }
  347. #endif
  348. }}}
  349. namespace boost { namespace spirit { namespace traits
  350. {
  351. namespace detail
  352. {
  353. template <typename RuleAttribute, typename Attribute>
  354. struct nonterminal_handles_container
  355. : mpl::and_<
  356. traits::is_container<RuleAttribute>
  357. , is_convertible<Attribute, RuleAttribute> >
  358. {};
  359. }
  360. ///////////////////////////////////////////////////////////////////////////
  361. template <
  362. typename IteratorA, typename IteratorB, typename Attribute
  363. , typename Context, typename T1, typename T2, typename T3, typename T4>
  364. struct handles_container<
  365. karma::rule<IteratorA, T1, T2, T3, T4>, Attribute, Context
  366. , IteratorB>
  367. : detail::nonterminal_handles_container<
  368. typename attribute_of<
  369. karma::rule<IteratorA, T1, T2, T3, T4>
  370. , Context, IteratorB
  371. >::type, Attribute>
  372. {};
  373. }}}
  374. #if defined(BOOST_MSVC)
  375. # pragma warning(pop)
  376. #endif
  377. #endif