any_parser.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Copyright (c) 2013-2014 Agustin Berge
  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_X3_AUXILIARY_ANY_PARSER_APR_09_2014_1145PM)
  8. #define BOOST_SPIRIT_X3_AUXILIARY_ANY_PARSER_APR_09_2014_1145PM
  9. #include <boost/spirit/home/x3/core/parser.hpp>
  10. #include <boost/spirit/home/x3/support/context.hpp>
  11. #include <boost/spirit/home/x3/support/subcontext.hpp>
  12. #include <boost/spirit/home/x3/support/unused.hpp>
  13. #include <boost/spirit/home/x3/support/traits/container_traits.hpp>
  14. #include <boost/spirit/home/x3/support/traits/has_attribute.hpp>
  15. #include <boost/spirit/home/x3/support/traits/move_to.hpp>
  16. #include <boost/spirit/home/x3/support/traits/is_parser.hpp>
  17. #include <memory>
  18. #include <string>
  19. namespace boost { namespace spirit { namespace x3
  20. {
  21. template <
  22. typename Iterator
  23. , typename Attribute = unused_type
  24. , typename Context = subcontext<>>
  25. struct any_parser : parser<any_parser<Iterator, Attribute, Context>>
  26. {
  27. typedef Attribute attribute_type;
  28. static bool const has_attribute =
  29. !is_same<unused_type, attribute_type>::value;
  30. static bool const handles_container =
  31. traits::is_container<Attribute>::value;
  32. public:
  33. any_parser()
  34. : _content(nullptr) {}
  35. template <typename Expr,
  36. typename Enable = typename enable_if<traits::is_parser<Expr>>::type>
  37. any_parser(Expr const& expr)
  38. : _content(new holder<Expr>(expr)) {}
  39. any_parser(any_parser const& other)
  40. : _content(other._content ? other._content->clone() : nullptr) {}
  41. any_parser(any_parser&& other) = default;
  42. any_parser& operator=(any_parser const& other)
  43. {
  44. _content.reset(other._content ? other._content->clone() : nullptr);
  45. return *this;
  46. }
  47. any_parser& operator=(any_parser&& other) = default;
  48. template <typename Iterator_, typename Context_>
  49. bool parse(Iterator_& first, Iterator_ const& last
  50. , Context_ const& context, unused_type, Attribute& attr) const
  51. {
  52. BOOST_STATIC_ASSERT_MSG(
  53. (is_same<Iterator, Iterator_>::value)
  54. , "Incompatible iterator used"
  55. );
  56. BOOST_ASSERT_MSG(
  57. (_content != nullptr)
  58. , "Invalid use of uninitialized any_parser"
  59. );
  60. return _content->parse(first, last, context, attr);
  61. }
  62. template <typename Iterator_, typename Context_, typename Attribute_>
  63. bool parse(Iterator_& first, Iterator_ const& last
  64. , Context_ const& context, unused_type, Attribute_& attr_) const
  65. {
  66. Attribute attr;
  67. if (parse(first, last, context, unused, attr))
  68. {
  69. traits::move_to(attr, attr_);
  70. return true;
  71. }
  72. return false;
  73. }
  74. std::string get_info() const
  75. {
  76. return _content ? _content->get_info() : "";
  77. }
  78. private:
  79. struct placeholder
  80. {
  81. virtual placeholder* clone() const = 0;
  82. virtual bool parse(Iterator& first, Iterator const& last
  83. , Context const& context, Attribute& attr) const = 0;
  84. virtual std::string get_info() const = 0;
  85. virtual ~placeholder() {}
  86. };
  87. template <typename Expr>
  88. struct holder : placeholder
  89. {
  90. typedef typename extension::as_parser<Expr>::value_type parser_type;
  91. explicit holder(Expr const& p)
  92. : _parser(as_parser(p)) {}
  93. holder* clone() const override
  94. {
  95. return new holder(*this);
  96. }
  97. bool parse(Iterator& first, Iterator const& last
  98. , Context const& context, Attribute& attr) const override
  99. {
  100. return _parser.parse(first, last, context, unused, attr);
  101. }
  102. std::string get_info() const override
  103. {
  104. return x3::what(_parser);
  105. }
  106. parser_type _parser;
  107. };
  108. private:
  109. std::unique_ptr<placeholder> _content;
  110. };
  111. template <typename Iterator, typename Attribute, typename Context>
  112. struct get_info<any_parser<Iterator, Attribute, Context>>
  113. {
  114. typedef std::string result_type;
  115. std::string operator()(
  116. any_parser<Iterator, Attribute, Context> const& p) const
  117. {
  118. return p.get_info();
  119. }
  120. };
  121. }}}
  122. #endif