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

feat: use the new reserved rules api #263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

feat: use the new reserved rules api #263

wants to merge 1 commit into from

Conversation

amaanq
Copy link
Member

@amaanq amaanq commented Nov 23, 2024

⚠️ Needs tree-sitter/tree-sitter#3896 ⚠️

@calebdw you probably know PHP better than me, it'd be nice if you can audit this, but so far from what I can tell everything in the corpus parses as it should, and we have much better error recovery in a variety of cases, I just put 2 in a test (compare with error recovery w/o this PR to see a staggering difference :) ) (you will need to build tree-sitter from the branch in the linked PR to play w/ this)

@calebdw
Copy link
Collaborator

calebdw commented Nov 23, 2024

Awesome! Looks interesting, I'll play with it in the next couple of days.

I'm fairly certain named arguments could benefit from this as well

Copy link
Collaborator

@calebdw calebdw left a comment

Choose a reason for hiding this comment

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

For reference, here is the list of reserved PHP keywords:

The following words cannot be used as constants, class names, or function names. They are, however, allowed as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name.

readonly may be used as function name.

Generally, it seems like the reserved keywords are only reserved in the context of class, constant, function, trait, enum, interface, and (potentially namespace?) names

Comment on lines +117 to +118
// 'var' is allowed in variable names
variables: _ => BASE_RESERVED_KEYWORD_SET.concat([keyword('static', false)]),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be empty like properties, all of these keywords are allowed as variables

Comment on lines +6 to +13
$object->
class Test { // 'class' should not be treated as a property
}

class Example {
public function test() {
$this-> // incomplete
function process() {} // function should be recognized
Copy link
Collaborator

Choose a reason for hiding this comment

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

both class and function can technically be properties here so not sure if this will work

@@ -1214,7 +1242,7 @@ module.exports = function defineGrammar(dialect) {
),
),

_argument_name: $ => seq(
_argument_name: $ => reserved('properties', seq(
Copy link
Collaborator

Choose a reason for hiding this comment

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

it would be great if this choices list can be cleaned up here---this was needed because the grammar was having trouble parsing named arguments that could also be casts or other types / functions:

e.g.,:

$this->foo(array: $bar); // named argument
$this->foo((array) $bar); // array cast
$this->foo(array(...)); // array function (long form of [...])

However, not sure if this new feature would clean this up or if this is merely an exclusion list

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, the PR is currently adding support by default for non-reserved keywords to be usable as the word token if they overlap (the pattern for the word token matches the keyword in its entirety). So obviously operators wouldn't match, but any keywords like these would :) Really excited for that since a lot of grammars will no longer need this "_reserved_identifier" hack they currently have.

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