php-parser
TypeScript icon, indicating that this package has built-in type declarations

3.2.5 â€ĸ Public â€ĸ Published

php-parser

This JavaScript library parses PHP code and converts it to an AST.

Installation

This library is distributed with npm :

npm install php-parser --save

Usage

// initialize the php parser factory class
const fs = require("fs");
const path = require("path");
const engine = require("php-parser");

// initialize a new parser instance
const parser = new engine({
  // some options :
  parser: {
    extractDoc: true,
    php7: true,
  },
  ast: {
    withPositions: true,
  },
});

// Retrieve the AST from the specified source
const eval = parser.parseEval('echo "Hello World";');

// Retrieve an array of tokens (same as php function token_get_all)
const tokens = parser.tokenGetAll('<?php echo "Hello World";');

// Load a static file (Note: this file should exist on your computer)
const 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.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.2.569,775latest
3.1.0-beta.112,660beta

Version History

VersionDownloads (Last 7 Days)Published
3.2.569,775
3.2.41,805
3.2.313,938
3.2.226,520
3.2.12,835
3.2.061
3.1.569,561
3.1.46,573
3.1.314,044
3.1.21,884
3.1.1747
3.1.0232
3.1.0-beta.112,660
3.1.0-beta.1013
3.1.0-beta.963
3.1.0-beta.86
3.1.0-beta.715
3.1.0-beta.60
3.1.0-beta.5526
3.1.0-beta.4373
3.1.0-beta.30
3.1.0-beta.20
3.1.0-beta.114
3.1.0-beta.026
3.0.31,831
3.0.21,213
3.0.13,166
3.0.0115
3.0.0-prerelease.998
3.0.0-prerelease.872
3.0.0-prerelease.7183
3.0.0-prerelease.63
3.0.0-prerelease.50
3.0.0-prerelease.40
3.0.0-prerelease.31
3.0.0-prerelease.20
3.0.0-prerelease.10
3.0.0-alpha.30
3.0.0-alpha27
3.0.0-alpha10
2.2.05,602
2.1.033
2.0.740
2.0.695
2.0.50
2.0.46
2.0.373
2.0.238
2.0.10
2.0.00
2.0.0-pre81
2.0.0-pre70
2.0.0-pre60
2.0.0-pre50
2.0.0-pre42
2.0.0-pre20
2.0.0-alpha.20
2.0.0-alpha.10
2.0.0-pre10
1.0.411
1.0.30
1.0.245
1.0.172
1.0.00
0.1.525
0.1.40
0.1.30
0.1.20
0.1.10
0.1.020
0.0.104
0.0.90
0.0.810
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.11

Package Sidebar

Install

npm i php-parser

Weekly Downloads

224,438

Version

3.2.5

License

BSD-3-Clause

Unpacked Size

807 kB

Total Files

152

Last publish

Collaborators

  • ichiriac
  • czosel