tolerant-php-parser, 面向IDE使用场景的早期PHP解析器设计

分享于 

4分钟阅读

GitHub

  繁體 雙語
An early-stage PHP parser designed for IDE usage scenarios.
  • 源代码名称:tolerant-php-parser
  • 源代码网址:http://www.github.com/Microsoft/tolerant-php-parser
  • tolerant-php-parser源代码文档
  • tolerant-php-parser源代码下载
  • Git URL:
    git://www.github.com/Microsoft/tolerant-php-parser.git
    Git Clone代码到本地:
    git clone http://www.github.com/Microsoft/tolerant-php-parser
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/Microsoft/tolerant-php-parser
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    兼容的PHP解析器

    Build Status

    这是一个早期的PHP解析器,从头开始,用于IDE使用场景( 有关更详细的信息,请参阅设计目标)。 还有很多工作要做,因这里这个 repo 主要用作一个实验和一个对话开始。

    image

    开始

    配置好后,可以使用解析器通过友好的API生成和使用抽象语法树。

    
    
    
    
    <?php
    
    
    // Autoload required classes
    
    
    require __DIR__ . "/vendor/autoload.php";
    
    
    
    
    
    use MicrosoftPhpParser{DiagnosticsProvider, Node, Parser, PositionUtilities};
    
    
    
    
    
    // Instantiate new parser instance
    
    
    $parser = new 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 = DiagnosticsProvider::getDiagnostics($astNode);
    
    
    var_dump($errors);
    
    
    
    
    
    // Traverse all Node descendants of $astNode
    
    
    foreach ($astNode->getDescendantNodes() as $descendant) {
    
    
     if ($descendant instanceof NodeStringLiteral) {
    
    
     // 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 NodeExpressionEchoExpression) {
    
    
     $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 = PositionUtilities::getLineCharacterPositionFromPosition(
    
    
     $echoKeywordStartPosition,
    
    
     $descendant->getFileContents()
    
    
     );
    
    
     echo "line: $lineCharacterPosition->line, character: $lineCharacterPosition->character";
    
    
     }
    
    
    }
    
    
    
    

    注意:API插件还没有完成,所以请文件问题告诉我们你希望公开什么功能,我们将看到我们可以执行的操作。 也请在解析树中存档具有意外行为的任何 Bug。 我们还在早期阶段,你所得到的任何反馈都非常感谢


    STA  PAR  PHP  DES  设计  parse  
    相关文章