if.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Joel de Guzman
  3. Copyright (c) 2002 Juan Carlos Arevalo-Baeza
  4. Copyright (c) 2002-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. #ifndef BOOST_SPIRIT_IF_HPP
  10. #define BOOST_SPIRIT_IF_HPP
  11. #include <boost/spirit/home/classic/namespace.hpp>
  12. #include <boost/spirit/home/classic/core/parser.hpp>
  13. #include <boost/spirit/home/classic/core/composite/composite.hpp>
  14. #include <boost/spirit/home/classic/dynamic/impl/conditions.ipp>
  15. namespace boost { namespace spirit {
  16. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  17. namespace impl {
  18. //////////////////////////////////
  19. // if-else-parser, holds two alternative parsers and a conditional functor
  20. // that selects between them.
  21. template <typename ParsableTrueT, typename ParsableFalseT, typename CondT>
  22. struct if_else_parser
  23. : public condition_evaluator<typename as_parser<CondT>::type>
  24. , public binary
  25. <
  26. typename as_parser<ParsableTrueT>::type,
  27. typename as_parser<ParsableFalseT>::type,
  28. parser< if_else_parser<ParsableTrueT, ParsableFalseT, CondT> >
  29. >
  30. {
  31. typedef if_else_parser<ParsableTrueT, ParsableFalseT, CondT> self_t;
  32. typedef as_parser<ParsableTrueT> as_parser_true_t;
  33. typedef as_parser<ParsableFalseT> as_parser_false_t;
  34. typedef typename as_parser_true_t::type parser_true_t;
  35. typedef typename as_parser_false_t::type parser_false_t;
  36. typedef as_parser<CondT> cond_as_parser_t;
  37. typedef typename cond_as_parser_t::type condition_t;
  38. typedef binary<parser_true_t, parser_false_t, parser<self_t> > base_t;
  39. typedef condition_evaluator<condition_t> eval_t;
  40. if_else_parser
  41. (
  42. ParsableTrueT const& p_true,
  43. ParsableFalseT const& p_false,
  44. CondT const& cond_
  45. )
  46. : eval_t(cond_as_parser_t::convert(cond_))
  47. , base_t
  48. (
  49. as_parser_true_t::convert(p_true),
  50. as_parser_false_t::convert(p_false)
  51. )
  52. { }
  53. template <typename ScannerT>
  54. struct result
  55. {
  56. typedef typename match_result<ScannerT, nil_t>::type type;
  57. };
  58. template <typename ScannerT>
  59. typename parser_result<self_t, ScannerT>::type
  60. parse(ScannerT const& scan) const
  61. {
  62. typedef typename parser_result
  63. <parser_true_t, ScannerT>::type then_result_t;
  64. typedef typename parser_result
  65. <parser_false_t, ScannerT>::type else_result_t;
  66. typename ScannerT::iterator_t const save(scan.first);
  67. std::ptrdiff_t length = this->evaluate(scan);
  68. if (length >= 0)
  69. {
  70. then_result_t then_result(this->left().parse(scan));
  71. if (then_result)
  72. {
  73. length += then_result.length();
  74. return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
  75. }
  76. }
  77. else
  78. {
  79. else_result_t else_result(this->right().parse(scan));
  80. if (else_result)
  81. {
  82. length = else_result.length();
  83. return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
  84. }
  85. }
  86. return scan.no_match();
  87. }
  88. };
  89. //////////////////////////////////
  90. // if-else-parser generator, takes the false-parser in brackets
  91. // and returns the if-else-parser.
  92. template <typename ParsableTrueT, typename CondT>
  93. struct if_else_parser_gen
  94. {
  95. if_else_parser_gen(ParsableTrueT const& p_true_, CondT const& cond_)
  96. : p_true(p_true_)
  97. , cond(cond_) {}
  98. template <typename ParsableFalseT>
  99. if_else_parser
  100. <
  101. ParsableTrueT,
  102. ParsableFalseT,
  103. CondT
  104. >
  105. operator[](ParsableFalseT const& p_false) const
  106. {
  107. return if_else_parser<ParsableTrueT, ParsableFalseT, CondT>
  108. (
  109. p_true,
  110. p_false,
  111. cond
  112. );
  113. }
  114. ParsableTrueT const &p_true;
  115. CondT const &cond;
  116. };
  117. //////////////////////////////////
  118. // if-parser, conditionally runs a parser is a functor condition is true.
  119. // If the condition is fales, it fails the parse.
  120. // It can optionally become an if-else-parser through the member else_p.
  121. template <typename ParsableT, typename CondT>
  122. struct if_parser
  123. : public condition_evaluator<typename as_parser<CondT>::type>
  124. , public unary
  125. <
  126. typename as_parser<ParsableT>::type,
  127. parser<if_parser<ParsableT, CondT> > >
  128. {
  129. typedef if_parser<ParsableT, CondT> self_t;
  130. typedef as_parser<ParsableT> as_parser_t;
  131. typedef typename as_parser_t::type parser_t;
  132. typedef as_parser<CondT> cond_as_parser_t;
  133. typedef typename cond_as_parser_t::type condition_t;
  134. typedef condition_evaluator<condition_t> eval_t;
  135. typedef unary<parser_t, parser<self_t> > base_t;
  136. if_parser(ParsableT const& p, CondT const& cond_)
  137. : eval_t(cond_as_parser_t::convert(cond_))
  138. , base_t(as_parser_t::convert(p))
  139. , else_p(p, cond_)
  140. {}
  141. template <typename ScannerT>
  142. struct result
  143. {
  144. typedef typename match_result<ScannerT, nil_t>::type type;
  145. };
  146. template <typename ScannerT>
  147. typename parser_result<self_t, ScannerT>::type
  148. parse(ScannerT const& scan) const
  149. {
  150. typedef typename parser_result<parser_t, ScannerT>::type t_result_t;
  151. typename ScannerT::iterator_t const save(scan.first);
  152. std::ptrdiff_t length = this->evaluate(scan);
  153. if (length >= 0)
  154. {
  155. t_result_t then_result(this->subject().parse(scan));
  156. if (then_result)
  157. {
  158. length += then_result.length();
  159. return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
  160. }
  161. return scan.no_match();
  162. }
  163. return scan.empty_match();
  164. }
  165. if_else_parser_gen<ParsableT, CondT> else_p;
  166. };
  167. //////////////////////////////////
  168. // if-parser generator, takes the true-parser in brackets and returns the
  169. // if-parser.
  170. template <typename CondT>
  171. struct if_parser_gen
  172. {
  173. if_parser_gen(CondT const& cond_) : cond(cond_) {}
  174. template <typename ParsableT>
  175. if_parser
  176. <
  177. ParsableT,
  178. CondT
  179. >
  180. operator[](ParsableT const& subject) const
  181. {
  182. return if_parser<ParsableT, CondT>(subject, cond);
  183. }
  184. CondT const &cond;
  185. };
  186. } // namespace impl
  187. //////////////////////////////////
  188. // if_p function, returns "if" parser generator
  189. template <typename CondT>
  190. impl::if_parser_gen<CondT>
  191. if_p(CondT const& cond)
  192. {
  193. return impl::if_parser_gen<CondT>(cond);
  194. }
  195. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  196. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  197. #endif // BOOST_SPIRIT_IF_HPP