Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion src/main/php/lang/ast/nodes/Block.class.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php namespace lang\ast\nodes;

use Traversable, IteratorAggregate;
use lang\ast\Node;

class Block extends Node {
class Block extends Node implements IteratorAggregate {
public $kind= 'block';
public $statements;

Expand All @@ -13,4 +14,7 @@ public function __construct($statements, $line= -1) {

/** @return iterable */
public function children() { return $this->statements; }

/** Iteration support */
public function getIterator(): Traversable { yield from $this->statements; }
Copy link
Copy Markdown
Member Author

@thekid thekid May 23, 2026

Choose a reason for hiding this comment

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

With this the library is backwards-compatible for all instances using foreach / emitAll() on functions', closures' and methods' body member.

}
4 changes: 2 additions & 2 deletions src/main/php/lang/ast/nodes/ClosureExpression.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class ClosureExpression extends Annotated {
public function __construct($signature, $use, $body, $static= false, $line= -1) {
$this->signature= $signature;
$this->use= $use;
$this->body= $body;
$this->body= is_array($body) ? new Block($body, $line) : $body;
$this->static= $static;
$this->line= $line;
}

/** @return iterable */
public function children() { return $this->body; }
public function children() { return [&$this->body]; }
}
4 changes: 2 additions & 2 deletions src/main/php/lang/ast/nodes/FunctionDeclaration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class FunctionDeclaration extends Annotated {
public function __construct($name, $signature, $body, $line= -1) {
$this->name= $name;
$this->signature= $signature;
$this->body= $body;
$this->body= is_array($body) ? new Block($body, $line) : $body;
$this->line= $line;
}

/** @return iterable */
public function children() { return $this->body; }
public function children() { return [&$this->body]; }
}
6 changes: 2 additions & 4 deletions src/main/php/lang/ast/nodes/LambdaExpression.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ class LambdaExpression extends Annotated {

public function __construct($signature, $body, $static= false, $line= -1) {
$this->signature= $signature;
$this->body= $body;
$this->body= is_array($body) ? new Block($body, $line) : $body;
$this->static= $static;
$this->line= $line;
}

/** @return iterable */
public function children() {
return is_array($this->body) ? $this->body : [&$this->body];
}
public function children() { return [&$this->body]; }
}
4 changes: 2 additions & 2 deletions src/main/php/lang/ast/nodes/Method.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function __construct($modifiers, $name, $signature, $body= null, $annotat
$this->name= $name;
$this->modifiers= $modifiers;
$this->signature= $signature;
$this->body= $body;
$this->body= is_array($body) ? new Block($body, $line) : $body;
parent::__construct($annotations, $comment, $line);
}

Expand Down Expand Up @@ -46,5 +46,5 @@ public function append($node) {
}

/** @return iterable */
public function children() { return (array)$this->body; }
public function children() { return null === $this->body ? [] : [&$this->body]; }
}
89 changes: 53 additions & 36 deletions src/main/php/lang/ast/syntax/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,6 @@ public function __construct() {
return new CloneExpression($arguments, $token->line);
});

$this->prefix('{', 0, function($parse, $token) {
$statements= $this->statements($parse);
$parse->expecting('}', 'block');
return new Block($statements, $token->line);
});

