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

Implement property hooks syntax #45

Merged
merged 20 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/php/lang/ast/nodes/Property.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class Property extends Annotated implements Member {
public $kind= 'property';
public $name, $modifiers, $expression, $type, $holder;
public $hooks= null;
Copy link
Member Author

Choose a reason for hiding this comment

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

We are not adding this as constructor parameter partly in order to prevent a BC break, partly because of how parsing works.


public function __construct($modifiers, $name, $type, $expression= null, $annotations= null, $comment= null, $line= -1, $holder= null) {
$this->modifiers= $modifiers;
Expand Down
35 changes: 31 additions & 4 deletions src/main/php/lang/ast/syntax/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ private function properties($parse, &$body, $meta, $modifiers, $type, $holder) {
$parse->comment= null;
$annotations= $meta[DETAIL_ANNOTATIONS] ?? null;

while (';' !== $parse->token->value) {
do {
$line= $parse->token->line;

// Untyped `$a` vs. typed `int $a`
Expand All @@ -1304,13 +1304,40 @@ private function properties($parse, &$body, $meta, $modifiers, $type, $holder) {
} else {
$expr= null;
}

$body[$lookup]= new Property($modifiers, $name, $type, $expr, $annotations, $comment, $line, $holder);

if (',' === $parse->token->value) {
// Check for property hooks
if (';' === $parse->token->value) {
$parse->forward();
return;
} else if (',' === $parse->token->value) {
$parse->forward();
continue;
} else if ('{' === $parse->token->value) {
$parse->forward();
do {
$hook= $parse->token->value;
$parse->forward();

if ('=>' === $parse->token->value) {
$parse->forward();
$body[$lookup]->hooks[$hook]= $this->expression($parse, 0);
thekid marked this conversation as resolved.
Show resolved Hide resolved
} else if ('{' === $parse->token->value) {
$line= $parse->token->line;
$parse->forward();
$body[$lookup]->hooks[$hook]= new Block($this->statements($parse), $line);
} else if ('}' === $parse->token->value) {
break;
}
} while (null !== $parse->token->value);

$parse->forward();
return;
} else {
$parse->expecting(';, , or {', 'field');
}
}
$parse->expecting(';', 'field declaration');
} while (null !== $parse->token->value);
}

/** Parses PHP 8 attributes (#[Test]) */
Expand Down
24 changes: 24 additions & 0 deletions src/test/php/lang/ast/unittest/parse/MembersTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use lang\ast\Type;
use lang\ast\nodes\{
Annotations,
Block,
ClassDeclaration,
Constant,
Expression,
Expand All @@ -11,6 +12,7 @@
Literal,
Method,
Property,
ReturnStatement,
ScopeExpression,
Signature,
Variable,
Expand Down Expand Up @@ -128,6 +130,28 @@ public function method_with_annotations() {
$this->assertParsed([$class], 'class A { #[Test, Ignore("Not implemented")] public function a() { } }');
}

#[Test]
public function property_with_get_and_set_hooks() {
$class= new ClassDeclaration([], new IsValue('\\A'), null, [], [], null, null, self::LINE);
$prop= new Property(['public'], 'a', null, null, null, null, self::LINE);
$return= new ReturnStatement(new Literal('"Hello"', self::LINE), self::LINE);
$prop->hooks['get']= new Block([$return], self::LINE);
$prop->hooks['set']= new Block([], self::LINE);
$class->declare($prop);

$this->assertParsed([$class], 'class A { public $a { get { return "Hello"; } set { } } }');
}

#[Test]
public function property_with_short_get_hook() {
$class= new ClassDeclaration([], new IsValue('\\A'), null, [], [], null, null, self::LINE);
$prop= new Property(['public'], 'a', null, null, null, null, self::LINE);
$prop->hooks['get']= new Literal('"Hello"', self::LINE);
$class->declare($prop);

$this->assertParsed([$class], 'class A { public $a { get => "Hello"; } }');
}

#[Test]
public function instance_property_access() {
$this->assertParsed(
Expand Down