JSPM

  • Created
  • Published
  • Downloads 225826
  • Score
    100M100P100Q164291F
  • License BSD-3-Clause

Parse PHP 5/7 code and returns its AST

Package Exports

  • php-parser

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (php-parser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

php-parser

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

npm version Build Status Coverage Status Gitter

Installation

This library is distributed with npm :

npm install php-parser --save

Usage

// initialize the php parser factory class
var engine = require('php-parser');
// initialize a new parser instance
var parser = new engine({
  // some options :
  parser: {
    extractDoc: true
  },
  ast: {
    withPositions: true
  }
});

// Retrieve the AST from the specified source
var AST = parser.parseEval('echo "Hello World";');
// AST.kind === 'program';
// AST.children[0].kind === 'echo';

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

Sample AST output

{
  'kind': 'program',
  'children': [
    {
      'kind': 'echo',
      'arguments': [
        {
          'kind': 'string',
          'isDoubleQuote': true,
          'value': 'Hello World'
        }
      ]
    }
  ]
}

Try it online (demo) : http://glayzzle.com/php-parser/#demo

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

  • 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
  • wp-pot : Generate pot file for WordPress plugins and themes
  • crane : PHP Intellisense/code-completion for VS Code

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

Misc

This library is released under BSD-3 license clause.

If you want to contribute please visit this repository https://github.com/glayzzle/php-parser-dev.