Go to file
Makhnev Petr 049ce7ddc6
PHP 8 (#1)
PHP 8 Update

- nullsafe operator (?->)
- Remove (real) cast
- Named arguments
- Remove (unset) cast
- Remove {} access
- match expression
- Union types in type hints and static typehint
- Block catch without variable
- Trailing comma in parameter lists
- throw can be used as an expression
- Concatenation precedence
- Declaring properties in the constructor
- Attributes
- Names in the namespace are treated as a single token
- Trailing comma in closure use list
- Check that ::class on object works
- Deferencable changes and arbitrary expressions in new/instanceof
2021-07-30 20:53:27 +03:00
cmd/php-parser rename package cfg to conf 2021-02-13 23:54:34 +02:00
internal PHP 8 (#1) 2021-07-30 20:53:27 +03:00
pkg PHP 8 (#1) 2021-07-30 20:53:27 +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 [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 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 PHP 8 (#1) 2021-07-30 20:53:27 +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/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(`<?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, cfg.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/z7zmey/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.