annotate vendor/nikic/php-parser/README.md @ 5:12f9dff5fda9 tip

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