mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 14:57:35 +00:00
LibJS: Fix incorrect token column values (#2401)
- initializing m_line_column to 1 in the lexer results in incorrect column values in tokens on the first line of input. - not incrementing m_line_column when EOF is reached results in an incorrect column value on the last token.
This commit is contained in:
parent
7bb69bb9bf
commit
11405c5139
4 changed files with 15 additions and 17 deletions
|
@ -149,8 +149,12 @@ Lexer::Lexer(StringView source)
|
||||||
|
|
||||||
void Lexer::consume()
|
void Lexer::consume()
|
||||||
{
|
{
|
||||||
if (m_position >= m_source.length()) {
|
if (m_position > m_source.length())
|
||||||
m_position = m_source.length() + 1;
|
return;
|
||||||
|
|
||||||
|
if (m_position == m_source.length()) {
|
||||||
|
m_position++;
|
||||||
|
m_line_column++;
|
||||||
m_current_char = EOF;
|
m_current_char = EOF;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,11 +55,11 @@ private:
|
||||||
bool match(char, char, char, char) const;
|
bool match(char, char, char, char) const;
|
||||||
|
|
||||||
StringView m_source;
|
StringView m_source;
|
||||||
size_t m_position = 0;
|
size_t m_position { 0 };
|
||||||
Token m_current_token;
|
Token m_current_token;
|
||||||
int m_current_char = 0;
|
int m_current_char { 0 };
|
||||||
size_t m_line_number = 1;
|
size_t m_line_number { 1 };
|
||||||
size_t m_line_column = 1;
|
size_t m_line_column { 0 };
|
||||||
|
|
||||||
struct TemplateState {
|
struct TemplateState {
|
||||||
bool in_expr;
|
bool in_expr;
|
||||||
|
|
|
@ -43,9 +43,7 @@ String MarkupGenerator::html_from_source(const StringView& source)
|
||||||
auto lexer = Lexer(source);
|
auto lexer = Lexer(source);
|
||||||
for (auto token = lexer.next(); token.type() != TokenType::Eof; token = lexer.next()) {
|
for (auto token = lexer.next(); token.type() != TokenType::Eof; token = lexer.next()) {
|
||||||
auto length = token.value().length();
|
auto length = token.value().length();
|
||||||
auto start = token.line_column();
|
auto start = token.line_column() - 1;
|
||||||
// FIXME: Why do we need to do this magic math? This math isn't even accurate enough, code like "let x = 10" renders incorrectly.
|
|
||||||
start = start < 2 ? 0 : start - 2;
|
|
||||||
|
|
||||||
if (start > source_cursor) {
|
if (start > source_cursor) {
|
||||||
builder.append(source.substring_view(source_cursor, start - source_cursor));
|
builder.append(source.substring_view(source_cursor, start - source_cursor));
|
||||||
|
@ -321,4 +319,4 @@ String MarkupGenerator::wrap_string_in_style(String source, StyleType type)
|
||||||
return String::format("<span style=\"%s\">%s</span>", style_from_style_type(type).characters(), source.characters());
|
return String::format("<span style=\"%s\">%s</span>", style_from_style_type(type).characters(), source.characters());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -528,19 +528,15 @@ int main(int argc, char** argv)
|
||||||
editor.stylize(span, styles);
|
editor.stylize(span, styles);
|
||||||
};
|
};
|
||||||
editor.strip_styles();
|
editor.strip_styles();
|
||||||
StringBuilder builder;
|
|
||||||
builder.append(editor.line());
|
|
||||||
// FIXME: The lexer returns weird position information without this
|
|
||||||
builder.append(" ");
|
|
||||||
String str = builder.build();
|
|
||||||
|
|
||||||
size_t open_indents = s_repl_line_level;
|
size_t open_indents = s_repl_line_level;
|
||||||
|
|
||||||
JS::Lexer lexer(str);
|
auto line = editor.line();
|
||||||
|
JS::Lexer lexer(line);
|
||||||
bool indenters_starting_line = true;
|
bool indenters_starting_line = true;
|
||||||
for (JS::Token token = lexer.next(); token.type() != JS::TokenType::Eof; token = lexer.next()) {
|
for (JS::Token token = lexer.next(); token.type() != JS::TokenType::Eof; token = lexer.next()) {
|
||||||
auto length = token.value().length();
|
auto length = token.value().length();
|
||||||
auto start = token.line_column() - 2;
|
auto start = token.line_column() - 1;
|
||||||
auto end = start + length;
|
auto end = start + length;
|
||||||
if (indenters_starting_line) {
|
if (indenters_starting_line) {
|
||||||
if (token.type() != JS::TokenType::ParenClose && token.type() != JS::TokenType::BracketClose && token.type() != JS::TokenType::CurlyClose) {
|
if (token.type() != JS::TokenType::ParenClose && token.type() != JS::TokenType::BracketClose && token.type() != JS::TokenType::CurlyClose) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue