subrule.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*=============================================================================
  2. Copyright (c) 2009 Francois Barel
  3. Copyright (c) 2001-2011 Joel de Guzman
  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. ==============================================================================*/
  7. #if !defined(BOOST_SPIRIT_REPOSITORY_QI_SUBRULE_AUGUST_06_2009_0239AM)
  8. #define BOOST_SPIRIT_REPOSITORY_QI_SUBRULE_AUGUST_06_2009_0239AM
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/spirit/home/qi/domain.hpp>
  13. #include <boost/spirit/home/qi/meta_compiler.hpp>
  14. #include <boost/spirit/home/qi/parser.hpp>
  15. #include <boost/spirit/home/qi/reference.hpp>
  16. #include <boost/spirit/home/qi/nonterminal/detail/parameterized.hpp>
  17. #include <boost/spirit/home/qi/nonterminal/detail/parser_binder.hpp>
  18. #include <boost/spirit/home/support/argument.hpp>
  19. #include <boost/spirit/home/support/assert_msg.hpp>
  20. #include <boost/spirit/home/qi/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 qi
  49. {
  50. ///////////////////////////////////////////////////////////////////////////
  51. // subrule_group_parser:
  52. // - parser representing a group of subrule definitions (one or more),
  53. // invokes first subrule on entry,
  54. ///////////////////////////////////////////////////////////////////////////
  55. template <typename Defs>
  56. struct subrule_group_parser
  57. : spirit::qi::parser<subrule_group_parser<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_parser<Defs> this_type;
  63. explicit subrule_group_parser(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 Iterator, typename Context
  96. , typename Skipper, typename Attribute>
  97. bool parse(Iterator& first, Iterator const& last
  98. , Context& context, Skipper const& skipper
  99. , Attribute& attr) const
  100. {
  101. // Forward to first subrule.
  102. return parse_subrule(fusion::front(defs).second
  103. , first, last, context, skipper, attr);
  104. }
  105. template <typename Iterator, typename Context
  106. , typename Skipper, typename Attribute, typename Params>
  107. bool parse(Iterator& first, Iterator const& last
  108. , Context& context, Skipper const& skipper
  109. , Attribute& attr, Params const& params) const
  110. {
  111. // Forward to first subrule.
  112. return parse_subrule(fusion::front(defs).second
  113. , first, last, context, skipper, attr, params);
  114. }
  115. template <int ID, typename Iterator, typename Context
  116. , typename Skipper, typename Attribute>
  117. bool parse_subrule_id(Iterator& first, Iterator const& last
  118. , Context& context, Skipper const& skipper
  119. , Attribute& attr) const
  120. {
  121. return parse_subrule(def<ID>()
  122. , first, last, context, skipper, attr);
  123. }
  124. template <int ID, typename Iterator, typename Context
  125. , typename Skipper, typename Attribute, typename Params>
  126. bool parse_subrule_id(Iterator& first, Iterator const& last
  127. , Context& context, Skipper const& skipper
  128. , Attribute& attr, Params const& params) const
  129. {
  130. return parse_subrule(def<ID>()
  131. , first, last, context, skipper, attr, params);
  132. }
  133. template <typename Def
  134. , typename Iterator, typename Context
  135. , typename Skipper, typename Attribute>
  136. bool parse_subrule(Def const& def
  137. , Iterator& first, Iterator const& last
  138. , Context& /*caller_context*/, Skipper const& skipper
  139. , Attribute& attr) const
  140. {
  141. // compute context type for this subrule
  142. typedef typename Def::locals_type subrule_locals_type;
  143. typedef typename Def::attr_type subrule_attr_type;
  144. typedef typename Def::attr_reference_type subrule_attr_reference_type;
  145. typedef typename Def::parameter_types subrule_parameter_types;
  146. typedef
  147. subrule_context<
  148. this_type
  149. , fusion::cons<
  150. subrule_attr_reference_type, subrule_parameter_types>
  151. , subrule_locals_type
  152. >
  153. context_type;
  154. // do down-stream transformation, provides attribute for
  155. // rhs parser
  156. typedef traits::transform_attribute<
  157. Attribute, subrule_attr_type, spirit::qi::domain>
  158. transform;
  159. typename transform::type attr_ = transform::pre(attr);
  160. // If you are seeing a compilation error here, you are probably
  161. // trying to use a subrule which has inherited attributes,
  162. // without passing values for them.
  163. context_type context(*this, attr_);
  164. if (def.binder(first, last, context, skipper))
  165. {
  166. // do up-stream transformation, this integrates the results
  167. // back into the original attribute value, if appropriate
  168. transform::post(attr, attr_);
  169. return true;
  170. }
  171. // inform attribute transformation of failed rhs
  172. transform::fail(attr);
  173. return false;
  174. }
  175. template <typename Def
  176. , typename Iterator, typename Context
  177. , typename Skipper, typename Attribute, typename Params>
  178. bool parse_subrule(Def const& def
  179. , Iterator& first, Iterator const& last
  180. , Context& caller_context, Skipper const& skipper
  181. , Attribute& attr, Params const& params) const
  182. {
  183. // compute context type for this subrule
  184. typedef typename Def::locals_type subrule_locals_type;
  185. typedef typename Def::attr_type subrule_attr_type;
  186. typedef typename Def::attr_reference_type subrule_attr_reference_type;
  187. typedef typename Def::parameter_types subrule_parameter_types;
  188. typedef
  189. subrule_context<
  190. this_type
  191. , fusion::cons<
  192. subrule_attr_reference_type, subrule_parameter_types>
  193. , subrule_locals_type
  194. >
  195. context_type;
  196. // do down-stream transformation, provides attribute for
  197. // rhs parser
  198. typedef traits::transform_attribute<
  199. Attribute, subrule_attr_type, spirit::qi::domain>
  200. transform;
  201. typename transform::type attr_ = transform::pre(attr);
  202. // If you are seeing a compilation error here, you are probably
  203. // trying to use a subrule which has inherited attributes,
  204. // passing values of incompatible types for them.
  205. context_type context(*this, attr_, params, caller_context);
  206. if (def.binder(first, last, context, skipper))
  207. {
  208. // do up-stream transformation, this integrates the results
  209. // back into the original attribute value, if appropriate
  210. transform::post(attr, attr_);
  211. return true;
  212. }
  213. // inform attribute transformation of failed rhs
  214. transform::fail(attr);
  215. return false;
  216. }
  217. template <typename Context>
  218. info what(Context& context) const
  219. {
  220. // Forward to first subrule.
  221. return fusion::front(defs).second.binder.p.what(context);
  222. }
  223. Defs defs;
  224. };
  225. ///////////////////////////////////////////////////////////////////////////
  226. // subrule_group:
  227. // - a Proto terminal, so that a group behaves like any Spirit
  228. // expression.
  229. ///////////////////////////////////////////////////////////////////////////
  230. template <typename Defs>
  231. struct subrule_group
  232. : proto::extends<
  233. typename proto::terminal<
  234. subrule_group_parser<Defs>
  235. >::type
  236. , subrule_group<Defs>
  237. >
  238. {
  239. typedef subrule_group_parser<Defs> parser_type;
  240. typedef typename proto::terminal<parser_type>::type terminal;
  241. static size_t const params_size =
  242. // Forward to first subrule.
  243. remove_reference<
  244. typename fusion::result_of::front<Defs>::type
  245. >::type::second_type::params_size;
  246. explicit subrule_group(Defs const& defs)
  247. : subrule_group::proto_extends(terminal::make(parser_type(defs)))
  248. {
  249. }
  250. parser_type const& parser() const { return proto::value(*this); }
  251. Defs const& defs() const { return parser().defs; }
  252. template <typename Defs2>
  253. subrule_group<
  254. typename fusion::result_of::as_map<
  255. typename fusion::result_of::join<
  256. Defs const, Defs2 const>::type>::type>
  257. operator,(subrule_group<Defs2> const& other) const
  258. {
  259. typedef subrule_group<
  260. typename fusion::result_of::as_map<
  261. typename fusion::result_of::join<
  262. Defs const, Defs2 const>::type>::type> result_type;
  263. return result_type(fusion::as_map(fusion::join(defs(), other.defs())));
  264. }
  265. // non-const versions needed to suppress proto's comma op kicking in
  266. template <typename Defs2>
  267. friend subrule_group<
  268. typename fusion::result_of::as_map<
  269. typename fusion::result_of::join<
  270. Defs const, Defs2 const>::type>::type>
  271. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  272. operator,(subrule_group&& left, subrule_group<Defs2>&& other)
  273. #else
  274. operator,(subrule_group& left, subrule_group<Defs2>& other)
  275. #endif
  276. {
  277. return static_cast<subrule_group const&>(left)
  278. .operator,(static_cast<subrule_group<Defs2> const&>(other));
  279. }
  280. // bring in the operator() overloads
  281. parser_type const& get_parameterized_subject() const { return parser(); }
  282. typedef parser_type parameterized_subject_type;
  283. #include <boost/spirit/home/qi/nonterminal/detail/fcall.hpp>
  284. };
  285. ///////////////////////////////////////////////////////////////////////////
  286. // subrule_definition: holds one particular definition of a subrule
  287. ///////////////////////////////////////////////////////////////////////////
  288. template <
  289. int ID_
  290. , typename Locals
  291. , typename Attr
  292. , typename AttrRef
  293. , typename Parameters
  294. , size_t ParamsSize
  295. , typename Subject
  296. , bool Auto_
  297. >
  298. struct subrule_definition
  299. {
  300. typedef mpl::int_<ID_> id_type;
  301. BOOST_STATIC_CONSTANT(int, ID = ID_);
  302. typedef Locals locals_type;
  303. typedef Attr attr_type;
  304. typedef AttrRef attr_reference_type;
  305. typedef Parameters parameter_types;
  306. static size_t const params_size = ParamsSize;
  307. typedef Subject subject_type;
  308. typedef mpl::bool_<Auto_> auto_type;
  309. BOOST_STATIC_CONSTANT(bool, Auto = Auto_);
  310. typedef spirit::qi::detail::parser_binder<
  311. Subject, auto_type> binder_type;
  312. subrule_definition(Subject const& subject, std::string const& name)
  313. : binder(subject), name(name)
  314. {
  315. }
  316. binder_type const binder;
  317. std::string const name;
  318. };
  319. ///////////////////////////////////////////////////////////////////////////
  320. // subrule placeholder:
  321. // - on subrule definition: helper for creation of subrule_group,
  322. // - on subrule invocation: Proto terminal and parser.
  323. ///////////////////////////////////////////////////////////////////////////
  324. template <
  325. int ID_
  326. , typename T1 = unused_type
  327. , typename T2 = unused_type
  328. >
  329. struct subrule
  330. : proto::extends<
  331. typename proto::terminal<
  332. spirit::qi::reference<subrule<ID_, T1, T2> const>
  333. >::type
  334. , subrule<ID_, T1, T2>
  335. >
  336. , spirit::qi::parser<subrule<ID_, T1, T2> >
  337. {
  338. typedef mpl::int_<ID_> id_type;
  339. BOOST_STATIC_CONSTANT(int, ID = ID_);
  340. typedef subrule<ID_, T1, T2> this_type;
  341. typedef spirit::qi::reference<this_type const> reference_;
  342. typedef typename proto::terminal<reference_>::type terminal;
  343. typedef proto::extends<terminal, this_type> base_type;
  344. typedef mpl::vector<T1, T2> template_params;
  345. // The subrule's locals_type: a sequence of types to be used as local variables
  346. typedef typename
  347. spirit::detail::extract_locals<template_params>::type
  348. locals_type;
  349. // The subrule's encoding type
  350. typedef typename
  351. spirit::detail::extract_encoding<template_params>::type
  352. encoding_type;
  353. // The subrule's signature
  354. typedef typename
  355. spirit::detail::extract_sig<template_params, encoding_type
  356. , spirit::qi::domain>::type
  357. sig_type;
  358. // This is the subrule's attribute type
  359. typedef typename
  360. spirit::detail::attr_from_sig<sig_type>::type
  361. attr_type;
  362. typedef typename add_reference<attr_type>::type attr_reference_type;
  363. // parameter_types is a sequence of types passed as parameters to the subrule
  364. typedef typename
  365. spirit::detail::params_from_sig<sig_type>::type
  366. parameter_types;
  367. static size_t const params_size =
  368. fusion::result_of::size<parameter_types>::type::value;
  369. explicit subrule(std::string const& name_ = "unnamed-subrule")
  370. : base_type(terminal::make(reference_(*this)))
  371. , name_(name_)
  372. {
  373. }
  374. // compute type of this subrule's definition for expr type Expr
  375. template <typename Expr, bool Auto>
  376. struct def_type_helper
  377. {
  378. // Report invalid expression error as early as possible.
  379. // If you got an error_invalid_expression error message here,
  380. // then the expression (Expr) is not a valid spirit qi expression.
  381. BOOST_SPIRIT_ASSERT_MATCH(spirit::qi::domain, Expr);
  382. typedef typename result_of::compile<
  383. spirit::qi::domain, Expr>::type subject_type;
  384. typedef subrule_definition<
  385. ID_
  386. , locals_type
  387. , attr_type
  388. , attr_reference_type
  389. , parameter_types
  390. , params_size
  391. , subject_type
  392. , Auto
  393. > const type;
  394. };
  395. // compute type of subrule group containing only this
  396. // subrule's definition for expr type Expr
  397. template <typename Expr, bool Auto>
  398. struct group_type_helper
  399. {
  400. typedef typename def_type_helper<Expr, Auto>::type def_type;
  401. // create Defs map with only one entry: (ID -> def)
  402. typedef typename
  403. #ifndef BOOST_FUSION_HAS_VARIADIC_MAP
  404. fusion::result_of::make_map<id_type, def_type>::type
  405. #else
  406. fusion::result_of::make_map<id_type>::template apply<def_type>::type
  407. #endif
  408. defs_type;
  409. typedef subrule_group<defs_type> type;
  410. };
  411. template <typename Expr>
  412. typename group_type_helper<Expr, false>::type
  413. operator=(Expr const& expr) const
  414. {
  415. typedef group_type_helper<Expr, false> helper;
  416. typedef typename helper::def_type def_type;
  417. typedef typename helper::type result_type;
  418. return result_type(fusion::make_map<id_type>(
  419. def_type(compile<spirit::qi::domain>(expr), name_)));
  420. }
  421. #define SUBRULE_MODULUS_ASSIGN_OPERATOR(lhs_ref, rhs_ref) \
  422. template <typename Expr> \
  423. friend typename group_type_helper<Expr, true>::type \
  424. operator%=(subrule lhs_ref sr, Expr rhs_ref expr) \
  425. { \
  426. typedef group_type_helper<Expr, true> helper; \
  427. typedef typename helper::def_type def_type; \
  428. typedef typename helper::type result_type; \
  429. return result_type(fusion::make_map<id_type>( \
  430. def_type(compile<spirit::qi::domain>(expr), sr.name_))); \
  431. } \
  432. /**/
  433. // non-const versions needed to suppress proto's %= kicking in
  434. SUBRULE_MODULUS_ASSIGN_OPERATOR(const&, const&)
  435. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  436. SUBRULE_MODULUS_ASSIGN_OPERATOR(const&, &&)
  437. #else
  438. SUBRULE_MODULUS_ASSIGN_OPERATOR(const&, &)
  439. #endif
  440. SUBRULE_MODULUS_ASSIGN_OPERATOR(&, const&)
  441. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  442. SUBRULE_MODULUS_ASSIGN_OPERATOR(&, &&)
  443. #else
  444. SUBRULE_MODULUS_ASSIGN_OPERATOR(&, &)
  445. #endif
  446. #undef SUBRULE_MODULUS_ASSIGN_OPERATOR
  447. std::string const& name() const
  448. {
  449. return name_;
  450. }
  451. void name(std::string const& str)
  452. {
  453. name_ = str;
  454. }
  455. template <typename Context, typename Iterator>
  456. struct attribute
  457. {
  458. typedef attr_type type;
  459. };
  460. template <typename Iterator, typename Group
  461. , typename Attributes, typename Locals
  462. , typename Skipper, typename Attribute>
  463. bool parse(Iterator& first, Iterator const& last
  464. , subrule_context<Group, Attributes, Locals>& context
  465. , Skipper const& skipper, Attribute& attr) const
  466. {
  467. return context.group.template parse_subrule_id<ID_>(
  468. first, last, context, skipper, attr);
  469. }
  470. template <typename Iterator, typename Context
  471. , typename Skipper, typename Attribute>
  472. bool parse(Iterator& /*first*/, Iterator const& /*last*/
  473. , Context& /*context*/
  474. , Skipper const& /*skipper*/, Attribute& /*attr*/) const
  475. {
  476. // If you are seeing a compilation error here, you are trying
  477. // to use a subrule as a parser outside of a subrule group.
  478. BOOST_SPIRIT_ASSERT_FAIL(Iterator
  479. , subrule_used_outside_subrule_group, (id_type));
  480. return false;
  481. }
  482. template <typename Iterator, typename Group
  483. , typename Attributes, typename Locals
  484. , typename Skipper, typename Attribute
  485. , typename Params>
  486. bool parse(Iterator& first, Iterator const& last
  487. , subrule_context<Group, Attributes, Locals>& context
  488. , Skipper const& skipper, Attribute& attr
  489. , Params const& params) const
  490. {
  491. return context.group.template parse_subrule_id<ID_>(
  492. first, last, context, skipper, attr, params);
  493. }
  494. template <typename Iterator, typename Context
  495. , typename Skipper, typename Attribute
  496. , typename Params>
  497. bool parse(Iterator& /*first*/, Iterator const& /*last*/
  498. , Context& /*context*/
  499. , Skipper const& /*skipper*/, Attribute& /*attr*/
  500. , Params const& /*params*/) const
  501. {
  502. // If you are seeing a compilation error here, you are trying
  503. // to use a subrule as a parser outside of a subrule group.
  504. BOOST_SPIRIT_ASSERT_FAIL(Iterator
  505. , subrule_used_outside_subrule_group, (id_type));
  506. return false;
  507. }
  508. template <typename Context>
  509. info what(Context& /*context*/) const
  510. {
  511. return info(name_);
  512. }
  513. // bring in the operator() overloads
  514. this_type const& get_parameterized_subject() const { return *this; }
  515. typedef this_type parameterized_subject_type;
  516. #include <boost/spirit/home/qi/nonterminal/detail/fcall.hpp>
  517. std::string name_;
  518. };
  519. }}}}
  520. #if defined(BOOST_MSVC)
  521. # pragma warning(pop)
  522. #endif
  523. #endif