# mck89/rebuilder architecture

> Architecture diagram of the GitHub repository mck89/rebuilder, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mck89/rebuilder
- Repository: https://github.com/mck89/rebuilder

## Overview

##### Project type and purpose
- This is an open‐source PHP library (REBuilder) whose sole purpose is to parse and build regular expressions via an object‐oriented API. It exposes two primary flows:
- Parsing a regex string into an in‐memory AST (Abstract Syntax Tree).
- Building a regex string by composing AST nodes via a fluent PHP interface.

##### File structure
- Top‐level files:
- composer.json → declares PSR‐4 autoloading and the library dependency.
- README.md → usage examples for parse() and create().
- phpunit.xml → config for the test suite.
- lib/REBuilder/Exception → all custom exception classes raised during parsing or building.
- lib/REBuilder/Parser →
- Tokenizer.php → breaks a regex string into tokens.
- Token.php → token data structure.
- Rules.php → grammar definitions.
- Parser.php → consumes tokens, builds AST.
- Builder.php → auxiliary helper to assemble AST nodes during parse.
- lib/REBuilder/Pattern →
- Abstract base classes (AbstractPattern, AbstractContainer, etc.).
- Concrete node types (Char, CharClass, AlternationGroup, Repetition/*, Assertion, etc.).
- Pattern\Regex → top‐level root node holding delimiter info and child nodes.
- lib/REBuilder/REBuilder.php → façade giving static parse() and create() entry points.
- test/REBuilder → PHPUnit tests organized by Pattern and subdirectories, covering parsing and rendering.

##### Key architecture insights from README
- Two public API calls: parse() → returns a Pattern\Regex AST. create() → returns an empty Pattern\Regex builder.
- AST nodes implement render() to output the final regex string.
- Fluent interface for building (addCharAndContinue(), addGenericCharType(), setRepetition(), getParent(), etc.).

##### Elements
- Blue for core modules (Parser, Builder, Pattern)
- Red for Exception classes
- Green for external integration (Composer Autoloader, Client Code)
- Gray for tests

##### Detailed but not over‐engineered
- Layer (Facade → Parser → AST → Renderer)
- AST node hierarchy abstractly (e.g., “Pattern\*” composite) rather than listing every class
- Pathways (parse → AST, create → AST → render)
- Exception flows back to the client

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client Code"]:::external
    Facade["REBuilder Facade"]:::core

    subgraph ParserModule["Parser Module"]
        Tokenizer["Tokenizer"]:::core
        Token["Token"]:::core
        Rules["Rules"]:::core
        ParserNode["Parser"]:::core
        ParserBuilder["Parser.Builder"]:::core
    end

    subgraph PatternAST["Pattern AST Package"]
        subgraph Abstracts["Abstract Base Classes"]
            AP["AbstractPattern"]:::core
            AC["AbstractContainer"]:::core
            ACh["AbstractChar"]:::core
            AI["AbstractIdentifier"]:::core
        end
        subgraph Concrete["Concrete Node Types"]
            CChar["Char"]:::core
            CClass["CharClass"]:::core
            Alt["Alternation"]:::core
            AG["AlternationGroup"]:::core
            Asrt["Assertion"]:::core
            BRef["BackReference"]:::core
            Dot["Dot"]:::core
            GCT["GenericCharType"]:::core
            SubP["SubPattern"]:::core
            RP["RecursivePattern"]:::core
            RegexNode["Regex"]:::core
        end
    end

    subgraph ExceptionModule["Exception Module"]
        EEmpty["EmptyRegex"]:::exception
        EGen["Generic"]:::exception
        EDelim["InvalidDelimiter"]:::exception
        EMod["InvalidModifier"]:::exception
        ERep["InvalidRepetition"]:::exception
    end

    Autoloader["Composer Autoloader"]:::integration

    subgraph Tests["PHPUnit Test Suite"]
        TestsPattern["test/REBuilder/Pattern"]:::tests
        TestsRep["test/REBuilder/Pattern/Repetition"]:::tests
        PHPUnitConfig["phpunit.xml"]:::tests
    end

    Docs["README.md"]:::docs

    Client -->|"parse()"| Facade
    Client -->|"create()"| Facade
    Facade -->|"calls parse"| ParserModule
    Facade -->|"returns Regex AST"| Client
    Facade -->|"calls create AST"| PatternAST

    ParserModule --> Tokenizer
    Tokenizer --> Token
    ParserModule --> Rules
    ParserModule --> ParserNode
    ParserNode -->|"uses Builder"| ParserBuilder
    ParserNode --> PatternAST
    ParserBuilder --> PatternAST

    PatternAST -->|"render()"| Client

    ParserModule --> ExceptionModule
    PatternAST --> ExceptionModule

    Autoloader --> Facade
    Autoloader --> ParserModule
    Autoloader --> PatternAST
    Autoloader --> ExceptionModule
    Autoloader --> Tests

    Tests --> ParserModule
    Tests --> PatternAST
    Tests --> ExceptionModule
    Tests --> Facade

    Docs --> Client

    click Facade "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/REBuilder.php"
    click Tokenizer "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Parser/Tokenizer.php"
    click Token "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Parser/Token.php"
    click Rules "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Parser/Rules.php"
    click ParserNode "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Parser/Parser.php"
    click ParserBuilder "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Parser/Builder.php"
    click AP "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/AbstractPattern.php"
    click AC "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/AbstractContainer.php"
    click ACh "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/AbstractChar.php"
    click AI "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/AbstractIdentifier.php"
    click CChar "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/Char.php"
    click CClass "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/CharClass.php"
    click Alt "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/Alternation.php"
    click AG "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/AlternationGroup.php"
    click Asrt "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/Assertion.php"
    click BRef "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/BackReference.php"
    click Dot "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/Dot.php"
    click GCT "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/GenericCharType.php"
    click SubP "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/SubPattern.php"
    click RP "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/RecursivePattern.php"
    click RegexNode "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Pattern/Regex.php"
    click EEmpty "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Exception/EmptyRegex.php"
    click EGen "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Exception/Generic.php"
    click EDelim "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Exception/InvalidDelimiter.php"
    click EMod "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Exception/InvalidModifier.php"
    click ERep "https://github.com/mck89/rebuilder/blob/master/lib/REBuilder/Exception/InvalidRepetition.php"
    click Autoloader "https://github.com/mck89/rebuilder/blob/master/composer.json"
    click TestsPattern "https://github.com/mck89/rebuilder/tree/master/test/REBuilder/Pattern"
    click TestsRep "https://github.com/mck89/rebuilder/tree/master/test/REBuilder/Pattern/Repetition"
    click PHPUnitConfig "https://github.com/mck89/rebuilder/blob/master/phpunit.xml"
    click Docs "https://github.com/mck89/rebuilder/blob/master/README.md"

    classDef core fill:#BBDEFB,stroke:#0277BD,color:#000
    classDef exception fill:#FFCDD2,stroke:#C62828,color:#000
    classDef integration fill:#C8E6C9,stroke:#2E7D32,color:#000
    classDef tests fill:#E0E0E0,stroke:#757575,color:#000
    classDef external fill:#C8E6C9,stroke:#2E7D32,color:#000
    classDef docs fill:#FFF9C4,stroke:#F9A825,color:#000
```
