php-parser

module
v0.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2022 License: MIT

README

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

Directories

Path Synopsis
cmd
internal
php8
line internal/php8/php8.y:2 line internal/php8/scanner.rl:1
line internal/php8/php8.y:2 line internal/php8/scanner.rl:1
scanner
line internal/scanner/scanner.rl:1
line internal/scanner/scanner.rl:1
pkg
ast
parser
A Parser for PHP written in Go Package 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(`<? echo "Hello world";`) // Error handler var parserErrors []*errors.Error errorHandler := func(e *errors.Error) { parsmakeerErrors = append(parserErrors, e) } // Parse rootNode, err := parser.Parse(src, conf.Config{ Version: &version.Version{Major: 5, Minor: 6}, ErrorHandlerFunc: errorHandler, }) if err != nil { log.Fatal("Error:" + err.Error()) } // Dump goDumper := dumper.NewDumper(os.Stdout).
A Parser for PHP written in Go Package 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(`<? echo "Hello world";`) // Error handler var parserErrors []*errors.Error errorHandler := func(e *errors.Error) { parsmakeerErrors = append(parserErrors, e) } // Parse rootNode, err := parser.Parse(src, conf.Config{ Version: &version.Version{Major: 5, Minor: 6}, ErrorHandlerFunc: errorHandler, }) if err != nil { log.Fatal("Error:" + err.Error()) } // Dump goDumper := dumper.NewDumper(os.Stdout).
visitor/nsresolver
Package visitor contains walker.visitor implementations
Package visitor contains walker.visitor implementations

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL