A cross-platform, error-tolerant PHP parser that provides a complete representation of your source code.
This parser enables the integration of PHP language support in IDEs and other custom environments, which allows developers to focus on creating additional code analysis and transformation features.
- Supports the latest PHP 7 features.
- Works anywhere a JavaScript environment is available.
- Optimized for V8 to be faster and use less memory than other parsers (see benchmarks).
npm install @mattacosta/php-parser
Run the following commands to get the latest files, install any dependencies, and build the project.
git clone https://github.com/mattacosta/php-parser.git
npm install
npx gulp compile
Parsing a file and getting diagnostics:
let tree = PhpSyntaxTree.fromText('<?php var_dump($a);');
for (let diagnostic of tree.getDiagnostics()) {
let message = ErrorCodeInfo.formatMessage(diagnostic.code, diagnostic.messageArgs);
console.log(message);
}
Syntax nodes also contain additional properties and methods to traverse the
syntax tree. If visiting multiple nodes in a tree, consider extending the
SyntaxVisitor
or SyntaxTransform
classes.
If you find any bugs, typos, or something that could be improved, submit an issue after reading the contribution guide.