1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 10:18:13 +00:00

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.
This commit is contained in:
Itamar 2022-02-05 18:10:03 +02:00 committed by Andreas Kling
parent ae68355a56
commit 4f1c77a059
2 changed files with 10 additions and 2 deletions

View file

@ -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;
}

View file

@ -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();