1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-23 18:15:06 +00:00

LibJS: Plumb line and column information through Lexer / Parser

While debugging test failures, it's pretty frustrating to have to go do
printf debugging to figure out what test is failing right now. While
watching your JS Raytracer stream it seemed like this was pretty
furstrating as well. So I wanted to start working on improving the
diagnostics here.

In the future I hope we can eventually be able to plumb the info down
to the Error classes so any thrown exceptions will contain enough
metadata to know where they came from.
This commit is contained in:
Brian Gianforcaro 2020-04-05 02:34:03 -07:00 committed by Andreas Kling
parent 4f200def9c
commit dd112421b4
4 changed files with 34 additions and 7 deletions

View file

@ -967,7 +967,12 @@ Token Parser::consume(TokenType type)
{
if (m_parser_state.m_current_token.type() != type) {
m_parser_state.m_has_errors = true;
fprintf(stderr, "Error: Unexpected token %s. Expected %s\n", m_parser_state.m_current_token.name(), Token::name(type));
auto& current_token = m_parser_state.m_current_token;
fprintf(stderr, "Error: Unexpected token %s. Expected %s (line: %zu, column: %zu))\n",
current_token.name(),
Token::name(type),
current_token.line_number(),
current_token.line_column());
}
return consume();
}
@ -975,7 +980,12 @@ Token Parser::consume(TokenType type)
void Parser::expected(const char* what)
{
m_parser_state.m_has_errors = true;
fprintf(stderr, "Error: Unexpected token %s. Expected %s\n", m_parser_state.m_current_token.name(), what);
auto& current_token = m_parser_state.m_current_token;
fprintf(stderr, "Error: Unexpected token %s. Expected %s (line: %zu, column: %zu)\n",
current_token.name(),
what,
current_token.line_number(),
current_token.line_column());
}
void Parser::save_state()