Skip to content

buildwithhp/gophpparser

Repository files navigation

PHP Parser to JSON

A comprehensive Go-based PHP code parser that converts PHP source code into JSON format Abstract Syntax Tree (AST). This parser supports modern PHP syntax including classes, namespaces, exception handling, and advanced language features.

Features

Core Language Support

  • ✅ Variables ($var) and all literal types (string, int, float, boolean, null)
  • ✅ Functions (declarations, calls, parameters, return statements)
  • ✅ Control structures (if/else/elseif, for, while, foreach)
  • ✅ Break and continue statements with optional levels
  • ✅ All operators (arithmetic, comparison, logical, assignment, increment/decrement)
  • ✅ String operations and basic interpolation
  • ✅ Echo and print statements

Advanced Arrays

  • ✅ Indexed arrays ([1, 2, 3])
  • ✅ Associative arrays (["key" => "value"])
  • ✅ Multi-dimensional arrays
  • ✅ Array access ($arr[0], $arr["key"])

Object-Oriented Programming

  • ✅ Class declarations with properties and methods
  • ✅ Visibility modifiers (public, private, protected)
  • ✅ Static members and methods
  • ✅ Object instantiation (new ClassName())
  • ✅ Object property/method access ($obj->property, $obj->method())
  • ✅ Static access (Class::method(), Class::$property)
  • ✅ Class inheritance (extends)
  • ✅ Method and property declarations

Advanced Features

  • ✅ Namespaces (namespace App\Controllers;)
  • ✅ Use statements (use Models\User;, use Models\User as UserModel;)
  • ✅ Exception handling (try/catch/finally blocks)
  • ✅ Throw statements
  • ✅ Anonymous functions/closures with use clauses
  • ✅ Generator functions with yield expressions
  • ✅ Comprehensive comment handling (// and /* */)

Installation

go build -o php-parser

Usage

Parse a PHP file and output JSON:

./php-parser test.php

Show help:

./php-parser --help

Example

Given this PHP code (test.php):

<?php
namespace App\Services;

use Models\User;

class UserService {
    private $users = [];
    
    public function __construct() {
        $this->users = [];
    }
    
    public function processUser($name) {
        try {
            $user = new User($name);
            $callback = function($data) use ($user) {
                yield $user->id => $data;
            };
            return $callback;
        } catch (Exception $e) {
            throw new ServiceException("Processing failed");
        } finally {
            $this->cleanup();
        }
    }
    
    private function cleanup() {
        // Cleanup logic
    }
}

$service = new UserService();
$processor = $service->processUser("John");
?>

The parser outputs a comprehensive JSON AST including:

  • Namespace declarations
  • Use statements
  • Class definitions with methods and properties
  • Object instantiation and method calls
  • Exception handling blocks
  • Anonymous functions with closures
  • Generator expressions with yield

Testing

Run the comprehensive test suite (150+ tests):

go test -v

Test specific features:

go test -run TestParseClass
go test -run TestParseNamespace  
go test -run TestParseTryStatement

Architecture

  • Lexer (lexer.go): Tokenizes PHP source code with 50+ token types
  • Parser (parser.go): Builds AST using recursive descent parsing with Pratt parsing for expressions
  • AST (ast.go): Defines 25+ node structures and complete JSON serialization
  • Token (token.go): Comprehensive token definitions and PHP keyword mapping
  • Error (error.go): Robust error handling and reporting

Current Capabilities

This parser successfully handles:

  • Professional PHP codebases with modern syntax
  • Framework code (Laravel, Symfony patterns)
  • Object-oriented applications with complex inheritance
  • Namespace organization and autoloading patterns
  • Exception handling and error management
  • Advanced functions including generators and closures
  • Real-world PHP applications with comprehensive feature coverage

Latest Production-Ready Features ✨

Modern PHP Language Support

  • Interfaces - Full interface declaration and implementation support
  • Traits - Trait declaration and usage with use statements
  • Constants - Class constants with visibility modifiers
  • Modern operators - Null coalescing (??), nullsafe (?->), assignment operators
  • Advanced tokens - Comprehensive token support for PHP 8+ syntax

PHP 8+ Features (In Progress)

  • 🚧 Match expressions - Pattern matching for complex conditionals
  • 🚧 Union and intersection types - Advanced type declarations
  • 🚧 Attributes/annotations - Metadata support
  • 🚧 Named arguments - Function call improvements
  • 🚧 Constructor property promotion - Simplified property declaration

Remaining Enhancements

  • Abstract classes and methods
  • Magic methods (__construct, __destruct, etc.)
  • Complex string interpolation ("${expression}")
  • Include/require statements with path resolution
  • Global variables and superglobals
  • Variable variables ($$var)
  • Reference operators (&$var)
  • Arrow functions (fn() =>)

File System & Advanced Features

  • Include/require with dependency resolution
  • Multi-file namespace parsing
  • PHP configuration parsing (ini files)
  • Built-in function definitions

The current implementation provides a solid foundation for these enhancements and covers 80%+ of typical PHP application syntax.

License

MIT License

About

PHP code to JSON parser

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors