1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-10 06:47:34 +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

@ -182,6 +182,7 @@ public:
virtual ~Parameter() override = default; virtual ~Parameter() override = default;
virtual const char* class_name() const override { return "Parameter"; } virtual const char* class_name() const override { return "Parameter"; }
virtual void dump(FILE* = stdout, size_t indent = 0) const override; virtual void dump(FILE* = stdout, size_t indent = 0) const override;
virtual bool is_parameter() const override { return true; }
Parameter(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename, StringView name) Parameter(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename, StringView name)
: VariableOrParameterDeclaration(parent, start, end, filename) : VariableOrParameterDeclaration(parent, start, end, filename)
@ -189,8 +190,6 @@ public:
m_name = name; m_name = name;
} }
virtual bool is_parameter() const override { return true; }
bool m_is_ellipsis { false }; bool m_is_ellipsis { false };
}; };

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 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 start_token_index = index_of_token_at(start);
auto end_node_index = index_of_token_at(end); auto end_node_index = index_of_token_at(end);
VERIFY(start_token_index.has_value()); VERIFY(start_token_index.has_value());
VERIFY(end_node_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) { 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) void Parser::error(StringView message)

View file

@ -49,6 +49,7 @@ public:
Preprocessor::DefinedValue preprocessor_value; Preprocessor::DefinedValue preprocessor_value;
}; };
const Vector<TokenAndPreprocessorDefinition>& replaced_preprocessor_tokens() const { return m_replaced_preprocessor_tokens; } const Vector<TokenAndPreprocessorDefinition>& replaced_preprocessor_tokens() const { return m_replaced_preprocessor_tokens; }
Vector<Token> tokens_in_range(Position start, Position end) const;
private: private:
enum class DeclarationType { enum class DeclarationType {