-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from glayzzle/1.0.0
release 1.0.0 on master
- Loading branch information
Showing
179 changed files
with
9,667 additions
and
8,479 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/bin/ | ||
/test/ | ||
/docs/ | ||
/test/ | ||
/gruntfile.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,107 @@ | ||
php-parser | ||
========== | ||
|
||
Parse PHP code from NodeJS and convert it to AST. This library is a standalone module of a larger project named [Glayzzle](http://glayzzle.com). | ||
This javascript library parses PHP code and convert it to AST. | ||
|
||
[](https://www.npmjs.com/package/php-parser) | ||
[](https://travis-ci.org/glayzzle/php-parser) | ||
[](https://coveralls.io/r/glayzzle/php-parser) | ||
[](https://gitter.im/glayzzle/Lobby) | ||
|
||
|
||
# Install it | ||
Installation | ||
------------ | ||
|
||
```sh | ||
$ npm install php-parser --save | ||
``` | ||
|
||
# Try it | ||
This library is distributed with [npm](https://www.npmjs.com/package/php-parser) : | ||
|
||
```sh | ||
$ cd bin | ||
$ node test.js -e "echo 'Hello World';" | ||
npm install php-parser --save | ||
``` | ||
|
||
Will output : | ||
Usage | ||
----- | ||
|
||
```js | ||
*** START TESTING *** | ||
// 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'; | ||
|
||
-- TOKENS : | ||
T_ECHO T_CONSTANT_ENCAPSED_STRING ; | ||
// Retrieve an array of tokens (same as php function token_get_all) | ||
var tokens = parser.tokenGetAll('<?php echo "Hello World";'); | ||
``` | ||
|
||
-- AST : | ||
Sample AST output | ||
----------------- | ||
|
||
[ | ||
'program', <-- program node | ||
[ | ||
[ 'sys', <-- first child, typed system call | ||
'echo', <-- operation echo | ||
[ | ||
[ 'string', '"Hello World"' ] <-- first argument | ||
```js | ||
{ | ||
'kind': 'program', | ||
'children': [ | ||
{ | ||
'kind': 'echo', | ||
'arguments': [ | ||
{ | ||
'kind': 'string', | ||
'isDoubleQuote': true, | ||
'value': 'Hello World' | ||
} | ||
] | ||
] | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
|
||
Try it online (demo) : | ||
http://glayzzle.com/php-parser/#demo | ||
|
||
# Use it | ||
API Overview | ||
------------ | ||
|
||
```js | ||
// initialize a new parser instance | ||
var parser = require('php-parser').create(); | ||
The main API exposes a class with the following methods : | ||
|
||
// how to retrieve the AST | ||
var AST = parser.parseEval('echo "Hello World";'); | ||
- **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. | ||
|
||
// how to list tokens | ||
var tokens = parser.tokenGetAll('<?php echo "Hello World";'); | ||
``` | ||
You can also [pass options](https://github.com/glayzzle/php-parser/wiki/Options) that change the behavior of the parser/lexer. | ||
|
||
For more details please [visit he wiki](https://github.com/glayzzle/php-parser/wiki). | ||
Documentation | ||
------------- | ||
|
||
# Join the dev | ||
- [AST nodes definition](https://github.com/glayzzle/php-parser/blob/master/docs/AST.md) | ||
- [List of options](https://github.com/glayzzle/php-parser/wiki/Options) | ||
- [Main API](https://github.com/glayzzle/php-parser/tree/master/docs) | ||
- [Lexer API](https://github.com/glayzzle/php-parser/blob/master/docs/lexer.md) | ||
- [Parser API](https://github.com/glayzzle/php-parser/blob/master/docs/parser.md) | ||
|
||
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. | ||
Related projects | ||
---------------- | ||
|
||
The command line options : | ||
- [php-unparser](https://github.com/chris-l/php-unparser) : Produce code that uses the style format recommended by PSR-1 and PSR-2. | ||
- [php-writer](https://github.com/glayzzle/php-writer) : Update PHP scripts from their AST | ||
- [ts-php-inspections](https://github.com/DaGhostman/ts-php-inspections) : Provide PHP code inspections written in typescript | ||
- [php-reflection](https://github.com/glayzzle/php-reflection) : Reflection API for PHP files | ||
- [wp-pot](https://github.com/rasmusbe/wp-pot) : Generate pot file for WordPress plugins and themes | ||
- [crane](https://github.com/HvyIndustries/crane) : PHP Intellisense/code-completion for VS Code | ||
|
||
```sh | ||
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. | ||
> 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.