PHP 解析器 Tolerant PHP Parser

码农软件 · 软件分类 · 语法解析工具 · 2019-12-12 10:59:15

软件介绍

这是一个早期的 PHP 解析器,相当于实现了 PHP 对 PHP 脚本的解析。

示例代码:

<?php
// Autoload required classes
require "vendor/autoload.php";

// Instantiate new parser instance
$parser = new PhpParser\Parser();

// Return and print an AST from string contents
$astNode = $parser->parseSourceFile('<?php /* comment */ echo "hi!"');
var_dump($astNode);

// Gets and prints errors from AST Node. The parser handles errors gracefully,
// so it can be used in IDE usage scenarios (where code is often incomplete).
$errors = PhpParser\Utilities::getDiagnostics($astNode);
var_dump(iterator_to_array($errors));

// Traverse all Node descendants of $astNode
foreach ($astNode->getDescendantNodes() as $descendant) {
    if ($descendant instanceof \PhpParser\Node\StringLiteral) {
        // Print the Node text (without whitespace or comments)
        var_dump($descendant->getText());

        // All Nodes link back to their parents, so it's easy to navigate the tree.
        $grandParent = $descendant->getParent()->getParent();
        var_dump($grandParent->getNodeKindName());

        // The AST is fully-representative, and round-trippable to the original source.
        // This enables consumers to build reliable formatting and refactoring tools.
        var_dump($grandParent->getLeadingCommentAndWhitespaceText());
    }

    // In addition to retrieving all children or descendants of a Node,
    // Nodes expose properties specific to the Node type.
    if ($descendant instanceof \PhpParser\Node\Expression\EchoExpression) {
        $echoKeywordStartPosition = $descendant->echoKeyword->getStartPosition();
        // To cut down on memory consumption, positions are represented as a single integer 
        // index into the document, but their line and character positions are easily retrieved.
        $lineCharacterPosition = \PhpParser\Utilities::getLineCharacterPositionFromPosition(
            $echoKeywordStartPosition
        );
        echo "line: $lineCharacterPosition->line, character: $lineCharacterPosition->character";
    }
}

本文地址:https://codercto.com/soft/d/20976.html

DOOM启世录

DOOM启世录

[美] 大卫·卡什诺 / 孙振南 / 电子工业出版社 / 2004-4 / 29.00元

由David Kushner 撰写之著作 《Master of DOOM》在 Amazon 和 eBook上的销售喜人。本书的中文版权由我公司拿到,将在2004年4月出版。本书忠实详尽地讲述了两个玩家是如何走上游戏之路,如何制作出迄今为止影响力最大的游戏作品--DOOM和Quake,以及他们为何在最辉煌的时候分道扬镳。本书是国内第一部游戏领域的传记。与所有传记一样,不同的读者能从中得到不同的体验:......一起来看看 《DOOM启世录》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具