mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:37:35 +00:00
LibCpp: Remove node_span_size, add index_of_node_at
This commit is contained in:
parent
26d9485562
commit
97f2cd596b
2 changed files with 24 additions and 22 deletions
|
@ -699,7 +699,7 @@ bool Parser::done()
|
||||||
|
|
||||||
StringView Parser::text_of_token(const Cpp::Token& token) const
|
StringView Parser::text_of_token(const Cpp::Token& token) const
|
||||||
{
|
{
|
||||||
return token.text();
|
return token.text();
|
||||||
}
|
}
|
||||||
|
|
||||||
StringView Parser::text_of_node(const ASTNode& node) const
|
StringView Parser::text_of_node(const ASTNode& node) const
|
||||||
|
@ -776,18 +776,33 @@ RefPtr<ASTNode> Parser::eof_node() const
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<ASTNode> Parser::node_at(Position pos) const
|
RefPtr<ASTNode> Parser::node_at(Position pos) const
|
||||||
|
{
|
||||||
|
auto index = index_of_node_at(pos);
|
||||||
|
if (!index.has_value())
|
||||||
|
return nullptr;
|
||||||
|
return m_nodes[index.value()];
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<size_t> Parser::index_of_node_at(Position pos) const
|
||||||
{
|
{
|
||||||
VERIFY(!m_tokens.is_empty());
|
VERIFY(!m_tokens.is_empty());
|
||||||
RefPtr<ASTNode> match_node;
|
Optional<size_t> match_node_index;
|
||||||
for (auto& node : m_nodes) {
|
|
||||||
|
auto node_span = [](const ASTNode& node) {
|
||||||
|
VERIFY(node.end().line >= node.start().line);
|
||||||
|
VERIFY((node.end().line > node.start().line) || (node.end().column >= node.start().column));
|
||||||
|
return Position { node.end().line - node.start().line, node.start().line != node.end().line ? 0 : node.end().column - node.start().column };
|
||||||
|
};
|
||||||
|
|
||||||
|
for (size_t node_index = 0; node_index < m_nodes.size(); ++node_index) {
|
||||||
|
auto& node = m_nodes[node_index];
|
||||||
if (node.start() > pos || node.end() < pos)
|
if (node.start() > pos || node.end() < pos)
|
||||||
continue;
|
continue;
|
||||||
if (!match_node)
|
|
||||||
match_node = node;
|
if (!match_node_index.has_value() || (node_span(node) < node_span(m_nodes[match_node_index.value()])))
|
||||||
else if (node_span_size(node) < node_span_size(*match_node))
|
match_node_index = node_index;
|
||||||
match_node = node;
|
|
||||||
}
|
}
|
||||||
return match_node;
|
return match_node_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<Token> Parser::token_at(Position pos) const
|
Optional<Token> Parser::token_at(Position pos) const
|
||||||
|
@ -800,18 +815,6 @@ Optional<Token> Parser::token_at(Position pos) const
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Parser::node_span_size(const ASTNode& node) const
|
|
||||||
{
|
|
||||||
if (node.start().line == node.end().line)
|
|
||||||
return node.end().column - node.start().column;
|
|
||||||
|
|
||||||
size_t span_size = m_lines[node.start().line].length() - node.start().column;
|
|
||||||
for (size_t line = node.start().line + 1; line < node.end().line; ++line) {
|
|
||||||
span_size += m_lines[line].length();
|
|
||||||
}
|
|
||||||
return span_size + m_lines[node.end().line].length() - node.end().column;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Parser::print_tokens() const
|
void Parser::print_tokens() const
|
||||||
{
|
{
|
||||||
for (auto& token : m_tokens) {
|
for (auto& token : m_tokens) {
|
||||||
|
|
|
@ -45,6 +45,7 @@ public:
|
||||||
|
|
||||||
RefPtr<ASTNode> eof_node() const;
|
RefPtr<ASTNode> eof_node() const;
|
||||||
RefPtr<ASTNode> node_at(Position) const;
|
RefPtr<ASTNode> node_at(Position) const;
|
||||||
|
Optional<size_t> index_of_node_at(Position) const;
|
||||||
Optional<Token> token_at(Position) const;
|
Optional<Token> token_at(Position) const;
|
||||||
RefPtr<const TranslationUnit> root_node() const { return m_root_node; }
|
RefPtr<const TranslationUnit> root_node() const { return m_root_node; }
|
||||||
StringView text_of_node(const ASTNode&) const;
|
StringView text_of_node(const ASTNode&) const;
|
||||||
|
@ -135,8 +136,6 @@ private:
|
||||||
|
|
||||||
void error(StringView message = {});
|
void error(StringView message = {});
|
||||||
|
|
||||||
size_t node_span_size(const ASTNode& node) const;
|
|
||||||
|
|
||||||
template<class T, class... Args>
|
template<class T, class... Args>
|
||||||
NonnullRefPtr<T>
|
NonnullRefPtr<T>
|
||||||
create_ast_node(ASTNode& parent, const Position& start, Optional<Position> end, Args&&... args)
|
create_ast_node(ASTNode& parent, const Position& start, Optional<Position> end, Args&&... args)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue