Go to file
2021-07-31 18:06:46 +03:00
cmd/php-parser cnd: default version has been changed to 8.0 2021-07-30 21:03:47 +03:00
internal php8.1: added never type (#8) 2021-07-31 18:06:46 +03:00
pkg php8.1: added never type (#8) 2021-07-31 18:06:46 +03: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 all: renamed github.com/z7zmey/php-parser with github.com/VKCOM/php-parser 2021-07-30 21:01:34 +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 PHP 8 (#1) 2021-07-30 20:53:27 +03:00
parser.jpg #5 update logo 2018-05-16 09:08:16 +03:00
README.md readme: use correct conf pkg instead of cfg for config (#4) 2021-07-31 13:19:33 +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 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())
	}

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