Chris@0: Introduction Chris@0: ============ Chris@0: Chris@17: This project is a PHP 5.2 to PHP 7.3 parser **written in PHP itself**. Chris@0: Chris@0: What is this for? Chris@0: ----------------- Chris@0: Chris@0: A parser is useful for [static analysis][0], manipulation of code and basically any other Chris@0: application dealing with code programmatically. A parser constructs an [Abstract Syntax Tree][1] Chris@0: (AST) of the code and thus allows dealing with it in an abstract and robust way. Chris@0: Chris@0: There are other ways of processing source code. One that PHP supports natively is using the Chris@0: token stream generated by [`token_get_all`][2]. The token stream is much more low level than Chris@0: the AST and thus has different applications: It allows to also analyze the exact formatting of Chris@0: a file. On the other hand the token stream is much harder to deal with for more complex analysis. Chris@13: For example, an AST abstracts away the fact that, in PHP, variables can be written as `$foo`, but also Chris@0: as `$$bar`, `${'foobar'}` or even `${!${''}=barfoo()}`. You don't have to worry about recognizing Chris@0: all the different syntaxes from a stream of tokens. Chris@0: Chris@0: Another question is: Why would I want to have a PHP parser *written in PHP*? Well, PHP might not be Chris@0: a language especially suited for fast parsing, but processing the AST is much easier in PHP than it Chris@0: would be in other, faster languages like C. Furthermore the people most probably wanting to do Chris@0: programmatic PHP code analysis are incidentally PHP developers, not C developers. Chris@0: Chris@0: What can it parse? Chris@0: ------------------ Chris@0: Chris@17: The parser supports parsing PHP 5.2-7.3. Chris@0: Chris@0: As the parser is based on the tokens returned by `token_get_all` (which is only able to lex the PHP Chris@0: version it runs on), additionally a wrapper for emulating tokens from newer versions is provided. Chris@17: This allows to parse PHP 7.3 source code running on PHP 7.0, for example. This emulation is somewhat Chris@0: hacky and not perfect, but it should work well on any sane code. Chris@0: Chris@0: What output does it produce? Chris@0: ---------------------------- Chris@0: Chris@13: The parser produces an [Abstract Syntax Tree][1] (AST) also known as a node tree. How this looks Chris@0: can best be seen in an example. The program `