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; 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. /// Collection of messages that are considered internal and should not be printed to the end user.
namespace internal namespace internal
{ {
@ -195,6 +203,7 @@ namespace errors
/// All possible error types /// All possible error types
using Details = std::variant< using Details = std::variant<
Closing_Token_Without_Opening,
Expected_Expression_Separator_Before, Expected_Expression_Separator_Before,
Failed_Numeric_Parsing, Failed_Numeric_Parsing,
Literal_As_Identifier, 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::Expected_Expression_Separator_Before const&) { return "Missing semicolon"; },
[](errors::Literal_As_Identifier const&) { return "Literal used in place of an identifier"; }, [](errors::Literal_As_Identifier const&) { return "Literal used in place of an identifier"; },
[](errors::Out_Of_Range const&) { return "Index out of range"; }, [](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) { [](errors::Unsupported_Types_For const& type) {
return type.type == errors::Unsupported_Types_For::Function return type.type == errors::Unsupported_Types_For::Function
? "Function called with wrong arguments" ? "Function called with wrong arguments"
@ -352,6 +357,17 @@ std::ostream& operator<<(std::ostream& os, Error const& err)
print_error_line(loc); 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(); }, [&](errors::Unexpected_Keyword const&) { unimplemented(); },
}, err.details); }, err.details);
@ -367,5 +383,7 @@ void errors::all_tokens_were_not_parsed(std::span<Token> tokens)
std::cerr << token << '\n'; std::cerr << token << '\n';
} }
encourage_contact(std::cerr);
std::exit(1); 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 .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)); errors::all_tokens_were_not_parsed(std::span(parser.tokens).subspan(parser.token_id));
} }