Error reporting when too many closing parens at the end

This commit is contained in:
Robert Bendun 2022-09-04 13:48:06 +02:00
parent f2a78cd331
commit aabcc42625
3 changed files with 40 additions and 0 deletions

View File

@ -176,6 +176,14 @@ namespace errors
size_t size;
};
struct Closing_Token_Without_Opening
{
enum {
Block = ']',
Paren = ')'
} type;
};
/// Collection of messages that are considered internal and should not be printed to the end user.
namespace internal
{
@ -195,6 +203,7 @@ namespace errors
/// All possible error types
using Details = std::variant<
Closing_Token_Without_Opening,
Expected_Expression_Separator_Before,
Failed_Numeric_Parsing,
Literal_As_Identifier,

View File

@ -146,6 +146,11 @@ std::ostream& operator<<(std::ostream& os, Error const& err)
[](errors::Expected_Expression_Separator_Before const&) { return "Missing semicolon"; },
[](errors::Literal_As_Identifier const&) { return "Literal used in place of an identifier"; },
[](errors::Out_Of_Range const&) { return "Index out of range"; },
[](errors::Closing_Token_Without_Opening const& err) {
return err.type == errors::Closing_Token_Without_Opening::Block
? "Block closing without opening"
: "Closing parentheses without opening";
},
[](errors::Unsupported_Types_For const& type) {
return type.type == errors::Unsupported_Types_For::Function
? "Function called with wrong arguments"
@ -352,6 +357,17 @@ std::ostream& operator<<(std::ostream& os, Error const& err)
print_error_line(loc);
},
[&](errors::Closing_Token_Without_Opening const& err) {
if (err.type == errors::Closing_Token_Without_Opening::Block) {
os << "Found strange block closing ']' without previous block opening '['\n";
} else {
os << "Found strange closing ')' without previous '('\n";
}
os << '\n';
print_error_line(loc);
},
[&](errors::Unexpected_Keyword const&) { unimplemented(); },
}, err.details);
@ -367,5 +383,7 @@ void errors::all_tokens_were_not_parsed(std::span<Token> tokens)
std::cerr << token << '\n';
}
encourage_contact(std::cerr);
std::exit(1);
}

View File

@ -70,6 +70,19 @@ Result<Ast> Parser::parse(std::string_view source, std::string_view filename, un
.location = parser.peek()->location
};
}
if (parser.expect(Token::Type::Close_Paren) || parser.expect(Token::Type::Close_Block)) {
auto const tok = parser.consume();
return Error {
.details = errors::Closing_Token_Without_Opening {
.type = tok.type == Token::Type::Close_Paren
? errors::Closing_Token_Without_Opening::Paren
: errors::Closing_Token_Without_Opening::Block
},
.location = tok.location
};
}
errors::all_tokens_were_not_parsed(std::span(parser.tokens).subspan(parser.token_id));
}