Util: Calculate JSON line number before lexing
This commit is contained in:
+12
-2
@@ -4,8 +4,9 @@
|
|||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sstream> // istringstream
|
#include <algorithm> // count
|
||||||
#include <string> // getline
|
#include <sstream> // istringstream
|
||||||
|
#include <string> // getline
|
||||||
|
|
||||||
#include "util/json/job.h"
|
#include "util/json/job.h"
|
||||||
|
|
||||||
@@ -13,6 +14,8 @@ namespace Json {
|
|||||||
|
|
||||||
Job::Job(const std::string& input)
|
Job::Job(const std::string& input)
|
||||||
: m_input(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;
|
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
|
// JSON line
|
||||||
std::string lineFormat = " %"
|
std::string lineFormat = " %"
|
||||||
|
|||||||
@@ -30,8 +30,6 @@ public:
|
|||||||
|
|
||||||
void printErrorLine(Token token, const char* message);
|
void printErrorLine(Token token, const char* message);
|
||||||
|
|
||||||
void setLineNumbersWidth(size_t width) { m_lineNumbersWidth = width; }
|
|
||||||
|
|
||||||
const std::string& input() { return m_input; }
|
const std::string& input() { return m_input; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -113,9 +113,6 @@ void Lexer::analyze()
|
|||||||
m_index++;
|
m_index++;
|
||||||
m_column++;
|
m_column++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: handle case where the file doenst have a trailing newline
|
|
||||||
m_job->setLineNumbersWidth(std::to_string(m_line - 1).length());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user