Skip to content

Commit 971c63a

Browse files
committed
Merge commit 'f0c77136cb044190048daf3a6df31202174fe299' as 'PHP-Parser'
2 parents e9a9d76 + f0c7713 commit 971c63a

File tree

290 files changed

+19469
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

290 files changed

+19469
-0
lines changed

PHP-Parser/.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: php
2+
3+
php:
4+
- 5.2
5+
- 5.3
6+
- 5.4

PHP-Parser/CHANGELOG.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
Version 0.9.3-dev
2+
-----------------
3+
4+
Nothing yet.
5+
6+
Version 0.9.2 (07.07.2012)
7+
--------------------------
8+
* Add `Class->getMethods()` function, which returns all methods contained in the `stmts` array of the class node. This
9+
does not take inherited methods into account.
10+
11+
* Add `isPublic()`, `isProtected()`, `isPrivate()`. `isAbstract()`, `isFinal()` and `isStatic()` accessors to the
12+
`ClassMethod`, `Property` and `Class` nodes. (`Property` and `Class` obviously only have the accessors relevant to
13+
them.)
14+
15+
* Fix parsing of new expressions in parentheses, e.g. `return(new Foo);`.
16+
17+
* [BC] Due to the below changes nodes now optionally accept an `$attributes` array as the
18+
last parameter, instead of the previously used `$line` and `$docComment` parameters.
19+
20+
* Add mechanism for adding attributes to nodes in the lexer.
21+
22+
The following attributes are now added by default:
23+
24+
* `startLine`: The line the node started in.
25+
* `endLine`: The line the node ended in.
26+
* `comments`: An array of comments. The comments are instances of `PHPParser_Comment`
27+
(or `PHPParser_Comment_Doc` for doc comments).
28+
29+
The methods `getLine()` and `setLine()` still exist and function as before, but internally
30+
operator on the `startLine` attribute.
31+
32+
`getDocComment()` also continues to exist. It returns the last comment in the `comments`
33+
attribute if it is a doc comment, otherwise `null`. As `getDocComment()` now returns a
34+
comment object (which can be modified using `->setText()`) the `setDocComment()` method was
35+
removed. Comment objects implement a `__toString()` method, so `getDocComment()` should
36+
continue to work properly with old code.
37+
38+
* [BC] Use inject-once approach for lexer:
39+
40+
Now the lexer is injected only once when creating the parser. Instead of
41+
42+
$parser = new PHPParser_Parser;
43+
$parser->parse(new PHPParser_Lexer($code));
44+
$parser->parse(new PHPParser_Lexer($code2));
45+
46+
you write:
47+
48+
$parser = new PHPParser_Parser(new PHPParser_Lexer);
49+
$parser->parse($code);
50+
$parser->parse($code2);
51+
52+
* Fix `NameResolver` visitor to also resolve class names in `catch` blocks.
53+
54+
Version 0.9.1 (24.04.2012)
55+
--------------------------
56+
57+
* Add ability to add attributes to nodes:
58+
59+
It is now possible to add attributes to a node using `$node->setAttribute('name', 'value')` and to retrieve them using
60+
`$node->getAttribute('name' [, 'default'])`. Additionally the existance of an attribute can be checked with
61+
`$node->hasAttribute('name')` and all attributes can be returned using `$node->getAttributes()`.
62+
63+
* Add code generation features: Builders and templates.
64+
65+
For more infos, see the [code generation documentation][1].
66+
67+
* [BC] Don't traverse nodes merged by another visitor:
68+
69+
If a NodeVisitor returns an array of nodes to merge, these will no longer be traversed by all other visitors. This
70+
behavior only caused problems.
71+
72+
* Fix line numbers for some list structures.
73+
* Fix XML unserialization of empty nodes.
74+
* Fix parsing of integers that overflow into floats.
75+
* Fix emulation of NOWDOC and binary floats.
76+
77+
Version 0.9.0 (05.01.2012)
78+
--------------------------
79+
80+
First version.
81+
82+
[1]: https://github.com/nikic/PHP-Parser/blob/master/doc/3_Code_generation.markdown

PHP-Parser/LICENSE

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Copyright (c) 2011 by Nikita Popov.
2+
3+
Some rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are
7+
met:
8+
9+
* Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above
13+
copyright notice, this list of conditions and the following
14+
disclaimer in the documentation and/or other materials provided
15+
with the distribution.
16+
17+
* The names of the contributors may not be used to endorse or
18+
promote products derived from this software without specific
19+
prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PHP-Parser/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
PHP Parser
2+
==========
3+
4+
This is a PHP 5.4 (and older) parser written in PHP. It's purpose is to simplify static code analysis and
5+
manipulation.
6+
7+
Documentation can be found in the [`doc/`][1] directory.
8+
9+
***Note: This project is experimental, so the API is subject to change.***
10+
11+
In a Nutshell
12+
-------------
13+
14+
Basically, the parser does nothing more than turn some PHP code into an abstract syntax tree. ("nothing
15+
more" is kind of sarcastic here as PHP has a ... uhm, let's just say "not nice" ... grammar, which makes
16+
parsing PHP very hard.)
17+
18+
For example, if you stick this code in the parser:
19+
20+
```php
21+
<?php
22+
echo 'Hi', 'World';
23+
hello\world('foo', 'bar' . 'baz');
24+
```
25+
26+
You'll get a syntax tree looking roughly like this:
27+
28+
```
29+
array(
30+
0: Stmt_Echo(
31+
exprs: array(
32+
0: Scalar_String(
33+
value: Hi
34+
)
35+
1: Scalar_String(
36+
value: World
37+
)
38+
)
39+
)
40+
1: Expr_FuncCall(
41+
name: Name(
42+
parts: array(
43+
0: hello
44+
1: world
45+
)
46+
)
47+
args: array(
48+
0: Arg(
49+
value: Scalar_String(
50+
value: foo
51+
)
52+
byRef: false
53+
)
54+
1: Arg(
55+
value: Expr_Concat(
56+
left: Scalar_String(
57+
value: bar
58+
)
59+
right: Scalar_String(
60+
value: baz
61+
)
62+
)
63+
byRef: false
64+
)
65+
)
66+
)
67+
)
68+
```
69+
70+
You can then work with this syntax tree, for example to statically analyze the code (e.g. to find
71+
programming errors or security issues).
72+
73+
Additionally, you can convert a syntax tree back to PHP code. This allows you to do code preprocessing
74+
(like automatedly porting code to older PHP versions).
75+
76+
So, that's it, in a nutshell. You can find everything else in the [docs][1].
77+
78+
[1]: https://github.com/nikic/PHP-Parser/tree/master/doc

