You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
5.9 KiB

3 years ago
  1. PHP Parser
  2. ==========
  3. [![Coverage Status](https://coveralls.io/repos/github/nikic/PHP-Parser/badge.svg?branch=master)](https://coveralls.io/github/nikic/PHP-Parser?branch=master)
  4. This is a PHP 5.2 to PHP 8.0 parser written in PHP. Its purpose is to simplify static code analysis and
  5. manipulation.
  6. [**Documentation for version 4.x**][doc_master] (stable; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 8.0).
  7. [Documentation for version 3.x][doc_3_x] (unsupported; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2).
  8. Features
  9. --------
  10. The main features provided by this library are:
  11. * Parsing PHP 5, PHP 7, and PHP 8 code into an abstract syntax tree (AST).
  12. * Invalid code can be parsed into a partial AST.
  13. * The AST contains accurate location information.
  14. * Dumping the AST in human-readable form.
  15. * Converting an AST back to PHP code.
  16. * Experimental: Formatting can be preserved for partially changed ASTs.
  17. * Infrastructure to traverse and modify ASTs.
  18. * Resolution of namespaced names.
  19. * Evaluation of constant expressions.
  20. * Builders to simplify AST construction for code generation.
  21. * Converting an AST into JSON and back.
  22. Quick Start
  23. -----------
  24. Install the library using [composer](https://getcomposer.org):
  25. php composer.phar require nikic/php-parser
  26. Parse some PHP code into an AST and dump the result in human-readable form:
  27. ```php
  28. <?php
  29. use PhpParser\Error;
  30. use PhpParser\NodeDumper;
  31. use PhpParser\ParserFactory;
  32. $code = <<<'CODE'
  33. <?php
  34. function test($foo)
  35. {
  36. var_dump($foo);
  37. }
  38. CODE;
  39. $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
  40. try {
  41. $ast = $parser->parse($code);
  42. } catch (Error $error) {
  43. echo "Parse error: {$error->getMessage()}\n";
  44. return;
  45. }
  46. $dumper = new NodeDumper;
  47. echo $dumper->dump($ast) . "\n";
  48. ```
  49. This dumps an AST looking something like this:
  50. ```
  51. array(
  52. 0: Stmt_Function(
  53. byRef: false
  54. name: Identifier(
  55. name: test
  56. )
  57. params: array(
  58. 0: Param(
  59. type: null
  60. byRef: false
  61. variadic: false
  62. var: Expr_Variable(
  63. name: foo
  64. )
  65. default: null
  66. )
  67. )
  68. returnType: null
  69. stmts: array(
  70. 0: Stmt_Expression(
  71. expr: Expr_FuncCall(
  72. name: Name(
  73. parts: array(
  74. 0: var_dump
  75. )
  76. )
  77. args: array(
  78. 0: Arg(
  79. value: Expr_Variable(
  80. name: foo
  81. )
  82. byRef: false
  83. unpack: false
  84. )
  85. )
  86. )
  87. )
  88. )
  89. )
  90. )
  91. ```
  92. Let's traverse the AST and perform some kind of modification. For example, drop all function bodies:
  93. ```php
  94. use PhpParser\Node;
  95. use PhpParser\Node\Stmt\Function_;
  96. use PhpParser\NodeTraverser;
  97. use PhpParser\NodeVisitorAbstract;
  98. $traverser = new NodeTraverser();
  99. $traverser->addVisitor(new class extends NodeVisitorAbstract {
  100. public function enterNode(Node $node) {
  101. if ($node instanceof Function_) {
  102. // Clean out the function body
  103. $node->stmts = [];
  104. }
  105. }
  106. });
  107. $ast = $traverser->traverse($ast);
  108. echo $dumper->dump($ast) . "\n";
  109. ```
  110. This gives us an AST where the `Function_::$stmts` are empty:
  111. ```
  112. array(
  113. 0: Stmt_Function(
  114. byRef: false
  115. name: Identifier(
  116. name: test
  117. )
  118. params: array(
  119. 0: Param(
  120. type: null
  121. byRef: false
  122. variadic: false
  123. var: Expr_Variable(
  124. name: foo
  125. )
  126. default: null
  127. )
  128. )
  129. returnType: null
  130. stmts: array(
  131. )
  132. )
  133. )
  134. ```
  135. Finally, we can convert the new AST back to PHP code:
  136. ```php
  137. use PhpParser\PrettyPrinter;
  138. $prettyPrinter = new PrettyPrinter\Standard;
  139. echo $prettyPrinter->prettyPrintFile($ast);
  140. ```
  141. This gives us our original code, minus the `var_dump()` call inside the function:
  142. ```php
  143. <?php
  144. function test($foo)
  145. {
  146. }
  147. ```
  148. For a more comprehensive introduction, see the documentation.
  149. Documentation
  150. -------------
  151. 1. [Introduction](doc/0_Introduction.markdown)
  152. 2. [Usage of basic components](doc/2_Usage_of_basic_components.markdown)
  153. Component documentation:
  154. * [Walking the AST](doc/component/Walking_the_AST.markdown)
  155. * Node visitors
  156. * Modifying the AST from a visitor
  157. * Short-circuiting traversals
  158. * Interleaved visitors
  159. * Simple node finding API
  160. * Parent and sibling references
  161. * [Name resolution](doc/component/Name_resolution.markdown)
  162. * Name resolver options
  163. * Name resolution context
  164. * [Pretty printing](doc/component/Pretty_printing.markdown)
  165. * Converting AST back to PHP code
  166. * Customizing formatting
  167. * Formatting-preserving code transformations
  168. * [AST builders](doc/component/AST_builders.markdown)
  169. * Fluent builders for AST nodes
  170. * [Lexer](doc/component/Lexer.markdown)
  171. * Lexer options
  172. * Token and file positions for nodes
  173. * Custom attributes
  174. * [Error handling](doc/component/Error_handling.markdown)
  175. * Column information for errors
  176. * Error recovery (parsing of syntactically incorrect code)
  177. * [Constant expression evaluation](doc/component/Constant_expression_evaluation.markdown)
  178. * Evaluating constant/property/etc initializers
  179. * Handling errors and unsupported expressions
  180. * [JSON representation](doc/component/JSON_representation.markdown)
  181. * JSON encoding and decoding of ASTs
  182. * [Performance](doc/component/Performance.markdown)
  183. * Disabling Xdebug
  184. * Reusing objects
  185. * Garbage collection impact
  186. * [Frequently asked questions](doc/component/FAQ.markdown)
  187. * Parent and sibling references
  188. [doc_3_x]: https://github.com/nikic/PHP-Parser/tree/3.x/doc
  189. [doc_master]: https://github.com/nikic/PHP-Parser/tree/master/doc