Skip to content

Adds support for Enums as implemented in PHP 8.1#89

Merged
cfroystad merged 2 commits intotree-sitter:masterfrom
cfroystad:enums
Jul 12, 2021
Merged

Adds support for Enums as implemented in PHP 8.1#89
cfroystad merged 2 commits intotree-sitter:masterfrom
cfroystad:enums

Conversation

@cfroystad
Copy link
Collaborator

@cfroystad cfroystad commented Jul 10, 2021

RFC

This PR implements Enumerations for PHP as implemented in PHP 8.1

enum Suit {
  case Hearts;
  case Diamonds;
  case Clubs;
  case Spades;
}

turns into:

(enum_declaration
    (name)
    (enum_declaration_list 
      (enum_case (name))
      (enum_case (name))
      (enum_case (name))
      (enum_case (name))
   )
)

An enum in PHP might have methods and implement interfaces:

enum Suit implements Bar, Baz {
  case Hearts;
  case Diamonds;
  case Clubs;
  case Spades;

  // Fulfills the interface contract.
  public function color(): string {
    return match($this) {
      Suit::Hearts, Suit::Diamonds => 'Red',
      Suit::Clubs, Suit::Spades => 'Black',
    };
  }
}

turns into:

(enum_declaration
    (name)
    (class_interface_clause
      (name)
      (name)
    )
    (enum_declaration_list 
      (enum_case (name))
      (enum_case (name))
      (enum_case (name))
      (enum_case (name))

      (comment)
      
      (method_declaration
        (visibility_modifier)
        (name)
        (formal_parameters)
        (type_list (primitive_type))
        (compound_statement
          (return_statement
            (match_expression
              (parenthesized_expression
                (variable_name (name))
              )
              (match_block
                (match_conditional_expression
                  (match_condition_list
                    (class_constant_access_expression (name) (name))
                    (class_constant_access_expression (name) (name))
                  )
                  (string)
                )
                (match_conditional_expression
                  (match_condition_list
                    (class_constant_access_expression (name) (name))
                    (class_constant_access_expression (name) (name))
                  )
                  (string)
                )
              )
            )
          )
        )
      )
   )
)

This change brings us to 100% support for PHP 8.1 (minus pending and draft PRs) scheduled for release in november 🎉

Checklist:

  • All tests pass in CI
  • There are sufficient tests for the new fix/feature
  • 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: 2116, PR: 2552)

@cfroystad cfroystad mentioned this pull request Jul 10, 2021
32 tasks
@cfroystad cfroystad merged commit b179440 into tree-sitter:master Jul 12, 2021
@cfroystad
Copy link
Collaborator Author

After looking a bit more into this, I realized that the change increased the parser line count from 111 476 to 135 526. I'm not sure what's causing this and if there's anything to be done about it.

I'll see if I find the time to investigate, but if you have any hunches I'd be happy to learn!

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.

Sorry, I noticed a couple minor issues. Could you take a look at these comments when you get a chance?

'}',
),

_enum_member_declartaion: $ => choice(
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, there's a typo in this name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in the bugfixes branch

optional(field('attributes', $.attribute_list)),
'case',
field('name', $.name),
optional(field('value', seq('=', choice($.string, $.integer)))),
Copy link
Contributor

Choose a reason for hiding this comment

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

The value field should probably only be on the string/integer, and not on the equals sign.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in the bugfixes branch

@cfroystad
Copy link
Collaborator Author

Most certainly! Both of those are almost embarrassing, so thanks for identifying them 🙂

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