rule.hpp 15 KB

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