alternative.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. Copyright (c) 2001 Daniel Nuffer
  4. Copyright (c) 2002 Hartmut Kaiser
  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_ALTERNATIVE_HPP)
  10. #define BOOST_SPIRIT_ALTERNATIVE_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/primitives/primitives.hpp>
  14. #include <boost/spirit/home/classic/core/composite/composite.hpp>
  15. #include <boost/spirit/home/classic/meta/as_parser.hpp>
  16. namespace boost { namespace spirit {
  17. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  18. ///////////////////////////////////////////////////////////////////////////
  19. //
  20. // alternative class
  21. //
  22. // Handles expressions of the form:
  23. //
  24. // a | b
  25. //
  26. // where a and b are parsers. The expression returns a composite
  27. // parser that matches a or b. One (not both) of the operands may
  28. // be a literal char, wchar_t or a primitive string char const*,
  29. // wchar_t const*.
  30. //
  31. // The expression is short circuit evaluated. b is never touched
  32. // when a is returns a successful match.
  33. //
  34. ///////////////////////////////////////////////////////////////////////////
  35. struct alternative_parser_gen;
  36. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  37. #pragma warning(push)
  38. #pragma warning(disable:4512) //assignment operator could not be generated
  39. #endif
  40. template <typename A, typename B>
  41. struct alternative
  42. : public binary<A, B, parser<alternative<A, B> > >
  43. {
  44. typedef alternative<A, B> self_t;
  45. typedef binary_parser_category parser_category_t;
  46. typedef alternative_parser_gen parser_generator_t;
  47. typedef binary<A, B, parser<self_t> > base_t;
  48. alternative(A const& a, B const& b)
  49. : base_t(a, b) {}
  50. template <typename ScannerT>
  51. typename parser_result<self_t, ScannerT>::type
  52. parse(ScannerT const& scan) const
  53. {
  54. typedef typename parser_result<self_t, ScannerT>::type result_t;
  55. typedef typename ScannerT::iterator_t iterator_t;
  56. { // scope for save
  57. iterator_t save = scan.first;
  58. if (result_t hit = this->left().parse(scan))
  59. return hit;
  60. scan.first = save;
  61. }
  62. return this->right().parse(scan);
  63. }
  64. };
  65. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  66. #pragma warning(pop)
  67. #endif
  68. struct alternative_parser_gen
  69. {
  70. template <typename A, typename B>
  71. struct result
  72. {
  73. typedef
  74. alternative<
  75. typename as_parser<A>::type
  76. , typename as_parser<B>::type
  77. >
  78. type;
  79. };
  80. template <typename A, typename B>
  81. static alternative<
  82. typename as_parser<A>::type
  83. , typename as_parser<B>::type
  84. >
  85. generate(A const& a, B const& b)
  86. {
  87. return alternative<BOOST_DEDUCED_TYPENAME as_parser<A>::type,
  88. BOOST_DEDUCED_TYPENAME as_parser<B>::type>
  89. (as_parser<A>::convert(a), as_parser<B>::convert(b));
  90. }
  91. };
  92. template <typename A, typename B>
  93. alternative<A, B>
  94. operator|(parser<A> const& a, parser<B> const& b);
  95. template <typename A>
  96. alternative<A, chlit<char> >
  97. operator|(parser<A> const& a, char b);
  98. template <typename B>
  99. alternative<chlit<char>, B>
  100. operator|(char a, parser<B> const& b);
  101. template <typename A>
  102. alternative<A, strlit<char const*> >
  103. operator|(parser<A> const& a, char const* b);
  104. template <typename B>
  105. alternative<strlit<char const*>, B>
  106. operator|(char const* a, parser<B> const& b);
  107. template <typename A>
  108. alternative<A, chlit<wchar_t> >
  109. operator|(parser<A> const& a, wchar_t b);
  110. template <typename B>
  111. alternative<chlit<wchar_t>, B>
  112. operator|(wchar_t a, parser<B> const& b);
  113. template <typename A>
  114. alternative<A, strlit<wchar_t const*> >
  115. operator|(parser<A> const& a, wchar_t const* b);
  116. template <typename B>
  117. alternative<strlit<wchar_t const*>, B>
  118. operator|(wchar_t const* a, parser<B> const& b);
  119. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  120. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  121. #endif
  122. #include <boost/spirit/home/classic/core/composite/impl/alternative.ipp>