Kaynağa Gözat

LibJS/Parser: Implement the parsing of unary/prefixed update expressions

0xtechnobabble 5 yıl önce
ebeveyn
işleme
2e90a7eb2d
2 değiştirilmiş dosya ile 39 ekleme ve 1 silme
  1. 37 1
      Libraries/LibJS/Parser.cpp
  2. 2 0
      Libraries/LibJS/Parser.h

+ 37 - 1
Libraries/LibJS/Parser.cpp

@@ -233,6 +233,29 @@ NonnullOwnPtr<Expression> Parser::parse_primary_expression()
     }
     }
 }
 }
 
 
+NonnullOwnPtr<Expression> Parser::parse_unary_prefixed_expression()
+{
+    switch (m_current_token.type()) {
+    case TokenType::PlusPlus:
+        consume();
+        return make<UpdateExpression>(UpdateOp::Increment, parse_primary_expression(), true);
+    case TokenType::MinusMinus:
+        consume();
+        return make<UpdateExpression>(UpdateOp::Decrement, parse_primary_expression(), true);
+    case TokenType::ExclamationMark:
+        consume();
+        return make<UnaryExpression>(UnaryOp::Not, parse_primary_expression());
+    case TokenType::Tilde:
+        consume();
+        return make<UnaryExpression>(UnaryOp::BitwiseNot, parse_primary_expression());
+    default:
+        m_has_errors = true;
+        expected("primary expression (missing switch case)");
+        consume();
+        return make<ErrorExpression>();
+    }
+}
+
 NonnullOwnPtr<ObjectExpression> Parser::parse_object_expression()
 NonnullOwnPtr<ObjectExpression> Parser::parse_object_expression()
 {
 {
     // FIXME: Parse actual object expression
     // FIXME: Parse actual object expression
@@ -243,6 +266,9 @@ NonnullOwnPtr<ObjectExpression> Parser::parse_object_expression()
 
 
 NonnullOwnPtr<Expression> Parser::parse_expression(int min_precedence, Associativity associativity)
 NonnullOwnPtr<Expression> Parser::parse_expression(int min_precedence, Associativity associativity)
 {
 {
+    if (match_unary_prefixed_expression())
+        return parse_unary_prefixed_expression();
+
     auto expression = parse_primary_expression();
     auto expression = parse_primary_expression();
     while (match_secondary_expression()) {
     while (match_secondary_expression()) {
         int new_precedence = operator_precedence(m_current_token.type());
         int new_precedence = operator_precedence(m_current_token.type());
@@ -477,7 +503,17 @@ bool Parser::match_expression() const
         || type == TokenType::New
         || type == TokenType::New
         || type == TokenType::CurlyOpen
         || type == TokenType::CurlyOpen
         || type == TokenType::BracketOpen
         || type == TokenType::BracketOpen
-        || type == TokenType::ParenOpen;
+        || type == TokenType::ParenOpen
+        || match_unary_prefixed_expression();
+}
+
+bool Parser::match_unary_prefixed_expression() const
+{
+    auto type = m_current_token.type();
+    return type == TokenType::PlusPlus
+        || type == TokenType::MinusMinus
+        || type == TokenType::ExclamationMark
+        || type == TokenType::Tilde;
 }
 }
 
 
 bool Parser::match_secondary_expression() const
 bool Parser::match_secondary_expression() const

+ 2 - 0
Libraries/LibJS/Parser.h

@@ -52,6 +52,7 @@ public:
 
 
     NonnullOwnPtr<Expression> parse_expression(int min_precedence, Associativity associate = Associativity::Right);
     NonnullOwnPtr<Expression> parse_expression(int min_precedence, Associativity associate = Associativity::Right);
     NonnullOwnPtr<Expression> parse_primary_expression();
     NonnullOwnPtr<Expression> parse_primary_expression();
+    NonnullOwnPtr<Expression> parse_unary_prefixed_expression();
     NonnullOwnPtr<ObjectExpression> parse_object_expression();
     NonnullOwnPtr<ObjectExpression> parse_object_expression();
     NonnullOwnPtr<Expression> parse_secondary_expression(NonnullOwnPtr<Expression>, int min_precedence, Associativity associate = Associativity::Right);
     NonnullOwnPtr<Expression> parse_secondary_expression(NonnullOwnPtr<Expression>, int min_precedence, Associativity associate = Associativity::Right);
     NonnullOwnPtr<CallExpression> parse_call_expression(NonnullOwnPtr<Expression>);
     NonnullOwnPtr<CallExpression> parse_call_expression(NonnullOwnPtr<Expression>);
@@ -62,6 +63,7 @@ private:
     int operator_precedence(TokenType) const;
     int operator_precedence(TokenType) const;
     Associativity operator_associativity(TokenType) const;
     Associativity operator_associativity(TokenType) const;
     bool match_expression() const;
     bool match_expression() const;
+    bool match_unary_prefixed_expression() const;
     bool match_secondary_expression() const;
     bool match_secondary_expression() const;
     bool match_statement() const;
     bool match_statement() const;
     bool match(TokenType type) const;
     bool match(TokenType type) const;