Boost Spirit x3: błąd czasu kompilacji atrybutu złożonego (klasa wyliczenia)

Jan 13 2021

Niedawno pisałem najprostszy możliwy parser przy użyciu boost spirit x3 . Zawiera 2 zasady: identyfikator i operator pojedynczego znaku . Oczywiście zaimplementowałem operatora za pomocą tablicy symboli, która tworzy typ operatora enum class. Identyfikatory są analizowane jako std::strings. Jednak kod odmawia kompilacji podczas łączenia identyfikatorów i operatorów w jeden parser (patrz fragment kodu na końcu pytania).

Zwróć uwagę, że jeśli zmienisz typ operatora wyliczenie na liczbę całkowitą, wszystko będzie działać poprawnie. Operatory i identyfikatory są dobrze analizowane, gdy są oddzielne.

Komunikat o błędzie szablonu jest dość duży, aby można go było dołączyć, i zbyt niejasny, abym mógł go zrozumieć, ale podejrzewam, że ma to coś wspólnego z semantyczną konstrukcją / ruchem std::variant<std::string, OperType>. Nie enum classpowinien jednak drastycznie różnić się od zwykłego int. Czy ma to coś wspólnego z enum classdomyślnym konstruktorem? Jak można to ominąć?

Oto kod

#include <variant>
#include <string>

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

namespace x3 = boost::spirit::x3;

auto addCharacter = [](auto &context) {
    x3::_val(context).push_back(x3::_attr(context));
};

x3::rule<class IdentifierTag, std::string> identifier{"identifier"};
const auto identifier_def = x3::lexeme[x3::char_("a-zA-Z")[addCharacter] >> *(x3::char_("a-zA-Z0-9")[addCharacter])];

BOOST_SPIRIT_DEFINE(identifier);

enum class OperType
{
    plus,
    minus
};

struct Opers_ : x3::symbols<OperType>
{
    Opers_()
    {
        add("+", OperType::plus)("-", OperType::minus);
    }
} opers_;

x3::rule<class OperTypeTag, OperType> oper{"operator"};
const auto oper_def = x3::lexeme[opers_];

BOOST_SPIRIT_DEFINE(oper);

int main()
{
    std::string input{"iden1 + - iden2"};

    std::vector<std::variant<std::string, OperType>> tokens;

    auto start = input.cbegin();
    auto result = x3::phrase_parse(start, input.cend(), (+(identifier | oper)), x3::space, tokens);

    return 0;
}

Czy są jakieś pułapki podczas pisania złożonych parserów? czego mi brakuje? Dziękuję za Twój czas.

Odpowiedzi

2 sehe Jan 13 2021 at 10:26

std::variant nie jest jeszcze obsługiwany ze względu na zgodność atrybutów.

Zmiana na boost::variantpowoduje kompilację: Eksplorator kompilatora

Alternatywnie, oto sposoby, aby to faktycznie działało, jeśli potrzebujesz std :: variant: Przejście do parsera Boost Spirit z boost :: variant na std :: variant

#include <boost/spirit/home/x3.hpp>
#include <variant>
#include <fmt/ranges.h>
#include <fmt/ostream.h>

namespace x3 = boost::spirit::x3;

auto addCharacter = [](auto& context) {
    x3::_val(context).push_back(x3::_attr(context));
};

x3::rule<class IdentifierTag, std::string> identifier{"identifier"};
const auto identifier_def =
    x3::lexeme[x3::char_("a-zA-Z")[addCharacter] >> *(x3::char_("a-zA-Z0-9")[addCharacter])];

BOOST_SPIRIT_DEFINE(identifier)

enum class OperType
{
    plus,
    minus
};

static inline std::ostream& operator<<(std::ostream& os, OperType ot) {
    switch(ot) {
        case OperType::plus: return os << "plus";
        case OperType::minus: return os << "minus";
    }
    return os << "?";
}

struct Opers_ : x3::symbols<OperType>
{
    Opers_()
    {
        add("+", OperType::plus)
           ("-", OperType::minus);
    }
} opers_;

x3::rule<class OperTypeTag, OperType> oper{"operator"};
const auto oper_def = x3::lexeme[opers_];

BOOST_SPIRIT_DEFINE(oper)

int main() {
    std::string const input{"iden1 + - iden2"};

    std::vector<boost::variant<std::string, OperType>> tokens;

    auto f = input.begin(), l = input.end();
    auto result = x3::phrase_parse(
            f, l,
            +(identifier | oper),
            x3::space,
            tokens);

    if (result) {
        fmt::print("Parsed: {}\n", tokens);
    } else {
        fmt::print("Parse failed\n");
    }

    if (f!=l) {
        fmt::print("Remaining: '{}'\n", std::string(f,l));
    }
}

Wydruki

Parsed: {iden1, plus, minus, iden2}