|
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 | +------------ |
13 | 14 |
|
14 | | -```sh |
15 | | -$ npm install php-parser --save |
16 | | -``` |
17 | | - |
18 | | -# Try it |
| 15 | +This library is distributed with [npm](https://www.npmjs.com/package/php-parser) : |
19 | 16 |
|
20 | 17 | ```sh |
21 | | -$ cd bin |
22 | | -$ node test.js -e "echo 'Hello World';" |
| 18 | +npm install php-parser --save |
23 | 19 | ``` |
24 | 20 |
|
25 | | -Will output : |
| 21 | +Usage |
| 22 | +----- |
| 23 | + |
26 | 24 | ```js |
27 | | -*** START TESTING *** |
| 25 | +// initialize the php parser factory class |
| 26 | +var engine = require('php-parser'); |
| 27 | +// initialize a new parser instance |
| 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 |
| 39 | +var AST = parser.parseEval('echo "Hello World";'); |
| 40 | +// AST.kind === 'program'; |
| 41 | +// AST.children[0].kind === 'echo'; |
28 | 42 |
|
29 | | --- TOKENS : |
30 | | -T_ECHO T_CONSTANT_ENCAPSED_STRING ; |
| 43 | +// Retrieve an array of tokens (same as php function token_get_all) |
| 44 | +var tokens = parser.tokenGetAll('<?php echo "Hello World";'); |
| 45 | +``` |
31 | 46 |
|
32 | | --- AST : |
| 47 | +Sample AST output |
| 48 | +----------------- |
33 | 49 |
|
34 | | -[ |
35 | | - 'program', <-- program node |
36 | | - [ |
37 | | - [ 'sys', <-- first child, typed system call |
38 | | - 'echo', <-- operation echo |
39 | | - [ |
40 | | - [ 'string', '"Hello World"' ] <-- first argument |
| 50 | +```js |
| 51 | +{ |
| 52 | + 'kind': 'program', |
| 53 | + 'children': [ |
| 54 | + { |
| 55 | + 'kind': 'echo', |
| 56 | + 'arguments': [ |
| 57 | + { |
| 58 | + 'kind': 'string', |
| 59 | + 'isDoubleQuote': true, |
| 60 | + 'value': 'Hello World' |
| 61 | + } |
41 | 62 | ] |
42 | | - ] |
| 63 | + } |
43 | 64 | ] |
44 | | -] |
| 65 | +} |
45 | 66 | ``` |
46 | 67 |
|
47 | 68 | Try it online (demo) : |
48 | 69 | http://glayzzle.com/php-parser/#demo |
49 | 70 |
|
50 | | -# Use it |
| 71 | +API Overview |
| 72 | +------------ |
51 | 73 |
|
52 | | -```js |
53 | | -// initialize a new parser instance |
54 | | -var parser = require('php-parser').create(); |
| 74 | +The main API exposes a class with the following methods : |
55 | 75 |
|
56 | | -// how to retrieve the AST |
57 | | -var AST = parser.parseEval('echo "Hello World";'); |
| 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. |
58 | 79 |
|
59 | | -// how to list tokens |
60 | | -var tokens = parser.tokenGetAll('<?php echo "Hello World";'); |
61 | | -``` |
| 80 | +You can also [pass options](https://github.com/glayzzle/php-parser/wiki/Options) that change the behavior of the parser/lexer. |
62 | 81 |
|
63 | | -For more details please [visit he wiki](https://github.com/glayzzle/php-parser/wiki). |
| 82 | +Documentation |
| 83 | +------------- |
64 | 84 |
|
65 | | -# Join the dev |
| 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) |
66 | 90 |
|
67 | | -If you want to change/fix the lexer you will find code to `./src/lexer/`. |
68 | | -You can also implement the parser, the code is into `./src/parser/`. |
69 | | -To check your changes add tests into `./test/parser/`, and run `npm run test`. |
70 | | -Try to keep or improve the coverage levels. |
| 91 | +Related projects |
| 92 | +---------------- |
71 | 93 |
|
72 | | -The command line options : |
| 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 |
73 | 100 |
|
74 | | -```sh |
75 | | -Usage: test [options] [-f] <file> |
76 | | - |
77 | | - -f <file> Parse and test the specified file |
78 | | - -d <path> Parse each file in the specified path |
79 | | - -r Use recursivity with the specified path |
80 | | - -e Eval the specified input and shows AST |
81 | | - -v Enable verbose mode and show debug |
82 | | - -h, --help Print help and exit |
83 | | -``` |
84 | | - |
85 | | -If you run into problems with a test, run it with the cli and add the `--debug` flag. |
| 101 | +> You can add here your own project by opening an issue request. |
86 | 102 |
|
87 | 103 | # Misc |
88 | 104 |
|
89 | 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