GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - eou/php-parser: :herb: NodeJS PHP Parser. Plugin: Unparser with code format

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

1,168 Commits
1,168 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
dist
dist
 
 
docs
docs
 
 
plugin
plugin
 
 
src
src
 
 
test
test
 
 
tutorials
tutorials
 
 
.codeclimate.yml
.codeclimate.yml
 
 
.coveralls.yml
.coveralls.yml
 
 
.eslintignore
.eslintignore
 
 
.eslintrc.js
.eslintrc.js
 
 
.gitignore
.gitignore
 
 
.gitmodules
.gitmodules
 
 
.istanbul.yml
.istanbul.yml
 
 
.jsdoc.json
.jsdoc.json
 
 
.npmignore
.npmignore
 
 
.travis.yml
.travis.yml
 
 
CODE_OF_CONDUCT.md
CODE_OF_CONDUCT.md
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
RELEASE.md
RELEASE.md
 
 
babel.config.js
babel.config.js
 
 
image.svg
image.svg
 
 
index.d.ts
index.d.ts
 
 
madge.js
madge.js
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 
webpack.config.js
webpack.config.js
 
 

Repository files navigation

php-parser

Coverage Status

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

Installation

This library is distributed with npm :

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'
        }
      ]
    }
  ]
}
  • Try it online (demo) : http://glayzzle.com/php-parser/
  • Or from AST Explorer : https://astexplorer.net/

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

  • AST nodes definition
  • Sandbox
  • List of options

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.

FOSSA Status

About

🌿 NodeJS PHP Parser. Plugin: Unparser with code format

Topics

astjavascriptparserphpunparser

Resources

Readme
BSD-3-Clause license

Code of conduct

Code of conduct

Contributing

Contributing
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.