Skip to content

A cross-platform, error-tolerant PHP parser that provides a complete representation of your source code.

License

Notifications You must be signed in to change notification settings

mattacosta/php-parser

Repository files navigation

php-parser

CI

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.

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).

Installation

Using a package manager:

npm install @mattacosta/php-parser

Building from source code:

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

Getting started

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.

Documentation

Contributing

If you find any bugs, typos, or something that could be improved, submit an issue after reading the contribution guide.

About

A cross-platform, error-tolerant PHP parser that provides a complete representation of your source code.

Topics

Resources

License

Stars

Watchers

Forks

Languages