How mck89/rebuilder works
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