Class Kitab\Compiler\IntermediateRepresentation\Into

class Into extends PhpParser\NodeVisitorAbstract
{
    public function __construct(string $filename);
    public function leaveNode(PhpParser\Node $node);
    protected function intoConstants(PhpParser\Node\Stmt\ClassLike $node): array;
    protected function intoAttributes(PhpParser\Node\Stmt\ClassLike $node): array;
    protected function intoMethods(PhpParser\Node\Stmt\ClassLike $node): array;
    protected function intoInputs($node): array;
    protected function intoOutput($node): Kitab\Compiler\IntermediateRepresentation\Type;
    protected function intoType($node): Kitab\Compiler\IntermediateRepresentation\Type;
    public function collect(): Kitab\Compiler\IntermediateRepresentation\File;
}

A visitor to transform an Abstract Syntax Tree (AST) into an Intermediate Representation (IR).

This visitor implements the API from PhpParser\NodeVisitorAbstract. It is applied when leaving a node of the AST to allow other visitors to transform the currently visited node and its children. For instance, the name resolver visitor modifies the AST when entering a node. This visitor needs the information from the name resolver visitor, so it must executes after. The best way to get all these information for a node and its children is to run each node when the visitor leaves a node.

Examples

See the examples of the current namespace.

Attributes

protected $_file = null;

Name of the file where the AST comes from.

private $_prettyPrinter = null;

Pretty print visitor to transform the AST into its PHP representation.

It is used for example to get the PHP representation of a default value for an attribute.

This is a reference to the PHP pretty printer allocated in the Kitab\Compiler\Parser class.

Methods

public function __construct(string $filename)

Allocate a new visitor. The only mandatory information is the name of the file where the AST comes from.

public function leaveNode(PhpParser\Node $node)

Transform a node of the AST into an IR.

This method returns nothing because it is called several times by the traverser API. To get the final result, see the collect method.

protected function intoConstants(PhpParser\Node\Stmt\ClassLike $node): array

Extract constant nodes and transform them into a collection of Kitab\Compiler\IntermediateRepresentation\Constant objects.

It supports both declaration forms:

  • public const FOO = 42; public const BAR = 153;, and
  • public const FOO = 42, BAR = 153;.

The resulting IR will be the same and will correspond to the former form.

protected function intoAttributes(PhpParser\Node\Stmt\ClassLike $node): array

Extract attribute nodes and transform them into a collection of Kitab\Compiler\IntermediateRepresentation\Attribute objects.

It supports both declaration forms:

  • public $foo = 42; public $bar = 153;, and
  • public $foo = 42, $bar = 153;.

The resulting IR will be the same and will correspond to the former form.

protected function intoMethods(PhpParser\Node\Stmt\ClassLike $node): array

Extract method nodes and transform them into a collection of Kitab\Compiler\IntermediateRepresentation\Method objects.

protected function intoInputs($node): array

Extract nodes representing parameters of a function and transform them into a collection of Kitab\Compiler\IntermediateRepresentation\Parameter objects.

protected function intoOutput($node): Kitab\Compiler\IntermediateRepresentation\Type

Extract node representing the output of a function and transform it into a Kitab\Compiler\IntermediateRepresentation\Type object.

protected function intoType($node): Kitab\Compiler\IntermediateRepresentation\Type

Extract node representing a type and transform it into a Kitab\Compiler\IntermediateRepresentation\Type object.

public function collect(): Kitab\Compiler\IntermediateRepresentation\File

Because the visitor runs for every node in the AST, the only way to collect the resulting IR is to call this method.

This method can be called at any time but it is best to call it when the traverser returns. It means the transformation will be complete.