php-parser
  • Introduction
  • Overview
  • Demo
  • Changelog
  • Getting Started
    • Todo
  • Guides
    • Options
  • API
    • Parser
    • AST
    • Lexer
Powered by GitBook
On this page
  • Installation
  • Usage
  • Sample AST output
  • API Overview
  • Documentation
  • Related projects
  • License

Was this helpful?

Overview

PreviousIntroductionNextDemo

Last updated 6 years ago

Was this helpful?

php-parser

This javascript library parses PHP code and convert it to AST.

Installation

This library is distributed with :

npm install php-parser --save

Usage

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

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

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

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

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

Documentation

Related projects

You can add here your own project by opening an issue request.

License

This library is released under BSD-3 license clause.

Try it online (demo) :

Or from AST Explorer :

You can also that change the behavior of the parser/lexer.

: Prettier PHP Plugin

: Babel preset for converting PHP syntax to JavaScript. It can run subset of PHP in the browser or in Node.js

: Generate pot file for WordPress plugins and themes

: PHP Intellisense/code-completion for VS Code

: Produce code that uses the style format recommended by PSR-1 and PSR-2.

: Update PHP scripts from their AST

: Provide PHP code inspections written in typescript

: Reflection API for PHP files

: vscode phpunit extension

http://glayzzle.com/php-parser/
https://astexplorer.net/
pass options
AST nodes definition
List of options
Main API
Lexer API
Parser API
prettier/plugin-php
babel-preset-php
wp-pot
crane
php-unparser
php-writer
ts-php-inspections
php-reflection
vscode-phpunit
npm
Coverage Status
FOSSA Status