Go to file
2023-03-26 01:33:03 +01:00
.github .github: setup GitHub related things (#13) 2021-07-31 20:01:44 +03:00
cmd/php-parser style: simplify comparison 2023-03-25 15:16:54 +01:00
internal test: add php8.2 constants in traits test 2023-03-25 16:11:37 +01:00
pkg fix: use go:build tools instead of ignore so go mod tidy doesn't remove deps 2023-03-26 01:33:03 +01:00
.editorconfig ci/style: add editorconfig and golangci config 2023-03-25 15:15:54 +01: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
.golangci.yml ci/style: add editorconfig and golangci config 2023-03-25 15:15:54 +01:00
CHANGELOG.md changelog: added v0.8.2 (#30) 2022-06-26 03:40:01 +03: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 feat: add enter and leave checks for traversers 2023-03-25 22:03:02 +01:00
go.sum feat: add enter and leave checks for traversers 2023-03-25 22:03:02 +01:00
LICENSE Create LICENSE 2018-01-02 14:37:19 +02:00
Makefile refactor: change makefile to reflect drop of php5 and run make 2023-03-25 15:29:51 +01:00
parser.jpg #5 update logo 2018-05-16 09:08:16 +03:00
README.md cleanup 2022-06-26 03:42:25 +03:00

This is a fork of the z7zmey parser that adds PHP 8 support.

PHP Parser written in Go

PHP Parser written in Go

GoDoc Build Status Go Report Card

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-8.2 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

Usage example

package main

import (
	"log"
	"os"

	"github.com/VKCOM/php-parser/pkg/conf"
	"github.com/VKCOM/php-parser/pkg/errors"
	"github.com/VKCOM/php-parser/pkg/parser"
	"github.com/VKCOM/php-parser/pkg/version"
	"github.com/VKCOM/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, conf.Config{
		Version:          &version.Version{Major: 8, Minor: 0},
		ErrorHandlerFunc: errorHandler,
	})

	if err != nil {
		log.Fatal("Error:" + err.Error())
	}
	
	if len(parserErrors) > 0 {
		for _, e := range parserErrors {
			log.Println(e.String())
		}
		os.Exit(1)
	}

	// Dump

	goDumper := dumper.NewDumper(os.Stdout).
		WithTokens().
		WithPositions()

	rootNode.Accept(goDumper)
}

Install

go get github.com/VKCOM/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
--pb bool Print AST back into the parsed file
--time bool Print execution time
--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, Constant nodes it saves name with current namespace.
  • For Name, Relative, FullyQualified nodes it resolves use aliases and saves a fully qualified name.