Skip to content

feat: use the new reserved rules api - #263

Closed
amaanq wants to merge 1 commit into
masterfrom
reserved
Closed

amaanq wants to merge 1 commit into
masterfrom
reserved

Conversation

@amaanq

@amaanq amaanq commented Nov 23, 2024 •

Copy link
Copy Markdown
Member

⚠️ 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

calebdw commented Nov 23, 2024

Copy link
Copy Markdown
Collaborator

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

@calebdw calebdw left a comment

Copy link
Copy Markdown
Collaborator

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 thread common/define-grammar.js
Comment on lines +117 to +118
// 'var' is allowed in variable names
variables: _ => BASE_RESERVED_KEYWORD_SET.concat([keyword('static', false)]),

Copy link
Copy Markdown
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 thread test/corpus/errors.txt
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
Copy Markdown
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

Comment thread common/define-grammar.js
),

_argument_name: $ => seq(
_argument_name: $ => reserved('properties', seq(

Copy link
Copy Markdown
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
Copy Markdown
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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@amaanq, this means you can remove the $._reserved_identifier rule right? I don't see that in this PR?

@calebdw

calebdw commented Jun 2, 2025

Copy link
Copy Markdown
Collaborator

Superseded by #275

@calebdw calebdw closed this Jun 2, 2025
@calebdw
calebdw deleted the reserved branch August 11, 2025 12:26
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