summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Lexer.cpp
diff options
context:
space:
mode:
authorBrian Gianforcaro <b.gianfo@gmail.com>2020-04-05 02:34:03 -0700
committerAndreas Kling <kling@serenityos.org>2020-04-05 12:43:39 +0200
commitdd112421b4de94c4203f9b7551cbb74e35c2c81b (patch)
tree300c14b79cfad93db9c32120deaf39c9f9f29399 /Libraries/LibJS/Lexer.cpp
parent4f200def9c735c97a0a2a2571ad455d3af644aa9 (diff)
downloadserenity-dd112421b4de94c4203f9b7551cbb74e35c2c81b.zip
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.
Diffstat (limited to 'Libraries/LibJS/Lexer.cpp')
-rw-r--r--Libraries/LibJS/Lexer.cpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/Libraries/LibJS/Lexer.cpp b/Libraries/LibJS/Lexer.cpp
index 94bf21ccc1..ddf2297306 100644
--- a/Libraries/LibJS/Lexer.cpp
+++ b/Libraries/LibJS/Lexer.cpp
@@ -39,7 +39,7 @@ HashMap<char, TokenType> Lexer::s_single_char_tokens;
Lexer::Lexer(StringView source)
: m_source(source)
- , m_current_token(TokenType::Eof, StringView(nullptr), StringView(nullptr))
+ , m_current_token(TokenType::Eof, StringView(nullptr), StringView(nullptr), 0, 0)
{
if (s_keywords.is_empty()) {
s_keywords.set("await", TokenType::Await);
@@ -146,6 +146,13 @@ void Lexer::consume()
return;
}
+ if (m_current_char == '\n') {
+ m_line_number++;
+ m_line_column = 1;
+ } else {
+ m_line_column++;
+ }
+
m_current_char = m_source[m_position++];
}
@@ -182,7 +189,7 @@ bool Lexer::is_block_comment_end() const
void Lexer::syntax_error(const char* msg)
{
m_has_errors = true;
- fprintf(stderr, "Syntax Error: %s\n", msg);
+ fprintf(stderr, "Syntax Error: %s (line: %zu, column: %zu)\n", msg, m_line_number, m_line_column);
}
Token Lexer::next()
@@ -317,7 +324,9 @@ Token Lexer::next()
m_current_token = Token(
token_type,
m_source.substring_view(trivia_start - 1, value_start - trivia_start),
- m_source.substring_view(value_start - 1, m_position - value_start));
+ m_source.substring_view(value_start - 1, m_position - value_start),
+ m_line_number,
+ m_line_column);
return m_current_token;
}