Program Listing for File ParsingError.hpp

Return to documentation for file (src/rdf4cpp/parser/ParsingError.hpp)

#ifndef RDF4CPP_PARSER_PARSINGERROR_HPP
#define RDF4CPP_PARSER_PARSINGERROR_HPP

#include <cstdint>
#include <ostream>
#include <string>

namespace rdf4cpp::parser {

struct ParsingError {
    enum struct Type : uint8_t {
        Internal = 0,
        EofReached,
        BadSyntax,
        BadIri,
        BadCurie,
        BadLiteral,
        BadBlankNode,
    };

    Type error_type;
    uint64_t line;
    uint64_t col;
    std::string message;
};

} // namespace rdf4cpp::parser

namespace std {

template<>
struct formatter<::rdf4cpp::parser::ParsingError::Type> : formatter<string_view> {
    template<typename FmtCtx>
    auto format(::rdf4cpp::parser::ParsingError::Type const &err, FmtCtx &ctx) const {
        using ::rdf4cpp::parser::ParsingError;
        switch (err) {
            case ParsingError::Type::Internal:
                return format_to(ctx.out(), "internal error");
            case ParsingError::Type::EofReached:
                return format_to(ctx.out(), "unexpected end of file");
            case ParsingError::Type::BadSyntax:
                return format_to(ctx.out(), "bad syntax");
            case ParsingError::Type::BadIri:
                return format_to(ctx.out(), "bad iri");
            case ParsingError::Type::BadCurie:
                return format_to(ctx.out(), "bad curie");
            case ParsingError::Type::BadLiteral:
                return format_to(ctx.out(), "bad literal");
            case ParsingError::Type::BadBlankNode:
                return format_to(ctx.out(), "bad blank node");
            default:
                return format_to(ctx.out(), "unknown");
        }
    }
};

template<>
struct formatter<::rdf4cpp::parser::ParsingError> : formatter<string_view> {
    template<typename FmtCtx>
    auto format(::rdf4cpp::parser::ParsingError const &err, FmtCtx &ctx) const {
        return format_to(ctx.out(), "{}:{}({}): {}", err.line, err.col, err.error_type, err.message);
    }
};

} // namespace std

namespace rdf4cpp::parser {

inline std::ostream &operator<<(std::ostream &os, ParsingError::Type const &error_type) {
    std::format_to(std::ostreambuf_iterator<char>{os}, "{}", error_type);
    return os;
}

inline std::ostream &operator<<(std::ostream &os, ParsingError const &error) {
    std::format_to(std::ostreambuf_iterator<char>{os}, "{}", error);
    return os;
}

} // namespace rdf4cpp::parser

#endif  //RDF4CPP_PARSER_PARSINGERROR_HPP