$this->prefix('[', 0, function($parse, $token) {
return new ArrayLiteral($this->list($parse, ']', 'array literal'), $token->line);
});
Expand Down Expand Up @@ -400,9 +394,8 @@ public function __construct() {

$this->prefix('fn', 0, function($parse, $token) {
$signature= $this->signature($parse);
$parse->expecting('=>', 'fn');

return new LambdaExpression($signature, $this->expression($parse, 0), false, $token->line);
$scope= $this->scope($parse, 'fn');
return new LambdaExpression($signature, $scope, false, $token->line);
});

$this->prefix('function', 0, function($parse, $token) {
Expand Down Expand Up @@ -435,8 +428,8 @@ public function __construct() {
} else if ('fn' === $parse->token->value) {
$parse->forward();
$signature= $this->signature($parse);
$parse->expecting('=>', 'fn');
return new LambdaExpression($signature, $this->expression($parse, 0), true, $token->line);
$scope= $this->scope($parse, 'fn');
return new LambdaExpression($signature, $scope, true, $token->line);
} else {
return new Literal($token->value, $token->line);
}
Expand All @@ -463,15 +456,14 @@ public function __construct() {
while ('}' !== $parse->token->value) {
if ('default' === $parse->token->value) {
$parse->forward();
$parse->expecting('=>', 'match');
$default= $this->expression($parse, 0);
$default= $this->scope($parse, 'match');
} else {
$match= [];
do {
$match[]= $this->expression($parse, 0);
} while (',' === $parse->token->value && $parse->forward() | true);
$parse->expecting('=>', 'match');
$cases[]= new MatchCondition($match, $this->expression($parse, 0), $parse->token->line);

$cases[]= new MatchCondition($match, $this->scope($parse, 'match'), $parse->token->line);
}

if (',' === $parse->token->value) {
Expand Down Expand Up @@ -533,6 +525,12 @@ public function __construct() {
return new Literal($token->value, $token->line);
});

// Blocks and standalone semicolons
$this->stmt('{', function($parse, $token) {
$statements= $this->statements($parse);
$parse->expecting('}', 'block');
return new Block($statements, $token->line);
});
$this->stmt(';', function($parse, $token) { return null; });

// Unexpected standalone symbols, warn but continue
Expand Down Expand Up @@ -886,14 +884,10 @@ public function __construct() {

// Function expression used as statement (e.g. for pure side-effects!)
if ('(' === $parse->token->value) {
$parse->queue= [$parse->token];
array_unshift($parse->queue, $parse->token);
$parse->token= new Token($this->symbol('function'));
$parse->token->line= $token->line;
$expr= $this->expression($parse, 0);

$parse->queue= [$parse->token];
$parse->token= new Token($this->symbol(';'));
return $expr;
return $this->expression($parse, 0);
}

if ('&' === $parse->token->value) {
Expand All @@ -907,11 +901,9 @@ public function __construct() {
$parse->forward();

$signature= $this->signature($parse, $byref);
$parse->expecting('{', 'function');
$statements= $this->statements($parse);
$parse->expecting('}', 'function');
$scope= $this->scope($parse, 'function');

return new FunctionDeclaration($name, $signature, $statements, $token->line);
return new FunctionDeclaration($name, $signature, $scope, $token->line);
});

$this->stmt('class', function($parse, $token) {
Expand Down Expand Up @@ -1138,23 +1130,28 @@ public function __construct() {
$parse->forward();
$signature= $this->signature($parse, $byref);

if ('{' === $parse->token->value) { // Regular body
// Cannot use scope() here as we require a semicolon after single-expressions
if ('{' === $parse->token->value) {
$parse->forward();
$statements= $this->statements($parse);
$scope= new Block($this->statements($parse), $parse->token->line);
$parse->expecting('}', 'method declaration');
} else if (';' === $parse->token->value) { // Abstract or interface method
$statements= null;
} else if ('=>' === $parse->token->value) {
$parse->forward();
$scope= $this->expression($parse, 0);
$parse->expecting(';', 'method declaration');
} else if (';' === $parse->token->value) {
$scope= null;
$parse->expecting(';', 'method declaration');
} else {
$parse->expecting('{ or ;', 'method declaration');
$parse->expecting('{, => or ;', 'method declaration');
return;
}

$body[$lookup]= new Method(
$modifiers,
$name,
$signature,
$statements,
$scope,
$meta[DETAIL_ANNOTATIONS] ?? null,
$comment,
$line
Expand Down Expand Up @@ -1591,6 +1588,29 @@ private function parameters($parse) {
return $parameters;
}

public function scope($parse, $context) {
if ('=>' === $parse->token->value) {
$parse->forward();

// BC: Support deprecated arrow block syntax
if ('{' === $parse->token->value) {
trigger_error('Arrow block syntax `=> { ... }`', E_USER_DEPRECATED);
goto block;
}
Comment on lines +1595 to +1599
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.

This can be removed in the next major release


return $this->expression($parse, 0);
} else if ('{' === $parse->token->value) {
block: $line= $parse->token->line;
$parse->forward();
$statements= $this->statements($parse);
$parse->expecting('}', $context);
return new Block($statements, $line);
} else {
$parse->expecting('=> or { ... }', $context);
return null;
}
}

public function body($id, $func) {
$this->body[$id]= $func->bindTo($this, static::class);
}
Expand Down Expand Up @@ -1689,11 +1709,8 @@ public function closure($parse, $static) {
$return= null;
}

$parse->expecting('{', 'function');
$statements= $this->statements($parse);
$parse->expecting('}', 'function');

return new ClosureExpression(new Signature($parameters, $return, false, $line), $use, $statements, $static, $line);
$scope= $this->scope($parse, 'function');
return new ClosureExpression(new Signature($parameters, $return, false, $line), $use, $scope, $static, $line);
}

public function block($parse) {
Expand Down
3 changes: 2 additions & 1 deletion src/test/php/lang/ast/unittest/parse/BracedTest.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace lang\ast\unittest\parse;

use lang\ast\nodes\{
Block,
Braced,
ClosureExpression,
InvokeExpression,
Expand Down Expand Up @@ -70,7 +71,7 @@ public function function_with_typed_reference_arg() {
self::LINE
);
$this->assertParsed(
[new Braced(new ClosureExpression($signature, null, [], false, self::LINE), self::LINE)],
[new Braced(new ClosureExpression($signature, null, new Block([], self::LINE), false, self::LINE), self::LINE)],
'(function(T &$arg) { });'
);
}
Expand Down
Loading
Loading