From bc01b34e5892a32fa15f3b9032472ec96fc26399 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 25 Jun 2022 14:05:45 +0200 Subject: [PATCH] Util: Calculate JSON line number before lexing --- src/util/json/job.cpp | 14 ++++++++++++-- src/util/json/job.h | 2 -- src/util/json/lexer.cpp | 3 --- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/util/json/job.cpp b/src/util/json/job.cpp index 0b72626..0c22e5f 100644 --- a/src/util/json/job.cpp +++ b/src/util/json/job.cpp @@ -4,8 +4,9 @@ * SPDX-License-Identifier: MIT */ -#include // istringstream -#include // getline +#include // count +#include // istringstream +#include // getline #include "util/json/job.h" @@ -13,6 +14,8 @@ namespace Json { Job::Job(const std::string& input) : m_input(input) + // FIXME: handle case where the file doenst have a trailing newline + , m_lineNumbersWidth(std::to_string(std::count(m_input.begin(), m_input.end(), '\n') - 1).length()) { } @@ -47,6 +50,13 @@ void Job::printErrorLine(Token token, const char* message) break; } } + // Replace tab indentation with spaces + size_t oldLineLength = line.length(); + size_t tabs = line.find_first_not_of('\t'); + if (tabs > 0 && tabs < line.size()) { + line = std::string(tabs * 4, ' ') + line.substr(tabs); + } + token.column += line.length() - oldLineLength; // JSON line std::string lineFormat = " %" diff --git a/src/util/json/job.h b/src/util/json/job.h index e35405b..42aca97 100644 --- a/src/util/json/job.h +++ b/src/util/json/job.h @@ -30,8 +30,6 @@ public: void printErrorLine(Token token, const char* message); - void setLineNumbersWidth(size_t width) { m_lineNumbersWidth = width; } - const std::string& input() { return m_input; } private: diff --git a/src/util/json/lexer.cpp b/src/util/json/lexer.cpp index af72995..f356994 100644 --- a/src/util/json/lexer.cpp +++ b/src/util/json/lexer.cpp @@ -113,9 +113,6 @@ void Lexer::analyze() m_index++; m_column++; } - - // FIXME: handle case where the file doenst have a trailing newline - m_job->setLineNumbersWidth(std::to_string(m_line - 1).length()); } // -----------------------------------------