Update README.md

This commit is contained in:
Vadym Slizov 2018-05-16 14:02:28 +03:00 committed by GitHub
parent 58a1ab4b79
commit 72cf9d106e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,13 @@
<!-- <!--
Title: PHP Parser Title: PHP Parser
Description: A Parser for PHP written in Go. Description: A Parser for PHP written in Go.
Author: Slizov Vadim Author: Slizov Vadym
Keywords: go golang php php-parser ast Keywords: php parser go golang ast
--> -->
PHP Parser written in Go
========================
<img src="./parser.jpg" alt="PHP Parser written in Go" width="980"/> <img src="./parser.jpg" alt="PHP Parser written in Go" width="980"/>
[![Go Report Card](https://goreportcard.com/badge/github.com/z7zmey/php-parser)](https://goreportcard.com/report/github.com/z7zmey/php-parser) [![Go Report Card](https://goreportcard.com/badge/github.com/z7zmey/php-parser)](https://goreportcard.com/report/github.com/z7zmey/php-parser)
@ -14,26 +17,32 @@
#### Try it online: [demo](https://php-parser.com) #### Try it online: [demo](https://php-parser.com)
## Features: Features:
---------
- Fully support PHP 5 and PHP 7 syntax - Fully support PHP 5 and PHP 7 syntax
- Abstract syntax tree (AST) representation - Abstract syntax tree (AST) representation
- Traversing AST - Traversing AST
- Namespace resolver - Namespace resolver
- Able to parse syntax-invalid PHP files - Able to parse syntax-invalid PHP files
## Roadmap Roadmap
-------
- Saving comments and empty lines
- Control Flow Graph (CFG) - Control Flow Graph (CFG)
- PhpDocComment parser - PhpDocComment parser
- Stabilize api - Stabilize api
## Install Install
-------
``` ```
go get github.com/z7zmey/php-parser go get github.com/z7zmey/php-parser
``` ```
## CLI CLI
---
``` ```
php-parser [-php5] <path> ... php-parser [-php5] <path> ...
@ -41,7 +50,9 @@ php-parser [-php5] <path> ...
Dump AST to stdout. Dump AST to stdout.
## Example Example
-------
```Golang ```Golang
package main package main
@ -76,32 +87,51 @@ func main() {
} }
``` ```
## Namespace resolver Namespace resolver
------------------
Namespace resolver is a visitor that resolves nodes fully qualified name and saves into `map[node.Node]string` structure Namespace resolver is a visitor that resolves nodes fully qualified name and saves into `map[node.Node]string` structure
- For `Class`, `Interface`, `Trait`, `Function`, `Constant` nodes it saves name with current namespace. - For `Class`, `Interface`, `Trait`, `Function`, `Constant` nodes it saves name with current namespace.
- For `Name`, `Relative`, `FullyQualified` nodes it resolves `use` aliases and saves a fully qualified name. - For `Name`, `Relative`, `FullyQualified` nodes it resolves `use` aliases and saves a fully qualified name.
## Parsing syntax-invalid PHP files Parsing syntax-invalid PHP files
--------------------------------
If we try to parse `$a$b;` then the parser triggers error 'syntax error: unexpected T_VARIABLE'. Token `$b` is unexpected, but parser recovers parsing process and returns `$b;` statement to AST, because it is syntactically correct. If we try to parse `$a$b;` then the parser triggers error 'syntax error: unexpected T_VARIABLE'. Token `$b` is unexpected, but parser recovers parsing process and returns `$b;` statement to AST, because it is syntactically correct.
## Pretty printer Pretty printer
--------------
```Golang ```Golang
nodes := &stmt.StmtList{ nodes := &stmt.StmtList{
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Namespace{ &stmt.Namespace{
NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}}, NamespaceName: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Foo"},
},
},
}, },
&stmt.Class{ &stmt.Class{
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}}, Modifiers: []node.Node{
ClassName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}}, &node.Identifier{Value: "abstract"},
Extends: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}}, },
ClassName: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Bar"},
},
},
Extends: &name.Name{
Parts: []node.Node{
&name.NamePart{Value: "Baz"},
},
},
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.ClassMethod{ &stmt.ClassMethod{
Modifiers: []node.Node{&node.Identifier{Value: "public"}}, Modifiers: []node.Node{
&node.Identifier{Value: "public"},
},
MethodName: &node.Identifier{Value: "greet"}, MethodName: &node.Identifier{Value: "greet"},
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Echo{ &stmt.Echo{
@ -121,7 +151,8 @@ p := printer.NewPrinter(file, " ")
p.PrintFile(nodes) p.PrintFile(nodes)
``` ```
Output: It prints to stdout:
```PHP ```PHP
<?php <?php
namespace Foo; namespace Foo;