# idni/parser architecture

> Architecture diagram of the GitHub repository idni/parser, 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/idni/parser
- Repository: https://github.com/idni/parser

## Overview

##### Main Components
- Core Parsing Engine (src): This includes the fundamental modules for defining grammars (e.g., “grammar”, “nonterminals”, “lit”, “prod”) and for performing the parsing process (e.g., “parser”, “parser::result”, “forest”, “traverser”).
- Grammar Construction and Representation: Classes/functions for programmatically constructing grammars, including Boolean, conjunctive, and TGF-related modules.
- TGF Tool (tools/tgf): A command-line interface component dedicated to reading a TGF file, debugging grammars, and generating C++ parser code from the specified grammar.
- CLI & REPL Components (within src such as cli.cpp/repl.h): Modules responsible for managing and processing command-line arguments and interactive sessions.
- Utilities & Helpers (docs/devhelpers.md, src/devhelpers.h): Helper code for forest transformations and additional debugging or transformation functionalities.
- Build & Test Infrastructure: Managed by CMake (CMakeLists.txt in several folders) and a collection of test applications and scripts (e.g., “tests”, “benchmark.sh”, “debug.sh”).
- Examples and Tutorials (examples): A set of usage examples aimed at demonstrating how to create parsers for various tasks (e.g., CSV parsers).

##### Relationships and Interactions
- The Central Core (Parsing Engine) interacts with the Grammar Construction modules to create a valid grammar structure. This design emphasizes templating and generic programming.
- The Parser module uses the grammar to perform scannerless parsing (i.e., both lexing and parsing without a dedicated lexer).
- The resulting parse forest is then manipulated using traversal and rewriting functions to transform ambiguous parses or to extract trees and graphs.
- The CLI components and the TGF tool serve as user interface layers that interface directly with the underlying parser API. They take input (grammar files or command-line input) and invoke the core engine to perform parsing or code generation.
- External scripts and CMake configuration files orchestrate the build process and potential integration/continuous integration pipelines.

##### Important Architectural Patterns and Design Principles
- Modularity: There is clear separation between the parser core, tools, tests, and examples. This reflects an intent to keep the core logic reusable.
- Templating and Generic Programming: Most modules are templated so that the parser can work with different character and terminal types (the type parameters “C” and “T”).
- Command-Line Interface Design: The “tgf” tool and CLI functions provide a front-end that makes the library accessible both programmatically and interactively.
- Build and Configuration Management: Usage of CMake provides a consistent build strategy across multiple platforms (including Windows with MinGW-w64, as seen in the cmake configurations).

##### Technologies, Frameworks, and Libraries
- C++ as the primary programming language.
- CMake for build configuration and dependency management.
- Shell scripts (e.g., build.sh, debug.sh) used for automation.
- No external dependencies are required, which simplifies integration.

## Diagram (Mermaid)

```mermaid
graph TD
    CLI["CLI & REPL (src/cli.cpp)"]:::ui
    TGFTool["TGF Tool (tools/tgf)"]:::ui
    CorePE["Core Parsing Engine (src)"]:::core
    GrammarConstruction["Grammar Construction (src/grammar.tmpl.h)"]:::core
    ParserModule["Parser Module (src/parser.cpp)"]:::core
    ForestTraversal["Forest & Traverser (src/traverser.h)"]:::core
    Utilities["Utilities & Helpers (src/devhelpers.h)"]:::utility
    BuildTest["Build & Test Infrastructure (tests/test_parser.cpp)"]:::build
    Examples["Examples & Tutorials (examples)"]:::examples

    %% Connections from UI to Core
    CLI -->|"uses"| CorePE
    TGFTool -->|"uses"| CorePE

    %% Internal flow within Core Parsing Engine
    CorePE -->|"defines"| GrammarConstruction
    GrammarConstruction -->|"feeds"| ParserModule
    ParserModule -->|"produces"| ForestTraversal

    %% Additional supporting interactions
    Utilities -->|"supports"| CorePE
    BuildTest -->|"orchestrates"| CorePE
    Examples -->|"demonstrates"| CorePE

    %% Click Events
    click CorePE "https://github.com/idni/parser/tree/master/src"
    click GrammarConstruction "https://github.com/idni/parser/blob/master/src/grammar.tmpl.h"
    click ParserModule "https://github.com/idni/parser/blob/master/src/parser.cpp"
    click ForestTraversal "https://github.com/idni/parser/blob/master/src/traverser.h"
    click CLI "https://github.com/idni/parser/blob/master/src/cli.cpp"
    click TGFTool "https://github.com/idni/parser/tree/master/tools/tgf"
    click Utilities "https://github.com/idni/parser/blob/master/src/devhelpers.h"
    click BuildTest "https://github.com/idni/parser/blob/master/tests/test_parser.cpp"
    click Examples "https://github.com/idni/parser/tree/master/examples"

    %% Styles
    classDef core fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef ui fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef utility fill:#D3D3D3,stroke:#000,stroke-width:2px;
    classDef build fill:#FFCC00,stroke:#000,stroke-width:2px;
    classDef examples fill:#FFB6C1,stroke:#000,stroke-width:2px;
```
