rule.hpp 16 KB

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