A PHP parser written in Go. It lexes and parses PHP 5, 7 and 8 source into an AST that downstream tools (linters, refactoring, metrics, formatters) can traverse.
go get github.com/rectorphp/php-parser-in-go
package main import ( "fmt" "github.com/rectorphp/php-parser-in-go/pkg/conf" "github.com/rectorphp/php-parser-in-go/pkg/parser" "github.com/rectorphp/php-parser-in-go/pkg/version" "github.com/rectorphp/php-parser-in-go/pkg/visitor/dumper" ) func main() { phpVersion, _ := version.New("8.3") root, err := parser.Parse([]byte("<?php echo 'hi';"), conf.Config{ Version: phpVersion, }) if err != nil { panic(err) } // walk / print / dump the AST fmt.Print(dumper.Dump(root)) }
pkg/parser—parser.Parse(source, conf.Config{...})entry point.pkg/ast— AST node definitions.pkg/visitor— traverser, printer, dumper, namespace and class resolvers, formatter.pkg/token,pkg/position,pkg/version,pkg/errors,pkg/conf.
benchmark/ parses every .php file under a directory and reports the wall-clock time. Point it at any corpus:
make bench-corpus DIR=./laravel
# parsed 2966 files in 1215 msCI runs it against a full Laravel framework checkout (src/ + Composer vendor/) on every push and every 12 hours, reporting the average over 5 runs and peak memory in the run's Summary.
internal/*/php*.go and internal/*/scanner.go are generated. Edit the grammar source instead:
Run make build to regenerate and build (both goyacc and ragel must be installed). make test runs the tests.
Based on z7zmey/php-parser, the original PHP parser written in Go.