any_parser.hpp 4.8 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/*=============================================================================
    Copyright (c) 2001-2014 Joel de Guzman
    Copyright (c) 2013-2014 Agustin Berge

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#if !defined(BOOST_SPIRIT_X3_AUXILIARY_ANY_PARSER_APR_09_2014_1145PM)
#define BOOST_SPIRIT_X3_AUXILIARY_ANY_PARSER_APR_09_2014_1145PM

#if defined(_MSC_VER)
#pragma once
#endif

#include <boost/spirit/home/x3/core/parser.hpp>
#include <boost/spirit/home/x3/support/context.hpp>
#include <boost/spirit/home/x3/support/subcontext.hpp>
#include <boost/spirit/home/x3/support/unused.hpp>
#include <boost/spirit/home/x3/support/traits/container_traits.hpp>
#include <boost/spirit/home/x3/support/traits/has_attribute.hpp>
#include <boost/spirit/home/x3/support/traits/move_to.hpp>
#include <boost/spirit/home/x3/support/traits/is_parser.hpp>
#include <memory>
#include <string>

namespace boost { namespace spirit { namespace x3
{
    template <
        typename Iterator
      , typename Attribute = unused_type
      , typename Context = subcontext<>>
    struct any_parser : parser<any_parser<Iterator, Attribute, Context>>
    {
        typedef Attribute attribute_type;

        static bool const has_attribute =
            !is_same<unused_type, attribute_type>::value;
        static bool const handles_container =
            traits::is_container<Attribute>::value;

    public:
        any_parser()
          : _content(nullptr) {}

        template <typename Expr,
            typename Enable = typename enable_if<traits::is_parser<Expr>>::type>
        any_parser(Expr const& expr)
          : _content(new holder<Expr>(expr)) {}

        any_parser(any_parser const& other)
          : _content(other._content ? other._content->clone() : nullptr) {}

        any_parser(any_parser&& other) = default;

        any_parser& operator=(any_parser const& other)
        {
            _content.reset(other._content ? other._content->clone() : nullptr);
            return *this;
        }

        any_parser& operator=(any_parser&& other) = default;

        template <typename Iterator_, typename Context_>
        bool parse(Iterator_& first, Iterator_ const& last
          , Context_ const& context, unused_type, Attribute& attr) const
        {
            BOOST_STATIC_ASSERT_MSG(
                (is_same<Iterator, Iterator_>::value)
              , "Incompatible iterator used"
            );

            BOOST_ASSERT_MSG(
                (_content != nullptr)
              , "Invalid use of uninitialized any_parser"
            );

            return _content->parse(first, last, context, attr);
        }

        template <typename Iterator_, typename Context_, typename Attribute_>
        bool parse(Iterator_& first, Iterator_ const& last
          , Context_ const& context, unused_type, Attribute_& attr_) const
        {
            Attribute attr;
            if (parse(first, last, context, unused, attr))
            {
                traits::move_to(attr, attr_);
                return true;
            }
            return false;
        }

        std::string get_info() const
        {
            return _content ? _content->get_info() : "";
        }

    private:

        struct placeholder
        {
            virtual placeholder* clone() const = 0;

            virtual bool parse(Iterator& first, Iterator const& last
              , Context const& context, Attribute& attr) const = 0;

            virtual std::string get_info() const = 0;

            virtual ~placeholder() {}
        };

        template <typename Expr>
        struct holder : placeholder
        {
            typedef typename extension::as_parser<Expr>::value_type parser_type;

            explicit holder(Expr const& p)
              : _parser(as_parser(p)) {}

            holder* clone() const override
            {
                return new holder(*this);
            }

            bool parse(Iterator& first, Iterator const& last
              , Context const& context, Attribute& attr) const override
            {
                return _parser.parse(first, last, context, unused, attr);
            }

            std::string get_info() const override
            {
                return x3::what(_parser);
            }

            parser_type _parser;
        };

    private:
        std::unique_ptr<placeholder> _content;
    };

    template <typename Iterator, typename Attribute, typename Context>
    struct get_info<any_parser<Iterator, Attribute, Context>>
    {
        typedef std::string result_type;
        std::string operator()(
            any_parser<Iterator, Attribute, Context> const& p) const
        {
            return p.get_info();
        }
    };
}}}

#endif