Git Product home page Git Product logo

php-parser's Introduction

A parser for PHP written in Go

Go Report Card Exago Exago GoDoc

Try it online: demo

Features:

  • Fully support PHP5 and PHP7 syntax
  • Abstract syntax tree representation
  • Traversing AST
  • Namespace resolver

Install

go get github.com/z7zmey/php-parser

Example

package main

import (
	"bytes"

	"github.com/z7zmey/php-parser/php7"
	"github.com/z7zmey/php-parser/visitor"
)

func main() {
	src := bytes.NewBufferString(`<? echo "Hello world";`)
	nodes, comments, positions := php7.Parse(src, "example.php")

	visitor := visitor.Dumper{
		Indent:    "",
		Comments:  comments,
		Positions: positions,
	}
	nodes.Walk(visitor)
}

CLI dumper

$GOPATH/bin/php-parser /path/to/file/or/dir

Namespace resolver

Namespace resolver is a visitor that traverses nodes and resolves nodes fully qualified name. It does not change AST but collects resolved names into map[node.Node]string

  • For Class, Interface, Trait, Function, ConstList nodes collects name with current namespace.
  • For Name, Relative, FullyQualified nodes resolves use aliases and collects a fully qualified name.

Pretty printer

nodes := &stmt.StmtList{
	Stmts: []node.Node{
		&stmt.Namespace{
			NamespaceName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Foo"}}},
		},
		&stmt.Class{
			Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
			ClassName: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}},
			Extends: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}},
			Stmts: []node.Node{
				&stmt.ClassMethod{
					Modifiers:  []node.Node{&node.Identifier{Value: "public"}},
					MethodName: &node.Identifier{Value: "greet"},
					Stmts: []node.Node{
						&stmt.Echo{
							Exprs: []node.Node{
								&scalar.String{Value: "'Hello world'"},
							},
						},
					},
				},
			},
		},
	},
}

file := os.Stdout
p := printer.NewPrinter(file, "    ")
p.PrintFile(nodes)

Output:

<?php
namespace Foo;
abstract class Bar extends Baz
{
    public function greet()
    {
        echo 'Hello world';
    }
}

Roadmap

  • Lexer
  • PHP 7 syntax analyzer
  • AST nodes
  • AST visitor
  • AST dumper
  • node position
  • handling comments
  • PHP 5 syntax analyzer
  • Tests
  • Namespace resolver
  • Pretty printer
  • PhpDocComment parser
  • Error handling
  • Stabilize api
  • Documentation
  • Code flow graph

php-parser's People

Contributors

z7zmey avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.