# tdewolff/parse architecture

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

- Interactive diagram: https://gitdiagram.com/tdewolff/parse
- Repository: https://github.com/tdewolff/parse

## Overview

##### Main Components
- Buffering & Lexing Core: This is the foundation of the library and resides in the “buffer” package. It includes key components such as:
- Buffer.Reader: A custom byte reader similar to bytes.Reader.
- Buffer.Writer: A growing buffer for writing output that supports reset functionality.
- Buffer.Lexer & StreamLexer: The actual lexing mechanisms that tokenize the input based on start/end positions, peek functions, shifting, and buffer management.
- Language/Format Specific Modules: Each subpackage (css, html, js, json, xml) encapsulates a language parser or lexer:
- CSS Parser/Lexer: Implements CSS3 lexing and parsing following the CSS Syntax Module Level 3.
- HTML Lexer: Implements the HTML5 lexing process.
- JS Lexer: Implements ECMAScript (edition 6) lexing.
- JSON Parser: Implements JSON (per ECMA-404) parsing.
- XML Lexer: Implements XML1.0 lexing.
- Utility Modules:
- Strconv: Custom conversion functions optimized for performance needs compared to the standard library.
- Common utilities and error handling (files like common.go, error.go) that support multiple parts of the library.
- Tests: A comprehensive “tests” folder for corpus-based test cases organized by lexing or parsing roundtrips for each supported language or feature.

##### Relationships and Interactions
- Data Flow: External clients (or command-line tools indicated by files like binary.go) provide an input stream (io.Reader) that is first passed to the Buffer’s Reader.
- Lexical Analysis: The Buffer’s Lexer (or StreamLexer) reads the input stream in a buffered manner, managing positions and token boundaries.
- Language-Specific Modules: The lexers built on top of the buffering layer tokenize the input according to formal language specifications and then optionally feed these tokens into a parser layer (for CSS/JSON) to build higher-level representations.
- Utilities: The Strconv package and common utilities are used by the language modules to handle specialized tasks (such as high-speed numeric/string conversions used within, say, the minification process).

##### Architectural Patterns
- Layered Architecture: The lower layer (buffer package) is reused across all higher-level modules, showing a clear separation of concerns.
- Streaming Design: Every lexer is implemented to work in a streaming mode which highlights efficient memory and IO handling.
- Modularity: Each language-specific package (css, html, js, json, xml) is a self-contained module that adheres to its own specification yet leverages a shared lower-level infrastructure.

##### Relevant Technologies and Design Choices
- Go Language: Indicating the use of Go’s concurrency or memory management practices where applicable.
- I/O Abstraction: The use of io.Reader and io.Writer interfaces for compatibility, extensibility, and testability.
- Buffer Pooling: The use of buffer pools within the StreamLexer to efficiently handle varying input sizes.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input Source
    Input["Input (io.Reader)"]:::input

    %% Buffering & Lexing Core Subgraph
    subgraph "Buffering & Lexing Core"
        BufferReader["Buffer.Reader"]:::core
        BufferWriter["Buffer.Writer"]:::core
        BufferLexer["Buffer.Lexer"]:::core
        StreamLexer["Buffer.StreamLexer"]:::core
    end

    %% Language/Format Specific Modules Subgraph
    subgraph "Language/Format Specific Modules"
        CSS["CSS Parser/Lexer"]:::lang
        HTML["HTML Lexer"]:::lang
        JS["JS Lexer/Parser"]:::lang
        JSON["JSON Parser"]:::lang
        XML["XML Lexer"]:::lang
    end

    %% Utility Modules Subgraph
    subgraph "Utility Modules"
        Strconv["Strconv Utility Module"]:::util
        COMMON["Common Utilities & Error Handling"]:::util
    end

    %% Tests Subgraph
    subgraph "Tests"
        Test["Lexing/Parsing Tests"]:::test
    end

    %% Connections - Data Flow
    Input -->|"feeds"| BufferReader
    BufferReader -->|"buffers"| BufferLexer
    BufferWriter --- BufferLexer
    BufferLexer -->|"streams"| StreamLexer

    %% Connections - Language Modules depend on Buffer Core
    BufferLexer -->|"tokenizes"| CSS
    BufferLexer -->|"tokenizes"| HTML
    BufferLexer -->|"tokenizes"| JS
    BufferLexer -->|"tokenizes"| JSON
    BufferLexer -->|"tokenizes"| XML

    %% Connections - Utility Dependencies
    CSS -->|"uses"| Strconv
    HTML -->|"uses"| Strconv
    JS -->|"uses"| Strconv
    JSON -->|"uses"| Strconv
    XML -->|"uses"| Strconv

    CSS -->|"handles"| COMMON
    HTML -->|"handles"| COMMON
    JS -->|"handles"| COMMON
    JSON -->|"handles"| COMMON
    XML -->|"handles"| COMMON

    %% Connections - Test Validation
    Test -->|"validates"| BufferLexer
    Test -->|"validates"| CSS
    Test -->|"validates"| HTML
    Test -->|"validates"| JS
    Test -->|"validates"| JSON
    Test -->|"validates"| XML

    %% Click Events for Buffering & Lexing Core
    click BufferReader "https://github.com/tdewolff/parse/blob/master/buffer/reader.go"
    click BufferWriter "https://github.com/tdewolff/parse/blob/master/buffer/writer.go"
    click BufferLexer "https://github.com/tdewolff/parse/blob/master/buffer/lexer.go"
    click StreamLexer "https://github.com/tdewolff/parse/blob/master/buffer/streamlexer.go"

    %% Click Events for Language/Format Modules
    click CSS "https://github.com/tdewolff/parse/blob/master/css/lex.go"
    click HTML "https://github.com/tdewolff/parse/blob/master/html/lex.go"
    click JS "https://github.com/tdewolff/parse/blob/master/js/lex.go"
    click JSON "https://github.com/tdewolff/parse/blob/master/json/parse.go"
    click XML "https://github.com/tdewolff/parse/blob/master/xml/lex.go"

    %% Click Events for Utility Modules
    click Strconv "https://github.com/tdewolff/parse/blob/master/strconv/decimal.go"
    click COMMON "https://github.com/tdewolff/parse/blob/master/common.go"

    %% Click Events for Tests
    click Test "https://github.com/tdewolff/parse/tree/master/tests"

    %% Style Definitions
    classDef core fill:#FFD700,stroke:#B8860B,stroke-width:2px;
    classDef lang fill:#ADD8E6,stroke:#00008B,stroke-width:2px;
    classDef util fill:#FFB6C1,stroke:#C71585,stroke-width:2px;
    classDef test fill:#98FB98,stroke:#2E8B57,stroke-width:2px;
    classDef input fill:#E0FFFF,stroke:#20B2AA,stroke-width:2px;
```
