Repository navigation
2 1 segmentation and tokenization
Source: https://deepwiki.com/PonvasanthPon/TransCompilator/2-1-segmentation-and-tokenization
Relevant source files
The following files were used as context for generating this wiki page:
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.
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.
Tokens are categorized via the TokenKind enum and represented by the Token struct:
-
TokenKind::Word: An English word or identifier crates/tc-syntax/src/token.rs:6-7. Case-folded spellings are stored inToken::lowercrates/tc-syntax/src/token.rs:21-22. -
TokenKind::Number: Integer or decimal literals, including binary (0b), octal (0o), and hexadecimal (0x) variants crates/tc-syntax/src/token.rs:8-9. -
TokenKind::String: Quoted string literals including their enclosing delimiters crates/tc-syntax/src/token.rs:10-11. -
TokenKind::Symbol: Operators, punctuation, or special lexical markers such as'scrates/tc-syntax/src/token.rs:12-13.
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]()
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
Sources: crates/tc-syntax/src/token.rs:1-193]()
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.
The segmentation engine organizes files into a hierarchical representation defined in crates/tc-syntax/src/segment.rs:
-
RawSentence: Represents a single natural-language sentence with cleaned text, raw source text, source location (Span), and extracted leading discoursefillerscrates/tc-syntax/src/segment.rs:13-23. -
RawItem: An enum representing either a leafRawItem::Sentenceor a nestedRawItem::Blockcrates/tc-syntax/src/segment.rs:25-32. -
RawBlock: A headed block (such as anIf,While, function definition, or struct declaration) containing a block header and an ordered vector of items crates/tc-syntax/src/segment.rs:34-43. -
RawDocument: The top-level container holding ordered document items crates/tc-syntax/src/segment.rs:45-50.
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]()
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)"]
Sources: crates/tc-syntax/src/segment.rs:52-211]()
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):
-
first_word(text: &str) -> String: Returns the lowercase spelling of the first word token crates/tc-semantics/src/textutil.rs:9-14. -
starts_with_word(text: &str, word: &str) -> bool: Checks case-insensitively if the text starts with a specific word token crates/tc-semantics/src/textutil.rs:16-23. -
find_top_level_word(text: &str, word: &str) -> Option<(usize, usize)>: Locates the byte range of a top-level word, ignoring nested scopes crates/tc-semantics/src/textutil.rs:25-43. -
find_top_level_phrase(text: &str, phrase: &str) -> Option<(usize, usize)>: Finds multi-word phrases at the top nesting level crates/tc-semantics/src/textutil.rs:45-76. -
find_top_level_symbol(text: &str, symbol: &str) -> Option<(usize, usize)>: Locates top-level symbols like=or,crates/tc-semantics/src/textutil.rs:78-96. -
split_top_level(text: &str, separator: &str) -> Vec<String>: Splits text segments on top-level separators crates/tc-semantics/src/textutil.rs:98-131. -
split_where(text: &str) -> (String, Option<String>): Separates a statement from an optional trailingwhereclause crates/tc-semantics/src/textutil.rs:133-147. -
is_lvalue_text(text: &str) -> bool: Validates whether a token slice conforms to a mutable lvalue structure (such as array indexing, field access, or pointer dereference) crates/tc-semantics/src/textutil.rs:149-208.
Sources: crates/tc-semantics/src/textutil.rs:1-210]()
The TransCompilator Project