rule.hpp 16 KB

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