function hygienize(TokenStream $ts, string $scope) : TokenStream { $ts->reset(); traverse(either(chain(token(T_STRING, '·unsafe'), parentheses()), either(token(T_VARIABLE)->as('target'), chain(identifier()->as('target'), token(':')), chain(token(T_GOTO), identifier()->as('target')))->onCommit(function (Ast $result) use($scope) { (function () use($scope) { if ((string) $this !== '$this') { $this->value = (string) $this . '·' . $scope; } })->call($result->target); }), any()))->parse($ts); $ts->reset(); return $ts; }
function hygienize(TokenStream $ts, array $context) : TokenStream { $ts->reset(); $cg = (object) ['node' => null, 'context' => $context, 'ts' => $ts]; $saveNode = function (Parser $parser) use($cg) { return midrule(function ($ts) use($cg, $parser) { $cg->node = $ts->index(); return $parser->parse($ts); }); }; traverse(chain(token(T_STRING, '··unsafe'), either(parentheses(), braces())), either($saveNode(token(T_VARIABLE)), chain($saveNode(identifier()), token(':')), chain(token(T_GOTO), $saveNode(identifier())))->onCommit(function (Ast $result) use($cg) { if (($t = $cg->node->token) && ($value = (string) $t) !== '$this') { $cg->node->token = new Token($t->type(), "{$value}·{$cg->context['scope']}", $t->line()); } }))->parse($ts); $ts->reset(); return $ts; }