Chris@0
|
1 PHP Parser
|
Chris@0
|
2 ==========
|
Chris@0
|
3
|
Chris@0
|
4 [](https://travis-ci.org/nikic/PHP-Parser) [](https://coveralls.io/github/nikic/PHP-Parser?branch=master)
|
Chris@0
|
5
|
Chris@0
|
6 This is a PHP 5.2 to PHP 7.1 parser written in PHP. Its purpose is to simplify static code analysis and
|
Chris@0
|
7 manipulation.
|
Chris@0
|
8
|
Chris@0
|
9 [**Documentation for version 3.x**][doc_master] (stable; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.1).
|
Chris@0
|
10
|
Chris@0
|
11 [Documentation for version 2.x][doc_2_x] (stable; for running on PHP >= 5.4; for parsing PHP 5.2 to PHP 7.0).
|
Chris@0
|
12
|
Chris@0
|
13 [Documentation for version 1.x][doc_1_x] (unsupported; for running on PHP >= 5.3; for parsing PHP 5.2 to PHP 5.6).
|
Chris@0
|
14
|
Chris@0
|
15 In a Nutshell
|
Chris@0
|
16 -------------
|
Chris@0
|
17
|
Chris@0
|
18 The parser turns PHP source code into an abstract syntax tree. For example, if you pass the following code into the
|
Chris@0
|
19 parser:
|
Chris@0
|
20
|
Chris@0
|
21 ```php
|
Chris@0
|
22 <?php
|
Chris@0
|
23 echo 'Hi', 'World';
|
Chris@0
|
24 hello\world('foo', 'bar' . 'baz');
|
Chris@0
|
25 ```
|
Chris@0
|
26
|
Chris@0
|
27 You'll get a syntax tree looking roughly like this:
|
Chris@0
|
28
|
Chris@0
|
29 ```php
|
Chris@0
|
30 array(
|
Chris@0
|
31 0: Stmt_Echo(
|
Chris@0
|
32 exprs: array(
|
Chris@0
|
33 0: Scalar_String(
|
Chris@0
|
34 value: Hi
|
Chris@0
|
35 )
|
Chris@0
|
36 1: Scalar_String(
|
Chris@0
|
37 value: World
|
Chris@0
|
38 )
|
Chris@0
|
39 )
|
Chris@0
|
40 )
|
Chris@0
|
41 1: Expr_FuncCall(
|
Chris@0
|
42 name: Name(
|
Chris@0
|
43 parts: array(
|
Chris@0
|
44 0: hello
|
Chris@0
|
45 1: world
|
Chris@0
|
46 )
|
Chris@0
|
47 )
|
Chris@0
|
48 args: array(
|
Chris@0
|
49 0: Arg(
|
Chris@0
|
50 value: Scalar_String(
|
Chris@0
|
51 value: foo
|
Chris@0
|
52 )
|
Chris@0
|
53 byRef: false
|
Chris@0
|
54 )
|
Chris@0
|
55 1: Arg(
|
Chris@0
|
56 value: Expr_Concat(
|
Chris@0
|
57 left: Scalar_String(
|
Chris@0
|
58 value: bar
|
Chris@0
|
59 )
|
Chris@0
|
60 right: Scalar_String(
|
Chris@0
|
61 value: baz
|
Chris@0
|
62 )
|
Chris@0
|
63 )
|
Chris@0
|
64 byRef: false
|
Chris@0
|
65 )
|
Chris@0
|
66 )
|
Chris@0
|
67 )
|
Chris@0
|
68 )
|
Chris@0
|
69 ```
|
Chris@0
|
70
|
Chris@0
|
71 You can then work with this syntax tree, for example to statically analyze the code (e.g. to find
|
Chris@0
|
72 programming errors or security issues).
|
Chris@0
|
73
|
Chris@0
|
74 Additionally, you can convert a syntax tree back to PHP code. This allows you to do code preprocessing
|
Chris@0
|
75 (like automatedly porting code to older PHP versions).
|
Chris@0
|
76
|
Chris@0
|
77 Installation
|
Chris@0
|
78 ------------
|
Chris@0
|
79
|
Chris@0
|
80 The preferred installation method is [composer](https://getcomposer.org):
|
Chris@0
|
81
|
Chris@0
|
82 php composer.phar require nikic/php-parser
|
Chris@0
|
83
|
Chris@0
|
84 Documentation
|
Chris@0
|
85 -------------
|
Chris@0
|
86
|
Chris@0
|
87 1. [Introduction](doc/0_Introduction.markdown)
|
Chris@0
|
88 2. [Usage of basic components](doc/2_Usage_of_basic_components.markdown)
|
Chris@0
|
89 3. [Other node tree representations](doc/3_Other_node_tree_representations.markdown)
|
Chris@0
|
90 4. [Code generation](doc/4_Code_generation.markdown)
|
Chris@0
|
91
|
Chris@0
|
92 Component documentation:
|
Chris@0
|
93
|
Chris@0
|
94 1. [Error handling](doc/component/Error_handling.markdown)
|
Chris@0
|
95 2. [Lexer](doc/component/Lexer.markdown)
|
Chris@0
|
96
|
Chris@0
|
97 [doc_1_x]: https://github.com/nikic/PHP-Parser/tree/1.x/doc
|
Chris@0
|
98 [doc_2_x]: https://github.com/nikic/PHP-Parser/tree/2.x/doc
|
Chris@0
|
99 [doc_master]: https://github.com/nikic/PHP-Parser/tree/master/doc
|