From 4f1c77a0595b0656714811461846dcad66694ef2 Mon Sep 17 00:00:00 2001 From: Itamar Date: Sat, 5 Feb 2022 18:10:03 +0200 Subject: [PATCH] LibCpp: Fix end position calculation for various AST node types Previously, the end position of the Type and Name nodes was incorrect. It was incorrectly set to the start position of the next unconsumed token. This commit adds a previous_token_end() method and uses it to correctly get the end position for these node types. --- Userland/Libraries/LibCpp/Parser.cpp | 11 +++++++++-- Userland/Libraries/LibCpp/Parser.h | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp index 60ffd952a2..f1dec3a00d 100644 --- a/Userland/Libraries/LibCpp/Parser.cpp +++ b/Userland/Libraries/LibCpp/Parser.cpp @@ -950,6 +950,13 @@ Position Parser::position() const return peek().start(); } +Position Parser::previous_token_end() const +{ + if (m_state.token_index < 1) + return {}; + return m_tokens[m_state.token_index - 1].end(); +} + RefPtr Parser::node_at(Position pos) const { VERIFY(m_saved_states.is_empty()); @@ -1266,7 +1273,7 @@ NonnullRefPtr Parser::parse_type(ASTNode& parent) type = fn_type; } - type->set_end(position()); + type->set_end(previous_token_end()); return type; } @@ -1451,7 +1458,7 @@ NonnullRefPtr Parser::parse_name(ASTNode& parent) consume(Token::Type::Greater); } - name_node->set_end(position()); + name_node->set_end(previous_token_end()); return name_node; } diff --git a/Userland/Libraries/LibCpp/Parser.h b/Userland/Libraries/LibCpp/Parser.h index 9a0f457fac..ee74b28720 100644 --- a/Userland/Libraries/LibCpp/Parser.h +++ b/Userland/Libraries/LibCpp/Parser.h @@ -133,6 +133,7 @@ private: Token peek(size_t offset = 0) const; Optional peek(Token::Type) const; Position position() const; + Position previous_token_end() const; String text_in_range(Position start, Position end) const; void save_state();