PHP-Parser/composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "nikic/php-parser",
3+
"description": "A PHP parser written in PHP",
4+
"keywords": ["php", "parser"],
5+
"type": "library",
6+
"license": "BSD",
7+
"authors": [
8+
{
9+
"name": "Nikita Popov"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.2"
14+
},
15+
"autoload": {
16+
"psr-0": { "PHPParser": "lib/" }
17+
}
18+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Introduction
2+
============
3+
4+
This project is a PHP 5.4 (and older) parser **written in PHP itself**.
5+
6+
What is this for?
7+
-----------------
8+
9+
A parser is useful for [static analysis][0] and manipulation of code and basically any other
10+
application dealing with code programmatically. A parser constructs an [Abstract Syntax Tree][1]
11+
(AST) of the code and thus allows dealing with it in an abstract and robust way.
12+
13+
There are other ways of dealing with 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
15+
the AST and thus has different applications: It allows to also analyize the exact formating of
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
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.
20+
21+
Another questions 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
23+
would be in other, faster languages like C. Furthermore the people most probably wanting to do
24+
programmatic PHP code analysis are incidentially PHP developers, not C developers.
25+
26+
What can it parse?
27+
------------------
28+
29+
The parser uses a PHP 5.4 compliant grammar, which is backwards compatible with at least PHP 5.3 and PHP
30+
5.2 (and maybe older).
31+
32+
As the parser is based on the tokens returned by `token_get_all` (which is only able to lex the PHP
33+
version it runs on), additionally a wrapper for emulating new tokens from 5.3 and 5.4 is provided. This
34+
allows to parse PHP 5.4 source code running on PHP 5.2, for example. This emulation is very hacky and not
35+
yet perfect, but it should work well on any sane code.
36+
37+
What output does it produce?
38+
----------------------------
39+
40+
The parser produces an [Abstract Syntax Tree][1] (AST) also known as a node tree. How this looks like
41+
can best be seen in an example. The program `<?php echo 'Hi', 'World';` will give you a node tree
42+
roughly looking like this:
43+
44+
```
45+
array(
46+
0: Stmt_Echo(
47+
exprs: array(
48+
0: Scalar_String(
49+
value: Hi
50+
)
51+
1: Scalar_String(
52+
value: World
53+
)
54+
)
55+
)
56+
)
57+
```
58+
59+
This matches the semantics the program had: An echo statement, which takes two strings as expressions,
60+
with the values `Hi` and `World!`.
61+
62+
You can also see that the AST does not contain any whitespace information (but most comments are saved).
63+
So using it for formatting analysis is not possible.
64+
65+
What else can it do?
66+
--------------------
67+
68+
Apart from the parser itself this package also bundles support for some other, related features:
69+
70+
* Support for pretty printing, which is the act of converting an AST into PHP code. Please note
71+
that "pretty printing" does not imply that the output is especially pretty. It's just how it's
72+
called ;)
73+
* Support for serializing and unserializing the node tree to XML
74+
* Support for dumping the node tree in a human readable form (see the section above for an
75+
example of how the output looks like)
76+
* Infrastructure for traversing and changing the AST (node traverser and node visitors)
77+
* A node visitor for resolving namespaced names
78+
79+
[0]: http://en.wikipedia.org/wiki/Static_program_analysis
80+
[1]: http://en.wikipedia.org/wiki/Abstract_syntax_tree
81+
[2]: http://php.net/token_get_all
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Installation
2+
============
3+
4+
There are multiple ways to include the PHP parser into your project:
5+
6+
Installing from the Zip- or Tarball
7+
-----------------------------------
8+
9+
Download the latest version from [the download page][2], unpack it and move the files somewhere into your project.
10+
11+
Installing via Composer
12+
-----------------------
13+
14+
Create a `composer.json` file in your project root and use it to define your dependencies:
15+
16+
{
17+
"require": {
18+
"nikic/php-parser": "0.9.2"
19+
}
20+
}
21+
22+
Then install Composer in your project (or [download the composer.phar][1] directly):
23+
24+
curl -s http://getcomposer.org/installer | php
25+
26+
And finally ask Composer to install the dependencies:
27+
28+
php composer.phar install
29+
30+
Installing as a PEAR package
31+
----------------------------
32+
33+
Run the following two commands:
34+
35+
pear channel-discover nikic.github.com/pear
36+
pear install channel://nikic.github.com/pear/PHPParser-0.9.2
37+
38+
Installing as a Git Submodule
39+
-----------------------------
40+
41+
Run the following command to install the parser into the `vendor/PHP-Parser` folder:
42+
43+
git submodule add git://github.com/nikic/PHP-Parser.git vendor/PHP-Parser
44+
45+
46+
47+
[1]: http://getcomposer.org/composer.phar
48+
[2]: https://github.com/nikic/PHP-Parser/tags

0 commit comments

Comments
 (0)