Skip to content

PHP8: Adds support for match expressions#73

Merged
cfroystad merged 1 commit intotree-sitter:masterfrom
cfroystad:match_expression_v2
May 3, 2021
Merged

PHP8: Adds support for match expressions#73
cfroystad merged 1 commit intotree-sitter:masterfrom
cfroystad:match_expression_v2

Conversation

@cfroystad
Copy link
Collaborator

RFC
Documentation

Introduces support for the match expression. I've taken inspiration from the switch statments implementation (they're closely related, after all).

This introduces the following new nodes:

  • match_expression
  • match_block
  • match_conditional_expression
  • match_condition_list (since the left hand side may have any number of comma separated conditional expressions)
  • match_default_expression

Example:

$statement = match ($this->lexer->lookahead['type']) {
    Lexer::T_SELECT => $this->SelectStatement(),
    Lexer::T_UPDATE => $this->UpdateStatement(),
    Lexer::T_DELETE => $this->DeleteStatement(),
    default => $this->syntaxError('SELECT, UPDATE or DELETE'),
};

Is parsed into:

(program
  (php_tag)
  (expression_statement
    (assignment_expression
      (variable_name (name))
      (match_expression
        (parenthesized_expression (variable_name (name)))
        (match_block
          (match_conditional_expression
            (match_condition_list
              (class_constant_access_expression
                (qualified_name (name))
                (name)
              )
              (variable_name (name))
            )
            (variable_name (name))
          )
          (match_conditional_expression
            (match_condition_list
              (class_constant_access_expression
                (qualified_name (name))
                (name)
              )
            )
            (function_call_expression (qualified_name (name)) (arguments))
          )
          (match_conditional_expression
            (match_condition_list
              (class_constant_access_expression
                (qualified_name (name))
                (name)
              )
            )
            (member_call_expression
              (variable_name (name))
              (name)
              (arguments)
            )
          )
          (match_default_expression
            (member_call_expression
              (variable_name (name))
              (name)
              (arguments (string))
            )
          )
        )
      )
    )
  )
)

Checklist:

  • All tests pass in CI.
  • There are sufficient tests for the new fix/feature (see tests in expressions.txt)
  • Grammar rules have not been renamed unless absolutely necessary (0 rules renamed)
  • The conflicts section hasn't grown too much (0 new conflicts)
  • The parser size hasn't grown too much (master: 1664 , PR: 1700)

@cfroystad cfroystad mentioned this pull request Apr 29, 2021
32 tasks
Copy link
Contributor

@maxbrunsfeld maxbrunsfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Thanks for the reference links and great description.

@cfroystad cfroystad force-pushed the match_expression_v2 branch from 5087b11 to f25ee27 Compare May 3, 2021 16:39
@cfroystad cfroystad merged commit b5fed3d into tree-sitter:master May 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants