Skip to content

Commit

Permalink
fix: Improved Lexer and Parser error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
alinalihassan committed Mar 10, 2022
1 parent 8220711 commit 09a97f3
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 22 deletions.
15 changes: 0 additions & 15 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
# [0.4.0](https://github.com/alinalihassan/Lesma/compare/v0.3.0...v0.4.0) (2022-03-10)


### Bug Fixes

* Fixed inverted location (line:col) ([d981a58](https://github.com/alinalihassan/Lesma/commit/d981a58c1b4c3f16d0b7561b6dca4610e0c54ae4))
* Fixed tests ([35779e3](https://github.com/alinalihassan/Lesma/commit/35779e322188e946cc97bcca7b2d8d961a36f76b))
* Removed extra newline from error reporting ([2d67d15](https://github.com/alinalihassan/Lesma/commit/2d67d15777c3ffff5a36e1a10060d30a51b20bba))


### Features

* Initial change to swap from location to Span, added helper function to generate error messages ([3debf85](https://github.com/alinalihassan/Lesma/commit/3debf856309a2873b10d114fa254fefc807c4989))
* Proper error handling finished for compiler, parser and lexer ([4299f50](https://github.com/alinalihassan/Lesma/commit/4299f50126c9f3daf371c580f70022018ef48a96))

# [0.3.0](https://github.com/alinalihassan/Lesma/compare/v0.2.0...v0.3.0) (2022-03-08)


Expand Down
2 changes: 1 addition & 1 deletion src/Common/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace lesma {

// Third line
fmt::print(accent, "{} |", std::string(int(log10(span.Start.Line) + 1), ' '));
fmt::print(color | fmt::emphasis::bold, "{}{}",
fmt::print(color | fmt::emphasis::bold, "{}{}\n",
std::string(span.Start.Col, ' '), std::string(span.End.Col - span.Start.Col, '^'));

// TODO: Support multiline
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,5 @@ Token Lexer::AddIdentifierToken() {
char Lexer::LastChar() { return srcs_->at(current_lex_pos_); }

void Lexer::Error(const std::string &msg) const {
throw LexerError(Span{begin_loc, loc}, "[line {}, col {}] {}", loc.Line, loc.Col, msg);
throw LexerError(Span{begin_loc, loc}, msg);
}
10 changes: 5 additions & 5 deletions src/Frontend/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ Token Parser::Consume(TokenType type, const std::string &error_message) {
Token Parser::ConsumeNewline() {
if (Check(TokenType::NEWLINE) || Peek()->type == TokenType::EOF_TOKEN)
return Advance();
Error(Peek(), std::string{"Expected: NEWLINE or EOF, found: " + std::string{NAMEOF_ENUM(Peek()->type)}});
Error(Peek(), fmt::format("Expected: NEWLINE or EOF, found: {}", NAMEOF_ENUM(Peek()->type)));
return Token{};
}

void Parser::Error(const Token &token, const std::string &error_message) {
throw ParserError(token->span, "{}: {}", token->Dump(), error_message);
throw ParserError(token->span, "{}", error_message);
}

// TODO: Parse Type
Expand All @@ -60,7 +60,7 @@ Type *Parser::ParseType() {
return new Type(type->span, type->lexeme, type->type);
}

Error(type, "Unknown type");
Error(type, fmt::format("Unknown type: {}", type->lexeme));

return nullptr;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ Expression *Parser::ParseTerm() {
return new Literal(token->span, token->lexeme, TokenType::BOOL);
}
default:
Error(Peek(), std::string{NAMEOF_ENUM(Peek()->type)} + " " + std::string{"Unknown literal"});
Error(Peek(), fmt::format("Unknown literal: {}", Peek()->lexeme));
}

return nullptr;
Expand Down Expand Up @@ -279,7 +279,7 @@ Statement *Parser::ParseAssignment() {
return new Assignment({identifier.getStart(), expr->getEnd()}, var, op, expr);
}

Error(Peek(), "Unsupported assignment operator");
Error(Peek(), fmt::format("Unsupported assignment operator: {}", Peek()->lexeme));

return nullptr;
}
Expand Down

0 comments on commit 09a97f3

Please sign in to comment.