php parser安装,php-parser

Parse PHP code and returns its AST

php-parser

php-parser.svg?branch=2.2.0

badge.svg?branch=master&v=20170115

php-parser.svg

php-parser.svg?style=flat

GITTER-join%20chat-green.svg

git%2Bgithub.com%2Fglayzzle%2Fphp-parser.svg?type=shield

This javascript library parses PHP code and convert it to AST.

Installation

This library is distributed with npm :

npm install php-parser --save

Usage

// initialize the php parser factory class

var fs = require('fs');

var path = require('path');

var engine = require('php-parser');

// initialize a new parser instance

var parser = new engine({

// some options :

parser: {

extractDoc: true,

php7: true

},

ast: {

withPositions: true

}

});

// Retrieve the AST from the specified source

var eval = parser.parseEval('echo "Hello World";');

// Retrieve an array of tokens (same as php function token_get_all)

var tokens = parser.tokenGetAll('<?php echo "Hello World";');

// Load a static file (Note: this file should exist on your computer)

var phpFile = fs.readFileSync( './example.php' );

// Log out results

console.log( 'Eval parse:', eval );

console.log( 'Tokens parse:', tokens );

console.log( 'File parse:', parser.parseCode(phpFile) );

Sample AST output

{

'kind': 'program',

'children': [

{

'kind': 'echo',

'arguments': [

{

'kind': 'string',

'isDoubleQuote': true,

'value': 'Hello World'

}

]

}

]

}

API Overview

The main API exposes a class with the following methods :

parseEval(String|Buffer) : parse a PHP code in eval style mode (without php open tags)

parseCode(String|Buffer, String filename) : parse a PHP code by using php open tags.

tokenGetAll(String|Buffer) : retrieves a list of all tokens from the specified input.

You can also pass options that change the behavior of the parser/lexer.

Documentation

Related projects

prettier/plugin-php : Prettier PHP Plugin

babel-preset-php : Babel preset for converting PHP syntax to JavaScript. It can run subset of PHP in the browser or in Node.js

wp-pot : Generate pot file for WordPress plugins and themes

crane : PHP Intellisense/code-completion for VS Code

php-unparser : Produce code that uses the style format recommended by PSR-1 and PSR-2.

php-writer : Update PHP scripts from their AST

ts-php-inspections : Provide PHP code inspections written in typescript

php-reflection : Reflection API for PHP files

vscode-phpunit : vscode phpunit extension

lua2php : a Lua to PHP transpiler

You can add here your own project by opening an issue request.

License

This library is released under BSD-3 license clause.

git%2Bgithub.com%2Fglayzzle%2Fphp-parser.svg?type=large

HomePage

Repository

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个早期的 PHP 解析器,相当于实现了 PHPPHP 脚本的解析。示例代码:<?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";     } } 标签:Tolerant
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值