Go to file
2021-08-09 15:26:26 +05: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: fixed a bug with # comments (#22) 2021-08-02 12:37:08 +03:00
pkg php8.1: added first class callable syntax (#18) 2021-07-31 23:50:59 +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
CHANGELOG.md changelog: added v0.8.1-rc.1 (#23) 2021-08-09 15:26:26 +05: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 readme: handle parse errors in example (#20) 2021-08-01 22:00:01 +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())
	}
	
	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
--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.