Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/z7zmey/php-parser
PHP parser written in Go
https://github.com/z7zmey/php-parser
ast go parser php
Last synced: 2 months ago
JSON representation
PHP parser written in Go
- Host: GitHub
- URL: https://github.com/z7zmey/php-parser
- Owner: z7zmey
- License: mit
- Created: 2017-11-07T06:20:46.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-04-28T03:22:19.000Z (over 3 years ago)
- Last Synced: 2024-09-30T09:06:33.557Z (4 months ago)
- Topics: ast, go, parser, php
- Language: Go
- Homepage: https://php-parser.com
- Size: 12.1 MB
- Stars: 938
- Watchers: 31
- Forks: 63
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- go-awesome - z7zmey/php-parser - PHP AST syntax parsing (Open source library / Interpreter)
- awesome-golang-repositories - php-parser
- awesome-go - php-parser - PHP parser written in Go - ★ 528 (Code Analysis)
- awesome-go-extra - php-parser - 11-07T06:20:46Z|2021-04-28T03:22:19Z| (Code Analysis / Routers)
README
PHP Parser written in Go
========================[![GoDoc](https://godoc.org/github.com/z7zmey/php-parser?status.svg)](https://godoc.org/github.com/z7zmey/php-parser)
[![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)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.
#### Try it online: [demo](https://php-parser.com)
Features:
---------- Fully support PHP 5 and PHP 7 syntax
- Abstract syntax tree (AST) representation
- Traversing AST
- Resolving namespaced names
- Parsing syntax-invalid PHP files
- Saving and printing free-floating comments and whitespacesWho Uses
--------[VKCOM/noverify](https://github.com/VKCOM/noverify) - NoVerify is a pretty fast linter for PHP
[quasilyte/phpgrep](https://github.com/quasilyte/phpgrep) - phpgrep is a tool for syntax-aware PHP code search
Usage example
-------```Golang
package mainimport (
"log"
"os""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"
)func main() {
src := []byte(` echo "Hello world";`)// Error handler
var parserErrors []*errors.Error
errorHandler := func(e *errors.Error) {
parserErrors = append(parserErrors, e)
}// 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())
}// Dump
goDumper := dumper.NewDumper(os.Stdout).
WithTokens().
WithPositions()rootNode.Accept(goDumper)
}
```Roadmap
-------- Control Flow Graph (CFG)
- PHP8Install
-------```
go get github.com/z7zmey/php-parser/cmd/php-parser
```CLI
---```
php-parser [flags] ...
```| 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) |Namespace resolver
------------------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 `Name`, `Relative`, `FullyQualified` nodes it resolves `use` aliases and saves a fully qualified name.