From 5b22f6f45a832d7b56fd1a9a566aeb35072c6972 Mon Sep 17 00:00:00 2001 From: Itamar Date: Fri, 12 Mar 2021 15:56:30 +0200 Subject: [PATCH] LibCpp: Parser no longer holds the program's source After we moved to storing the text of each token in the token itself, we no longer have to store the source of the program in the Parser. This makes more sense because the parser should deal with tokens, not with raw source code. --- Userland/Libraries/LibCpp/Parser.cpp | 14 +++++--------- Userland/Libraries/LibCpp/Parser.h | 4 +--- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp index a477517813..c9f819e0a5 100644 --- a/Userland/Libraries/LibCpp/Parser.cpp +++ b/Userland/Libraries/LibCpp/Parser.cpp @@ -38,14 +38,11 @@ namespace Cpp { Parser::Parser(const StringView& program, const String& filename, Preprocessor::Definitions&& definitions) - : m_program(program) - , m_definitions(move(definitions)) + : m_definitions(move(definitions)) , m_filename(filename) { - initialize_program_tokens(); + initialize_program_tokens(program); #if CPP_DEBUG - dbgln("Program:"); - dbgln("{}", m_program); dbgln("Tokens:"); for (auto& token : m_tokens) { StringView text; @@ -57,9 +54,9 @@ Parser::Parser(const StringView& program, const String& filename, Preprocessor:: } #endif } -void Parser::initialize_program_tokens() +void Parser::initialize_program_tokens(const StringView& program) { - Lexer lexer(m_program); + Lexer lexer(program); for (auto& token : lexer.lex()) { if (token.type() == Token::Type::Whitespace) continue; @@ -713,8 +710,7 @@ String Parser::text_in_range(Position start, Position end) const 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) - { + for (size_t i = start_token_index.value(); i <= end_node_index.value(); ++i) { text.append(m_tokens[i].text()); } return text.build(); diff --git a/Userland/Libraries/LibCpp/Parser.h b/Userland/Libraries/LibCpp/Parser.h index 818818508c..62523ddc9e 100644 --- a/Userland/Libraries/LibCpp/Parser.h +++ b/Userland/Libraries/LibCpp/Parser.h @@ -159,13 +159,11 @@ private: bool match_attribute_specification(); void consume_attribute_specification(); bool match_ellipsis(); - void initialize_program_tokens(); + void initialize_program_tokens(const StringView& program); void add_tokens_for_preprocessor(Token& replaced_token, Preprocessor::DefinedValue&); Vector parse_type_qualifiers(); - StringView m_program; Preprocessor::Definitions m_definitions; - Vector m_lines; String m_filename; Vector m_tokens; State m_state;