# haskell/parsec architecture

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

- Interactive diagram: https://gitdiagram.com/haskell/parsec
- Repository: https://github.com/haskell/parsec

## Overview

##### Project Type and Purpose
- This is an open-source Haskell library (Parsec), not a full-stack application.
- It provides a monadic parser-combinator framework: core parsing primitives, combinators, error reporting and language/token support.
- The library is shipped via Cabal; it has CI (GitHub Actions) and a comprehensive test suite.

##### File Structure Analysis
- Top-level:
- Setup.hs, parsec.cabal, cabal.project           ← build and packaging
- .github/workflows/haskell-ci.yml                ← CI pipeline
- src/                                            ← library source
- test/                                           ← test suite
- src/Text/Parsec:
- Modules organized by concern:
– Prim, Combinator, Char, String, Text, ByteString, Expr, Language, Perm, Pos, Token, Error
- src/Text/ParserCombinators:
- Compatibility wrapper modules
- test/:
- Bugs.hs, Features.hs, issue*.hs, and per-bug/feature subdirectories

##### Key README Insights
- Parsec is designed from scratch for performance, safety, good error messages.
- It depends on packages: transformers, mtl, bytestring, text, deepseq, array.
- Encourages contributions via GitHub; uses Hackage for distribution.

a. Main Components
1. Core Parser Engine
– Modules: Text.Parsec.Prim, Text.Parsec.Combinator
2. Token and Language Support
– Text.Parsec.Token, Text.Parsec.Language, Expr, Perm, Pos
3. Input Abstractions
– Char, String, Text (strict and lazy), ByteString (strict and lazy)
4. Error Handling
– Text.Parsec.Error
5. Compatibility Layer
– src/Text/ParserCombinators modules
6. Build & Packaging
– parsec.cabal, Setup.hs, cabal.project
7. CI Pipeline
– .github/workflows/haskell-ci.yml
8. Test Suite
– test/Main.hs, Bugs, Features, issue tests

b. Relationships and Data Flow
- Users call high-level combinators (Parser type) defined in Prim & Combinator.
- Combinators build on primitives from Prim.
- Token and Language modules import core combinators to provide lexing/parsing helpers.
- Char/String/Text/ByteString modules supply input streams to the core engine.
- Error module is invoked by the core engine to generate parse errors.
- ParserCombinators modules wrap core Parsec API for backward compatibility.
- The test suite invokes public API modules and validates behavior.
- Build system pulls in external dependencies; CI runs build & tests on each push.

c. Architectural Patterns and Principles
- Functional, modular design: small focused modules.
- Monadic parser combinator pattern: composition over grammar specification.
- Separation of concerns: core engine vs. input abstractions vs. error reporting.
- Compatibility layer to isolate breaking changes.

