php-parser/README.md

117 lines
3.4 KiB
Markdown
Raw Normal View History

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
2018-02-19 12:34:49 +00:00
[![GoDoc](https://godoc.org/github.com/z7zmey/php-parser?status.svg)](https://godoc.org/github.com/z7zmey/php-parser)
2018-08-06 18:16:47 +00:00
[![Build Status](https://travis-ci.org/z7zmey/php-parser.svg?branch=master)](https://travis-ci.org/z7zmey/php-parser)
[![Go Report Card](https://goreportcard.com/badge/github.com/z7zmey/php-parser)](https://goreportcard.com/report/github.com/z7zmey/php-parser)
[![Maintainability](https://api.codeclimate.com/v1/badges/950783b2e739db26e0ed/maintainability)](https://codeclimate.com/github/z7zmey/php-parser/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/950783b2e739db26e0ed/test_coverage)](https://codeclimate.com/github/z7zmey/php-parser/test_coverage)
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
2018-03-04 13:47:41 +00:00
#### Try it online: [demo](https://php-parser.com)
2018-05-16 11:02:28 +00:00
Features:
---------
2018-04-10 20:45:26 +00:00
- Fully support PHP 5 and PHP 7 syntax
- Abstract syntax tree (AST) representation
2018-01-09 16:36:06 +00:00
- Traversing AST
2019-02-25 15:01:06 +00:00
- Resolving namespaced names
- 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
2019-03-06 08:36:48 +00:00
[VKCOM/noverify](https://github.com/VKCOM/noverify) - NoVerify is a pretty fast linter for PHP
2018-02-06 10:52:31 +00:00
2019-08-13 01:00:36 +00:00
[quasilyte/phpgrep](https://github.com/quasilyte/phpgrep) - phpgrep is a 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"
"os"
2018-01-12 08:04:31 +00:00
2021-02-13 20:16:54 +00:00
"github.com/z7zmey/php-parser/pkg/cfg"
"github.com/z7zmey/php-parser/pkg/errors"
"github.com/z7zmey/php-parser/pkg/parser"
"github.com/z7zmey/php-parser/pkg/version"
"github.com/z7zmey/php-parser/pkg/visitor/dumper"
2018-01-12 08:04:31 +00:00
)
func main() {
2019-03-10 21:37:01 +00:00
src := []byte(`<? 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
rootNode, err := parser.Parse(src, cfg.Config{
Version: &version.Version{Major: 5, Minor: 6},
ErrorHandlerFunc: errorHandler,
})
if err != nil {
log.Fatal("Error:" + err.Error())
2018-02-04 19:35:46 +00:00
}
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
Roadmap
-------
- Control Flow Graph (CFG)
2021-02-13 20:16:54 +00:00
- PHP8
2019-03-06 08:35:38 +00:00
Install
-------
```
2021-02-13 20:16:54 +00:00
go get github.com/z7zmey/php-parser/cmd/php-parser
2019-03-06 08:35:38 +00:00
```
CLI
---
```
php-parser [flags] <path> ...
```
2021-02-13 20:16:54 +00:00
| flag | type | description |
| ------- | ------ | --------------------------------- |
| -p | bool | print filepath |
| -e | bool | print errors |
| -d | bool | dump in golang format |
| -r | bool | resolve names |
| -prof | string | start profiler: [cpu, mem, trace] |
| -phpver | string | php version (default: 7.4) |
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
2018-04-10 20:45:26 +00:00
Namespace resolver is a visitor that resolves nodes fully qualified name and saves into `map[node.Node]string` structure
2018-02-06 10:52:31 +00:00
2018-04-10 20:45:26 +00:00
- 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.