PHP 8 Update
- nullsafe operator (?->)
- Remove (real) cast
- Named arguments
- Remove (unset) cast
- Remove {} access
- match expression
- Union types in type hints and static typehint
- Block catch without variable
- Trailing comma in parameter lists
- throw can be used as an expression
- Concatenation precedence
- Declaring properties in the constructor
- Attributes
- Names in the namespace are treated as a single token
- Trailing comma in closure use list
- Check that ::class on object works
- Deferencable changes and arbitrary expressions in new/instanceof
		
	
This is a fork of the z7zmey parser that adds PHP 8 support.
PHP Parser written in Go
 
This project uses goyacc and ragel tools to create PHP parser. It parses source code into AST. It can be used to write static analysis, refactoring, metrics, code style formatting tools.
Features
- Fully support PHP 5, PHP 7 and PHP 8.0 syntax
- Abstract syntax tree (AST) representation
- Traversing AST
- Resolving namespace names
- Parsing syntax-invalid PHP files
- Saving and printing free-floating comments and whitespaces
Who Uses
- VKCOM/noverify — pretty fast linter for PHP
- VKCOM/nocolor — architecture validation tool for PHP based on the concept of colored functions
- quasilyte/phpgrep — tool for syntax-aware PHP code search
Usage example
package main
import (
	"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(`<?php 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: 8, Minor: 0},
		ErrorHandlerFunc: errorHandler,
	})
	if err != nil {
		log.Fatal("Error:" + err.Error())
	}
	// Dump
	goDumper := dumper.NewDumper(os.Stdout).
		WithTokens().
		WithPositions()
	rootNode.Accept(goDumper)
}
Install
go get github.com/z7zmey/php-parser/cmd/php-parser
CLI
php-parser [flags] <path> ...
| flag | type | description | 
|---|---|---|
| --p | bool | Print file paths | 
| --e | bool | Print errors | 
| --d | bool | Dump AST in Golang format | 
| --r | bool | Resolve names | 
| --prof | string | Start profiler: [cpu, mem, trace] | 
| --phpver | string | PHP version (default: 8.0) | 
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,Constantnodes it saves name with current namespace.
- For Name,Relative,FullyQualifiednodes it resolvesusealiases and saves a fully qualified name.
Description
				
					Languages
				
				
								
								
									Go
								
								92.7%
							
						
							
								
								
									Yacc
								
								6%
							
						
							
								
								
									Ragel
								
								1.3%