|
1 | 1 | php-parser
|
2 | 2 | ==========
|
3 | 3 |
|
4 |
| -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). |
| 4 | +This javascript library parses PHP code and convert it to AST. |
5 | 5 |
|
6 | 6 | [](https://www.npmjs.com/package/php-parser)
|
7 | 7 | [](https://travis-ci.org/glayzzle/php-parser)
|
8 | 8 | [](https://coveralls.io/r/glayzzle/php-parser)
|
9 | 9 | [](https://gitter.im/glayzzle/Lobby)
|
10 | 10 |
|
11 | 11 |
|
12 |
| -# Install it |
| 12 | +Installation |
| 13 | +------------ |
| 14 | + |
| 15 | +This library is distributed with [npm](https://www.npmjs.com/package/php-parser) : |
13 | 16 |
|
14 | 17 | ```sh
|
15 |
| -$ npm install php-parser --save |
| 18 | +npm install php-parser --save |
16 | 19 | ```
|
17 | 20 |
|
18 |
| -# Use it |
| 21 | +Usage |
| 22 | +----- |
19 | 23 |
|
20 | 24 | ```js
|
| 25 | +// initialize the php parser factory class |
| 26 | +var engine = require('php-parser'); |
21 | 27 | // initialize a new parser instance
|
22 |
| -var parser = require('php-parser').create(); |
23 |
| - |
24 |
| -// how to retrieve the AST |
| 28 | +var parser = new engine({ |
| 29 | + // some options : |
| 30 | + parser: { |
| 31 | + extractDoc: true |
| 32 | + }, |
| 33 | + ast: { |
| 34 | + withPositions: true |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +// Retrieve the AST from the specified source |
25 | 39 | var AST = parser.parseEval('echo "Hello World";');
|
| 40 | +// AST.kind === 'program'; |
| 41 | +// AST.children[0].kind === 'echo'; |
26 | 42 |
|
27 |
| -// how to list tokens |
| 43 | +// Retrieve an array of tokens (same as php function token_get_all) |
28 | 44 | var tokens = parser.tokenGetAll('<?php echo "Hello World";');
|
29 | 45 | ```
|
30 | 46 |
|
31 |
| -For more details please [visit he wiki](https://github.com/glayzzle/php-parser/docs). |
32 |
| - |
33 |
| -# Output |
| 47 | +Sample AST output |
| 48 | +----------------- |
34 | 49 |
|
35 | 50 | ```js
|
36 |
| -[ |
37 |
| - 'program', <-- program node |
38 |
| - [ |
39 |
| - [ 'sys', <-- first child, typed system call |
40 |
| - 'echo', <-- operation echo |
41 |
| - [ |
42 |
| - [ 'string', '"Hello World"' ] <-- first argument |
| 51 | +{ |
| 52 | + 'kind': 'program', |
| 53 | + 'children': [ |
| 54 | + { |
| 55 | + 'kind': 'echo', |
| 56 | + 'arguments': [ |
| 57 | + { |
| 58 | + 'kind': 'string', |
| 59 | + 'isDoubleQuote': true, |
| 60 | + 'value': 'Hello World' |
| 61 | + } |
43 | 62 | ]
|
44 |
| - ] |
| 63 | + } |
45 | 64 | ]
|
46 |
| -] |
| 65 | +} |
47 | 66 | ```
|
48 | 67 |
|
49 | 68 | Try it online (demo) :
|
50 | 69 | http://glayzzle.com/php-parser/#demo
|
51 | 70 |
|
52 |
| -# Contributing |
| 71 | +API Overview |
| 72 | +------------ |
53 | 73 |
|
54 |
| -If you want to contribute please visit this repository https://github.com/glayzzle/php-parser-dev. |
| 74 | +The main API exposes a class with the following methods : |
| 75 | + |
| 76 | +- **parseEval**(String buffer) : parse a PHP code in eval style mode (without php open tags) |
| 77 | +- **parseCode**(String buffer, String filename) : parse a PHP code by using php open tags. |
| 78 | +- **tokenGetAll**(String buffer) : retrieves a list of all tokens from the specified input. |
| 79 | + |
| 80 | +You can also [pass options](https://github.com/glayzzle/php-parser/wiki/Options) that change the behavior of the parser/lexer. |
| 81 | + |
| 82 | +Documentation |
| 83 | +------------- |
| 84 | + |
| 85 | +- [AST nodes definition](https://github.com/glayzzle/php-parser/blob/master/docs/AST.md) |
| 86 | +- [List of options](https://github.com/glayzzle/php-parser/wiki/Options) |
| 87 | +- [Main API](https://github.com/glayzzle/php-parser/tree/master/docs) |
| 88 | +- [Lexer API](https://github.com/glayzzle/php-parser/blob/master/docs/lexer.md) |
| 89 | +- [Parser API](https://github.com/glayzzle/php-parser/blob/master/docs/parser.md) |
| 90 | + |
| 91 | +Related projects |
| 92 | +---------------- |
| 93 | + |
| 94 | +- [php-unparser](https://github.com/chris-l/php-unparser) : Produce code that uses the style format recommended by PSR-1 and PSR-2. |
| 95 | +- [php-writer](https://github.com/glayzzle/php-writer) : Update PHP scripts from their AST |
| 96 | +- [ts-php-inspections](https://github.com/DaGhostman/ts-php-inspections) : Provide PHP code inspections written in typescript |
| 97 | +- [php-reflection](https://github.com/glayzzle/php-reflection) : Reflection API for PHP files |
| 98 | +- [wp-pot](https://github.com/rasmusbe/wp-pot) : Generate pot file for WordPress plugins and themes |
| 99 | +- [crane](https://github.com/HvyIndustries/crane) : PHP Intellisense/code-completion for VS Code |
| 100 | + |
| 101 | +> You can add here your own project by opening an issue request. |
55 | 102 |
|
56 | 103 | # Misc
|
57 | 104 |
|
58 | 105 | This library is released under BSD-3 license clause.
|
| 106 | + |
| 107 | +If you want to contribute please visit this repository https://github.com/glayzzle/php-parser-dev. |
0 commit comments