Engine Internals

Architecture & Pipeline Specifications

High-throughput tokenization, deterministic stream parsing, and ISO 20022 message synthesis with zero runtime allocations.

Bank Statement Parser is architected around a deterministic, four-stage extraction and validation pipeline. Every stage executes in-process with zero outbound telemetry, zero heap allocation in the critical path, and strict mathematical balance verification before emitting ISO 20022 messages.

Four-Stage Processing Pipeline

01

Stream Ingest

Zero-copy memory mapping (mmap) of input statement bytes with instant magic-byte format detection (PDF, CSV, OFX, MT940, CAMT.053).

< 0.2 ms Ingest Latency
02

Layout Tokenizer

Spatial coordinate mapping and multi-column tabular boundary reconstruction, preserving text flow across complex multi-page financial tables.

Zero Heap Allocations
03

Field Normalizer

Canonical standardization of 24 global date dialects, ISO 4217 currencies, and mod-97 IBAN checksum verification.

ISO 20022 BTC Mapping
04

Schema Validator

Mathematical invariant verification (Opening + ΣCredits - ΣDebits = Closing) before emitting validated JSON or ISO 20022 XML.

100% Invariant Check

Deterministic Pipeline Execution Trace

The trace below demonstrates the sub-millisecond execution lifecycle of the four-stage engine processing a multi-page PDF statement into validated ISO 20022 CAMT.053 XML:

pipeline-trace — bankstatementparser v0.0.2
$bankstatementparser --input statement.pdf --trace --format camt053
[0.00ms] [ingest] Zero-copy mmap statement.pdf (142,896 bytes) • Magic: %PDF-1.7
[0.18ms] [tokenizer] Spatial scan complete: 4 tables detected, 128 transaction rows clustered
[0.45ms] [normalizer] Currency: GBP (ISO 4217) • Account: GB29BARC20000012345678 (mod-97 OK)
[0.72ms] [validator] Opening: 14,250.00 | Credits: +8,300.50 | Debits: -3,120.25 | Closing: 19,430.25
[0.89ms] [validator] Invariant Check: 14,250.00 + 5,180.25 == 19,430.25 [PASS: Delta = 0.0000]
[1.12ms] [synthesis] Emitted ISO 20022 camt.053.001.08 XML (42,810 bytes, schema valid)
Status: 200 OK • Latency: 1.12ms • Memory: 1.4 MB • Telemetry: Zero Outbound

Stage 1: Zero-Copy Stream Ingestion

The ingestion subsystem interfaces directly with the host filesystem via memory-mapped files (mmap), bypassing kernel-to-userspace buffer duplication. Byte slices are analyzed using non-backtracking magic-byte sniffers:

  • PDF Documents: Validates file header prefix %PDF- and reads the cross-reference table (xref) directly from the trailer offset.
  • OFX Streams: Sniffs OFXHEADER SGML tags or XML prologue <?xml version="1.0" to branch into streaming parser trees.
  • SWIFT MT940 / MT942: Checks for field delimiter sequences (:20:, :25:, :28C:) to route into deterministic line tokenizers.
  • CSV Delimited Files: Evaluates entropy across candidate delimiters (comma, semicolon, tab, pipe) over the first 50 rows to infer column geometry without full-file scans.

Stage 2: Spatial Layout Tokenization

For unstructured formats such as digital PDF statements, standard text extraction destroys critical vertical alignment. Bank Statement Parser utilizes a deterministic coordinate-clustering engine:

  • Glyph Extraction: Maps character matrices, font bounding boxes, and baseline coordinates from content stream operators (BT, ET, Tj, TJ, Tm).
  • Row Clustering: Groups glyphs with overlapping vertical tolerances into discrete lines, preventing footnote and header collision.
  • Column Geometric Alignment: Computes vertical whitespace gutters to delineate Booking Date, Value Date, Narrative, Amount, and Running Balance columns.
  • Multi-line Narrative Stitching: Heuristically associates wrapped description text back to the initiating transaction row.
Bank Statement Parser — Pipeline Architecture and High-Throughput Engine
High-assurance statement parsing: zero-copy memory mapping, deterministic tokenization, and ISO 20022 message synthesis.

Stage 3: Field Normalization & Standard Mapping

Extracted raw string tokens are converted into strongly-typed canonical primitives:

  • Date Canonicalization: Resolves 24 regional date formats (including DD/MM/YYYY, MM/DD/YYYY, YYYY-MM-DD, and multi-lingual month names) into ISO 8601 UTC timestamps.
  • Monetary Amounts: Parses European period/comma inversion (e.g., 1.234,56 vs 1,234.56), preserving scale as exact 64-bit fixed-point integers to eliminate IEEE 754 floating-point rounding drift.
  • Account & Bank Identifiers: Verifies ISO 13616 International Bank Account Numbers (IBAN) using mod-97 arithmetic (ISO 7064) and validates ISO 9362 Business Identifier Codes (BIC).
  • Transaction Code Classification: Maps proprietary transaction labels to standardized ISO 20022 Bank Transaction Codes (BTC), distinguishing card settlements, direct debits, credit transfers, and fee deductions.

Stage 4: Mathematical Validation & Schema Synthesis

Before any extracted statement is emitted, the engine evaluates strict balance invariants to guarantee data integrity:

mathematical-invariant — balance check
Invariant Equation: Opening_Balance + Sum(Credits) - Sum(Debits) == Closing_Balance
# If Delta != 0.00, extraction fails with explicit discrepancy error and line offset

Upon invariant satisfaction, the engine streams the normalized data tree into target schemas:

  • ISO 20022 CAMT.053: Generates conforming camt.053.001.08 Bank-to-Customer Statement XML messages ready for direct core banking and treasury ingest.
  • Canonical JSON: Emits clean, strongly-typed JSON with ISO 8601 dates and exact decimal numbers for downstream API consumption.
  • Reconciliation CSV: Serializes standardized transaction rows with consistent column headers for enterprise ERP systems.