Class Kitab\Compiler\IntermediateRepresentation\Into

class Into extends PhpParser\NodeVisitorAbstract
{
    pub fn __construct(string $filename);
    pub fn leaveNode(Node $node);
    pro fn intoConstants(ClassLike $node): array;
    pro fn intoAttributes(ClassLike $node): array;
    pro fn intoMethods(ClassLike $node): array;
    pro fn intoInputs($node): array;
    pro fn intoOutput($node): Type;
    pro fn intoType($node): Type;
    pub fn collect(): 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

pro $_file = null;

Name of the file where the AST comes from.

pri $_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

pub fn __construct(string $filename)

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

pub fn leaveNode(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.

pro fn intoConstants(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.

pro fn intoAttributes(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.

pro fn intoMethods(ClassLike $node): array

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

pro fn intoInputs($node): array

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

pro fn intoOutput($node): Type

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

pro fn intoType($node): Type

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

pub fn collect(): 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.