Class Kitab\Compiler\Parser

class Parser
{
    public function parse(Hoa\File\SplFileInfo $file): Kitab\Compiler\IntermediateRepresentation\File;
    protected function intoIntermediateRepresentation(array $statements, string $fileName): Kitab\Compiler\IntermediateRepresentation\File;
    public static function getPhpParser(): PhpParser\Parser\Multiple;
    protected static function getTraverser(): PhpParser\NodeTraverser;
    public static function getPhpPrettyPrinter(): PhpParser\PrettyPrinter\Standard;
    public static function extractFromComment($comment);
}

A parser producing an Intermediate Representation.

This parser takes one file, parses it, generates an Abstract Syntax Tree, and transforms it into an Intermediate Representation.

The PHP 7 form is prefered over PHP 5 and lower forms, it means it will try to parse with PHP 7 strategy first. They are small but subtle differences with previous PHP versions.

All the work done by the parser is delegated to PHP-Parser.

Attributes

protected static $_phpParser = null;

The PHP parser, aka PHP-Parser, that is used to parse PHP files.

The PHP parser is allocated once, hence the static declaration.

protected static $_phpTraverser = null;

A traverser, for PHP-Parser, is a set of visitors visiting the Abstract Syntax Tree produced by the parser. This traverser will be used to apply visitors on the AST. It always contain a name resolver visitor, to get fully qualified names everywhere in the code.

The traverser is allocated once, hence the static declaration.

protected static $_phpPrettyPrinter = null;

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

The pretty printer is allocated once, hence the static declaration.

Methods

public function parse(Hoa\File\SplFileInfo $file): Kitab\Compiler\IntermediateRepresentation\File

The parse methods parses a file aiming at containing PHP code, and produces the Intermediate Representation of it if valid. Get more information about the general workflow.

Examples

$file   = new Hoa\File\SplFileInfo('path/to/a/file.php');
$parser = new Kitab\Compiler\Parser();

$intermediateRepresentation = $parser->parse($file);

Exceptions

The Kitab\Exception\PhpParserError can be thrown if the given file does not contain valid PHP code. The exception contains the Error exception from PHP-Parser, which holds more information, as a previous exception.

protected function intoIntermediateRepresentation(array $statements, string $fileName): Kitab\Compiler\IntermediateRepresentation\File

Transform the Abstract Syntax Tree into its Intermediate Representation.

In PHP-Parser, the AST is a hashmap of n-dimensions of statements. The produced IR is a tree of structures. The file name of the original file is kept to provide more context.

The transformation is applied by a visitor. It is added on the traverser (see self::$_phpTraverser), run, and remove. The visitor contains the resulting IR.

public static function getPhpParser(): PhpParser\Parser\Multiple

Get the statically allocated PHP-Parser instance.

protected static function getTraverser(): PhpParser\NodeTraverser

Get the statically allocated traverser instance.

public static function getPhpPrettyPrinter(): PhpParser\PrettyPrinter\Standard

Get the statically allocated pretty printer

public static function extractFromComment($comment)

Extract content from a comment of kind block (/**).

This is a small utility used to extract documentation from the code.

Examples

$content = 'foobar';
$input   = '/**' . $content . '*' . '/';

assert(Kitab\Compiler\Parser::extractFromComment($input) === $content);