JSPM

  • Created
  • Published
  • Downloads 225189
  • Score
    100M100P100Q161606F
  • License BSD-3-Clause

Parse PHP code with NodeJS and convert it to 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

Parse PHP code from NodeJS and convert it to AST. This library is a standalone module of a larger project named Glayzzle.

npm version Build Status Coverage Status Gitter

Install it

$ npm install php-parser --save

Try it

$ cd bin
$ node test.js -e "echo 'Hello World';"

Will output :

*** START TESTING ***

-- TOKENS :
T_ECHO T_CONSTANT_ENCAPSED_STRING ;

-- AST :

[
  'program',  <-- program node
  [
    [ 'sys',  <-- first child, typed system call
      'echo', <-- operation echo
      [
        [ 'string', '"Hello World"' ]  <-- first argument
      ]
    ]
  ]
]

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

Use it

// initialize a new parser instance
var parser = require('php-parser').create();

// how to retrieve the AST
var AST = parser.parseEval('echo "Hello World";');

// how to list tokens
var tokens = parser.tokenGetAll('<?php echo "Hello World";');

For more details please visit he wiki.

Join the dev

If you want to change/fix the lexer you will find code to ./src/lexer/. You can also implement the parser, the code is into ./src/parser/. To check your changes add tests into ./test/parser/, and run npm run test. Try to keep or improve the coverage levels.

The command line options :

Usage: test [options] [-f] <file>

  -f <file>                      Parse and test the specified file
  -d <path>                      Parse each file in the specified path
  -r                             Use recursivity with the specified path
  -e                             Eval the specified input and shows AST
  -v                             Enable verbose mode and show debug
  -h, --help                     Print help and exit

If you run into problems with a test, run it with the cli and add the --debug flag.

Misc

This library is released under BSD-3 license clause.