Skip to content

2 1 segmentation and tokenization

TransCompilator edited this page Nov 26, 2026 · 1 revision

2.1 Segmentation and Tokenization

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/2-1-segmentation-and-tokenization

Segmentation and Tokenization

Relevant source files

The following files were used as context for generating this wiki page:

Purpose and Scope

This page details the lexical front-end processing in TransCompilator (tc_r1), covering paragraph, block, and sentence segmentation, the token model (tc-syntax), and the shared text utilities used by the semantic front end (tc-semantics). These components transform unstructured or structured English .tc source files into deterministic lexical tokens, hierarchical blocks, and raw sentences without relying on machine learning or network-based models.


1. Tokenization Model (tc-syntax)

The tokenization layer converts raw sentence text into a sequence of coarse-grained tokens while preserving string literal contents, decimal precision, numeric formats (binary, octal, hexadecimal, exponential), possessive markers ('s), and multi-character operators crates/tc-syntax/src/token.rs:1-193.

Token Categories and Structures

Tokens are categorized via the TokenKind enum and represented by the Token struct:

The primary entry point is tokenize(), which iterates over byte slices while tracking boundary offsets (start and end) for precise diagnostics crates/tc-syntax/src/token.rs:53-193.

Sources: crates/tc-syntax/src/token.rs:1-193]()


Diagram: Natural Language Source to Lexical Tokens

graph TD
    NL["Raw English Text (.tc)"] --> Tokenizer["tokenize(text: &str) in crates/tc-syntax/src/token.rs"]
    Tokenizer --> K1["TokenKind::Word"]
    Tokenizer --> K2["TokenKind::Number"]
    Tokenizer --> K3["TokenKind::String"]
    Tokenizer --> K4["TokenKind::Symbol"]
    
    K1 --> Struct["Token Struct (text, lower, kind, start, end)"]
    K2 --> Struct
    K3 --> Struct
    K4 --> Struct
Loading

Sources: crates/tc-syntax/src/token.rs:1-193]()


2. Paragraph, Block, and Sentence Segmentation (tc-syntax)

Source files are represented by the SourceFile struct, which tracks the file name and UTF-8 text, providing lowercased module name derivation via SourceFile::module_name() crates/tc-syntax/src/segment.rs:52-90.

Document Hierarchy

The segmentation engine organizes files into a hierarchical representation defined in crates/tc-syntax/src/segment.rs:

Segmentation Algorithm

The segmenter processes files line by line, inspecting indentation (leading_indent) crates/tc-syntax/src/segment.rs:100-115, stripping numeric step markers or list bullets (strip_step_marker) crates/tc-syntax/src/segment.rs:117-143, identifying structural control flow headers (structural_header) crates/tc-syntax/src/segment.rs:145-175, and splitting lines at colon boundaries (split_at_header_colon) while ignoring quoted strings and bracket depths crates/tc-syntax/src/segment.rs:177-211.

Sources: crates/tc-syntax/src/segment.rs:1-222]()


Diagram: Natural Language Space to Code Entity Space — Segmentation Pipeline

graph TD
    SF["SourceFile (name, text) in crates/tc-syntax/src/segment.rs"] --> Seg["segment_source()"]
    Seg --> Indent["leading_indent()"]
    Indent --> Step["strip_step_marker()"]
    Step --> Head["structural_header() & split_at_header_colon()"]
    Head --> Doc["RawDocument in crates/tc-syntax/src/segment.rs"]
    Doc --> Items["Vec<RawItem>"]
    Items --> SBlock["RawItem::Block(RawBlock)"]
    Items --> SSent["RawItem::Sentence(RawSentence)"]
Loading

Sources: crates/tc-syntax/src/segment.rs:52-211]()


3. Shared Text Utilities (tc-semantics)

To support intent classification, normalization, and lowering without duplicating lexical inspection logic, tc-semantics relies on helper functions defined in crates/tc-semantics/src/textutil.rs crates/tc-semantics/src/textutil.rs:1-210. These functions operate over token streams while respecting nesting depth (parentheses, brackets, and string literals):

Sources: crates/tc-semantics/src/textutil.rs:1-210]()


Clone this wiki locally