Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-parser-in-go

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.

Install

go get github.com/rectorphp/php-parser-in-go

Usage

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))
}

Packages

  • pkg/parserparser.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

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 ms

CI 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.

Generated code

internal/*/php*.go and internal/*/scanner.go are generated. Edit the grammar source instead:

  • *.y files are regenerated with goyacc.
  • *.rl files are regenerated with ragel.

Run make build to regenerate and build (both goyacc and ragel must be installed). make test runs the tests.

Credits

Based on z7zmey/php-parser, the original PHP parser written in Go.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages