2023-03-26 01:13:26 +00:00
> This is a fork of the [VKCOM php-parse](https://github.com/VKCOM/php-parser), which in itself is a fork of [z7zmey](https://github.com/z7zmey) [parser](https://github.com/z7zmey/php-parser) that adds PHP 8 support.
2021-07-30 17:53:27 +00:00
2018-05-16 11:02:28 +00:00
PHP Parser written in Go
========================
2018-04-10 20:45:26 +00:00
< img src = "./parser.jpg" alt = "PHP Parser written in Go" width = "980" / >
2018-01-09 16:36:06 +00:00
2021-07-30 18:01:34 +00:00
[![GoDoc ](https://godoc.org/github.com/VKCOM/php-parser?status.svg )](https://godoc.org/github.com/VKCOM/php-parser)
2021-07-31 17:01:44 +00:00
[![Build Status ](https://github.com/VKCOM/php-parser/workflows/Go/badge.svg )](https://github.com/VKCOM/php-parser/workflows/Go)
2021-07-30 18:01:34 +00:00
[![Go Report Card ](https://goreportcard.com/badge/github.com/VKCOM/php-parser )](https://goreportcard.com/report/github.com/VKCOM/php-parser)
2018-01-09 16:36:06 +00:00
2019-03-10 21:37:01 +00:00
This project uses [goyacc ](https://godoc.org/golang.org/x/tools/cmd/goyacc ) and [ragel ](https://www.colm.net/open-source/ragel/ ) tools to create PHP parser. It parses source code into [AST ](https://en.wikipedia.org/wiki/Abstract_syntax_tree ). It can be used to write static analysis, refactoring, metrics, code style formatting tools.
2018-06-07 16:52:44 +00:00
2021-07-30 17:53:27 +00:00
Features
2018-05-16 11:02:28 +00:00
---------
2022-06-26 00:36:20 +00:00
- Fully support PHP 5, PHP 7 and PHP 8.0-8.2 syntax
2018-04-10 20:45:26 +00:00
- Abstract syntax tree (AST) representation
2018-01-09 16:36:06 +00:00
- Traversing AST
2021-07-30 17:53:27 +00:00
- Resolving namespace names
2019-02-25 15:01:06 +00:00
- Parsing syntax-invalid PHP files
- Saving and printing free-floating comments and whitespaces
2018-04-10 20:45:26 +00:00
2019-03-06 08:36:48 +00:00
Who Uses
--------
2018-02-06 10:52:31 +00:00
2021-07-30 17:53:27 +00:00
- [VKCOM/noverify ](https://github.com/VKCOM/noverify ) — pretty fast linter for PHP
- [VKCOM/nocolor ](https://github.com/VKCOM/nocolor ) — architecture validation tool for PHP based on the [*concept of colored functions* ](https://github.com/VKCOM/nocolor/blob/master/docs/introducing_colors.md )
- [quasilyte/phpgrep ](https://github.com/quasilyte/phpgrep ) — tool for syntax-aware PHP code search
2018-04-10 20:45:26 +00:00
2019-03-06 08:35:38 +00:00
Usage example
2018-05-16 11:02:28 +00:00
-------
2018-01-12 08:04:31 +00:00
```Golang
package main
import (
2021-02-13 20:16:54 +00:00
"log"
2018-04-07 13:01:59 +00:00
"os"
2018-01-12 08:04:31 +00:00
2021-07-31 10:19:33 +00:00
"github.com/VKCOM/php-parser/pkg/conf"
2021-07-30 18:01:34 +00:00
"github.com/VKCOM/php-parser/pkg/errors"
"github.com/VKCOM/php-parser/pkg/parser"
"github.com/VKCOM/php-parser/pkg/version"
"github.com/VKCOM/php-parser/pkg/visitor/dumper"
2018-01-12 08:04:31 +00:00
)
func main() {
2021-07-30 17:53:27 +00:00
src := []byte(`< ?php echo "Hello world";`)
2018-04-09 20:30:44 +00:00
2021-02-13 20:16:54 +00:00
// Error handler
2018-04-10 12:51:00 +00:00
2021-02-13 20:16:54 +00:00
var parserErrors []*errors.Error
errorHandler := func(e *errors.Error) {
parserErrors = append(parserErrors, e)
2018-04-10 12:51:00 +00:00
}
2018-01-12 08:04:31 +00:00
2021-02-13 20:16:54 +00:00
// Parse
2021-07-31 10:19:33 +00:00
rootNode, err := parser.Parse(src, conf.Config{
2021-07-30 17:53:27 +00:00
Version: & version.Version{Major: 8, Minor: 0},
2021-02-13 20:16:54 +00:00
ErrorHandlerFunc: errorHandler,
})
if err != nil {
log.Fatal("Error:" + err.Error())
2018-02-04 19:35:46 +00:00
}
2023-03-26 01:13:26 +00:00
2021-08-01 19:00:01 +00:00
if len(parserErrors) > 0 {
for _, e := range parserErrors {
log.Println(e.String())
}
os.Exit(1)
}
2018-04-10 12:51:00 +00:00
2021-02-13 20:16:54 +00:00
// Dump
goDumper := dumper.NewDumper(os.Stdout).
WithTokens().
WithPositions()
rootNode.Accept(goDumper)
2018-01-12 08:04:31 +00:00
}
```
2019-03-06 08:35:38 +00:00
Install
-------
```
2021-07-30 18:01:34 +00:00
go get github.com/VKCOM/php-parser/cmd/php-parser
2019-03-06 08:35:38 +00:00
```
CLI
---
```
php-parser [flags] < path > ...
```
2021-07-30 17:53:27 +00:00
| flag | type | description |
2022-06-25 17:09:48 +00:00
|------------|----------|-------------------------------------|
| `-p` | `bool` | Print file paths |
| `-e` | `bool` | Print errors |
| `-d` | `bool` | Dump AST in Golang format |
| `-r` | `bool` | Resolve names |
| `--pb` | `bool` | Print AST back into the parsed file |
| `--time` | `bool` | Print execution time |
2021-07-30 17:53:27 +00:00
| `--prof` | `string` | Start profiler: `[cpu, mem, trace]` |
| `--phpver` | `string` | PHP version (default: 8.0) |
2019-03-06 08:35:38 +00:00
2018-05-16 11:02:28 +00:00
Namespace resolver
------------------
2018-02-06 10:52:31 +00:00
2023-04-24 18:23:01 +00:00
Namespace resolver is a visitor that resolves nodes fully qualified name and saves into `map[ast.Vertex]string` structure
2018-02-06 10:52:31 +00:00
2023-04-24 18:23:01 +00:00
- For `Class` , `Interface` , `Trait` , `Enum` , `Function` , `Constant` nodes it saves name with current namespace.
2021-07-31 10:19:33 +00:00
- For `Name` , `Relative` , `FullyQualified` nodes it resolves `use` aliases and saves a fully qualified name.