Chris@13: type ? $this->p($node->type) . ' ' : '') Chris@0: . ($node->byRef ? '&' : '') Chris@0: . ($node->variadic ? '...' : '') Chris@13: . $this->p($node->var) Chris@0: . ($node->default ? ' = ' . $this->p($node->default) : ''); Chris@0: } Chris@0: Chris@0: protected function pArg(Node\Arg $node) { Chris@0: return ($node->byRef ? '&' : '') . ($node->unpack ? '...' : '') . $this->p($node->value); Chris@0: } Chris@0: Chris@0: protected function pConst(Node\Const_ $node) { Chris@0: return $node->name . ' = ' . $this->p($node->value); Chris@0: } Chris@0: Chris@0: protected function pNullableType(Node\NullableType $node) { Chris@13: return '?' . $this->p($node->type); Chris@13: } Chris@13: Chris@13: protected function pIdentifier(Node\Identifier $node) { Chris@13: return $node->name; Chris@13: } Chris@13: Chris@13: protected function pVarLikeIdentifier(Node\VarLikeIdentifier $node) { Chris@13: return '$' . $node->name; Chris@0: } Chris@0: Chris@0: // Names Chris@0: Chris@0: protected function pName(Name $node) { Chris@0: return implode('\\', $node->parts); Chris@0: } Chris@0: Chris@0: protected function pName_FullyQualified(Name\FullyQualified $node) { Chris@0: return '\\' . implode('\\', $node->parts); Chris@0: } Chris@0: Chris@0: protected function pName_Relative(Name\Relative $node) { Chris@0: return 'namespace\\' . implode('\\', $node->parts); Chris@0: } Chris@0: Chris@0: // Magic Constants Chris@0: Chris@0: protected function pScalar_MagicConst_Class(MagicConst\Class_ $node) { Chris@0: return '__CLASS__'; Chris@0: } Chris@0: Chris@0: protected function pScalar_MagicConst_Dir(MagicConst\Dir $node) { Chris@0: return '__DIR__'; Chris@0: } Chris@0: Chris@0: protected function pScalar_MagicConst_File(MagicConst\File $node) { Chris@0: return '__FILE__'; Chris@0: } Chris@0: Chris@0: protected function pScalar_MagicConst_Function(MagicConst\Function_ $node) { Chris@0: return '__FUNCTION__'; Chris@0: } Chris@0: Chris@0: protected function pScalar_MagicConst_Line(MagicConst\Line $node) { Chris@0: return '__LINE__'; Chris@0: } Chris@0: Chris@0: protected function pScalar_MagicConst_Method(MagicConst\Method $node) { Chris@0: return '__METHOD__'; Chris@0: } Chris@0: Chris@0: protected function pScalar_MagicConst_Namespace(MagicConst\Namespace_ $node) { Chris@0: return '__NAMESPACE__'; Chris@0: } Chris@0: Chris@0: protected function pScalar_MagicConst_Trait(MagicConst\Trait_ $node) { Chris@0: return '__TRAIT__'; Chris@0: } Chris@0: Chris@0: // Scalars Chris@0: Chris@0: protected function pScalar_String(Scalar\String_ $node) { Chris@0: $kind = $node->getAttribute('kind', Scalar\String_::KIND_SINGLE_QUOTED); Chris@0: switch ($kind) { Chris@0: case Scalar\String_::KIND_NOWDOC: Chris@0: $label = $node->getAttribute('docLabel'); Chris@0: if ($label && !$this->containsEndLabel($node->value, $label)) { Chris@0: if ($node->value === '') { Chris@13: return "<<<'$label'\n$label" . $this->docStringEndToken; Chris@0: } Chris@0: Chris@13: return "<<<'$label'\n$node->value\n$label" Chris@0: . $this->docStringEndToken; Chris@0: } Chris@0: /* break missing intentionally */ Chris@0: case Scalar\String_::KIND_SINGLE_QUOTED: Chris@13: return $this->pSingleQuotedString($node->value); Chris@0: case Scalar\String_::KIND_HEREDOC: Chris@0: $label = $node->getAttribute('docLabel'); Chris@0: if ($label && !$this->containsEndLabel($node->value, $label)) { Chris@0: if ($node->value === '') { Chris@13: return "<<<$label\n$label" . $this->docStringEndToken; Chris@0: } Chris@0: Chris@0: $escaped = $this->escapeString($node->value, null); Chris@13: return "<<<$label\n" . $escaped . "\n$label" Chris@0: . $this->docStringEndToken; Chris@0: } Chris@0: /* break missing intentionally */ Chris@0: case Scalar\String_::KIND_DOUBLE_QUOTED: Chris@0: return '"' . $this->escapeString($node->value, '"') . '"'; Chris@0: } Chris@0: throw new \Exception('Invalid string kind'); Chris@0: } Chris@0: Chris@0: protected function pScalar_Encapsed(Scalar\Encapsed $node) { Chris@0: if ($node->getAttribute('kind') === Scalar\String_::KIND_HEREDOC) { Chris@0: $label = $node->getAttribute('docLabel'); Chris@0: if ($label && !$this->encapsedContainsEndLabel($node->parts, $label)) { Chris@0: if (count($node->parts) === 1 Chris@0: && $node->parts[0] instanceof Scalar\EncapsedStringPart Chris@0: && $node->parts[0]->value === '' Chris@0: ) { Chris@13: return "<<<$label\n$label" . $this->docStringEndToken; Chris@0: } Chris@0: Chris@13: return "<<<$label\n" . $this->pEncapsList($node->parts, null) . "\n$label" Chris@13: . $this->docStringEndToken; Chris@0: } Chris@0: } Chris@0: return '"' . $this->pEncapsList($node->parts, '"') . '"'; Chris@0: } Chris@0: Chris@0: protected function pScalar_LNumber(Scalar\LNumber $node) { Chris@0: if ($node->value === -\PHP_INT_MAX-1) { Chris@0: // PHP_INT_MIN cannot be represented as a literal, Chris@0: // because the sign is not part of the literal Chris@0: return '(-' . \PHP_INT_MAX . '-1)'; Chris@0: } Chris@0: Chris@0: $kind = $node->getAttribute('kind', Scalar\LNumber::KIND_DEC); Chris@0: if (Scalar\LNumber::KIND_DEC === $kind) { Chris@0: return (string) $node->value; Chris@0: } Chris@0: Chris@0: $sign = $node->value < 0 ? '-' : ''; Chris@0: $str = (string) $node->value; Chris@0: switch ($kind) { Chris@0: case Scalar\LNumber::KIND_BIN: Chris@0: return $sign . '0b' . base_convert($str, 10, 2); Chris@0: case Scalar\LNumber::KIND_OCT: Chris@0: return $sign . '0' . base_convert($str, 10, 8); Chris@0: case Scalar\LNumber::KIND_HEX: Chris@0: return $sign . '0x' . base_convert($str, 10, 16); Chris@0: } Chris@0: throw new \Exception('Invalid number kind'); Chris@0: } Chris@0: Chris@0: protected function pScalar_DNumber(Scalar\DNumber $node) { Chris@0: if (!is_finite($node->value)) { Chris@0: if ($node->value === \INF) { Chris@0: return '\INF'; Chris@0: } elseif ($node->value === -\INF) { Chris@0: return '-\INF'; Chris@0: } else { Chris@0: return '\NAN'; Chris@0: } Chris@0: } Chris@0: Chris@0: // Try to find a short full-precision representation Chris@0: $stringValue = sprintf('%.16G', $node->value); Chris@0: if ($node->value !== (double) $stringValue) { Chris@0: $stringValue = sprintf('%.17G', $node->value); Chris@0: } Chris@0: Chris@0: // %G is locale dependent and there exists no locale-independent alternative. We don't want Chris@0: // mess with switching locales here, so let's assume that a comma is the only non-standard Chris@0: // decimal separator we may encounter... Chris@0: $stringValue = str_replace(',', '.', $stringValue); Chris@0: Chris@0: // ensure that number is really printed as float Chris@0: return preg_match('/^-?[0-9]+$/', $stringValue) ? $stringValue . '.0' : $stringValue; Chris@0: } Chris@0: Chris@16: protected function pScalar_EncapsedStringPart(Scalar\EncapsedStringPart $node) { Chris@16: throw new \LogicException('Cannot directly print EncapsedStringPart'); Chris@16: } Chris@16: Chris@0: // Assignments Chris@0: Chris@0: protected function pExpr_Assign(Expr\Assign $node) { Chris@13: return $this->pInfixOp(Expr\Assign::class, $node->var, ' = ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignRef(Expr\AssignRef $node) { Chris@13: return $this->pInfixOp(Expr\AssignRef::class, $node->var, ' =& ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_Plus(AssignOp\Plus $node) { Chris@13: return $this->pInfixOp(AssignOp\Plus::class, $node->var, ' += ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_Minus(AssignOp\Minus $node) { Chris@13: return $this->pInfixOp(AssignOp\Minus::class, $node->var, ' -= ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_Mul(AssignOp\Mul $node) { Chris@13: return $this->pInfixOp(AssignOp\Mul::class, $node->var, ' *= ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_Div(AssignOp\Div $node) { Chris@13: return $this->pInfixOp(AssignOp\Div::class, $node->var, ' /= ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_Concat(AssignOp\Concat $node) { Chris@13: return $this->pInfixOp(AssignOp\Concat::class, $node->var, ' .= ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_Mod(AssignOp\Mod $node) { Chris@13: return $this->pInfixOp(AssignOp\Mod::class, $node->var, ' %= ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_BitwiseAnd(AssignOp\BitwiseAnd $node) { Chris@13: return $this->pInfixOp(AssignOp\BitwiseAnd::class, $node->var, ' &= ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_BitwiseOr(AssignOp\BitwiseOr $node) { Chris@13: return $this->pInfixOp(AssignOp\BitwiseOr::class, $node->var, ' |= ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_BitwiseXor(AssignOp\BitwiseXor $node) { Chris@13: return $this->pInfixOp(AssignOp\BitwiseXor::class, $node->var, ' ^= ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_ShiftLeft(AssignOp\ShiftLeft $node) { Chris@13: return $this->pInfixOp(AssignOp\ShiftLeft::class, $node->var, ' <<= ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_ShiftRight(AssignOp\ShiftRight $node) { Chris@13: return $this->pInfixOp(AssignOp\ShiftRight::class, $node->var, ' >>= ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_AssignOp_Pow(AssignOp\Pow $node) { Chris@13: return $this->pInfixOp(AssignOp\Pow::class, $node->var, ' **= ', $node->expr); Chris@0: } Chris@0: Chris@17: protected function pExpr_AssignOp_Coalesce(AssignOp\Coalesce $node) { Chris@17: return $this->pInfixOp(AssignOp\Coalesce::class, $node->var, ' ??= ', $node->expr); Chris@17: } Chris@17: Chris@0: // Binary expressions Chris@0: Chris@0: protected function pExpr_BinaryOp_Plus(BinaryOp\Plus $node) { Chris@13: return $this->pInfixOp(BinaryOp\Plus::class, $node->left, ' + ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Minus(BinaryOp\Minus $node) { Chris@13: return $this->pInfixOp(BinaryOp\Minus::class, $node->left, ' - ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Mul(BinaryOp\Mul $node) { Chris@13: return $this->pInfixOp(BinaryOp\Mul::class, $node->left, ' * ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Div(BinaryOp\Div $node) { Chris@13: return $this->pInfixOp(BinaryOp\Div::class, $node->left, ' / ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Concat(BinaryOp\Concat $node) { Chris@13: return $this->pInfixOp(BinaryOp\Concat::class, $node->left, ' . ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Mod(BinaryOp\Mod $node) { Chris@13: return $this->pInfixOp(BinaryOp\Mod::class, $node->left, ' % ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_BooleanAnd(BinaryOp\BooleanAnd $node) { Chris@13: return $this->pInfixOp(BinaryOp\BooleanAnd::class, $node->left, ' && ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_BooleanOr(BinaryOp\BooleanOr $node) { Chris@13: return $this->pInfixOp(BinaryOp\BooleanOr::class, $node->left, ' || ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_BitwiseAnd(BinaryOp\BitwiseAnd $node) { Chris@13: return $this->pInfixOp(BinaryOp\BitwiseAnd::class, $node->left, ' & ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_BitwiseOr(BinaryOp\BitwiseOr $node) { Chris@13: return $this->pInfixOp(BinaryOp\BitwiseOr::class, $node->left, ' | ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_BitwiseXor(BinaryOp\BitwiseXor $node) { Chris@13: return $this->pInfixOp(BinaryOp\BitwiseXor::class, $node->left, ' ^ ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_ShiftLeft(BinaryOp\ShiftLeft $node) { Chris@13: return $this->pInfixOp(BinaryOp\ShiftLeft::class, $node->left, ' << ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_ShiftRight(BinaryOp\ShiftRight $node) { Chris@13: return $this->pInfixOp(BinaryOp\ShiftRight::class, $node->left, ' >> ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Pow(BinaryOp\Pow $node) { Chris@13: return $this->pInfixOp(BinaryOp\Pow::class, $node->left, ' ** ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_LogicalAnd(BinaryOp\LogicalAnd $node) { Chris@13: return $this->pInfixOp(BinaryOp\LogicalAnd::class, $node->left, ' and ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_LogicalOr(BinaryOp\LogicalOr $node) { Chris@13: return $this->pInfixOp(BinaryOp\LogicalOr::class, $node->left, ' or ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_LogicalXor(BinaryOp\LogicalXor $node) { Chris@13: return $this->pInfixOp(BinaryOp\LogicalXor::class, $node->left, ' xor ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Equal(BinaryOp\Equal $node) { Chris@13: return $this->pInfixOp(BinaryOp\Equal::class, $node->left, ' == ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_NotEqual(BinaryOp\NotEqual $node) { Chris@13: return $this->pInfixOp(BinaryOp\NotEqual::class, $node->left, ' != ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Identical(BinaryOp\Identical $node) { Chris@13: return $this->pInfixOp(BinaryOp\Identical::class, $node->left, ' === ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_NotIdentical(BinaryOp\NotIdentical $node) { Chris@13: return $this->pInfixOp(BinaryOp\NotIdentical::class, $node->left, ' !== ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Spaceship(BinaryOp\Spaceship $node) { Chris@13: return $this->pInfixOp(BinaryOp\Spaceship::class, $node->left, ' <=> ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Greater(BinaryOp\Greater $node) { Chris@13: return $this->pInfixOp(BinaryOp\Greater::class, $node->left, ' > ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_GreaterOrEqual(BinaryOp\GreaterOrEqual $node) { Chris@13: return $this->pInfixOp(BinaryOp\GreaterOrEqual::class, $node->left, ' >= ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Smaller(BinaryOp\Smaller $node) { Chris@13: return $this->pInfixOp(BinaryOp\Smaller::class, $node->left, ' < ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_SmallerOrEqual(BinaryOp\SmallerOrEqual $node) { Chris@13: return $this->pInfixOp(BinaryOp\SmallerOrEqual::class, $node->left, ' <= ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_BinaryOp_Coalesce(BinaryOp\Coalesce $node) { Chris@13: return $this->pInfixOp(BinaryOp\Coalesce::class, $node->left, ' ?? ', $node->right); Chris@0: } Chris@0: Chris@0: protected function pExpr_Instanceof(Expr\Instanceof_ $node) { Chris@13: return $this->pInfixOp(Expr\Instanceof_::class, $node->expr, ' instanceof ', $node->class); Chris@0: } Chris@0: Chris@0: // Unary expressions Chris@0: Chris@0: protected function pExpr_BooleanNot(Expr\BooleanNot $node) { Chris@13: return $this->pPrefixOp(Expr\BooleanNot::class, '!', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_BitwiseNot(Expr\BitwiseNot $node) { Chris@13: return $this->pPrefixOp(Expr\BitwiseNot::class, '~', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_UnaryMinus(Expr\UnaryMinus $node) { Chris@12: if ($node->expr instanceof Expr\UnaryMinus || $node->expr instanceof Expr\PreDec) { Chris@12: // Enforce -(-$expr) instead of --$expr Chris@12: return '-(' . $this->p($node->expr) . ')'; Chris@12: } Chris@13: return $this->pPrefixOp(Expr\UnaryMinus::class, '-', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_UnaryPlus(Expr\UnaryPlus $node) { Chris@12: if ($node->expr instanceof Expr\UnaryPlus || $node->expr instanceof Expr\PreInc) { Chris@12: // Enforce +(+$expr) instead of ++$expr Chris@12: return '+(' . $this->p($node->expr) . ')'; Chris@12: } Chris@13: return $this->pPrefixOp(Expr\UnaryPlus::class, '+', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_PreInc(Expr\PreInc $node) { Chris@13: return $this->pPrefixOp(Expr\PreInc::class, '++', $node->var); Chris@0: } Chris@0: Chris@0: protected function pExpr_PreDec(Expr\PreDec $node) { Chris@13: return $this->pPrefixOp(Expr\PreDec::class, '--', $node->var); Chris@0: } Chris@0: Chris@0: protected function pExpr_PostInc(Expr\PostInc $node) { Chris@13: return $this->pPostfixOp(Expr\PostInc::class, $node->var, '++'); Chris@0: } Chris@0: Chris@0: protected function pExpr_PostDec(Expr\PostDec $node) { Chris@13: return $this->pPostfixOp(Expr\PostDec::class, $node->var, '--'); Chris@0: } Chris@0: Chris@0: protected function pExpr_ErrorSuppress(Expr\ErrorSuppress $node) { Chris@13: return $this->pPrefixOp(Expr\ErrorSuppress::class, '@', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_YieldFrom(Expr\YieldFrom $node) { Chris@13: return $this->pPrefixOp(Expr\YieldFrom::class, 'yield from ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_Print(Expr\Print_ $node) { Chris@13: return $this->pPrefixOp(Expr\Print_::class, 'print ', $node->expr); Chris@0: } Chris@0: Chris@0: // Casts Chris@0: Chris@0: protected function pExpr_Cast_Int(Cast\Int_ $node) { Chris@13: return $this->pPrefixOp(Cast\Int_::class, '(int) ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_Cast_Double(Cast\Double $node) { Chris@17: $kind = $node->getAttribute('kind', Cast\Double::KIND_DOUBLE); Chris@17: if ($kind === Cast\Double::KIND_DOUBLE) { Chris@17: $cast = '(double)'; Chris@17: } elseif ($kind === Cast\Double::KIND_FLOAT) { Chris@17: $cast = '(float)'; Chris@17: } elseif ($kind === Cast\Double::KIND_REAL) { Chris@17: $cast = '(real)'; Chris@17: } Chris@17: return $this->pPrefixOp(Cast\Double::class, $cast . ' ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_Cast_String(Cast\String_ $node) { Chris@13: return $this->pPrefixOp(Cast\String_::class, '(string) ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_Cast_Array(Cast\Array_ $node) { Chris@13: return $this->pPrefixOp(Cast\Array_::class, '(array) ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_Cast_Object(Cast\Object_ $node) { Chris@13: return $this->pPrefixOp(Cast\Object_::class, '(object) ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_Cast_Bool(Cast\Bool_ $node) { Chris@13: return $this->pPrefixOp(Cast\Bool_::class, '(bool) ', $node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_Cast_Unset(Cast\Unset_ $node) { Chris@13: return $this->pPrefixOp(Cast\Unset_::class, '(unset) ', $node->expr); Chris@0: } Chris@0: Chris@0: // Function calls and similar constructs Chris@0: Chris@0: protected function pExpr_FuncCall(Expr\FuncCall $node) { Chris@0: return $this->pCallLhs($node->name) Chris@0: . '(' . $this->pMaybeMultiline($node->args) . ')'; Chris@0: } Chris@0: Chris@0: protected function pExpr_MethodCall(Expr\MethodCall $node) { Chris@0: return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name) Chris@0: . '(' . $this->pMaybeMultiline($node->args) . ')'; Chris@0: } Chris@0: Chris@0: protected function pExpr_StaticCall(Expr\StaticCall $node) { Chris@0: return $this->pDereferenceLhs($node->class) . '::' Chris@0: . ($node->name instanceof Expr Chris@0: ? ($node->name instanceof Expr\Variable Chris@0: ? $this->p($node->name) Chris@0: : '{' . $this->p($node->name) . '}') Chris@0: : $node->name) Chris@0: . '(' . $this->pMaybeMultiline($node->args) . ')'; Chris@0: } Chris@0: Chris@0: protected function pExpr_Empty(Expr\Empty_ $node) { Chris@0: return 'empty(' . $this->p($node->expr) . ')'; Chris@0: } Chris@0: Chris@0: protected function pExpr_Isset(Expr\Isset_ $node) { Chris@0: return 'isset(' . $this->pCommaSeparated($node->vars) . ')'; Chris@0: } Chris@0: Chris@0: protected function pExpr_Eval(Expr\Eval_ $node) { Chris@0: return 'eval(' . $this->p($node->expr) . ')'; Chris@0: } Chris@0: Chris@0: protected function pExpr_Include(Expr\Include_ $node) { Chris@13: static $map = [ Chris@0: Expr\Include_::TYPE_INCLUDE => 'include', Chris@0: Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once', Chris@0: Expr\Include_::TYPE_REQUIRE => 'require', Chris@0: Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once', Chris@13: ]; Chris@0: Chris@0: return $map[$node->type] . ' ' . $this->p($node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_List(Expr\List_ $node) { Chris@0: return 'list(' . $this->pCommaSeparated($node->items) . ')'; Chris@0: } Chris@0: Chris@0: // Other Chris@0: Chris@0: protected function pExpr_Error(Expr\Error $node) { Chris@0: throw new \LogicException('Cannot pretty-print AST with Error nodes'); Chris@0: } Chris@0: Chris@0: protected function pExpr_Variable(Expr\Variable $node) { Chris@0: if ($node->name instanceof Expr) { Chris@0: return '${' . $this->p($node->name) . '}'; Chris@0: } else { Chris@0: return '$' . $node->name; Chris@0: } Chris@0: } Chris@0: Chris@0: protected function pExpr_Array(Expr\Array_ $node) { Chris@0: $syntax = $node->getAttribute('kind', Chris@0: $this->options['shortArraySyntax'] ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG); Chris@0: if ($syntax === Expr\Array_::KIND_SHORT) { Chris@0: return '[' . $this->pMaybeMultiline($node->items, true) . ']'; Chris@0: } else { Chris@0: return 'array(' . $this->pMaybeMultiline($node->items, true) . ')'; Chris@0: } Chris@0: } Chris@0: Chris@0: protected function pExpr_ArrayItem(Expr\ArrayItem $node) { Chris@0: return (null !== $node->key ? $this->p($node->key) . ' => ' : '') Chris@0: . ($node->byRef ? '&' : '') . $this->p($node->value); Chris@0: } Chris@0: Chris@0: protected function pExpr_ArrayDimFetch(Expr\ArrayDimFetch $node) { Chris@0: return $this->pDereferenceLhs($node->var) Chris@0: . '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']'; Chris@0: } Chris@0: Chris@0: protected function pExpr_ConstFetch(Expr\ConstFetch $node) { Chris@0: return $this->p($node->name); Chris@0: } Chris@0: Chris@0: protected function pExpr_ClassConstFetch(Expr\ClassConstFetch $node) { Chris@13: return $this->p($node->class) . '::' . $this->p($node->name); Chris@0: } Chris@0: Chris@0: protected function pExpr_PropertyFetch(Expr\PropertyFetch $node) { Chris@0: return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name); Chris@0: } Chris@0: Chris@0: protected function pExpr_StaticPropertyFetch(Expr\StaticPropertyFetch $node) { Chris@0: return $this->pDereferenceLhs($node->class) . '::$' . $this->pObjectProperty($node->name); Chris@0: } Chris@0: Chris@0: protected function pExpr_ShellExec(Expr\ShellExec $node) { Chris@0: return '`' . $this->pEncapsList($node->parts, '`') . '`'; Chris@0: } Chris@0: Chris@0: protected function pExpr_Closure(Expr\Closure $node) { Chris@0: return ($node->static ? 'static ' : '') Chris@0: . 'function ' . ($node->byRef ? '&' : '') Chris@0: . '(' . $this->pCommaSeparated($node->params) . ')' Chris@13: . (!empty($node->uses) ? ' use(' . $this->pCommaSeparated($node->uses) . ')' : '') Chris@13: . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') Chris@13: . ' {' . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pExpr_ClosureUse(Expr\ClosureUse $node) { Chris@13: return ($node->byRef ? '&' : '') . $this->p($node->var); Chris@0: } Chris@0: Chris@0: protected function pExpr_New(Expr\New_ $node) { Chris@0: if ($node->class instanceof Stmt\Class_) { Chris@0: $args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : ''; Chris@0: return 'new ' . $this->pClassCommon($node->class, $args); Chris@0: } Chris@0: return 'new ' . $this->p($node->class) . '(' . $this->pMaybeMultiline($node->args) . ')'; Chris@0: } Chris@0: Chris@0: protected function pExpr_Clone(Expr\Clone_ $node) { Chris@0: return 'clone ' . $this->p($node->expr); Chris@0: } Chris@0: Chris@0: protected function pExpr_Ternary(Expr\Ternary $node) { Chris@0: // a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator. Chris@0: // this is okay because the part between ? and : never needs parentheses. Chris@13: return $this->pInfixOp(Expr\Ternary::class, Chris@0: $node->cond, ' ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else Chris@0: ); Chris@0: } Chris@0: Chris@0: protected function pExpr_Exit(Expr\Exit_ $node) { Chris@0: $kind = $node->getAttribute('kind', Expr\Exit_::KIND_DIE); Chris@0: return ($kind === Expr\Exit_::KIND_EXIT ? 'exit' : 'die') Chris@0: . (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : ''); Chris@0: } Chris@0: Chris@0: protected function pExpr_Yield(Expr\Yield_ $node) { Chris@0: if ($node->value === null) { Chris@0: return 'yield'; Chris@0: } else { Chris@0: // this is a bit ugly, but currently there is no way to detect whether the parentheses are necessary Chris@0: return '(yield ' Chris@0: . ($node->key !== null ? $this->p($node->key) . ' => ' : '') Chris@0: . $this->p($node->value) Chris@0: . ')'; Chris@0: } Chris@0: } Chris@0: Chris@0: // Declarations Chris@0: Chris@0: protected function pStmt_Namespace(Stmt\Namespace_ $node) { Chris@0: if ($this->canUseSemicolonNamespaces) { Chris@13: return 'namespace ' . $this->p($node->name) . ';' Chris@13: . $this->nl . $this->pStmts($node->stmts, false); Chris@0: } else { Chris@0: return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '') Chris@13: . ' {' . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: } Chris@0: Chris@0: protected function pStmt_Use(Stmt\Use_ $node) { Chris@0: return 'use ' . $this->pUseType($node->type) Chris@0: . $this->pCommaSeparated($node->uses) . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_GroupUse(Stmt\GroupUse $node) { Chris@0: return 'use ' . $this->pUseType($node->type) . $this->pName($node->prefix) Chris@0: . '\{' . $this->pCommaSeparated($node->uses) . '};'; Chris@0: } Chris@0: Chris@0: protected function pStmt_UseUse(Stmt\UseUse $node) { Chris@0: return $this->pUseType($node->type) . $this->p($node->name) Chris@13: . (null !== $node->alias ? ' as ' . $node->alias : ''); Chris@0: } Chris@0: Chris@0: protected function pUseType($type) { Chris@0: return $type === Stmt\Use_::TYPE_FUNCTION ? 'function ' Chris@0: : ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : ''); Chris@0: } Chris@0: Chris@0: protected function pStmt_Interface(Stmt\Interface_ $node) { Chris@0: return 'interface ' . $node->name Chris@0: . (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '') Chris@13: . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Class(Stmt\Class_ $node) { Chris@0: return $this->pClassCommon($node, ' ' . $node->name); Chris@0: } Chris@0: Chris@0: protected function pStmt_Trait(Stmt\Trait_ $node) { Chris@0: return 'trait ' . $node->name Chris@13: . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pStmt_TraitUse(Stmt\TraitUse $node) { Chris@0: return 'use ' . $this->pCommaSeparated($node->traits) Chris@0: . (empty($node->adaptations) Chris@0: ? ';' Chris@13: : ' {' . $this->pStmts($node->adaptations) . $this->nl . '}'); Chris@0: } Chris@0: Chris@0: protected function pStmt_TraitUseAdaptation_Precedence(Stmt\TraitUseAdaptation\Precedence $node) { Chris@0: return $this->p($node->trait) . '::' . $node->method Chris@0: . ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_TraitUseAdaptation_Alias(Stmt\TraitUseAdaptation\Alias $node) { Chris@0: return (null !== $node->trait ? $this->p($node->trait) . '::' : '') Chris@0: . $node->method . ' as' Chris@0: . (null !== $node->newModifier ? ' ' . rtrim($this->pModifiers($node->newModifier), ' ') : '') Chris@0: . (null !== $node->newName ? ' ' . $node->newName : '') Chris@0: . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Property(Stmt\Property $node) { Chris@17: return (0 === $node->flags ? 'var ' : $this->pModifiers($node->flags)) Chris@17: . ($node->type ? $this->p($node->type) . ' ' : '') Chris@17: . $this->pCommaSeparated($node->props) . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_PropertyProperty(Stmt\PropertyProperty $node) { Chris@0: return '$' . $node->name Chris@0: . (null !== $node->default ? ' = ' . $this->p($node->default) : ''); Chris@0: } Chris@0: Chris@0: protected function pStmt_ClassMethod(Stmt\ClassMethod $node) { Chris@0: return $this->pModifiers($node->flags) Chris@0: . 'function ' . ($node->byRef ? '&' : '') . $node->name Chris@0: . '(' . $this->pCommaSeparated($node->params) . ')' Chris@13: . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') Chris@0: . (null !== $node->stmts Chris@13: ? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}' Chris@0: : ';'); Chris@0: } Chris@0: Chris@0: protected function pStmt_ClassConst(Stmt\ClassConst $node) { Chris@0: return $this->pModifiers($node->flags) Chris@0: . 'const ' . $this->pCommaSeparated($node->consts) . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Function(Stmt\Function_ $node) { Chris@0: return 'function ' . ($node->byRef ? '&' : '') . $node->name Chris@0: . '(' . $this->pCommaSeparated($node->params) . ')' Chris@13: . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') Chris@13: . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Const(Stmt\Const_ $node) { Chris@0: return 'const ' . $this->pCommaSeparated($node->consts) . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Declare(Stmt\Declare_ $node) { Chris@0: return 'declare (' . $this->pCommaSeparated($node->declares) . ')' Chris@13: . (null !== $node->stmts ? ' {' . $this->pStmts($node->stmts) . $this->nl . '}' : ';'); Chris@0: } Chris@0: Chris@0: protected function pStmt_DeclareDeclare(Stmt\DeclareDeclare $node) { Chris@0: return $node->key . '=' . $this->p($node->value); Chris@0: } Chris@0: Chris@0: // Control flow Chris@0: Chris@0: protected function pStmt_If(Stmt\If_ $node) { Chris@0: return 'if (' . $this->p($node->cond) . ') {' Chris@13: . $this->pStmts($node->stmts) . $this->nl . '}' Chris@13: . ($node->elseifs ? ' ' . $this->pImplode($node->elseifs, ' ') : '') Chris@13: . (null !== $node->else ? ' ' . $this->p($node->else) : ''); Chris@0: } Chris@0: Chris@0: protected function pStmt_ElseIf(Stmt\ElseIf_ $node) { Chris@13: return 'elseif (' . $this->p($node->cond) . ') {' Chris@13: . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Else(Stmt\Else_ $node) { Chris@13: return 'else {' . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pStmt_For(Stmt\For_ $node) { Chris@0: return 'for (' Chris@0: . $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '') Chris@0: . $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '') Chris@0: . $this->pCommaSeparated($node->loop) Chris@13: . ') {' . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Foreach(Stmt\Foreach_ $node) { Chris@0: return 'foreach (' . $this->p($node->expr) . ' as ' Chris@0: . (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '') Chris@0: . ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {' Chris@13: . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pStmt_While(Stmt\While_ $node) { Chris@0: return 'while (' . $this->p($node->cond) . ') {' Chris@13: . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Do(Stmt\Do_ $node) { Chris@13: return 'do {' . $this->pStmts($node->stmts) . $this->nl Chris@0: . '} while (' . $this->p($node->cond) . ');'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Switch(Stmt\Switch_ $node) { Chris@0: return 'switch (' . $this->p($node->cond) . ') {' Chris@13: . $this->pStmts($node->cases) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Case(Stmt\Case_ $node) { Chris@0: return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':' Chris@0: . $this->pStmts($node->stmts); Chris@0: } Chris@0: Chris@0: protected function pStmt_TryCatch(Stmt\TryCatch $node) { Chris@13: return 'try {' . $this->pStmts($node->stmts) . $this->nl . '}' Chris@13: . ($node->catches ? ' ' . $this->pImplode($node->catches, ' ') : '') Chris@13: . ($node->finally !== null ? ' ' . $this->p($node->finally) : ''); Chris@0: } Chris@0: Chris@0: protected function pStmt_Catch(Stmt\Catch_ $node) { Chris@13: return 'catch (' . $this->pImplode($node->types, '|') . ' ' Chris@13: . $this->p($node->var) Chris@13: . ') {' . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Finally(Stmt\Finally_ $node) { Chris@13: return 'finally {' . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Break(Stmt\Break_ $node) { Chris@0: return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Continue(Stmt\Continue_ $node) { Chris@0: return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Return(Stmt\Return_ $node) { Chris@0: return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Throw(Stmt\Throw_ $node) { Chris@0: return 'throw ' . $this->p($node->expr) . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Label(Stmt\Label $node) { Chris@0: return $node->name . ':'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Goto(Stmt\Goto_ $node) { Chris@0: return 'goto ' . $node->name . ';'; Chris@0: } Chris@0: Chris@0: // Other Chris@0: Chris@13: protected function pStmt_Expression(Stmt\Expression $node) { Chris@13: return $this->p($node->expr) . ';'; Chris@13: } Chris@13: Chris@0: protected function pStmt_Echo(Stmt\Echo_ $node) { Chris@0: return 'echo ' . $this->pCommaSeparated($node->exprs) . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Static(Stmt\Static_ $node) { Chris@0: return 'static ' . $this->pCommaSeparated($node->vars) . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_Global(Stmt\Global_ $node) { Chris@0: return 'global ' . $this->pCommaSeparated($node->vars) . ';'; Chris@0: } Chris@0: Chris@0: protected function pStmt_StaticVar(Stmt\StaticVar $node) { Chris@13: return $this->p($node->var) Chris@0: . (null !== $node->default ? ' = ' . $this->p($node->default) : ''); Chris@0: } Chris@0: Chris@0: protected function pStmt_Unset(Stmt\Unset_ $node) { Chris@0: return 'unset(' . $this->pCommaSeparated($node->vars) . ');'; Chris@0: } Chris@0: Chris@0: protected function pStmt_InlineHTML(Stmt\InlineHTML $node) { Chris@0: $newline = $node->getAttribute('hasLeadingNewline', true) ? "\n" : ''; Chris@13: return '?>' . $newline . $node->value . 'remaining; Chris@0: } Chris@0: Chris@0: protected function pStmt_Nop(Stmt\Nop $node) { Chris@0: return ''; Chris@0: } Chris@0: Chris@0: // Helpers Chris@0: Chris@0: protected function pClassCommon(Stmt\Class_ $node, $afterClassToken) { Chris@0: return $this->pModifiers($node->flags) Chris@0: . 'class' . $afterClassToken Chris@0: . (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '') Chris@0: . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '') Chris@13: . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; Chris@0: } Chris@0: Chris@0: protected function pObjectProperty($node) { Chris@0: if ($node instanceof Expr) { Chris@0: return '{' . $this->p($node) . '}'; Chris@0: } else { Chris@0: return $node; Chris@0: } Chris@0: } Chris@0: Chris@0: protected function pEncapsList(array $encapsList, $quote) { Chris@0: $return = ''; Chris@0: foreach ($encapsList as $element) { Chris@0: if ($element instanceof Scalar\EncapsedStringPart) { Chris@0: $return .= $this->escapeString($element->value, $quote); Chris@0: } else { Chris@0: $return .= '{' . $this->p($element) . '}'; Chris@0: } Chris@0: } Chris@0: Chris@0: return $return; Chris@0: } Chris@0: Chris@13: protected function pSingleQuotedString(string $string) { Chris@13: return '\'' . addcslashes($string, '\'\\') . '\''; Chris@13: } Chris@13: Chris@0: protected function escapeString($string, $quote) { Chris@0: if (null === $quote) { Chris@0: // For doc strings, don't escape newlines Chris@0: $escaped = addcslashes($string, "\t\f\v$\\"); Chris@0: } else { Chris@0: $escaped = addcslashes($string, "\n\r\t\f\v$" . $quote . "\\"); Chris@0: } Chris@0: Chris@0: // Escape other control characters Chris@0: return preg_replace_callback('/([\0-\10\16-\37])(?=([0-7]?))/', function ($matches) { Chris@0: $oct = decoct(ord($matches[1])); Chris@0: if ($matches[2] !== '') { Chris@0: // If there is a trailing digit, use the full three character form Chris@13: return '\\' . str_pad($oct, 3, '0', \STR_PAD_LEFT); Chris@0: } Chris@0: return '\\' . $oct; Chris@0: }, $escaped); Chris@0: } Chris@0: Chris@0: protected function containsEndLabel($string, $label, $atStart = true, $atEnd = true) { Chris@0: $start = $atStart ? '(?:^|[\r\n])' : '[\r\n]'; Chris@0: $end = $atEnd ? '(?:$|[;\r\n])' : '[;\r\n]'; Chris@0: return false !== strpos($string, $label) Chris@0: && preg_match('/' . $start . $label . $end . '/', $string); Chris@0: } Chris@0: Chris@0: protected function encapsedContainsEndLabel(array $parts, $label) { Chris@0: foreach ($parts as $i => $part) { Chris@0: $atStart = $i === 0; Chris@0: $atEnd = $i === count($parts) - 1; Chris@0: if ($part instanceof Scalar\EncapsedStringPart Chris@0: && $this->containsEndLabel($part->value, $label, $atStart, $atEnd) Chris@0: ) { Chris@0: return true; Chris@0: } Chris@0: } Chris@0: return false; Chris@0: } Chris@0: Chris@0: protected function pDereferenceLhs(Node $node) { Chris@13: if (!$this->dereferenceLhsRequiresParens($node)) { Chris@0: return $this->p($node); Chris@0: } else { Chris@0: return '(' . $this->p($node) . ')'; Chris@0: } Chris@0: } Chris@0: Chris@0: protected function pCallLhs(Node $node) { Chris@13: if (!$this->callLhsRequiresParens($node)) { Chris@0: return $this->p($node); Chris@0: } else { Chris@0: return '(' . $this->p($node) . ')'; Chris@0: } Chris@0: } Chris@0: Chris@13: /** Chris@13: * @param Node[] $nodes Chris@13: * @return bool Chris@13: */ Chris@0: private function hasNodeWithComments(array $nodes) { Chris@0: foreach ($nodes as $node) { Chris@13: if ($node && $node->getComments()) { Chris@0: return true; Chris@0: } Chris@0: } Chris@0: return false; Chris@0: } Chris@0: Chris@0: private function pMaybeMultiline(array $nodes, $trailingComma = false) { Chris@0: if (!$this->hasNodeWithComments($nodes)) { Chris@0: return $this->pCommaSeparated($nodes); Chris@0: } else { Chris@13: return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl; Chris@0: } Chris@0: } Chris@0: }