rule.hpp 15 KB

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