comparison vendor/nikic/php-parser/doc/0_Introduction.markdown @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
1 Introduction 1 Introduction
2 ============ 2 ============
3 3
4 This project is a PHP 5.2 to PHP 7.1 parser **written in PHP itself**. 4 This project is a PHP 5.2 to PHP 7.2 parser **written in PHP itself**.
5 5
6 What is this for? 6 What is this for?
7 ----------------- 7 -----------------
8 8
9 A parser is useful for [static analysis][0], manipulation of code and basically any other 9 A parser is useful for [static analysis][0], manipulation of code and basically any other
12 12
13 There are other ways of processing source code. One that PHP supports natively is using the 13 There are other ways of processing source code. One that PHP supports natively is using the
14 token stream generated by [`token_get_all`][2]. The token stream is much more low level than 14 token stream generated by [`token_get_all`][2]. The token stream is much more low level than
15 the AST and thus has different applications: It allows to also analyze the exact formatting of 15 the AST and thus has different applications: It allows to also analyze the exact formatting of
16 a file. On the other hand the token stream is much harder to deal with for more complex analysis. 16 a file. On the other hand the token stream is much harder to deal with for more complex analysis.
17 For example an AST abstracts away the fact that in PHP variables can be written as `$foo`, but also 17 For example, an AST abstracts away the fact that, in PHP, variables can be written as `$foo`, but also
18 as `$$bar`, `${'foobar'}` or even `${!${''}=barfoo()}`. You don't have to worry about recognizing 18 as `$$bar`, `${'foobar'}` or even `${!${''}=barfoo()}`. You don't have to worry about recognizing
19 all the different syntaxes from a stream of tokens. 19 all the different syntaxes from a stream of tokens.
20 20
21 Another question is: Why would I want to have a PHP parser *written in PHP*? Well, PHP might not be 21 Another question is: Why would I want to have a PHP parser *written in PHP*? Well, PHP might not be
22 a language especially suited for fast parsing, but processing the AST is much easier in PHP than it 22 a language especially suited for fast parsing, but processing the AST is much easier in PHP than it
24 programmatic PHP code analysis are incidentally PHP developers, not C developers. 24 programmatic PHP code analysis are incidentally PHP developers, not C developers.
25 25
26 What can it parse? 26 What can it parse?
27 ------------------ 27 ------------------
28 28
29 The parser supports parsing PHP 5.2-5.6 and PHP 7. 29 The parser supports parsing PHP 5.2-7.2.
30 30
31 As the parser is based on the tokens returned by `token_get_all` (which is only able to lex the PHP 31 As the parser is based on the tokens returned by `token_get_all` (which is only able to lex the PHP
32 version it runs on), additionally a wrapper for emulating tokens from newer versions is provided. 32 version it runs on), additionally a wrapper for emulating tokens from newer versions is provided.
33 This allows to parse PHP 7.1 source code running on PHP 5.5, for example. This emulation is somewhat 33 This allows to parse PHP 7.2 source code running on PHP 5.5, for example. This emulation is somewhat
34 hacky and not perfect, but it should work well on any sane code. 34 hacky and not perfect, but it should work well on any sane code.
35 35
36 What output does it produce? 36 What output does it produce?
37 ---------------------------- 37 ----------------------------
38 38
39 The parser produces an [Abstract Syntax Tree][1] (AST) also known as a node tree. How this looks like 39 The parser produces an [Abstract Syntax Tree][1] (AST) also known as a node tree. How this looks
40 can best be seen in an example. The program `<?php echo 'Hi', 'World';` will give you a node tree 40 can best be seen in an example. The program `<?php echo 'Hi', 'World';` will give you a node tree
41 roughly looking like this: 41 roughly looking like this:
42 42
43 ``` 43 ```
44 array( 44 array(
67 Apart from the parser itself this package also bundles support for some other, related features: 67 Apart from the parser itself this package also bundles support for some other, related features:
68 68
69 * Support for pretty printing, which is the act of converting an AST into PHP code. Please note 69 * Support for pretty printing, which is the act of converting an AST into PHP code. Please note
70 that "pretty printing" does not imply that the output is especially pretty. It's just how it's 70 that "pretty printing" does not imply that the output is especially pretty. It's just how it's
71 called ;) 71 called ;)
72 * Support for serializing and unserializing the node tree to XML 72 * Support for serializing and unserializing the node tree to JSON
73 * Support for dumping the node tree in a human readable form (see the section above for an 73 * Support for dumping the node tree in a human readable form (see the section above for an
74 example of how the output looks like) 74 example of how the output looks like)
75 * Infrastructure for traversing and changing the AST (node traverser and node visitors) 75 * Infrastructure for traversing and changing the AST (node traverser and node visitors)
76 * A node visitor for resolving namespaced names 76 * A node visitor for resolving namespaced names
77 77