1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-02 23:32:06 +00:00

LibCpp: Add Parser::tokens_in_range(start, end)

This function returns the tokens that exist in the specified range.
This commit is contained in:
Itamar 2021-07-03 11:47:41 +03:00 committed by Andreas Kling
parent 9a31fb6673
commit 232013c05b
3 changed files with 15 additions and 5 deletions

View file

@ -869,16 +869,26 @@ String Parser::text_of_node(const ASTNode& node) const
}
String Parser::text_in_range(Position start, Position end) const
{
StringBuilder builder;
for (auto token : tokens_in_range(start, end)) {
builder.append(token.text());
}
return builder.to_string();
}
Vector<Token> Parser::tokens_in_range(Position start, Position end) const
{
auto start_token_index = index_of_token_at(start);
auto end_node_index = index_of_token_at(end);
VERIFY(start_token_index.has_value());
VERIFY(end_node_index.has_value());
StringBuilder text;
Vector<Token> tokens;
for (size_t i = start_token_index.value(); i <= end_node_index.value(); ++i) {
text.append(m_tokens[i].text());
tokens.append(m_tokens[i]);
}
return text.build();
return tokens;
}
void Parser::error(StringView message)