Browse Source

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 years ago
parent
commit
fd851ec5c9
2 changed files with 19 additions and 0 deletions
  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"))
     if (!match_keyword("struct") && !match_keyword("class"))
         return false;
         return false;
+
     consume(Token::Type::Keyword);
     consume(Token::Type::Keyword);
 
 
     if (!match(Token::Type::Identifier))
     if (!match(Token::Type::Identifier))
@@ -1439,6 +1440,8 @@ NonnullRefPtrVector<Declaration> Parser::parse_class_members(ASTNode& parent)
 {
 {
     NonnullRefPtrVector<Declaration> members;
     NonnullRefPtrVector<Declaration> members;
     while (!eof() && peek().type() != Token::Type::RightCurly) {
     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();
         auto member_type = match_class_member();
         if (member_type.has_value()) {
         if (member_type.has_value()) {
             members.append(parse_declaration(parent, member_type.value()));
             members.append(parse_declaration(parent, member_type.value()));
@@ -1450,4 +1453,18 @@ NonnullRefPtrVector<Declaration> Parser::parse_class_members(ASTNode& parent)
     return members;
     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_sizeof_expression();
     bool match_braced_init_list();
     bool match_braced_init_list();
     bool match_type();
     bool match_type();
+    bool match_access_specifier();
 
 
     Optional<NonnullRefPtrVector<Parameter>> parse_parameter_list(ASTNode& parent);
     Optional<NonnullRefPtrVector<Parameter>> parse_parameter_list(ASTNode& parent);
     Optional<Token> consume_whitespace();
     Optional<Token> consume_whitespace();
@@ -162,6 +163,7 @@ private:
 
 
     bool match_attribute_specification();
     bool match_attribute_specification();
     void consume_attribute_specification();
     void consume_attribute_specification();
+    void consume_access_specifier();
     bool match_ellipsis();
     bool match_ellipsis();
     void initialize_program_tokens(const StringView& program);
     void initialize_program_tokens(const StringView& program);
     void add_tokens_for_preprocessor(Token& replaced_token, Preprocessor::DefinedValue&);
     void add_tokens_for_preprocessor(Token& replaced_token, Preprocessor::DefinedValue&);