Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for attributes #69

Merged
merged 1 commit into from
May 4, 2021

Conversation

cfroystad
Copy link
Collaborator

@cfroystad cfroystad commented Apr 17, 2021

rfc
syntax documentation

Adds support for attributes on classes, methods, functions, parameters, properties and class constants.

Consequence of addition:

  • Single line comments may no longer start with #[
  • Added conflict on [$.qualified_name, $.attribute]

Checklist:

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

@cfroystad cfroystad force-pushed the attributes_v2 branch 4 times, most recently from 5c16bb6 to a8b3ddf Compare April 23, 2021 17:32
@cfroystad cfroystad marked this pull request as ready for review April 23, 2021 17:36
@cfroystad
Copy link
Collaborator Author

Unfortunately, this adds a new conflict to the parser - and I don't see a way around that. Could you have a look at your convenience, @maxbrunsfeld?

If I remove the conflict, these are the options provided by the builder:

Unresolved conflict for symbol sequence:

  php_tag  '#['  name  •  ','  …

Possible interpretations:

  1:  php_tag  '#['  (attribute  name)  •  ','  …
  2:  php_tag  '#['  (qualified_name  name)  •  ','  …

Possible resolutions:

  1:  Specify a higher precedence in `qualified_name` than in the other rules.
  2:  Specify a higher precedence in `attribute` than in the other rules.
  3:  Add a conflict for these rules: `qualified_name`, `attribute`

Relevant definition from documentation:

First, an attribute declaration is always enclosed with a starting #[ and a corresponding ending ]. Inside, one or many attributes are listed, separated by comma. The attribute name is an unqualified, qualified or fully-qualified name as described in Using Namespaces Basics. Arguments to the attribute are optional, but are enclosed in the usual parenthesis (). Arguments to attributes can only be literal values or constant expressions. Both positional and named arguments syntax can be used.

Note: I've not placed any restrictions on the parameter types, but allow parsing them to keep the parser simple. I assume any utility using the parser will handle this. Please correct me if that is a wrongful assumption and we should only parse strictly valid code (even though that might be tricky while supporting multiple PHP versions)

@maxbrunsfeld
Copy link
Contributor

It seems like in the existing grammar, qualified_name matches any name, even if it isn't qualified. So would it work to simply only allow qualified_name inside of attribute (remove the choice that allows $.name to be used directly)?

…ters lead on disallowing comments starting with #[.
@cfroystad
Copy link
Collaborator Author

Thanks for your guidance, that did indeed do the trick!

It does feels wrong to use a node named qualified_name to mean both qualified and unqualified, but I guess I'll try to address that later - if I find a suitable way.

Thanks again!

@maxbrunsfeld
Copy link
Contributor

I agree; it'd be great to address that at some point.

@cfroystad
Copy link
Collaborator Author

Peculiarly, according to the specification it seems to be correct... I'll have to study how other PHP parsers solve that before proposing a solution

@cfroystad cfroystad merged commit f75b3f7 into tree-sitter:master May 4, 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