parse.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #if !defined(BOOST_SPIRIT_PARSE_APRIL_16_2006_0442PM)
  8. #define BOOST_SPIRIT_PARSE_APRIL_16_2006_0442PM
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/spirit/home/support/context.hpp>
  13. #include <boost/spirit/home/support/nonterminal/locals.hpp>
  14. #include <boost/spirit/home/qi/detail/parse.hpp>
  15. #include <boost/iterator/iterator_concepts.hpp>
  16. namespace boost { namespace spirit { namespace qi
  17. {
  18. ///////////////////////////////////////////////////////////////////////////
  19. template <typename Iterator, typename Expr>
  20. inline bool
  21. parse(
  22. Iterator& first
  23. , Iterator last
  24. , Expr const& expr)
  25. {
  26. // Make sure the iterator is at least a readable forward traversal iterator.
  27. // If you got a compilation error here, then you are using a weaker iterator
  28. // while calling this function, you need to supply a readable forward traversal
  29. // iterator instead.
  30. BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIteratorConcept<Iterator>));
  31. BOOST_CONCEPT_ASSERT((boost_concepts::ForwardTraversalConcept<Iterator>));
  32. return detail::parse_impl<Expr>::call(first, last, expr);
  33. }
  34. template <typename Iterator, typename Expr>
  35. inline bool
  36. parse(
  37. Iterator const& first_
  38. , Iterator last
  39. , Expr const& expr)
  40. {
  41. Iterator first = first_;
  42. return qi::parse(first, last, expr);
  43. }
  44. ///////////////////////////////////////////////////////////////////////////
  45. namespace detail
  46. {
  47. template <typename T>
  48. struct make_context
  49. {
  50. typedef context<fusion::cons<T&>, locals<> > type;
  51. };
  52. template <>
  53. struct make_context<unused_type>
  54. {
  55. typedef unused_type type;
  56. };
  57. }
  58. template <typename Iterator, typename Expr, typename Attr>
  59. inline bool
  60. parse(
  61. Iterator& first
  62. , Iterator last
  63. , Expr const& expr
  64. , Attr& attr)
  65. {
  66. // Make sure the iterator is at least a readable forward traversal iterator.
  67. // If you got a compilation error here, then you are using a weaker iterator
  68. // while calling this function, you need to supply a readable forward traversal
  69. // iterator instead.
  70. BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIteratorConcept<Iterator>));
  71. BOOST_CONCEPT_ASSERT((boost_concepts::ForwardTraversalConcept<Iterator>));
  72. // Report invalid expression error as early as possible.
  73. // If you got an error_invalid_expression error message here,
  74. // then the expression (expr) is not a valid spirit qi expression.
  75. BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Expr);
  76. typename detail::make_context<Attr>::type context(attr);
  77. return compile<qi::domain>(expr).parse(first, last, context, unused, attr);
  78. }
  79. template <typename Iterator, typename Expr, typename Attr>
  80. inline bool
  81. parse(
  82. Iterator const& first_
  83. , Iterator last
  84. , Expr const& expr
  85. , Attr& attr)
  86. {
  87. Iterator first = first_;
  88. return qi::parse(first, last, expr, attr);
  89. }
  90. ///////////////////////////////////////////////////////////////////////////
  91. template <typename Iterator, typename Expr, typename Skipper>
  92. inline bool
  93. phrase_parse(
  94. Iterator& first
  95. , Iterator last
  96. , Expr const& expr
  97. , Skipper const& skipper
  98. , BOOST_SCOPED_ENUM(skip_flag) post_skip = skip_flag::postskip)
  99. {
  100. // Make sure the iterator is at least a readable forward traversal iterator.
  101. // If you got a compilation error here, then you are using a weaker iterator
  102. // while calling this function, you need to supply a readable forward traversal
  103. // iterator instead.
  104. BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIteratorConcept<Iterator>));
  105. BOOST_CONCEPT_ASSERT((boost_concepts::ForwardTraversalConcept<Iterator>));
  106. return detail::phrase_parse_impl<Expr>::call(
  107. first, last, expr, skipper, post_skip);
  108. }
  109. template <typename Iterator, typename Expr, typename Skipper>
  110. inline bool
  111. phrase_parse(
  112. Iterator const& first_
  113. , Iterator last
  114. , Expr const& expr
  115. , Skipper const& skipper
  116. , BOOST_SCOPED_ENUM(skip_flag) post_skip = skip_flag::postskip)
  117. {
  118. Iterator first = first_;
  119. return qi::phrase_parse(first, last, expr, skipper, post_skip);
  120. }
  121. ///////////////////////////////////////////////////////////////////////////
  122. template <typename Iterator, typename Expr, typename Skipper, typename Attr>
  123. inline bool
  124. phrase_parse(
  125. Iterator& first
  126. , Iterator last
  127. , Expr const& expr
  128. , Skipper const& skipper
  129. , BOOST_SCOPED_ENUM(skip_flag) post_skip
  130. , Attr& attr)
  131. {
  132. // Make sure the iterator is at least a readable forward traversal iterator.
  133. // If you got a compilation error here, then you are using a weaker iterator
  134. // while calling this function, you need to supply a readable forward traversal
  135. // iterator instead.
  136. BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIteratorConcept<Iterator>));
  137. BOOST_CONCEPT_ASSERT((boost_concepts::ForwardTraversalConcept<Iterator>));
  138. // Report invalid expression error as early as possible.
  139. // If you got an error_invalid_expression error message here,
  140. // then either the expression (expr) or skipper is not a valid
  141. // spirit qi expression.
  142. BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Expr);
  143. BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Skipper);
  144. typedef
  145. typename result_of::compile<qi::domain, Skipper>::type
  146. skipper_type;
  147. skipper_type const skipper_ = compile<qi::domain>(skipper);
  148. typename detail::make_context<Attr>::type context(attr);
  149. if (!compile<qi::domain>(expr).parse(
  150. first, last, context, skipper_, attr))
  151. return false;
  152. if (post_skip == skip_flag::postskip)
  153. qi::skip_over(first, last, skipper_);
  154. return true;
  155. }
  156. template <typename Iterator, typename Expr, typename Skipper, typename Attr>
  157. inline bool
  158. phrase_parse(
  159. Iterator const& first_
  160. , Iterator last
  161. , Expr const& expr
  162. , Skipper const& skipper
  163. , BOOST_SCOPED_ENUM(skip_flag) post_skip
  164. , Attr& attr)
  165. {
  166. Iterator first = first_;
  167. return qi::phrase_parse(first, last, expr, skipper, post_skip, attr);
  168. }
  169. ///////////////////////////////////////////////////////////////////////////
  170. template <typename Iterator, typename Expr, typename Skipper, typename Attr>
  171. inline bool
  172. phrase_parse(
  173. Iterator& first
  174. , Iterator last
  175. , Expr const& expr
  176. , Skipper const& skipper
  177. , Attr& attr)
  178. {
  179. return qi::phrase_parse(first, last, expr, skipper, skip_flag::postskip, attr);
  180. }
  181. template <typename Iterator, typename Expr, typename Skipper, typename Attr>
  182. inline bool
  183. phrase_parse(
  184. Iterator const& first_
  185. , Iterator last
  186. , Expr const& expr
  187. , Skipper const& skipper
  188. , Attr& attr)
  189. {
  190. Iterator first = first_;
  191. return qi::phrase_parse(first, last, expr, skipper, skip_flag::postskip, attr);
  192. }
  193. }}}
  194. #endif