Jelajahi Sumber

LibCpp: Handle class access-specifiers in the Parser

We can now handle access-specifier tags (for example 'private:') when
parsing class declarations.

We currently only consume these tags on move on. We'll need to add some
logic that accounts for the access level of symbols down the road.
Itamar 4 tahun lalu
induk
melakukan
fd851ec5c9
2 mengubah file dengan 19 tambahan dan 0 penghapusan
  1. 17 0
      Userland/Libraries/LibCpp/Parser.cpp
  2. 2 0
      Userland/Libraries/LibCpp/Parser.h

+ 17 - 0
Userland/Libraries/LibCpp/Parser.cpp

@@ -656,6 +656,7 @@ bool Parser::match_class_declaration()
 
     if (!match_keyword("struct") && !match_keyword("class"))
         return false;
+
     consume(Token::Type::Keyword);
 
     if (!match(Token::Type::Identifier))
@@ -1439,6 +1440,8 @@ NonnullRefPtrVector<Declaration> Parser::parse_class_members(ASTNode& parent)
 {
     NonnullRefPtrVector<Declaration> members;
     while (!eof() && peek().type() != Token::Type::RightCurly) {
+        if (match_access_specifier())
+            consume_access_specifier(); // FIXME: Do not ignore access specifiers
         auto member_type = match_class_member();
         if (member_type.has_value()) {
             members.append(parse_declaration(parent, member_type.value()));
@@ -1450,4 +1453,18 @@ NonnullRefPtrVector<Declaration> Parser::parse_class_members(ASTNode& parent)
     return members;
 }
 
+bool Parser::match_access_specifier()
+{
+    if (peek(1).type() != Token::Type::Colon)
+        return false;
+
+    return match_keyword("private") || match_keyword("protected") || match_keyword("public");
+}
+
+void Parser::consume_access_specifier()
+{
+    consume(Token::Type::Keyword);
+    consume(Token::Type::Colon);
+}
+
 }

+ 2 - 0
Userland/Libraries/LibCpp/Parser.h

@@ -75,6 +75,7 @@ private:
     bool match_sizeof_expression();
     bool match_braced_init_list();
     bool match_type();
+    bool match_access_specifier();
 
     Optional<NonnullRefPtrVector<Parameter>> parse_parameter_list(ASTNode& parent);
     Optional<Token> consume_whitespace();
@@ -162,6 +163,7 @@ private:
 
     bool match_attribute_specification();
     void consume_attribute_specification();
+    void consume_access_specifier();
     bool match_ellipsis();
     void initialize_program_tokens(const StringView& program);
     void add_tokens_for_preprocessor(Token& replaced_token, Preprocessor::DefinedValue&);