1
1
<h1 align =" center " >php-parser</h1 >
2
2
<p align =" center " >
3
- <a href =" https://circleci.com/gh/glayzzle/php-parser/tree/master " ><img src =" https://circleci.com/gh/glayzzle/php-parser/tree/master.svg?style=svg " ></a >
4
- <a href =" https://coveralls.io/github/glayzzle/php-parser?branch=master " ><img src =" https://coveralls.io/repos/github/glayzzle/php-parser/badge.svg?branch=master&v=20170115 " alt =" Coverage Status " /></a >
5
3
<a title =" npm version " href =" https://www.npmjs.com/package/php-parser " ><img src =" https://badge.fury.io/js/php-parser.svg " ></a >
6
4
<a title =" npm downloads " href =" https://www.npmjs.com/package/php-parser " ><img src =" https://img.shields.io/npm/dm/php-parser.svg?style=flat " ></a >
7
5
<a title =" Gitter " href =" https://gitter.im/glayzzle/Lobby " ><img src =" https://img.shields.io/badge/GITTER-join%20chat-green.svg " ></a >
8
- <a href =" https://app.fossa.io/projects/git%2Bgithub.com%2Fglayzzle%2Fphp-parser?ref=badge_shield " alt =" FOSSA Status " ><img src =" https://app.fossa.io/api/projects/git%2Bgithub.com%2Fglayzzle%2Fphp-parser.svg?type=shield " /></a >
9
6
</p >
10
- <p align =" center " >This javascript library parses PHP code and convert it to AST.</p >
7
+ <p align =" center " >This JavaScript library parses PHP code and converts it to an AST.</p >
11
8
12
- Installation
13
- ------------
9
+ ## Installation
14
10
15
11
This library is distributed with [ npm] ( https://www.npmjs.com/package/php-parser ) :
16
12
17
13
``` sh
18
14
npm install php-parser --save
19
15
```
20
16
21
- Usage
22
- -----
17
+ ## Usage
23
18
24
19
``` js
25
20
// initialize the php parser factory class
26
- var fs = require (' fs ' );
27
- var path = require (' path' );
28
- var engine = require (' php-parser' );
21
+ const fs = require (" fs " );
22
+ const path = require (" path" );
23
+ const engine = require (" php-parser" );
29
24
30
25
// initialize a new parser instance
31
- var parser = new engine ({
26
+ const parser = new engine ({
32
27
// some options :
33
28
parser: {
34
29
extractDoc: true ,
35
- php7: true
30
+ php7: true ,
36
31
},
37
32
ast: {
38
- withPositions: true
39
- }
33
+ withPositions: true ,
34
+ },
40
35
});
41
36
42
37
// Retrieve the AST from the specified source
43
- var eval = parser .parseEval (' echo "Hello World";' );
38
+ const eval = parser .parseEval (' echo "Hello World";' );
44
39
45
40
// Retrieve an array of tokens (same as php function token_get_all)
46
- var tokens = parser .tokenGetAll (' <?php echo "Hello World";' );
41
+ const tokens = parser .tokenGetAll (' <?php echo "Hello World";' );
47
42
48
43
// Load a static file (Note: this file should exist on your computer)
49
- var phpFile = fs .readFileSync ( ' ./example.php' );
44
+ const phpFile = fs .readFileSync (" ./example.php" );
50
45
51
46
// Log out results
52
- console .log ( ' Eval parse:' , eval );
53
- console .log ( ' Tokens parse:' , tokens );
54
- console .log ( ' File parse:' , parser .parseCode (phpFile) );
55
-
47
+ console .log (" Eval parse:" , eval);
48
+ console .log (" Tokens parse:" , tokens);
49
+ console .log (" File parse:" , parser .parseCode (phpFile));
56
50
```
57
51
58
- Sample AST output
59
- -----------------
52
+ ## Sample AST output
60
53
61
54
``` js
62
55
{
@@ -79,9 +72,7 @@ Sample AST output
79
72
- Try it online (demo) : http://glayzzle.com/php-parser/
80
73
- Or from AST Explorer : https://astexplorer.net/
81
74
82
-
83
- API Overview
84
- ------------
75
+ ## API Overview
85
76
86
77
The main API exposes a class with the following methods :
87
78
@@ -91,15 +82,13 @@ The main API exposes a class with the following methods :
91
82
92
83
You can also [ pass options] ( https://github.com/glayzzle/php-parser/wiki/Options ) that change the behavior of the parser/lexer.
93
84
94
- Documentation
95
- -------------
85
+ ## Documentation
96
86
97
87
- [ AST nodes definition] ( https://php-parser.glayzzle.com/api/ast.js )
98
88
- [ Sandbox] ( https://php-parser.glayzzle.com/demo )
99
89
- [ List of options] ( https://php-parser.glayzzle.com/guides/options )
100
90
101
- Related projects
102
- ----------------
91
+ ## Related projects
103
92
104
93
- [ prettier/plugin-php] ( https://github.com/prettier/plugin-php ) : Prettier PHP Plugin
105
94
- [ babel-preset-php] ( https://gitlab.com/kornelski/babel-preset-php ) : Babel preset for converting PHP syntax to JavaScript. It can run subset of PHP in the browser or in Node.js
@@ -117,5 +106,3 @@ Related projects
117
106
## License
118
107
119
108
This library is released under BSD-3 license clause.
120
-
121
- [ ![ FOSSA Status] ( https://app.fossa.io/api/projects/git%2Bgithub.com%2Fglayzzle%2Fphp-parser.svg?type=large )] ( https://app.fossa.io/projects/git%2Bgithub.com%2Fglayzzle%2Fphp-parser?ref=badge_large )
0 commit comments