Skip to content

Commit

Permalink
Avoid cyclic reference in parser
Browse files Browse the repository at this point in the history
Pass $this as an explicit $self argument to the reduce callbacks,
so we can make them static. This avoids a circular reference in
the parser, so that it can be immediately destroyed when it goes
out of scope.

Fixes #980.
  • Loading branch information
nikic committed Feb 21, 2024
1 parent 2218c22 commit af14fdb
Show file tree
Hide file tree
Showing 5 changed files with 1,869 additions and 1,868 deletions.
6 changes: 3 additions & 3 deletions grammar/parser.template
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php declare(strict_types=1);
$meta #
#semval($) $this->semValue
#semval($,%t) $this->semValue
#semval($) $self->semValue
#semval($,%t) $self->semValue
#semval(%n) $stackPos-(%l-%n)
#semval(%n,%t) $stackPos-(%l-%n)

Expand Down Expand Up @@ -97,7 +97,7 @@ class #(-p) extends \PhpParser\ParserAbstract
protected function initReduceCallbacks(): void {
$this->reduceCallbacks = [
#reduce
%n => function ($stackPos) {
%n => static function ($self, $stackPos) {
%b
},
#noact
Expand Down
1 change: 1 addition & 0 deletions grammar/phpyLang.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function preprocessGrammar($code) {
$code = resolveNodes($code);
$code = resolveMacros($code);
$code = resolveStackAccess($code);
$code = str_replace('$this', '$self', $code);

return $code;
}
Expand Down
Loading

0 comments on commit af14fdb

Please sign in to comment.