subrule.hpp 22 KB

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