What is php-parser?
The php-parser npm package is a JavaScript library that allows you to parse PHP code into an Abstract Syntax Tree (AST). This can be useful for various tasks such as static analysis, code transformation, and code generation.
What are php-parser's main functionalities?
Parsing PHP Code
This feature allows you to parse PHP code into an Abstract Syntax Tree (AST). The code sample demonstrates how to parse a simple PHP script and output the resulting AST.
const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
const ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');
console.log(JSON.stringify(ast, null, 2));
Traversing the AST
This feature allows you to traverse the AST to find specific nodes. The code sample demonstrates how to traverse the AST to find and log echo statements.
const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
const ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');
function traverse(node) {
if (node.kind === 'echo') {
console.log('Found an echo statement');
}
for (let key in node) {
if (node[key] && typeof node[key] === 'object') {
traverse(node[key]);
}
}
}
traverse(ast);
Modifying the AST
This feature allows you to modify the AST. The code sample demonstrates how to change the output of an echo statement from 'Hello, World!' to 'Hello, Universe!'.
const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
let ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');
function modifyEcho(node) {
if (node.kind === 'echo') {
node.arguments[0].value = 'Hello, Universe!';
}
for (let key in node) {
if (node[key] && typeof node[key] === 'object') {
modifyEcho(node[key]);
}
}
}
modifyEcho(ast);
console.log(JSON.stringify(ast, null, 2));
Other packages similar to php-parser
esprima
Esprima is a high-performance, standard-compliant ECMAScript parser written in JavaScript. It is used for parsing JavaScript code into an AST. While php-parser is used for PHP, Esprima serves a similar purpose for JavaScript.
acorn
Acorn is a small, fast, JavaScript-based JavaScript parser. It generates an AST from JavaScript code and is known for its performance and modularity. Like php-parser, it is used for parsing code into an AST, but it is specific to JavaScript.
php-parser
This javascript library parses PHP code and convert it to AST.
Installation
This library is distributed with npm :
npm install php-parser --save
Usage
var fs = require('fs');
var path = require('path');
var engine = require('php-parser');
var parser = new engine({
parser: {
extractDoc: true
},
ast: {
withPositions: true
}
});
var eval = parser.parseEval('echo "Hello World";');
var tokens = parser.tokenGetAll('<?php echo "Hello World";');
var phpFile = fs.readFileSync( './example.php' );
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'
}
]
}
]
}
Try it online (demo) :
http://glayzzle.com/php-parser/#demo
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
- babel-preset-php : Babel preset for converting PHP syntax to JavaScript. It can run subset of PHP in the browser or in Node.js. .
- 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
- wp-pot : Generate pot file for WordPress plugins and themes
- crane : PHP Intellisense/code-completion for VS Code
You can add here your own project by opening an issue request.
Misc
This library is released under BSD-3 license clause.
If you want to contribute please visit this repository https://github.com/glayzzle/php-parser-dev.