Browse Source

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.
Itamar 3 năm trước cách đây
mục cha
commit
4f1c77a059
2 tập tin đã thay đổi với 10 bổ sung2 xóa
  1. 9 2
      Userland/Libraries/LibCpp/Parser.cpp
  2. 1 0
      Userland/Libraries/LibCpp/Parser.h

+ 9 - 2
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<ASTNode> Parser::node_at(Position pos) const
 {
     VERIFY(m_saved_states.is_empty());
@@ -1266,7 +1273,7 @@ NonnullRefPtr<Type> 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<Name> Parser::parse_name(ASTNode& parent)
         consume(Token::Type::Greater);
     }
 
-    name_node->set_end(position());
+    name_node->set_end(previous_token_end());
     return name_node;
 }
 

+ 1 - 0
Userland/Libraries/LibCpp/Parser.h

@@ -133,6 +133,7 @@ private:
     Token peek(size_t offset = 0) const;
     Optional<Token> peek(Token::Type) const;
     Position position() const;
+    Position previous_token_end() const;
     String text_in_range(Position start, Position end) const;
 
     void save_state();