# matteobenvenuti/rpgleparser architecture

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

- Interactive diagram: https://gitdiagram.com/matteobenvenuti/rpgleparser
- Repository: https://github.com/matteobenvenuti/rpgleparser

## Overview

##### Main Components
- Grammar Files:
- Located under src/main/antlr4, they include “RpgLexer.g4” and “RpgParser.g4”. These files form the basis of the language grammar and are used by ANTLR to generate the lexer and parser classes.
- Java Processing and Utility Classes:
- Under src/main/java, you have several packages (e.g., org/rpgleparser/tokens and org/rpgleparser/utils) that include classes for token transformation (PreprocessTokenSource, TransformTokenSource, WrappedTokenSource) and utilities (FixedWidthBufferedReader, FixedWidthInputStream, TreePrinterListener, TreeUtils). These classes enrich the basic parsing functionality with custom token handling and parse tree processing.
- Example and Listener Implementations:
- The examples package (e.g., examples/loggingListener/LoggingListener.java) demonstrates how the parser’s outputs might be used in a real scenario, showing integration of listeners for logging and debugging.
- Testing Suite:
- A comprehensive set of unit and integration tests reside in src/test, including various directories for checking different aspects such as opcodes, expressions, directives, declarations, and more. This robust testing structure verifies correct parsing and transformation of RPG source code.

##### Relationships and Data Flow
- Input Stage:
- The system starts with a source of RPG code (in either fixed- or free-format).
- Lexical Analysis:
- The ANTLR-generated lexer (from RpgLexer.g4) tokenizes the input.
- Syntactic Parsing:
- The parser (from RpgParser.g4) builds a parse tree from the token stream.
- Token Source Transformation:
- Custom token sources (PreprocessTokenSource, TransformTokenSource, WrappedTokenSource) process tokens further, possibly applying pre-processing or transformations specific to RPG.
- Parse Tree Processing and Utilities:
- Utility classes (TreePrinterListener and TreeUtils) and the listener implementations (e.g., LoggingListener) help in traversing or handling the parse tree. Additionally, fixed-width utilities (FixedWidthBufferedReader, FixedWidthInputStream) manage special input requirements.
- Testing:
- The test suite feeds sample RPG source files through this pipeline to ensure the reliability of all the above components.

##### Architectural Patterns and Design Principles
- Layered Architecture:
- The project cleanly separates the grammar (parsing) layer, processing (custom token sources and utilities) layer, and testing/validation layer.
- Modular Organization:
- Organized into Maven-standard directories (src/main and src/test), it allows easy integration and extension.
- Usage of ANTLR v4:
- Leveraging ANTLR’s capabilities, the separation of lexer, parser, and listener implementation adheres to well-established compilation and transformation patterns.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["RPG Source Code (Input)"]:::input
    subgraph "Grammar Component"
        B["RpgLexer.g4"]:::grammar
        C["RpgParser.g4"]:::grammar
    end
    subgraph "Token Processing"
        D["PreprocessTokenSource"]:::token
        E["TransformTokenSource"]:::token
        F["WrappedTokenSource"]:::token
    end
    subgraph "Utility & Listener Services"
        G["FixedWidthBufferedReader"]:::utility
        H["FixedWidthInputStream"]:::utility
        I["TreePrinterListener"]:::utility
        J["TreeUtils"]:::utility
        K["LoggingListener (Example)"]:::utility
    end
    L["Testing Suite"]:::testing
    M["pom.xml"]:::build

    %% Main processing flow
    A -->|"Tokenization"| B
    B -->|"Lexing"| C
    C -->|"Parsing"| D
    D -->|"Preprocessing"| E
    E -->|"TransformingTokens"| F
    F -->|"ProcessedTokenStream"| I

    %% Fixed-width input branch
    A -->|"FixedWidthInput"| G
    G -->|"FixedWidthStream"| H
    H -->|"FeedsStream"| I

    %% Utility processing to testing
    I -->|"ParseTreeProcessing"| J
    I -->|"Validation"| L
    J -->|"TreeUtilities"| L
    K -->|"Logs"| L

    %% Build & Dependency Management influences components
    M -->|"BuildDependency"| B
    M -->|"BuildDependency"| D
    M -->|"BuildDependency"| I

    %% Styles
    classDef input fill:#DDA0DD,stroke:#000,stroke-width:2px;
    classDef grammar fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef token fill:#FFA07A,stroke:#000,stroke-width:2px;
    classDef utility fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef testing fill:#FFB6C1,stroke:#000,stroke-width:2px;
    classDef build fill:#FFD700,stroke:#000,stroke-width:2px;

    %% Click Events
    click B "https://github.com/matteobenvenuti/rpgleparser/blob/dev/src/main/antlr4/org/rpgleparser/RpgLexer.g4"
    click C "https://github.com/matteobenvenuti/rpgleparser/blob/dev/src/main/antlr4/org/rpgleparser/RpgParser.g4"
    click D "https://github.com/matteobenvenuti/rpgleparser/blob/dev/src/main/java/org/rpgleparser/tokens/PreprocessTokenSource.java"
    click E "https://github.com/matteobenvenuti/rpgleparser/blob/dev/src/main/java/org/rpgleparser/tokens/TransformTokenSource.java"
    click F "https://github.com/matteobenvenuti/rpgleparser/blob/dev/src/main/java/org/rpgleparser/tokens/WrappedTokenSource.java"
    click G "https://github.com/matteobenvenuti/rpgleparser/blob/dev/src/main/java/org/rpgleparser/utils/FixedWidthBufferedReader.java"
    click H "https://github.com/matteobenvenuti/rpgleparser/blob/dev/src/main/java/org/rpgleparser/utils/FixedWidthInputStream.java"
    click I "https://github.com/matteobenvenuti/rpgleparser/blob/dev/src/main/java/org/rpgleparser/utils/TreePrinterListener.java"
    click J "https://github.com/matteobenvenuti/rpgleparser/blob/dev/src/main/java/org/rpgleparser/utils/TreeUtils.java"
    click K "https://github.com/matteobenvenuti/rpgleparser/blob/dev/src/main/java/examples/loggingListener/LoggingListener.java"
    click L "https://github.com/matteobenvenuti/rpgleparser/tree/dev/src/test"
    click M "https://github.com/matteobenvenuti/rpgleparser/blob/dev/pom.xml"
```
