rule.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(BOOST_SPIRIT_RULE_FEBRUARY_12_2007_1020AM)
  7. #define BOOST_SPIRIT_RULE_FEBRUARY_12_2007_1020AM
  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_reference.hpp>
  16. #include <boost/type_traits/is_same.hpp>
  17. #include <boost/fusion/include/vector.hpp>
  18. #include <boost/fusion/include/size.hpp>
  19. #include <boost/fusion/include/make_vector.hpp>
  20. #include <boost/fusion/include/cons.hpp>
  21. #include <boost/fusion/include/as_list.hpp>
  22. #include <boost/fusion/include/as_vector.hpp>
  23. #include <boost/spirit/home/support/unused.hpp>
  24. #include <boost/spirit/home/support/argument.hpp>
  25. #include <boost/spirit/home/support/context.hpp>
  26. #include <boost/spirit/home/support/info.hpp>
  27. #include <boost/spirit/home/qi/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/qi/reference.hpp>
  31. #include <boost/spirit/home/qi/nonterminal/detail/parameterized.hpp>
  32. #include <boost/spirit/home/qi/nonterminal/detail/parser_binder.hpp>
  33. #include <boost/spirit/home/qi/nonterminal/nonterminal_fwd.hpp>
  34. #include <boost/spirit/home/qi/skip_over.hpp>
  35. #if defined(BOOST_MSVC)
  36. # pragma warning(push)
  37. # pragma warning(disable: 4355) // 'this' : used in base member initializer list warning
  38. #endif
  39. namespace boost { namespace spirit { namespace qi
  40. {
  41. BOOST_PP_REPEAT(SPIRIT_ATTRIBUTES_LIMIT, SPIRIT_USING_ATTRIBUTE, _)
  42. using spirit::_pass_type;
  43. using spirit::_val_type;
  44. using spirit::_a_type;
  45. using spirit::_b_type;
  46. using spirit::_c_type;
  47. using spirit::_d_type;
  48. using spirit::_e_type;
  49. using spirit::_f_type;
  50. using spirit::_g_type;
  51. using spirit::_h_type;
  52. using spirit::_i_type;
  53. using spirit::_j_type;
  54. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  55. using spirit::_pass;
  56. using spirit::_val;
  57. using spirit::_a;
  58. using spirit::_b;
  59. using spirit::_c;
  60. using spirit::_d;
  61. using spirit::_e;
  62. using spirit::_f;
  63. using spirit::_g;
  64. using spirit::_h;
  65. using spirit::_i;
  66. using spirit::_j;
  67. #endif
  68. using spirit::info;
  69. using spirit::locals;
  70. template <
  71. typename Iterator, typename T1, typename T2, typename T3
  72. , typename T4>
  73. struct rule
  74. : proto::extends<
  75. typename proto::terminal<
  76. reference<rule<Iterator, T1, T2, T3, T4> const>
  77. >::type
  78. , rule<Iterator, T1, T2, T3, T4>
  79. >
  80. , parser<rule<Iterator, T1, T2, T3, T4> >
  81. {
  82. typedef Iterator iterator_type;
  83. typedef rule<Iterator, T1, T2, T3, T4> this_type;
  84. typedef reference<this_type const> reference_;
  85. typedef typename proto::terminal<reference_>::type terminal;
  86. typedef proto::extends<terminal, this_type> base_type;
  87. typedef mpl::vector<T1, T2, T3, T4> template_params;
  88. // The rule's locals_type: a sequence of types to be used as local variables
  89. typedef typename
  90. spirit::detail::extract_locals<template_params>::type
  91. locals_type;
  92. // The rule's skip-parser type
  93. typedef typename
  94. spirit::detail::extract_component<
  95. qi::domain, template_params>::type
  96. skipper_type;
  97. // The rule's signature
  98. typedef typename
  99. spirit::detail::extract_sig<template_params>::type
  100. sig_type;
  101. // The rule's encoding type
  102. typedef typename
  103. spirit::detail::extract_encoding<template_params>::type
  104. encoding_type;
  105. // This is the rule's attribute type
  106. typedef typename
  107. spirit::detail::attr_from_sig<sig_type>::type
  108. attr_type;
  109. typedef typename add_reference<attr_type>::type attr_reference_type;
  110. // parameter_types is a sequence of types passed as parameters to the rule
  111. typedef typename
  112. spirit::detail::params_from_sig<sig_type>::type
  113. parameter_types;
  114. static size_t const params_size =
  115. fusion::result_of::size<parameter_types>::type::value;
  116. typedef context<
  117. fusion::cons<attr_reference_type, parameter_types>
  118. , locals_type>
  119. context_type;
  120. typedef function<
  121. bool(Iterator& first, Iterator const& last
  122. , context_type& context
  123. , skipper_type const& skipper
  124. )>
  125. function_type;
  126. typedef typename
  127. mpl::if_<
  128. is_same<encoding_type, unused_type>
  129. , unused_type
  130. , tag::char_code<tag::encoding, encoding_type>
  131. >::type
  132. encoding_modifier_type;
  133. explicit rule(std::string const& name = "unnamed-rule")
  134. : base_type(terminal::make(reference_(*this)))
  135. , name_(name)
  136. {
  137. }
  138. rule(rule const& rhs)
  139. : base_type(terminal::make(reference_(*this)))
  140. , name_(rhs.name_)
  141. , f(rhs.f)
  142. {
  143. }
  144. template <typename Auto, typename Expr>
  145. static void define(rule& /*lhs*/, Expr const& /*expr*/, mpl::false_)
  146. {
  147. // Report invalid expression error as early as possible.
  148. // If you got an error_invalid_expression error message here,
  149. // then the expression (expr) is not a valid spirit qi expression.
  150. BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Expr);
  151. }
  152. template <typename Auto, typename Expr>
  153. static void define(rule& lhs, Expr const& expr, mpl::true_)
  154. {
  155. lhs.f = detail::bind_parser<Auto>(
  156. compile<qi::domain>(expr, encoding_modifier_type()));
  157. }
  158. template <typename Expr>
  159. rule(Expr const& expr, std::string const& name = "unnamed-rule")
  160. : base_type(terminal::make(reference_(*this)))
  161. , name_(name)
  162. {
  163. define<mpl::false_>(*this, expr, traits::matches<qi::domain, Expr>());
  164. }
  165. rule& operator=(rule const& rhs)
  166. {
  167. // The following assertion fires when you try to initialize a rule
  168. // from an uninitialized one. Did you mean to refer to the right
  169. // hand side rule instead of assigning from it? In this case you
  170. // should write lhs = rhs.alias();
  171. BOOST_ASSERT(rhs.f && "Did you mean rhs.alias() instead of rhs?");
  172. f = rhs.f;
  173. name_ = rhs.name_;
  174. return *this;
  175. }
  176. std::string const& name() const
  177. {
  178. return name_;
  179. }
  180. void name(std::string const& str)
  181. {
  182. name_ = str;
  183. }
  184. template <typename Expr>
  185. rule& operator=(Expr const& expr)
  186. {
  187. define<mpl::false_>(*this, expr, traits::matches<qi::domain, Expr>());
  188. return *this;
  189. }
  190. // VC7.1 has problems to resolve 'rule' without explicit template parameters
  191. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1400)
  192. // g++ 3.3 barfs if this is a member function :(
  193. template <typename Expr>
  194. friend rule& operator%=(rule& r, Expr const& expr)
  195. {
  196. define<mpl::true_>(r, expr, traits::matches<qi::domain, Expr>());
  197. return r;
  198. }
  199. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  200. // non-const version needed to suppress proto's %= kicking in
  201. template <typename Expr>
  202. friend rule& operator%=(rule& r, Expr& expr)
  203. {
  204. return r %= static_cast<Expr const&>(expr);
  205. }
  206. #else
  207. // for rvalue references
  208. template <typename Expr>
  209. friend rule& operator%=(rule& r, Expr&& expr)
  210. {
  211. define<mpl::true_>(r, expr, traits::matches<qi::domain, Expr>());
  212. return r;
  213. }
  214. #endif
  215. #else
  216. // both friend functions have to be defined out of class as VC7.1
  217. // will complain otherwise
  218. template <typename OutputIterator_, typename T1_, typename T2_
  219. , typename T3_, typename T4_, typename Expr>
  220. friend rule<OutputIterator_, T1_, T2_, T3_, T4_>& operator%=(
  221. rule<OutputIterator_, T1_, T2_, T3_, T4_>& r, Expr const& expr);
  222. // non-const version needed to suppress proto's %= kicking in
  223. template <typename OutputIterator_, typename T1_, typename T2_
  224. , typename T3_, typename T4_, typename Expr>
  225. friend rule<OutputIterator_, T1_, T2_, T3_, T4_>& operator%=(
  226. rule<OutputIterator_, T1_, T2_, T3_, T4_>& r, Expr& expr);
  227. #endif
  228. template <typename Context, typename Iterator_>
  229. struct attribute
  230. {
  231. typedef attr_type type;
  232. };
  233. template <typename Context, typename Skipper, typename Attribute>
  234. bool parse(Iterator& first, Iterator const& last
  235. , Context& /*context*/, Skipper const& skipper
  236. , Attribute& attr_param) const
  237. {
  238. if (f)
  239. {
  240. // do a preskip if this is an implied lexeme
  241. if (is_same<skipper_type, unused_type>::value)
  242. qi::skip_over(first, last, skipper);
  243. typedef traits::make_attribute<attr_type, Attribute> make_attribute;
  244. // do down-stream transformation, provides attribute for
  245. // rhs parser
  246. typedef traits::transform_attribute<
  247. typename make_attribute::type, attr_type, domain>
  248. transform;
  249. typename make_attribute::type made_attr = make_attribute::call(attr_param);
  250. typename transform::type attr_ = transform::pre(made_attr);
  251. // If you are seeing a compilation error here, you are probably
  252. // trying to use a rule or a grammar which has inherited
  253. // attributes, without passing values for them.
  254. context_type context(attr_);
  255. // If you are seeing a compilation error here stating that the
  256. // fourth parameter can't be converted to a required target type
  257. // then you are probably trying to use a rule or a grammar with
  258. // an incompatible skipper type.
  259. if (f(first, last, context, skipper))
  260. {
  261. // do up-stream transformation, this integrates the results
  262. // back into the original attribute value, if appropriate
  263. traits::post_transform(attr_param, attr_);
  264. return true;
  265. }
  266. // inform attribute transformation of failed rhs
  267. traits::fail_transform(attr_param, attr_);
  268. }
  269. return false;
  270. }
  271. template <typename Context, typename Skipper
  272. , typename Attribute, typename Params>
  273. bool parse(Iterator& first, Iterator const& last
  274. , Context& caller_context, Skipper const& skipper
  275. , Attribute& attr_param, Params const& params) const
  276. {
  277. if (f)
  278. {
  279. // do a preskip if this is an implied lexeme
  280. if (is_same<skipper_type, unused_type>::value)
  281. qi::skip_over(first, last, skipper);
  282. typedef traits::make_attribute<attr_type, Attribute> make_attribute;
  283. // do down-stream transformation, provides attribute for
  284. // rhs parser
  285. typedef traits::transform_attribute<
  286. typename make_attribute::type, attr_type, domain>
  287. transform;
  288. typename make_attribute::type made_attr = make_attribute::call(attr_param);
  289. typename transform::type attr_ = transform::pre(made_attr);
  290. // If you are seeing a compilation error here, you are probably
  291. // trying to use a rule or a grammar which has inherited
  292. // attributes, passing values of incompatible types for them.
  293. context_type context(attr_, params, caller_context);
  294. // If you are seeing a compilation error here stating that the
  295. // fourth parameter can't be converted to a required target type
  296. // then you are probably trying to use a rule or a grammar with
  297. // an incompatible skipper type.
  298. if (f(first, last, context, skipper))
  299. {
  300. // do up-stream transformation, this integrates the results
  301. // back into the original attribute value, if appropriate
  302. traits::post_transform(attr_param, attr_);
  303. return true;
  304. }
  305. // inform attribute transformation of failed rhs
  306. traits::fail_transform(attr_param, attr_);
  307. }
  308. return false;
  309. }
  310. template <typename Context>
  311. info what(Context& /*context*/) const
  312. {
  313. return info(name_);
  314. }
  315. reference_ alias() const
  316. {
  317. return reference_(*this);
  318. }
  319. typename proto::terminal<this_type>::type copy() const
  320. {
  321. typename proto::terminal<this_type>::type result = {*this};
  322. return result;
  323. }
  324. // bring in the operator() overloads
  325. rule const& get_parameterized_subject() const { return *this; }
  326. typedef rule parameterized_subject_type;
  327. #include <boost/spirit/home/qi/nonterminal/detail/fcall.hpp>
  328. std::string name_;
  329. function_type f;
  330. };
  331. #if BOOST_WORKAROUND(BOOST_MSVC, < 1400)
  332. template <typename OutputIterator_, typename T1_, typename T2_
  333. , typename T3_, typename T4_, typename Expr>
  334. rule<OutputIterator_, T1_, T2_, T3_, T4_>& operator%=(
  335. rule<OutputIterator_, T1_, T2_, T3_, T4_>& r, Expr const& expr)
  336. {
  337. // Report invalid expression error as early as possible.
  338. // If you got an error_invalid_expression error message here,
  339. // then the expression (expr) is not a valid spirit qi expression.
  340. BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Expr);
  341. typedef typename
  342. rule<OutputIterator_, T1_, T2_, T3_, T4_>::encoding_modifier_type
  343. encoding_modifier_type;
  344. r.f = detail::bind_parser<mpl::true_>(
  345. compile<qi::domain>(expr, encoding_modifier_type()));
  346. return r;
  347. }
  348. template <typename Iterator_, typename T1_, typename T2_
  349. , typename T3_, typename T4_, typename Expr>
  350. rule<Iterator_, T1_, T2_, T3_, T4_>& operator%=(
  351. rule<Iterator_, T1_, T2_, T3_, T4_>& r, Expr& expr)
  352. {
  353. return r %= static_cast<Expr const&>(expr);
  354. }
  355. #endif
  356. }}}
  357. namespace boost { namespace spirit { namespace traits
  358. {
  359. ///////////////////////////////////////////////////////////////////////////
  360. template <
  361. typename IteratorA, typename IteratorB, typename Attribute
  362. , typename Context, typename T1, typename T2, typename T3, typename T4>
  363. struct handles_container<
  364. qi::rule<IteratorA, T1, T2, T3, T4>, Attribute, Context, IteratorB>
  365. : traits::is_container<
  366. typename attribute_of<
  367. qi::rule<IteratorA, T1, T2, T3, T4>, Context, IteratorB
  368. >::type
  369. >
  370. {};
  371. }}}
  372. #if defined(BOOST_MSVC)
  373. # pragma warning(pop)
  374. #endif
  375. #endif