mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 03:07:44 +00:00
LibCpp: Implement Parser::text_in_range using text of tokens
It was previously implemented by directly iterating over the program's source.
This commit is contained in:
parent
97f2cd596b
commit
8a102fe3ec
5 changed files with 35 additions and 32 deletions
|
@ -135,7 +135,7 @@ NonnullRefPtrVector<Declaration> ParserAutoComplete::get_available_declarations(
|
|||
return available_declarations;
|
||||
}
|
||||
|
||||
Vector<GUI::AutocompleteProvider::Entry> ParserAutoComplete::autocomplete_name(const DocumentData& document, const ASTNode& node, const StringView& partial_text) const
|
||||
Vector<GUI::AutocompleteProvider::Entry> ParserAutoComplete::autocomplete_name(const DocumentData& document, const ASTNode& node, const String& partial_text) const
|
||||
{
|
||||
auto available_declarations = get_available_declarations(document, node);
|
||||
Vector<StringView> available_names;
|
||||
|
@ -166,7 +166,7 @@ Vector<GUI::AutocompleteProvider::Entry> ParserAutoComplete::autocomplete_name(c
|
|||
return suggestions;
|
||||
}
|
||||
|
||||
Vector<GUI::AutocompleteProvider::Entry> ParserAutoComplete::autocomplete_property(const DocumentData& document, const MemberExpression& parent, const StringView partial_text) const
|
||||
Vector<GUI::AutocompleteProvider::Entry> ParserAutoComplete::autocomplete_property(const DocumentData& document, const MemberExpression& parent, const String partial_text) const
|
||||
{
|
||||
auto type = type_of(document, *parent.m_object);
|
||||
if (type.is_null()) {
|
||||
|
|
|
@ -81,8 +81,8 @@ private:
|
|||
OwnPtr<Parser> m_parser;
|
||||
};
|
||||
|
||||
Vector<GUI::AutocompleteProvider::Entry> autocomplete_property(const DocumentData&, const MemberExpression&, const StringView partial_text) const;
|
||||
Vector<GUI::AutocompleteProvider::Entry> autocomplete_name(const DocumentData&, const ASTNode&, const StringView& partial_text) const;
|
||||
Vector<GUI::AutocompleteProvider::Entry> autocomplete_property(const DocumentData&, const MemberExpression&, const String partial_text) const;
|
||||
Vector<GUI::AutocompleteProvider::Entry> autocomplete_name(const DocumentData&, const ASTNode&, const String& partial_text) const;
|
||||
String type_of(const DocumentData&, const Expression&) const;
|
||||
String type_of_property(const DocumentData&, const Identifier&) const;
|
||||
String type_of_variable(const Identifier&) const;
|
||||
|
|
|
@ -443,7 +443,7 @@ public:
|
|||
virtual const char* class_name() const override { return "StringLiteral"; }
|
||||
virtual void dump(size_t indent) const override;
|
||||
|
||||
StringView m_value;
|
||||
String m_value;
|
||||
};
|
||||
|
||||
class ReturnStatement : public Statement {
|
||||
|
|
|
@ -40,7 +40,6 @@ namespace Cpp {
|
|||
Parser::Parser(const StringView& program, const String& filename, Preprocessor::Definitions&& definitions)
|
||||
: m_program(program)
|
||||
, m_definitions(move(definitions))
|
||||
, m_lines(m_program.split_view("\n", true))
|
||||
, m_filename(filename)
|
||||
{
|
||||
initialize_program_tokens();
|
||||
|
@ -702,30 +701,23 @@ StringView Parser::text_of_token(const Cpp::Token& token) const
|
|||
return token.text();
|
||||
}
|
||||
|
||||
StringView Parser::text_of_node(const ASTNode& node) const
|
||||
String Parser::text_of_node(const ASTNode& node) const
|
||||
{
|
||||
return text_of_range(node.start(), node.end());
|
||||
return text_in_range(node.start(), node.end());
|
||||
}
|
||||
|
||||
StringView Parser::text_of_range(Position start, Position end) const
|
||||
String Parser::text_in_range(Position start, Position end) const
|
||||
{
|
||||
if (start.line == end.line) {
|
||||
VERIFY(start.column <= end.column);
|
||||
return m_lines[start.line].substring_view(start.column, end.column - start.column + 1);
|
||||
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;
|
||||
for(size_t i = start_token_index.value(); i <= end_node_index.value(); ++i)
|
||||
{
|
||||
text.append(m_tokens[i].text());
|
||||
}
|
||||
|
||||
auto index_of_position([this](auto position) {
|
||||
size_t start_index = 0;
|
||||
for (size_t line = 0; line < position.line; ++line) {
|
||||
start_index += m_lines[line].length() + 1;
|
||||
}
|
||||
start_index += position.column;
|
||||
return start_index;
|
||||
});
|
||||
auto start_index = index_of_position(start);
|
||||
auto end_index = index_of_position(end);
|
||||
VERIFY(end_index >= start_index);
|
||||
return m_program.substring_view(start_index, end_index - start_index);
|
||||
return text.build();
|
||||
}
|
||||
|
||||
void Parser::error(StringView message)
|
||||
|
@ -807,10 +799,19 @@ Optional<size_t> Parser::index_of_node_at(Position pos) const
|
|||
|
||||
Optional<Token> Parser::token_at(Position pos) const
|
||||
{
|
||||
for (auto& token : m_tokens) {
|
||||
auto index = index_of_token_at(pos);
|
||||
if (!index.has_value())
|
||||
return {};
|
||||
return m_tokens[index.value()];
|
||||
}
|
||||
|
||||
Optional<size_t> Parser::index_of_token_at(Position pos) const
|
||||
{
|
||||
for (size_t token_index = 0; token_index < m_tokens.size(); ++token_index) {
|
||||
auto token = m_tokens[token_index];
|
||||
if (token.start() > pos || token.end() < pos)
|
||||
continue;
|
||||
return token;
|
||||
return token_index;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
@ -880,7 +881,7 @@ NonnullRefPtr<StringLiteral> Parser::parse_string_literal(ASTNode& parent)
|
|||
Token start_token = m_tokens[start_token_index.value()];
|
||||
Token end_token = m_tokens[end_token_index.value()];
|
||||
|
||||
auto text = text_of_range(start_token.start(), end_token.end());
|
||||
auto text = text_in_range(start_token.start(), end_token.end());
|
||||
auto string_literal = create_ast_node<StringLiteral>(parent, start_token.start(), end_token.end());
|
||||
string_literal->m_value = text;
|
||||
return string_literal;
|
||||
|
|
|
@ -27,15 +27,16 @@
|
|||
#pragma once
|
||||
|
||||
#include "AK/NonnullRefPtr.h"
|
||||
#include <AK/Noncopyable.h>
|
||||
#include "AST.h"
|
||||
#include "Preprocessor.h"
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <LibCpp/Lexer.h>
|
||||
|
||||
namespace Cpp {
|
||||
|
||||
class Parser final {
|
||||
AK_MAKE_NONCOPYABLE(Parser);
|
||||
|
||||
public:
|
||||
explicit Parser(const StringView& program, const String& filename, Preprocessor::Definitions&& = {});
|
||||
~Parser() = default;
|
||||
|
@ -47,12 +48,13 @@ public:
|
|||
RefPtr<ASTNode> node_at(Position) const;
|
||||
Optional<size_t> index_of_node_at(Position) const;
|
||||
Optional<Token> token_at(Position) const;
|
||||
Optional<size_t> index_of_token_at(Position) const;
|
||||
RefPtr<const TranslationUnit> root_node() const { return m_root_node; }
|
||||
StringView text_of_node(const ASTNode&) const;
|
||||
String text_of_node(const ASTNode&) const;
|
||||
StringView text_of_token(const Cpp::Token& token) const;
|
||||
void print_tokens() const;
|
||||
Vector<String> errors() const { return m_errors; }
|
||||
const Preprocessor::Definitions& definitions() const {return m_definitions;}
|
||||
const Preprocessor::Definitions& definitions() const { return m_definitions; }
|
||||
|
||||
private:
|
||||
enum class DeclarationType {
|
||||
|
@ -119,7 +121,7 @@ private:
|
|||
Token peek(size_t offset = 0) const;
|
||||
Optional<Token> peek(Token::Type) const;
|
||||
Position position() const;
|
||||
StringView text_of_range(Position start, Position end) const;
|
||||
String text_in_range(Position start, Position end) const;
|
||||
|
||||
void save_state();
|
||||
void load_state();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue