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
Parse PHP code from NodeJS and convert it to AST. This library is a standalone module of a larger project named Glayzzle.
Install it
$ npm install php-parser --save
Try it
$ cd bin
$ node test.js -e "echo 'Hello World';"
Use it
var parser = require('php-parser');
var AST = parser.parse('echo "Hello World";');
console.log(AST);
Join the dev
If you want to change/fix the lexer you will find code to ./src/lexer/
. Do not change dirrectly ./src/lexer.js
or ./src/tokens.js
, they are generated with the command npm run build
You can also implement the parser, the code is into ./src/parser/
. To check your changes add tests into ./test/parser/
, and run npm run test
. Try to keep or improve the coverage levels.
The command line options :
Usage: test [options] [-f] <file>
-f <file> Parse and test the specified file
-d <path> Parse each file in the specified path
-r Use recursivity with the specified path
-e Eval the specified input and shows AST
-v Enable verbose mode and show debug
-h, --help Print help and exit
If you run into problems with a test, run it with the cli and add the --debug
flag.
#Misc
This library is released under BSD-3 license clause.