# tasuku43/puml-parser-php architecture

> Architecture diagram of the GitHub repository tasuku43/puml-parser-php, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tasuku43/puml-parser-php
- Repository: https://github.com/tasuku43/puml-parser-php

## Overview

##### Project Type and Purpose
- From the README, we see clear usage examples where a sample PlantUML file is tokenized, parsed, and then transformed to either JSON, array, or DTO representations.

##### File Structure
- Top-Level Files: The repository includes standard files (LICENSE, README.md, composer.json, sample files, etc.).
- The src directory is further split into several key subdirectories:
a. Dto – Contains definitions like Definition.php and PropertyDefinition.php; these are used to encapsulate the parsed data.
c. Node – Contains classes representing nodes (such as classes, enums, interfaces) in the AST.
d. Parser – Contains the Parser class and related exception classes; it works with the Lexer to build the AST.
- The tests directory includes unit tests for both Lexer and Parser components.

##### Key Architectural Components
a. Input Component:
- The PlantUML source file (like sample.puml) is the initial input.

b. Lexer Component:
- The Lexer class, which receives input from a PlantUML file.
- The specialized PumlTokenizer and its different Tokenizers (such as ArrowTokenizer, CurlyBracketTokenizer, ElementTokenizer, etc.) which emit tokens.

c. Parser Component:
- The Parser takes the stream of tokens produced by the Lexer.
- This component is responsible for processing tokens to build a coherent AST representing class definitions.

d. AST / Node Component:
- The Node directory that contains representations for the parsed structures (Class, Interface, Enum, etc.).

e. DTO Component:
- The Dto directory where the parsed AST is converted to DTO objects (e.g., Definition, PropertyDefinition) that encapsulate details like name, package, properties, and relationships.

f. Output Format Components:
- Mechanisms or adapters to convert the AST/DTOs to JSON, array, or used directly in PHP.

g. Integration & Testing:
- A note for the tests component which validates Lexer and Parser behavior.

##### Relationships and Data Flow
- The Lexer (along with its Tokenizers) processes the input to produce a stream or collection of tokens.
- The Parser takes these tokens to build an AST composed of Node objects.
- The AST is then converted to DTO objects for further processing (like converting to JSON or an array).
- Interactions such as inheritance (e.g., LeftArrowTokenizer extends ArrowTokenizer) and interface implementations (e.g., classes or traits implementing Tokenizeable).

##### Important Architectural Patterns and Design Principles
- Separation of concerns: Lexer, Parser, and DTO/AST are clearly separated.
- Modular design: The extensive sub-directories within Lexer and Token indicate modularization based on UML syntax elements.
- Extensible design: The design supports different output formats (json, array, Dto) and easy extension for new token types if PlantUML syntax evolves.
- Error handling: There are dedicated exception classes in both the Lexer and Parser components for robust design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Input"
        PUML["PlantUML Input"]:::input
    end

    subgraph "Lexer Component"
        LEXER["Lexer"]:::component
        P_TOKENIZER["Puml Tokenizer"]:::component
        ARROW["Arrow Tokenizer"]:::component
        CURLY["CurlyBracket Tokenizer"]:::component
        ELEMENT["Element Tokenizer"]:::component
    end

    subgraph "Parser Component"
        PARSER["Parser"]:::component
        PARSEREXC["ParserException"]:::component
    end

    subgraph "AST / Node Component"
        NODE["AST/Node"]:::component
    end

    subgraph "DTO Component"
        DTO["DTO Converter"]:::component
    end

    subgraph "Client Code"
        USAGE["Sample Usage"]:::component
    end

    subgraph "Integration & Testing"
        LTEST["Lexer Tests"]:::test
        PTEST["Parser Tests"]:::test
    end

    %% Data Flow
    PUML -->|"read"| LEXER
    LEXER -->|"invokes"| P_TOKENIZER
    P_TOKENIZER -->|"delegates"| ARROW
    P_TOKENIZER -->|"delegates"| CURLY
    P_TOKENIZER -->|"delegates"| ELEMENT
    LEXER -->|"produces tokens for"| PARSER
    PARSER -->|"builds AST"| NODE
    PARSER -.->|"throws"| PARSEREXC
    NODE -->|"converts to"| DTO
    DTO -->|"outputs format to"| USAGE

    %% Testing connections (dotted links)
    LEXER -.->|"tested by"| LTEST
    PARSER -.->|"tested by"| PTEST

    %% Click Events
    click PUML "https://github.com/tasuku43/puml-parser-php/blob/main/sample.puml"
    click LEXER "https://github.com/tasuku43/puml-parser-php/blob/main/src/Lexer/Lexer.php"
    click P_TOKENIZER "https://github.com/tasuku43/puml-parser-php/blob/main/src/Lexer/PumlTokenizer.php"
    click ARROW "https://github.com/tasuku43/puml-parser-php/blob/main/src/Lexer/Token/Arrow/ArrowTokenizer.php"
    click CURLY "https://github.com/tasuku43/puml-parser-php/blob/main/src/Lexer/Token/CurlyBracket/CurlyBracketTokenizer.php"
    click ELEMENT "https://github.com/tasuku43/puml-parser-php/blob/main/src/Lexer/Token/Element/ElementTokenizer.php"
    click PARSER "https://github.com/tasuku43/puml-parser-php/blob/main/src/Parser/Parser.php"
    click PARSEREXC "https://github.com/tasuku43/puml-parser-php/blob/main/src/Parser/Exception/ParserException.php"
    click NODE "https://github.com/tasuku43/puml-parser-php/tree/main/src/Node"
    click DTO "https://github.com/tasuku43/puml-parser-php/tree/main/src/Dto"
    click USAGE "https://github.com/tasuku43/puml-parser-php/blob/main/sample.php"
    click LTEST "https://github.com/tasuku43/puml-parser-php/blob/main/tests/Lexer/LexerTest.php"
    click PTEST "https://github.com/tasuku43/puml-parser-php/blob/main/tests/Parser/ParserTest.php"

    %% Styles
    classDef input fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef component fill:#ccffcc,stroke:#006600,stroke-width:2px;
    classDef test fill:#ccccff,stroke:#0000cc,stroke-width:2px;
```
