Browse Source

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.
Itamar 4 years ago
parent
commit
5b22f6f45a
2 changed files with 6 additions and 12 deletions
  1. 5 9
      Userland/Libraries/LibCpp/Parser.cpp
  2. 1 3
      Userland/Libraries/LibCpp/Parser.h

+ 5 - 9
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();

+ 1 - 3
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<StringView> parse_type_qualifiers();
 
-    StringView m_program;
     Preprocessor::Definitions m_definitions;
-    Vector<StringView> m_lines;
     String m_filename;
     Vector<Token> m_tokens;
     State m_state;