boost::spirit::qi::grammar and variadic templates

与世无争的帅哥 提交于 2019-12-06 06:15:16

What you are creating closely resembles what qi's auto parser already does: http://www.boost.org/doc/libs/1_64_0/libs/spirit/doc/html/spirit/qi/reference/auto.html

If you specialize create_parser<> for your datatypes you can simply use qi::auto_ straight-away:

Live On Coliru

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>

namespace Commands {
    namespace qi = boost::spirit::qi;

    template <class T> using Rule = qi::rule<std::string::const_iterator, T()>;

    template <typename... T>
    auto parse(std::string const& s) {
        boost::variant<T...> v;

        auto it = s.begin();
        if (qi::parse(it, s.end(), qi::auto_, v))
            return v;
        throw std::runtime_error(std::string(__FUNCTION__) + " failed");
    }
}

struct Latitude  { double lat_; };
BOOST_FUSION_ADAPT_STRUCT(Latitude, lat_)

struct Longitude { double lon_; };
BOOST_FUSION_ADAPT_STRUCT(Longitude, lon_)

namespace boost { namespace spirit { namespace traits {
    template <> struct create_parser<Latitude> {
        using type = Commands::Rule<Latitude>;
        static type const& call() {
            static type const s_rule = qi::skip(qi::space)["LAT=" >> qi::auto_];
            return s_rule;
        };
    };

    template <> struct create_parser<Longitude> {
        using type = Commands::Rule<Longitude>;
        static type const& call() {
            static type const s_rule = qi::skip(qi::space)["LON=" >> qi::auto_];
            return s_rule;
        };
    };
} } }

struct print {
    using result_type = void;
    void operator()(Latitude const &t)  const { std::cout << "Latitude = " << t.lat_ << " deg" << std::endl; }
    void operator()(Longitude const &t) const { std::cout << "Longitude = " << t.lon_ << " deg" << std::endl; }
};

#include <sstream>

int main() {
    std::istringstream iss("LAT=4.3\n LON=5.0");
    std::string s;

    print printer;

    while (std::getline(iss, s)) try {
        auto v = Commands::parse<Latitude, Longitude>(s);
        boost::apply_visitor(printer, v);
    }
    catch (std::exception const& e) {
        std::cout << "'" << s << "': " << e.what() << "\n";
    }
}

Prints

Latitude = 4.3 deg
Longitude = 5 deg

Nicer things

If you don't use qi::rule<> you don't need to hard-code the iterator either. Let's go full fun mode and get rid of the visitor too:

[Live On Coliru](http://coliru.stacked-crooked.com/a/84f7a8c9a453fc1b

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>

namespace Commands {
    template <typename... T>
    auto parse(std::string const& s) {
        boost::variant<T...> v;

        auto it = s.begin();
        if (boost::spirit::qi::parse(it, s.end(), boost::spirit::qi::auto_, v))
            return v;
        throw std::runtime_error(std::string(__FUNCTION__) + " failed");
    }

    struct Latitude { double lat_; };
    struct Longitude { double lon_; };

    static inline std::ostream& operator<<(std::ostream& os, Latitude const &t) { return os << "Latitude = " << t.lat_ << " deg"; }
    static inline std::ostream& operator<<(std::ostream& os, Longitude const &t) { return os << "Longitude = " << t.lon_ << " deg"; }
}

BOOST_FUSION_ADAPT_STRUCT(Commands::Latitude, lat_)
BOOST_FUSION_ADAPT_STRUCT(Commands::Longitude, lon_)

namespace boost { namespace spirit { namespace traits {
    #define MAP_PARSER(T, expr) \
    template <> struct create_parser<T> { \
        using type = decltype(qi::attr_cast<T, T>(qi::copy(expr))); \
        static type const& call() { static type const s_rule = qi::attr_cast<T, T>(qi::copy(expr)); return s_rule; }; \
    };

    #define AUTO_MAP_PARSER(T, caption) MAP_PARSER(T, qi::skip(qi::space)[qi::lit(caption) >> '=' >> qi::auto_])

    AUTO_MAP_PARSER(::Commands::Longitude, "LON")
    AUTO_MAP_PARSER(::Commands::Latitude, "LAT")
} } }

#include <sstream>

int main() {
    std::istringstream iss("LAT=4.3\n LON=5.0");
    std::string s;

    while (std::getline(iss, s)) try {
        using namespace Commands;
        std::cout << "Parsed '" << s << "' into " << parse<Latitude, Longitude>(s) << "\n";
    } catch (std::exception const& e) {
        std::cout << "'" << s << "': " << e.what() << "\n";
    }
}

Prints

Parsed 'LAT=4.3' into Latitude = 4.3 deg
Parsed ' LON=5.0' into Longitude = 5 deg

In addition to the Spirit Qi answer I gave:

If you can afford to enable c++1z, you can use Spirit X3 with fold-expressions:

Live On Coliru

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/home/x3.hpp>

namespace Commands {
    namespace x3 = boost::spirit::x3;
    template <typename... T>
    auto parse(std::string const& s) {
        using V = boost::variant<T...>;
        V v;

        auto it = s.begin();
        if (x3::parse(it, s.end(), parser_for(v), v))
            return v;
        throw std::runtime_error(std::string(__FUNCTION__) + " failed");
    }

    struct Latitude { double lat_; };
    struct Longitude { double lon_; };

    auto label_for(Latitude) { return "LAT"; }
    auto label_for(Longitude) { return "LON"; }

    template <typename T, typename P>
    auto as_cmd(P p) { return x3::rule<struct _, T>{} 
            = x3::skip(x3::space)[x3::lit(label_for(T{})) >> '=' >> p]; }

    template <typename T>    auto parser_for(T)                      { return as_cmd<T>(x3::double_); }
    template <typename... T> auto parser_for(boost::variant<T...> _) { return (parser_for(T{}) | ...); }

    static inline std::ostream& operator<<(std::ostream& os, Latitude const &t) { return os << "Latitude = " << t.lat_ << " deg"; }
    static inline std::ostream& operator<<(std::ostream& os, Longitude const &t) { return os << "Longitude = " << t.lon_ << " deg"; }
}

BOOST_FUSION_ADAPT_STRUCT(Commands::Latitude, lat_)
BOOST_FUSION_ADAPT_STRUCT(Commands::Longitude, lon_)

#include <iostream>
#include <sstream>

int main() {
    std::istringstream iss("LAT=4.3\n LON=5.0");
    std::string s;

    while (std::getline(iss, s)) try {
        using namespace Commands;
        std::cout << "Parsed '" << s << "' into " << parse<Latitude, Longitude>(s) << "\n";
    } catch (std::exception const& e) {
        std::cout << "'" << s << "': " << e.what() << "\n";
    }
}

Prints

Parsed 'LAT=4.3' into Latitude = 4.3 deg
Parsed ' LON=5.0' into Longitude = 5 deg
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!