subrule.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. // Copyright (c) 2009 Francois Barel
  2. // Copyright (c) 2001-2011 Joel de Guzman
  3. // Copyright (c) 2001-2012 Hartmut Kaiser
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #if !defined(BOOST_SPIRIT_REPOSITORY_KARMA_SUBRULE_AUGUST_12_2009_0813PM)
  8. #define BOOST_SPIRIT_REPOSITORY_KARMA_SUBRULE_AUGUST_12_2009_0813PM
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/spirit/home/karma/domain.hpp>
  13. #include <boost/spirit/home/karma/meta_compiler.hpp>
  14. #include <boost/spirit/home/karma/generator.hpp>
  15. #include <boost/spirit/home/karma/reference.hpp>
  16. #include <boost/spirit/home/karma/nonterminal/detail/generator_binder.hpp>
  17. #include <boost/spirit/home/karma/nonterminal/detail/parameterized.hpp>
  18. #include <boost/spirit/home/support/argument.hpp>
  19. #include <boost/spirit/home/support/assert_msg.hpp>
  20. #include <boost/spirit/home/karma/detail/attributes.hpp>
  21. #include <boost/spirit/home/support/info.hpp>
  22. #include <boost/spirit/home/support/unused.hpp>
  23. #include <boost/spirit/home/support/nonterminal/extract_param.hpp>
  24. #include <boost/spirit/home/support/nonterminal/locals.hpp>
  25. #include <boost/spirit/repository/home/support/subrule_context.hpp>
  26. #include <boost/static_assert.hpp>
  27. #include <boost/fusion/include/as_map.hpp>
  28. #include <boost/fusion/include/at_key.hpp>
  29. #include <boost/fusion/include/cons.hpp>
  30. #include <boost/fusion/include/front.hpp>
  31. #include <boost/fusion/include/has_key.hpp>
  32. #include <boost/fusion/include/join.hpp>
  33. #include <boost/fusion/include/make_map.hpp>
  34. #include <boost/fusion/include/make_vector.hpp>
  35. #include <boost/fusion/include/size.hpp>
  36. #include <boost/fusion/include/vector.hpp>
  37. #include <boost/mpl/bool.hpp>
  38. #include <boost/mpl/identity.hpp>
  39. #include <boost/mpl/int.hpp>
  40. #include <boost/mpl/vector.hpp>
  41. #include <boost/proto/extends.hpp>
  42. #include <boost/proto/traits.hpp>
  43. #include <boost/type_traits/is_const.hpp>
  44. #include <boost/type_traits/is_reference.hpp>
  45. #include <boost/type_traits/is_same.hpp>
  46. #include <boost/type_traits/remove_reference.hpp>
  47. #if defined(BOOST_MSVC)
  48. # pragma warning(push)
  49. # pragma warning(disable: 4355) // 'this' : used in base member initializer list warning
  50. #endif
  51. ///////////////////////////////////////////////////////////////////////////////
  52. namespace boost { namespace spirit { namespace repository { namespace karma
  53. {
  54. ///////////////////////////////////////////////////////////////////////////
  55. // subrule_group_generator:
  56. // - generator representing a group of subrule definitions (one or more),
  57. // invokes first subrule on entry,
  58. ///////////////////////////////////////////////////////////////////////////
  59. template <typename Defs>
  60. struct subrule_group_generator
  61. : spirit::karma::generator<subrule_group_generator<Defs> >
  62. {
  63. // Fusion associative sequence, associating each subrule ID in this
  64. // group (as an MPL integral constant) with its definition
  65. typedef Defs defs_type;
  66. typedef subrule_group_generator<Defs> this_type;
  67. explicit subrule_group_generator(Defs const& defs)
  68. : defs(defs)
  69. {
  70. }
  71. // from a subrule ID, get the type of a reference to its definition
  72. template <int ID>
  73. struct def_type
  74. {
  75. typedef mpl::int_<ID> id_type;
  76. // If you are seeing a compilation error here, you are trying
  77. // to use a subrule which was not defined in this group.
  78. BOOST_SPIRIT_ASSERT_MSG(
  79. (fusion::result_of::has_key<
  80. defs_type const, id_type>::type::value)
  81. , subrule_used_without_being_defined, (mpl::int_<ID>));
  82. typedef typename
  83. fusion::result_of::at_key<defs_type const, id_type>::type
  84. type;
  85. };
  86. // from a subrule ID, get a reference to its definition
  87. template <int ID>
  88. typename def_type<ID>::type def() const
  89. {
  90. return fusion::at_key<mpl::int_<ID> >(defs);
  91. }
  92. template <typename Context, typename Iterator>
  93. struct attribute
  94. // Forward to first subrule.
  95. : mpl::identity<
  96. typename remove_reference<
  97. typename fusion::result_of::front<Defs>::type
  98. >::type::second_type::attr_type> {};
  99. template <typename OutputIterator, typename Context
  100. , typename Delimiter, typename Attribute>
  101. bool generate(OutputIterator& sink, Context& context
  102. , Delimiter const& delimiter, Attribute const& attr) const
  103. {
  104. // Forward to first subrule.
  105. return generate_subrule(fusion::front(defs).second
  106. , sink, context, delimiter, attr);
  107. }
  108. template <typename OutputIterator, typename Context
  109. , typename Delimiter, typename Attribute
  110. , typename Params>
  111. bool generate(OutputIterator& sink, Context& context
  112. , Delimiter const& delimiter, Attribute const& attr
  113. , Params const& params) const
  114. {
  115. // Forward to first subrule.
  116. return generate_subrule(fusion::front(defs).second
  117. , sink, context, delimiter, attr, params);
  118. }
  119. template <int ID, typename OutputIterator, typename Context
  120. , typename Delimiter, typename Attribute>
  121. bool generate_subrule_id(OutputIterator& sink
  122. , Context& context, Delimiter const& delimiter
  123. , Attribute const& attr) const
  124. {
  125. return generate_subrule(def<ID>()
  126. , sink, context, delimiter, attr);
  127. }
  128. template <int ID, typename OutputIterator, typename Context
  129. , typename Delimiter, typename Attribute, typename Params>
  130. bool generate_subrule_id(OutputIterator& sink
  131. , Context& context, Delimiter const& delimiter
  132. , Attribute const& attr, Params const& params) const
  133. {
  134. return generate_subrule(def<ID>()
  135. , sink, context, delimiter, attr, params);
  136. }
  137. template <typename Def, typename OutputIterator, typename Context
  138. , typename Delimiter, typename Attribute>
  139. bool generate_subrule(Def const& def, OutputIterator& sink
  140. , Context& /*caller_context*/, Delimiter const& delimiter
  141. , Attribute const& attr) const
  142. {
  143. // compute context type for this subrule
  144. typedef typename Def::locals_type subrule_locals_type;
  145. typedef typename Def::attr_type subrule_attr_type;
  146. typedef typename Def::attr_reference_type subrule_attr_reference_type;
  147. typedef typename Def::parameter_types subrule_parameter_types;
  148. typedef
  149. subrule_context<
  150. this_type
  151. , fusion::cons<
  152. subrule_attr_reference_type, subrule_parameter_types>
  153. , subrule_locals_type
  154. >
  155. context_type;
  156. typedef traits::transform_attribute<Attribute const
  157. , subrule_attr_type, spirit::karma::domain> transform;
  158. // If you are seeing a compilation error here, you are probably
  159. // trying to use a subrule which has inherited attributes,
  160. // without passing values for them.
  161. context_type context(*this, transform::pre(attr));
  162. return def.binder(sink, context, delimiter);
  163. }
  164. template <typename Def, typename OutputIterator, typename Context
  165. , typename Delimiter, typename Attribute, typename Params>
  166. bool generate_subrule(Def const& def, OutputIterator& sink
  167. , Context& caller_context, Delimiter const& delimiter
  168. , Attribute const& attr, Params const& params) const
  169. {
  170. // compute context type for this subrule
  171. typedef typename Def::locals_type subrule_locals_type;
  172. typedef typename Def::attr_type subrule_attr_type;
  173. typedef typename Def::attr_reference_type subrule_attr_reference_type;
  174. typedef typename Def::parameter_types subrule_parameter_types;
  175. typedef
  176. subrule_context<
  177. this_type
  178. , fusion::cons<
  179. subrule_attr_reference_type, subrule_parameter_types>
  180. , subrule_locals_type
  181. >
  182. context_type;
  183. typedef traits::transform_attribute<Attribute const
  184. , subrule_attr_type, spirit::karma::domain> transform;
  185. // If you are seeing a compilation error here, you are probably
  186. // trying to use a subrule which has inherited attributes,
  187. // passing values of incompatible types for them.
  188. context_type context(*this
  189. , transform::pre(attr), params, caller_context);
  190. return def.binder(sink, context, delimiter);
  191. }
  192. template <typename Context>
  193. info what(Context& context) const
  194. {
  195. // Forward to first subrule.
  196. return fusion::front(defs).second.binder.g.what(context);
  197. }
  198. Defs defs;
  199. };
  200. ///////////////////////////////////////////////////////////////////////////
  201. // subrule_group:
  202. // - a Proto terminal, so that a group behaves like any Spirit
  203. // expression.
  204. ///////////////////////////////////////////////////////////////////////////
  205. template <typename Defs>
  206. struct subrule_group
  207. : proto::extends<
  208. typename proto::terminal<
  209. subrule_group_generator<Defs>
  210. >::type
  211. , subrule_group<Defs>
  212. >
  213. {
  214. typedef subrule_group_generator<Defs> generator_type;
  215. typedef typename proto::terminal<generator_type>::type terminal;
  216. struct properties
  217. // Forward to first subrule.
  218. : remove_reference<
  219. typename fusion::result_of::front<Defs>::type
  220. >::type::second_type::subject_type::properties {};
  221. static size_t const params_size =
  222. // Forward to first subrule.
  223. remove_reference<
  224. typename fusion::result_of::front<Defs>::type
  225. >::type::second_type::params_size;
  226. explicit subrule_group(Defs const& defs)
  227. : subrule_group::proto_extends(terminal::make(generator_type(defs)))
  228. {
  229. }
  230. generator_type const& generator() const { return proto::value(*this); }
  231. Defs const& defs() const { return generator().defs; }
  232. template <typename Defs2>
  233. subrule_group<
  234. typename fusion::result_of::as_map<
  235. typename fusion::result_of::join<
  236. Defs const, Defs2 const>::type>::type>
  237. operator,(subrule_group<Defs2> const& other) const
  238. {
  239. typedef subrule_group<
  240. typename fusion::result_of::as_map<
  241. typename fusion::result_of::join<
  242. Defs const, Defs2 const>::type>::type> result_type;
  243. return result_type(fusion::as_map(fusion::join(defs(), other.defs())));
  244. }
  245. // non-const versions needed to suppress proto's comma op kicking in
  246. template <typename Defs2>
  247. friend subrule_group<
  248. typename fusion::result_of::as_map<
  249. typename fusion::result_of::join<
  250. Defs const, Defs2 const>::type>::type>
  251. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  252. operator,(subrule_group&& left, subrule_group<Defs2>&& other)
  253. #else
  254. operator,(subrule_group& left, subrule_group<Defs2>& other)
  255. #endif
  256. {
  257. return static_cast<subrule_group const&>(left)
  258. .operator,(static_cast<subrule_group<Defs2> const&>(other));
  259. }
  260. // bring in the operator() overloads
  261. generator_type const& get_parameterized_subject() const { return generator(); }
  262. typedef generator_type parameterized_subject_type;
  263. #include <boost/spirit/home/karma/nonterminal/detail/fcall.hpp>
  264. };
  265. ///////////////////////////////////////////////////////////////////////////
  266. // subrule_definition: holds one particular definition of a subrule
  267. ///////////////////////////////////////////////////////////////////////////
  268. template <
  269. int ID_
  270. , typename Locals
  271. , typename Attr
  272. , typename AttrRef
  273. , typename Parameters
  274. , size_t ParamsSize
  275. , typename Subject
  276. , bool Auto_
  277. >
  278. struct subrule_definition
  279. {
  280. typedef mpl::int_<ID_> id_type;
  281. BOOST_STATIC_CONSTANT(int, ID = ID_);
  282. typedef Locals locals_type;
  283. typedef Attr attr_type;
  284. typedef AttrRef attr_reference_type;
  285. typedef Parameters parameter_types;
  286. static size_t const params_size = ParamsSize;
  287. typedef Subject subject_type;
  288. typedef mpl::bool_<Auto_> auto_type;
  289. BOOST_STATIC_CONSTANT(bool, Auto = Auto_);
  290. typedef spirit::karma::detail::generator_binder<
  291. Subject, auto_type> binder_type;
  292. subrule_definition(Subject const& subject, std::string const& name)
  293. : binder(subject), name(name)
  294. {
  295. }
  296. binder_type const binder;
  297. std::string const name;
  298. };
  299. ///////////////////////////////////////////////////////////////////////////
  300. // subrule placeholder:
  301. // - on subrule definition: helper for creation of subrule_group,
  302. // - on subrule invocation: Proto terminal and generator.
  303. ///////////////////////////////////////////////////////////////////////////
  304. template <
  305. int ID_
  306. , typename T1 = unused_type
  307. , typename T2 = unused_type
  308. >
  309. struct subrule
  310. : proto::extends<
  311. typename proto::terminal<
  312. spirit::karma::reference<subrule<ID_, T1, T2> const>
  313. >::type
  314. , subrule<ID_, T1, T2>
  315. >
  316. , spirit::karma::generator<subrule<ID_, T1, T2> >
  317. {
  318. //FIXME should go fetch the real properties of this subrule's definition in the current context, but we don't
  319. // have the context here (properties would need to be 'template<typename Context> struct properties' instead)
  320. typedef mpl::int_<
  321. spirit::karma::generator_properties::all_properties> properties;
  322. typedef mpl::int_<ID_> id_type;
  323. BOOST_STATIC_CONSTANT(int, ID = ID_);
  324. typedef subrule<ID_, T1, T2> this_type;
  325. typedef spirit::karma::reference<this_type const> reference_;
  326. typedef typename proto::terminal<reference_>::type terminal;
  327. typedef proto::extends<terminal, this_type> base_type;
  328. typedef mpl::vector<T1, T2> template_params;
  329. // The subrule's locals_type: a sequence of types to be used as local variables
  330. typedef typename
  331. spirit::detail::extract_locals<template_params>::type
  332. locals_type;
  333. // The subrule's encoding type
  334. typedef typename
  335. spirit::detail::extract_encoding<template_params>::type
  336. encoding_type;
  337. // The subrule's signature
  338. typedef typename
  339. spirit::detail::extract_sig<template_params, encoding_type
  340. , spirit::karma::domain>::type
  341. sig_type;
  342. // This is the subrule's attribute type
  343. typedef typename
  344. spirit::detail::attr_from_sig<sig_type>::type
  345. attr_type;
  346. BOOST_STATIC_ASSERT_MSG(
  347. !is_reference<attr_type>::value && !is_const<attr_type>::value,
  348. "Const/reference qualifiers on Karma subrule attribute are meaningless");
  349. typedef attr_type const& attr_reference_type;
  350. // parameter_types is a sequence of types passed as parameters to the subrule
  351. typedef typename
  352. spirit::detail::params_from_sig<sig_type>::type
  353. parameter_types;
  354. static size_t const params_size =
  355. fusion::result_of::size<parameter_types>::type::value;
  356. explicit subrule(std::string const& name_ = "unnamed-subrule")
  357. : base_type(terminal::make(reference_(*this)))
  358. , name_(name_)
  359. {
  360. }
  361. // compute type of this subrule's definition for expr type Expr
  362. template <typename Expr, bool Auto>
  363. struct def_type_helper
  364. {
  365. // Report invalid expression error as early as possible.
  366. // If you got an error_invalid_expression error message here,
  367. // then the expression (Expr) is not a valid spirit karma expression.
  368. BOOST_SPIRIT_ASSERT_MATCH(spirit::karma::domain, Expr);
  369. typedef typename result_of::compile<
  370. spirit::karma::domain, Expr>::type subject_type;
  371. typedef subrule_definition<
  372. ID_
  373. , locals_type
  374. , attr_type
  375. , attr_reference_type
  376. , parameter_types
  377. , params_size
  378. , subject_type
  379. , Auto
  380. > const type;
  381. };
  382. // compute type of subrule group containing only this
  383. // subrule's definition for expr type Expr
  384. template <typename Expr, bool Auto>
  385. struct group_type_helper
  386. {
  387. typedef typename def_type_helper<Expr, Auto>::type def_type;
  388. // create Defs map with only one entry: (ID -> def)
  389. typedef typename
  390. #ifndef BOOST_FUSION_HAS_VARIADIC_MAP
  391. fusion::result_of::make_map<id_type, def_type>::type
  392. #else
  393. fusion::result_of::make_map<id_type>::template apply<def_type>::type
  394. #endif
  395. defs_type;
  396. typedef subrule_group<defs_type> type;
  397. };
  398. template <typename Expr>
  399. typename group_type_helper<Expr, false>::type
  400. operator=(Expr const& expr) const
  401. {
  402. typedef group_type_helper<Expr, false> helper;
  403. typedef typename helper::def_type def_type;
  404. typedef typename helper::type result_type;
  405. return result_type(fusion::make_map<id_type>(
  406. def_type(compile<spirit::karma::domain>(expr), name_)));
  407. }
  408. #define BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(lhs_ref, rhs_ref) \
  409. template <typename Expr> \
  410. friend typename group_type_helper<Expr, true>::type \
  411. operator%=(subrule lhs_ref sr, Expr rhs_ref expr) \
  412. { \
  413. typedef group_type_helper<Expr, true> helper; \
  414. typedef typename helper::def_type def_type; \
  415. typedef typename helper::type result_type; \
  416. return result_type(fusion::make_map<id_type>( \
  417. def_type(compile<spirit::karma::domain>(expr), sr.name_))); \
  418. } \
  419. /**/
  420. // non-const versions needed to suppress proto's %= kicking in
  421. BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(const&, const&)
  422. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  423. BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(const&, &&)
  424. #else
  425. BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(const&, &)
  426. #endif
  427. BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(&, const&)
  428. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  429. BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(&, &&)
  430. #else
  431. BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(&, &)
  432. #endif
  433. #undef BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR
  434. std::string const& name() const
  435. {
  436. return name_;
  437. }
  438. void name(std::string const& str)
  439. {
  440. name_ = str;
  441. }
  442. template <typename Context, typename Iterator>
  443. struct attribute
  444. {
  445. typedef attr_type type;
  446. };
  447. template <typename OutputIterator, typename Group
  448. , typename Attributes, typename Locals
  449. , typename Delimiter, typename Attribute>
  450. bool generate(OutputIterator& sink
  451. , subrule_context<Group, Attributes, Locals>& context
  452. , Delimiter const& delimiter, Attribute const& attr) const
  453. {
  454. return context.group.template generate_subrule_id<ID_>(
  455. sink, context, delimiter, attr);
  456. }
  457. template <typename OutputIterator, typename Context
  458. , typename Delimiter, typename Attribute>
  459. bool generate(OutputIterator& /*sink*/
  460. , Context& /*context*/
  461. , Delimiter const& /*delimiter*/, Attribute const& /*attr*/) const
  462. {
  463. // If you are seeing a compilation error here, you are trying
  464. // to use a subrule as a generator outside of a subrule group.
  465. BOOST_SPIRIT_ASSERT_FAIL(OutputIterator
  466. , subrule_used_outside_subrule_group, (id_type));
  467. return false;
  468. }
  469. template <typename OutputIterator, typename Group
  470. , typename Attributes, typename Locals
  471. , typename Delimiter, typename Attribute
  472. , typename Params>
  473. bool generate(OutputIterator& sink
  474. , subrule_context<Group, Attributes, Locals>& context
  475. , Delimiter const& delimiter, Attribute const& attr
  476. , Params const& params) const
  477. {
  478. return context.group.template generate_subrule_id<ID_>(
  479. sink, context, delimiter, attr, params);
  480. }
  481. template <typename OutputIterator, typename Context
  482. , typename Delimiter, typename Attribute
  483. , typename Params>
  484. bool generate(OutputIterator& /*sink*/
  485. , Context& /*context*/
  486. , Delimiter const& /*delimiter*/, Attribute const& /*attr*/
  487. , Params const& /*params*/) const
  488. {
  489. // If you are seeing a compilation error here, you are trying
  490. // to use a subrule as a generator outside of a subrule group.
  491. BOOST_SPIRIT_ASSERT_FAIL(OutputIterator
  492. , subrule_used_outside_subrule_group, (id_type));
  493. return false;
  494. }
  495. template <typename Context>
  496. info what(Context& /*context*/) const
  497. {
  498. return info(name_);
  499. }
  500. // bring in the operator() overloads
  501. this_type const& get_parameterized_subject() const { return *this; }
  502. typedef this_type parameterized_subject_type;
  503. #include <boost/spirit/home/karma/nonterminal/detail/fcall.hpp>
  504. std::string name_;
  505. };
  506. }}}}
  507. #if defined(BOOST_MSVC)
  508. # pragma warning(pop)
  509. #endif
  510. #endif