any_parser.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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() = default;
  34. template <typename Expr,
  35. typename Enable = typename enable_if<traits::is_parser<Expr>>::type>
  36. any_parser(Expr const& expr)
  37. : _content(new holder<Expr>(expr)) {}
  38. any_parser(any_parser const& other)
  39. : _content(other._content ? other._content->clone() : nullptr) {}
  40. any_parser(any_parser&& other) = default;
  41. any_parser& operator=(any_parser const& other)
  42. {
  43. _content.reset(other._content ? other._content->clone() : nullptr);
  44. return *this;
  45. }
  46. any_parser& operator=(any_parser&& other) = default;
  47. template <typename Iterator_, typename Context_>
  48. bool parse(Iterator_& first, Iterator_ const& last
  49. , Context_ const& context, unused_type, Attribute& attr) const
  50. {
  51. BOOST_STATIC_ASSERT_MSG(
  52. (is_same<Iterator, Iterator_>::value)
  53. , "Incompatible iterator used"
  54. );
  55. BOOST_ASSERT_MSG(
  56. (_content != nullptr)
  57. , "Invalid use of uninitialized any_parser"
  58. );
  59. return _content->parse(first, last, context, attr);
  60. }
  61. template <typename Iterator_, typename Context_, typename Attribute_>
  62. bool parse(Iterator_& first, Iterator_ const& last
  63. , Context_ const& context, unused_type, Attribute_& attr_) const
  64. {
  65. Attribute attr;
  66. if (parse(first, last, context, unused, attr))
  67. {
  68. traits::move_to(attr, attr_);
  69. return true;
  70. }
  71. return false;
  72. }
  73. std::string get_info() const
  74. {
  75. return _content ? _content->get_info() : "";
  76. }
  77. private:
  78. struct placeholder
  79. {
  80. virtual placeholder* clone() const = 0;
  81. virtual bool parse(Iterator& first, Iterator const& last
  82. , Context const& context, Attribute& attr) const = 0;
  83. virtual std::string get_info() const = 0;
  84. virtual ~placeholder() {}
  85. };
  86. template <typename Expr>
  87. struct holder : placeholder
  88. {
  89. typedef typename extension::as_parser<Expr>::value_type parser_type;
  90. explicit holder(Expr const& p)
  91. : _parser(as_parser(p)) {}
  92. holder* clone() const override
  93. {
  94. return new holder(*this);
  95. }
  96. bool parse(Iterator& first, Iterator const& last
  97. , Context const& context, Attribute& attr) const override
  98. {
  99. return _parser.parse(first, last, context, unused, attr);
  100. }
  101. std::string get_info() const override
  102. {
  103. return x3::what(_parser);
  104. }
  105. parser_type _parser;
  106. };
  107. private:
  108. std::unique_ptr<placeholder> _content;
  109. };
  110. template <typename Iterator, typename Attribute, typename Context>
  111. struct get_info<any_parser<Iterator, Attribute, Context>>
  112. {
  113. typedef std::string result_type;
  114. std::string operator()(
  115. any_parser<Iterator, Attribute, Context> const& p) const
  116. {
  117. return p.get_info();
  118. }
  119. };
  120. }}}
  121. #endif