d. Technologies and Frameworks
- Haskell, GHC compiler.
- Cabal build system.
- GitHub Actions for CI.
- Hackage for distribution.
- Dependencies: transformers, mtl, text, bytestring, deepseq, array.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Input Abstractions"
        Char["Text.Parsec.Char"]:::input
        String["Text.Parsec.String"]:::input
        TextS["Text.Parsec.Text"]:::input
        TextL["Text.Parsec.Text.Lazy"]:::input
        BS["Text.Parsec.ByteString"]:::input
        BSL["Text.Parsec.ByteString.Lazy"]:::input
    end

    subgraph "Core Parser Engine"
        Prim["Text.Parsec.Prim"]:::core
        Comb["Text.Parsec.Combinator"]:::core
    end

    subgraph "Error Handling"
        Err["Text.Parsec.Error"]:::error
    end

    subgraph "Token & Language Support"
        Token["Text.Parsec.Token"]:::helper
        Lang["Text.Parsec.Language"]:::helper
        Expr["Text.Parsec.Expr"]:::helper
        Perm["Text.Parsec.Perm"]:::helper
        Pos["Text.Parsec.Pos"]:::helper
    end

    subgraph "Compatibility Layer"
        CPar["ParserCombinators/Parsec"]:::compat
        CPrim["ParserCombinators/Parsec/Prim"]:::compat
        CComb["ParserCombinators/Parsec/Combinator"]:::compat
        CChar["ParserCombinators/Parsec/Char"]:::compat
        CErr["ParserCombinators/Parsec/Error"]:::compat
        CExpr["ParserCombinators/Parsec/Expr"]:::compat
        CLang["ParserCombinators/Parsec/Language"]:::compat
        CPerm["ParserCombinators/Parsec/Perm"]:::compat
        CPos["ParserCombinators/Parsec/Pos"]:::compat
        CToken["ParserCombinators/Parsec/Token"]:::compat
    end

    subgraph "Build & Packaging"
        Cabal["parsec.cabal"]:::build
        Setup["Setup.hs"]:::build
        Proj["cabal.project"]:::build
    end

    CI["CI Pipeline"]:::ci

    subgraph "Test Suite"
        TMain["test/Main.hs"]:::test
        TBugs["test/Bugs.hs"]:::test
        TFeat["test/Features.hs"]:::test
        TI127["test/issue127.hs"]:::test
        TI171["test/issue171.hs"]:::test
        TI175["test/issue175.hs"]:::test
        Bug2["test/Bugs/Bug2.hs"]:::test
        Bug6["test/Bugs/Bug6.hs"]:::test
        Bug9["test/Bugs/Bug9.hs"]:::test
        Bug35["test/Bugs/Bug35.hs"]:::test
        F80["test/Features/Feature80.hs"]:::test
        F150["test/Features/Feature150.hs"]:::test
        TUtil["test/Util.hs"]:::test
    end

    Char -->|feeds input| Prim
    String -->|feeds input| Prim
    TextS -->|feeds input| Prim
    TextL -->|feeds input| Prim
    BS -->|feeds input| Prim
    BSL -->|feeds input| Prim

    Prim -->|primitives to| Comb
    Comb -->|uses primitives| Prim

    Prim -->|invokes error reporting| Err
    Comb -->|invokes error reporting| Err

    Prim -->|provides API to| Token
    Prim -->|provides API to| Lang
    Prim -->|provides API to| Expr
    Prim -->|provides API to| Perm
    Prim -->|provides API to| Pos
    Comb -->|provides API to| Token
    Comb -->|provides API to| Lang
    Comb -->|provides API to| Expr
    Comb -->|provides API to| Perm
    Comb -->|provides API to| Pos

    CPar -->|wraps| Prim
    CPar -->|wraps| Comb
    CPar -->|wraps| Char
    CPar -->|wraps| Err
    CPar -->|wraps| Expr
    CPar -->|wraps| Lang
    CPar -->|wraps| Perm
    CPar -->|wraps| Pos
    CPar -->|wraps| Token

    CPrim -->|wraps| Prim
    CComb -->|wraps| Comb
    CChar -->|wraps| Char
    CErr -->|wraps| Err
    CExpr -->|wraps| Expr
    CLang -->|wraps| Lang
    CPerm -->|wraps| Perm
    CPos -->|wraps| Pos
    CToken -->|wraps| Token

    TMain -->|tests API| Prim
    TMain -->|tests API| Comb
    TMain -->|tests API| Token
    TMain -->|tests API| Lang
    TBugs -->|tests API| Prim
    TFeat -->|tests API| Comb

    CI -->|runs build & tests| Cabal
    CI -->|runs build & tests| Setup
    CI -->|runs build & tests| Proj
    CI -->|runs build & tests| TMain

    classDef core fill:#ADD8E6,stroke:#000;
    classDef input fill:#98FB98,stroke:#000;
    classDef helper fill:#FFA500,stroke:#000;
    classDef compat fill:#D3D3D3,stroke:#000;
    classDef build fill:#E0E0E0,stroke:#000;
    classDef ci fill:#F5F5F5,stroke:#000;
    classDef test fill:#FFFACD,stroke:#000;
    classDef error fill:#FFCCCB,stroke:#000;

    click Prim "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/Prim.hs"
    click Comb "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/Combinator.hs"
    click Token "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/Token.hs"
    click Lang "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/Language.hs"
    click Expr "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/Expr.hs"
    click Perm "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/Perm.hs"
    click Pos "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/Pos.hs"
    click Char "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/Char.hs"
    click String "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/String.hs"
    click TextS "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/Text.hs"
    click TextL "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/Text/Lazy.hs"
    click BS "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/ByteString.hs"
    click BSL "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/ByteString/Lazy.hs"
    click Err "https://github.com/haskell/parsec/blob/master/src/Text/Parsec/Error.hs"
    click CPar "https://github.com/haskell/parsec/blob/master/src/Text/ParserCombinators/Parsec.hs"
    click CPrim "https://github.com/haskell/parsec/blob/master/src/Text/ParserCombinators/Parsec/Prim.hs"
    click CComb "https://github.com/haskell/parsec/blob/master/src/Text/ParserCombinators/Parsec/Combinator.hs"
    click CChar "https://github.com/haskell/parsec/blob/master/src/Text/ParserCombinators/Parsec/Char.hs"
    click CErr "https://github.com/haskell/parsec/blob/master/src/Text/ParserCombinators/Parsec/Error.hs"
    click CExpr "https://github.com/haskell/parsec/blob/master/src/Text/ParserCombinators/Parsec/Expr.hs"
    click CLang "https://github.com/haskell/parsec/blob/master/src/Text/ParserCombinators/Parsec/Language.hs"
    click CPerm "https://github.com/haskell/parsec/blob/master/src/Text/ParserCombinators/Parsec/Perm.hs"
    click CPos "https://github.com/haskell/parsec/blob/master/src/Text/ParserCombinators/Parsec/Pos.hs"
    click CToken "https://github.com/haskell/parsec/blob/master/src/Text/ParserCombinators/Parsec/Token.hs"
    click Cabal "https://github.com/haskell/parsec/blob/master/parsec.cabal"
    click Setup "https://github.com/haskell/parsec/blob/master/Setup.hs"
    click Proj "https://github.com/haskell/parsec/blob/master/cabal.project"
    click CI "https://github.com/haskell/parsec/blob/master/.github/workflows/haskell-ci.yml"
    click TMain "https://github.com/haskell/parsec/blob/master/test/Main.hs"
    click TBugs "https://github.com/haskell/parsec/blob/master/test/Bugs.hs"
    click TFeat "https://github.com/haskell/parsec/blob/master/test/Features.hs"
    click TI127 "https://github.com/haskell/parsec/blob/master/test/issue127.hs"
    click TI171 "https://github.com/haskell/parsec/blob/master/test/issue171.hs"
    click TI175 "https://github.com/haskell/parsec/blob/master/test/issue175.hs"
    click Bug2 "https://github.com/haskell/parsec/blob/master/test/Bugs/Bug2.hs"
    click Bug6 "https://github.com/haskell/parsec/blob/master/test/Bugs/Bug6.hs"
    click Bug9 "https://github.com/haskell/parsec/blob/master/test/Bugs/Bug9.hs"
    click Bug35 "https://github.com/haskell/parsec/blob/master/test/Bugs/Bug35.hs"
    click F80 "https://github.com/haskell/parsec/blob/master/test/Features/Feature80.hs"
    click F150 "https://github.com/haskell/parsec/blob/master/test/Features/Feature150.hs"
    click TUtil "https://github.com/haskell/parsec/blob/master/test/Util.hs"
```
