parser_traits.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Joel de Guzman
  3. Copyright (c) 2002-2003 Hartmut Kaiser
  4. Copyright (c) 2003 Martin Wille
  5. http://spirit.sourceforge.net/
  6. Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #if !defined(BOOST_SPIRIT_PARSER_TRAITS_HPP)
  10. #define BOOST_SPIRIT_PARSER_TRAITS_HPP
  11. #include <boost/type_traits/is_base_and_derived.hpp>
  12. #include <boost/static_assert.hpp>
  13. #include <boost/spirit/home/classic/namespace.hpp>
  14. #include <boost/spirit/home/classic/core/parser.hpp>
  15. #include <boost/spirit/home/classic/meta/impl/parser_traits.ipp>
  16. ///////////////////////////////////////////////////////////////////////////////
  17. namespace boost { namespace spirit {
  18. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  19. ///////////////////////////////////////////////////////////////////////////////
  20. //
  21. // Parser traits templates
  22. //
  23. // Used to determine the type and several other characteristics of a given
  24. // parser type.
  25. //
  26. ///////////////////////////////////////////////////////////////////////////////
  27. ///////////////////////////////////////////////////////////////////////////////
  28. //
  29. // The is_parser traits template can be used to tell wether a given
  30. // class is a parser.
  31. //
  32. ///////////////////////////////////////////////////////////////////////////////
  33. template <typename T>
  34. struct is_parser
  35. {
  36. BOOST_STATIC_CONSTANT(bool, value =
  37. (::boost::is_base_and_derived<parser<T>, T>::value));
  38. // [JDG 2/3/03] simplified implementation by
  39. // using boost::is_base_and_derived
  40. };
  41. ///////////////////////////////////////////////////////////////////////////////
  42. //
  43. // The is_unary_composite traits template can be used to tell if a given
  44. // parser is a unary parser as for instance kleene_star or optional.
  45. //
  46. ///////////////////////////////////////////////////////////////////////////////
  47. template <typename UnaryT>
  48. struct is_unary_composite {
  49. BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
  50. typename UnaryT::parser_category_t, unary_parser_category>::value));
  51. };
  52. ///////////////////////////////////////////////////////////////////////////////
  53. //
  54. // The is_acction_parser traits template can be used to tell if a given
  55. // parser is a action parser, i.e. it is a composite consisting of a
  56. // auxilliary parser and an attached semantic action.
  57. //
  58. ///////////////////////////////////////////////////////////////////////////////
  59. template <typename ActionT>
  60. struct is_action_parser {
  61. BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
  62. typename ActionT::parser_category_t, action_parser_category>::value));
  63. };
  64. ///////////////////////////////////////////////////////////////////////////////
  65. //
  66. // The is_binary_composite traits template can be used to tell if a given
  67. // parser is a binary parser as for instance sequence or difference.
  68. //
  69. ///////////////////////////////////////////////////////////////////////////////
  70. template <typename BinaryT>
  71. struct is_binary_composite {
  72. BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
  73. typename BinaryT::parser_category_t, binary_parser_category>::value));
  74. };
  75. ///////////////////////////////////////////////////////////////////////////////
  76. //
  77. // The is_composite_parser traits template can be used to tell if a given
  78. // parser is a unary or a binary parser composite type.
  79. //
  80. ///////////////////////////////////////////////////////////////////////////////
  81. template <typename CompositeT>
  82. struct is_composite_parser {
  83. BOOST_STATIC_CONSTANT(bool, value = (
  84. ::BOOST_SPIRIT_CLASSIC_NS::is_unary_composite<CompositeT>::value ||
  85. ::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<CompositeT>::value));
  86. };
  87. ///////////////////////////////////////////////////////////////////////////////
  88. template <typename ParserT>
  89. struct is_alternative {
  90. BOOST_STATIC_CONSTANT(bool, value = (
  91. ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_alternative));
  92. };
  93. template <typename ParserT>
  94. struct is_sequence {
  95. BOOST_STATIC_CONSTANT(bool, value = (
  96. ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_sequence));
  97. };
  98. template <typename ParserT>
  99. struct is_sequential_or {
  100. BOOST_STATIC_CONSTANT(bool, value = (
  101. ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_sequential_or));
  102. };
  103. template <typename ParserT>
  104. struct is_intersection {
  105. BOOST_STATIC_CONSTANT(bool, value = (
  106. ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_intersection));
  107. };
  108. template <typename ParserT>
  109. struct is_difference {
  110. BOOST_STATIC_CONSTANT(bool, value = (
  111. ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_difference));
  112. };
  113. template <typename ParserT>
  114. struct is_exclusive_or {
  115. BOOST_STATIC_CONSTANT(bool, value = (
  116. ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_exclusive_or));
  117. };
  118. template <typename ParserT>
  119. struct is_optional {
  120. BOOST_STATIC_CONSTANT(bool, value = (
  121. ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_optional));
  122. };
  123. template <typename ParserT>
  124. struct is_kleene_star {
  125. BOOST_STATIC_CONSTANT(bool, value = (
  126. ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_kleene_star));
  127. };
  128. template <typename ParserT>
  129. struct is_positive {
  130. BOOST_STATIC_CONSTANT(bool, value = (
  131. ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_positive));
  132. };
  133. ///////////////////////////////////////////////////////////////////////////////
  134. //
  135. // Parser extraction templates
  136. //
  137. ///////////////////////////////////////////////////////////////////////////////
  138. ///////////////////////////////////////////////////////////////////////////////
  139. //
  140. // The unary_subject template can be used to return the type of the
  141. // parser used as the subject of an unary parser.
  142. // If the parser under inspection is not an unary type parser the compilation
  143. // will fail.
  144. //
  145. ///////////////////////////////////////////////////////////////////////////////
  146. template <typename UnaryT>
  147. struct unary_subject {
  148. BOOST_STATIC_ASSERT(BOOST_SPIRIT_CLASSIC_NS::is_unary_composite<UnaryT>::value);
  149. typedef typename UnaryT::subject_t type;
  150. };
  151. ///////////////////////////////////////////////////////////////////////////////
  152. //
  153. // The get_unary_subject template function returns the parser object, which
  154. // is used as the subject of an unary parser.
  155. // If the parser under inspection is not an unary type parser the compilation
  156. // will fail.
  157. //
  158. ///////////////////////////////////////////////////////////////////////////////
  159. template <typename UnaryT>
  160. inline typename unary_subject<UnaryT>::type const &
  161. get_unary_subject(UnaryT const &unary_)
  162. {
  163. BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_unary_composite<UnaryT>::value);
  164. return unary_.subject();
  165. }
  166. ///////////////////////////////////////////////////////////////////////////////
  167. //
  168. // The binary_left_subject and binary_right_subject templates can be used to
  169. // return the types of the parsers used as the left and right subject of an
  170. // binary parser.
  171. // If the parser under inspection is not a binary type parser the compilation
  172. // will fail.
  173. //
  174. ///////////////////////////////////////////////////////////////////////////////
  175. template <typename BinaryT>
  176. struct binary_left_subject {
  177. BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
  178. typedef typename BinaryT::left_t type;
  179. };
  180. template <typename BinaryT>
  181. struct binary_right_subject {
  182. BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
  183. typedef typename BinaryT::right_t type;
  184. };
  185. ///////////////////////////////////////////////////////////////////////////////
  186. //
  187. // The get_binary_left_subject and get_binary_right_subject template functions
  188. // return the parser object, which is used as the left or right subject of a
  189. // binary parser.
  190. // If the parser under inspection is not a binary type parser the compilation
  191. // will fail.
  192. //
  193. ///////////////////////////////////////////////////////////////////////////////
  194. template <typename BinaryT>
  195. inline typename binary_left_subject<BinaryT>::type const &
  196. get_binary_left_subject(BinaryT const &binary_)
  197. {
  198. BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
  199. return binary_.left();
  200. }
  201. template <typename BinaryT>
  202. inline typename binary_right_subject<BinaryT>::type const &
  203. get_binary_right_subject(BinaryT const &binary_)
  204. {
  205. BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
  206. return binary_.right();
  207. }
  208. ///////////////////////////////////////////////////////////////////////////////
  209. //
  210. // The action_subject template can be used to return the type of the
  211. // parser used as the subject of an action parser.
  212. // If the parser under inspection is not an action type parser the compilation
  213. // will fail.
  214. //
  215. ///////////////////////////////////////////////////////////////////////////////
  216. template <typename ActionT>
  217. struct action_subject {
  218. BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
  219. typedef typename ActionT::subject_t type;
  220. };
  221. ///////////////////////////////////////////////////////////////////////////////
  222. //
  223. // The get_action_subject template function returns the parser object, which
  224. // is used as the subject of an action parser.
  225. // If the parser under inspection is not an action type parser the compilation
  226. // will fail.
  227. //
  228. ///////////////////////////////////////////////////////////////////////////////
  229. template <typename ActionT>
  230. inline typename action_subject<ActionT>::type const &
  231. get_action_subject(ActionT const &action_)
  232. {
  233. BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
  234. return action_.subject();
  235. }
  236. ///////////////////////////////////////////////////////////////////////////////
  237. //
  238. // The semantic_action template can be used to return the type of the
  239. // attached semantic action of an action parser.
  240. // If the parser under inspection is not an action type parser the compilation
  241. // will fail.
  242. //
  243. ///////////////////////////////////////////////////////////////////////////////
  244. template <typename ActionT>
  245. struct semantic_action {
  246. BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
  247. typedef typename ActionT::predicate_t type;
  248. };
  249. ///////////////////////////////////////////////////////////////////////////////
  250. //
  251. // The get_semantic_action template function returns the attached semantic
  252. // action of an action parser.
  253. // If the parser under inspection is not an action type parser the compilation
  254. // will fail.
  255. //
  256. ///////////////////////////////////////////////////////////////////////////////
  257. template <typename ActionT>
  258. inline typename semantic_action<ActionT>::type const &
  259. get_semantic_action(ActionT const &action_)
  260. {
  261. BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
  262. return action_.predicate();
  263. }
  264. ///////////////////////////////////////////////////////////////////////////////
  265. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  266. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  267. #endif // !defined(BOOST_SPIRIT_PARSER_TRAITS_HPP)