better error reporting
This commit is contained in:
parent
43223e685e
commit
74f4393d6a
4
Makefile
4
Makefile
@ -2,8 +2,8 @@ MAKEFLAGS="-j $(grep -c ^processor /proc/cpuinfo)"
|
|||||||
CXXFLAGS=-std=c++20 -Wall -Wextra -O2 -Werror=switch
|
CXXFLAGS=-std=c++20 -Wall -Wextra -O2 -Werror=switch
|
||||||
CPPFLAGS=-Ilib/expected/ -Ilib/ut/ -Isrc/
|
CPPFLAGS=-Ilib/expected/ -Ilib/ut/ -Isrc/
|
||||||
|
|
||||||
Obj=bin/lexer.o \
|
Obj=bin/errors.o \
|
||||||
bin/errors.o \
|
bin/lexer.o \
|
||||||
bin/unicode.o
|
bin/unicode.o
|
||||||
|
|
||||||
all: bin/musique bin/unit-tests
|
all: bin/musique bin/unit-tests
|
||||||
|
@ -20,9 +20,30 @@ std::ostream& operator<<(std::ostream& os, Error const& err)
|
|||||||
return os << "end of file\n";
|
return os << "end of file\n";
|
||||||
|
|
||||||
case errors::Unrecognized_Character:
|
case errors::Unrecognized_Character:
|
||||||
return os << "unrecognized charater 0x" << std::hex << err.invalid_character
|
if (err.invalid_character) {
|
||||||
<< "(char: '" << utf8::Print(err.invalid_character) << "')\n";
|
return os << "unrecognized charater U+" << std::hex << err.invalid_character
|
||||||
|
<< " (char: '" << utf8::Print{err.invalid_character} << "')\n";
|
||||||
|
} else {
|
||||||
|
return os << "unrecognized character\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return os << "unrecognized error type\n";
|
return os << "unrecognized error type\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Error errors::unrecognized_character(u32 invalid_character)
|
||||||
|
{
|
||||||
|
Error err;
|
||||||
|
err.type = errors::Unrecognized_Character;
|
||||||
|
err.invalid_character = invalid_character;
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
Error errors::unrecognized_character(u32 invalid_character, Location location)
|
||||||
|
{
|
||||||
|
Error err;
|
||||||
|
err.type = errors::Unrecognized_Character;
|
||||||
|
err.invalid_character = invalid_character;
|
||||||
|
err.location = std::move(location);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
@ -40,7 +40,7 @@ auto Lexer::next_token() -> Result<Token>
|
|||||||
return { Token::Type::Numeric, finish(), token_location };
|
return { Token::Type::Numeric, finish(), token_location };
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors::Unrecognized_Character;
|
return errors::unrecognized_character(peek(), token_location);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Lexer::peek() const -> u32
|
auto Lexer::peek() const -> u32
|
||||||
|
@ -9,6 +9,7 @@ std::string_view Source = R"musique(
|
|||||||
tl::expected<void, Error> Main()
|
tl::expected<void, Error> Main()
|
||||||
{
|
{
|
||||||
Lexer lexer{Source};
|
Lexer lexer{Source};
|
||||||
|
lexer.location.filename = "example.mq";
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
auto token = Try(lexer.next_token());
|
auto token = Try(lexer.next_token());
|
||||||
@ -20,7 +21,7 @@ int main()
|
|||||||
{
|
{
|
||||||
auto result = Main();
|
auto result = Main();
|
||||||
if (not result.has_value()) {
|
if (not result.has_value()) {
|
||||||
std::cerr << result.error() << std::endl;
|
std::cerr << result.error() << std::flush;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,18 +61,33 @@ struct Error
|
|||||||
bool operator==(errors::Type);
|
bool operator==(errors::Type);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
namespace errors
|
||||||
|
{
|
||||||
|
Error unrecognized_character(u32 invalid_character);
|
||||||
|
Error unrecognized_character(u32 invalid_character, Location location);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct Result : tl::expected<T, Error>
|
struct Result : tl::expected<T, Error>
|
||||||
{
|
{
|
||||||
|
using Storage = tl::expected<T, Error>;
|
||||||
|
|
||||||
constexpr Result() = default;
|
constexpr Result() = default;
|
||||||
|
|
||||||
constexpr Result(errors::Type error) : tl::expected<T, Error>(tl::unexpected(Error { error }))
|
constexpr Result(errors::Type error)
|
||||||
|
: Storage(tl::unexpected(Error { error }))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr Result(Error error)
|
||||||
|
: Storage(tl::unexpected(std::move(error)))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ...Args>
|
template<typename ...Args>
|
||||||
constexpr Result(Args&& ...args)
|
constexpr Result(Args&& ...args)
|
||||||
: tl::expected<T, Error>( T{ std::forward<Args>(args)... } )
|
: Storage( T{ std::forward<Args>(args)... } )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user