Go to file
2021-02-13 22:16:54 +02:00
cmd/php-parser refactoring: update api 2020-12-29 21:23:22 +02:00
internal internal: fixed parsing of anonymous classes 2021-02-07 07:52:29 +03:00
pkg [#120] move dog.go from root folder 2021-02-13 22:06:52 +02:00
.gitattributes [refactoring] add .gitattributes to mark autogenerated files 2020-07-03 00:42:16 +03:00
.gitignore init modules 2020-03-09 11:34:11 +02:00
.travis.yml update .travis.yml 2020-03-09 13:55:35 +02:00
CODE_OF_CONDUCT.md update CODE_OF_CONDUCT.md 2018-01-05 19:49:29 +02:00
CONTRIBUTING.md create CONTRIBUTING.md 2018-01-05 19:37:08 +02:00
go.mod [refactoring] fix tests 2020-05-17 23:16:01 +03:00
go.sum [refactoring] fix tests 2020-05-17 23:16:01 +03:00
ISSUE_TEMPLATE.md Update ISSUE_TEMPLATE.md 2018-07-09 21:19:33 +03:00
LICENSE Create LICENSE 2018-01-02 14:37:19 +02:00
Makefile [refactoring] update position builder 2020-12-08 02:08:59 +02:00
parser.jpg #5 update logo 2018-05-16 09:08:16 +03:00
README.md [#120] update readme 2021-02-13 22:16:54 +02:00

PHP Parser written in Go

PHP Parser written in Go

GoDoc Build Status Go Report Card Maintainability Test Coverage

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.

Try it online: demo

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 whitespaces

Who Uses

VKCOM/noverify - NoVerify is a pretty fast linter for PHP

quasilyte/phpgrep - phpgrep is a 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(`<? 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)
  • PHP8

Install

go get github.com/z7zmey/php-parser/cmd/php-parser

CLI

php-parser [flags] <path> ...
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.