Missing semicolon error!

This commit is contained in:
Robert Bendun 2022-05-29 23:54:05 +02:00
parent 016e48570e
commit 25e730c8d3
3 changed files with 43 additions and 21 deletions

View File

@ -115,7 +115,8 @@ std::ostream& operator<<(std::ostream& os, Error const& err)
[](errors::Unexpected_Empty_Source const&) { return "Unexpected end of file"; }, [](errors::Unexpected_Empty_Source const&) { return "Unexpected end of file"; },
[](errors::Unexpected_Keyword const&) { return "Unexpected keyword"; }, [](errors::Unexpected_Keyword const&) { return "Unexpected keyword"; },
[](errors::Unrecognized_Character const&) { return "Unrecognized character"; }, [](errors::Unrecognized_Character const&) { return "Unrecognized character"; },
[](errors::internal::Unexpected_Token const&) { return "Unexpected token"; } [](errors::internal::Unexpected_Token const&) { return "Unexpected token"; },
[](errors::Expected_Expression_Separator_Before const&) { return "Missing semicolon"; }
}, err.details); }, err.details);
error_heading(os, err.location, Error_Level::Error, short_description); error_heading(os, err.location, Error_Level::Error, short_description);
@ -148,13 +149,22 @@ std::ostream& operator<<(std::ostream& os, Error const& err)
encourage_contact(os); encourage_contact(os);
}, },
[&os](errors::Expected_Keyword const&) {}, [&os](errors::Expected_Expression_Separator_Before const& err) {
[&os](errors::Music_Literal_Used_As_Identifier const&) {}, os << "I failed to parse following code, due to missing semicolon before it!\n";
[&os](errors::Not_Callable const&) {},
[&os](errors::Undefined_Identifier const&) {}, if (err.what == "var") {
[&os](errors::Undefined_Operator const&) {}, os << "\nIf you want to create variable inside expression try wrapping them inside parentheses like this:\n";
[&os](errors::Unexpected_Keyword const&) {}, os << " 10 + (var i = 20)\n";
[&os](errors::Unexpected_Empty_Source const&) {} }
},
[&os](errors::Expected_Keyword const&) { unimplemented(); },
[&os](errors::Music_Literal_Used_As_Identifier const&) { unimplemented(); },
[&os](errors::Not_Callable const&) { unimplemented(); },
[&os](errors::Undefined_Identifier const&) { unimplemented(); },
[&os](errors::Undefined_Operator const&) { unimplemented(); },
[&os](errors::Unexpected_Keyword const&) { unimplemented(); },
[&os](errors::Unexpected_Empty_Source const&) { unimplemented(); }
}, err.details); }, err.details);
return os; return os;

View File

@ -69,6 +69,11 @@ namespace errors
std::string_view received_type = {}; std::string_view received_type = {};
}; };
struct Expected_Expression_Separator_Before
{
std::string_view what;
};
struct Unexpected_Keyword struct Unexpected_Keyword
{ {
std::string_view keyword; std::string_view keyword;
@ -109,6 +114,7 @@ namespace errors
} }
using Details = std::variant< using Details = std::variant<
Expected_Expression_Separator_Before,
Expected_Keyword, Expected_Keyword,
Failed_Numeric_Parsing, Failed_Numeric_Parsing,
Music_Literal_Used_As_Identifier, Music_Literal_Used_As_Identifier,

View File

@ -40,6 +40,12 @@ Result<Ast> Parser::parse(std::string_view source, std::string_view filename)
auto const result = parser.parse_sequence(); auto const result = parser.parse_sequence();
if (parser.token_id < parser.tokens.size()) { if (parser.token_id < parser.tokens.size()) {
if (parser.expect(Token::Type::Keyword, "var")) {
return Error {
.details = errors::Expected_Expression_Separator_Before { .what = "var" },
.location = parser.peek()->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));
} }
@ -54,10 +60,10 @@ Result<Ast> Parser::parse_sequence()
Result<Ast> Parser::parse_expression() Result<Ast> Parser::parse_expression()
{ {
auto var = parse_variable_declaration(); if (expect(Token::Type::Keyword, "var")) {
if (!var.has_value()) return parse_variable_declaration();
}
return parse_infix_expression(); return parse_infix_expression();
return var;
} }
Result<Ast> Parser::parse_variable_declaration() Result<Ast> Parser::parse_variable_declaration()