Browse Source

Util: Change Lexer line/column to 0-based

master
Riyyi 2 years ago
parent
commit
a3d1cd1d74
  1. 18
      src/util/json/job.cpp
  2. 4
      src/util/json/lexer.cpp
  3. 4
      src/util/json/lexer.h

18
src/util/json/job.cpp

@ -60,17 +60,15 @@ void Job::printErrorLine(Token token, const char* message)
"%s"
"\n";
printf(errorFormat.c_str(),
token.line,
token.column,
token.line + 1,
token.column + 1,
message);
// Get the JSON line that caused the error
std::istringstream input(m_input);
std::string line;
size_t count = 0;
while (std::getline(input, line)) {
count++;
if (count == token.line) {
for (size_t i = 0; std::getline(input, line); ++i) {
if (i == token.line) {
break;
}
}
@ -92,9 +90,9 @@ void Job::printErrorLine(Token token, const char* message)
"\033[0m" // Reset
"\n";
printf(lineFormat.c_str(),
token.line,
line.substr(0, token.column - 1).c_str(),
line.substr(token.column - 1).c_str());
token.line + 1,
line.substr(0, token.column).c_str(),
line.substr(token.column).c_str());
// Arrow pointer
std::string arrowFormat = " %s | "
@ -104,7 +102,7 @@ void Job::printErrorLine(Token token, const char* message)
"\n";
printf(arrowFormat.c_str(),
std::string(m_lineNumbersWidth, ' ').c_str(),
std::string(token.column - 1, ' ').c_str(),
std::string(token.column, ' ').c_str(),
std::string(line.length() - token.column, '~').c_str());
}

4
src/util/json/lexer.cpp

@ -103,11 +103,11 @@ void Lexer::analyze()
if (peekNext() == '\n') { // CRLF \r\n
break;
}
m_column = 0;
m_column = -1;
m_line++;
break;
case '\n':
m_column = 0;
m_column = -1;
m_line++;
break;
default:

4
src/util/json/lexer.h

@ -63,8 +63,8 @@ private:
Job* m_job { nullptr };
size_t m_index { 0 };
size_t m_column { 1 };
size_t m_line { 1 };
size_t m_column { 0 };
size_t m_line { 0 };
std::vector<Token>* m_tokens { nullptr };
};

Loading…
Cancel
Save