Go to file
Laytan Laats 86c10ca380 perf: reduce memory usage by reducing amt of position's created
The positions created were taking GB's of memory and were not being
GC'ed.
2022-09-09 00:40:11 +02:00
.github .github: setup GitHub related things (#13) 2021-07-31 20:01:44 +03:00
cmd/php-parser cmd: added file path output before errors 2021-07-31 19:45:26 +03:00
internal php8.1: added intersection types support (#29) 2022-06-26 03:31:29 +03:00
pkg perf: reduce memory usage by reducing amt of position's created 2022-09-09 00:40:11 +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
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 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
LICENSE Create LICENSE 2018-01-02 14:37:19 +02:00
Makefile .github: setup GitHub related things (#13) 2021-07-31 20:01:44 +03: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.