Human readable token type names for error reporting

This commit is contained in:
Robert Bendun 2022-05-29 22:46:20 +02:00
parent 605533de32
commit 9c4b57f217
3 changed files with 22 additions and 2 deletions

View File

@ -240,3 +240,21 @@ std::ostream& operator<<(std::ostream& os, Token::Type type)
}
unreachable();
}
std::string_view type_name(Token::Type type)
{
switch (type) {
case Token::Type::Chord: return "chord";
case Token::Type::Close_Block: return "]";
case Token::Type::Close_Paren: return ")";
case Token::Type::Expression_Separator: return "|";
case Token::Type::Keyword: return "keyword";
case Token::Type::Numeric: return "numeric";
case Token::Type::Open_Block: return "[";
case Token::Type::Open_Paren: return "(";
case Token::Type::Operator: return "operator";
case Token::Type::Parameter_Separator: return "parameter separator";
case Token::Type::Symbol: return "symbol";
}
unreachable();
}

View File

@ -350,6 +350,8 @@ struct Token
Location location;
};
std::string_view type_name(Token::Type type);
/// Token debug printing
std::ostream& operator<<(std::ostream& os, Token const& tok);

View File

@ -66,7 +66,7 @@ Result<Ast> Parser::parse_variable_declaration()
Error error;
errors::Expected_Keyword kw { .keyword = "var" };
if (token_id >= tokens.size()) {
kw.received_type = ""; // TODO Token type
kw.received_type = type_name(peek()->type);
error.location = peek()->location;
}
error.details = std::move(kw);
@ -170,7 +170,7 @@ Result<Ast> Parser::parse_atomic_expression()
default:
return Error {
.details = errors::internal::Unexpected_Token {
.type = "", // TODO fill type
.type = type_name(peek()->type),
.source = peek()->source,
.when = "atomic expression parsing"
},