Store spans instead of lexeme strings on tokens#76
Merged
StreamDemon merged 1 commit intoJul 2, 2026
Merged
Conversation
Every token carried an owned copy of its source text, so lexing a file allocated one String per token even though the source buffer already holds the same bytes. Token is now just a kind and a span; the new `Token::text(source)` slices the original buffer on demand. The parser threads the source string through and derives text only at the few places that need it (identifiers, literals, the `vec` head, the extern target). Unary and binary operator text now comes from the token kind rather than the lexeme, since the kind already determines it.
There was a problem hiding this comment.
No issues found across 2 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Architecture diagram
sequenceDiagram
participant Source as Source buffer (&str)
participant Lexer as Lexer
participant TokenStream as Token stream
participant Parser as Parser<'src>
participant ASTNode as AST nodes
Note over Source,ASTNode: Span-based token text flow (no allocation per token)
Source->>Lexer: lex(source)
Lexer->>Lexer: scan characters
Lexer->>Lexer: push(kind, start, end)
Lexer->>TokenStream: Token{kind, span}
Note over TokenStream,Parser: Each token stores only kind + byte span
Source->>Parser: parse_program(source)
Parser->>TokenStream: consume tokens
alt Parsing an identifier/token
Parser->>Source: text(&token) slices source by span
Source-->>Parser: &'src str slice
Parser->>ASTNode: store slice as String (only for Ident, Path, Literal)
else Parsing a binary operator (e.g., +, -, |>)
Parser->>Parser: derive op string from TokenKind (no source slice needed)
Parser->>ASTNode: store derived operator string
else Parsing a unary operator (e.g., !, -, *, &)
Parser->>Parser: match TokenKind to static string
Parser->>ASTNode: store derived operator string
else Checking for "vec" keyword
Parser->>Source: text(&token) slices source
Source-->>Parser: &'src str
Parser->>Parser: compare to "vec"
else Checking ident text (at_ident_text / eat_ident_text)
Parser->>Source: token.text(self.source)
Source-->>Parser: &'src str
Parser->>Parser: compare to target text
else Parsing extern block target
Parser->>Source: text(&token).to_string()
Source-->>Parser: String
Parser->>ASTNode: store target string
end
Note over Parser,ASTNode: Token text derived only where AST nodes need it
Requires human review: Refactor of core token and parser data structures; risk of subtle bugs despite no behavior change.
Re-trigger cubic
This was referenced Jul 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Tokenno longer stores an ownedlexeme: String— it is now just{ kind, span }, and the newToken::text(source)slices the original buffer on demand. Lexing is now allocation-free per token.source: &'src strthrough (Parser<'src>) and derives text only where it is actually consumed: identifiers, path segments, literals, thevechead check,at_ident_text, and the extern target.Second sub-PR of the enhancement wave (PR #71 roadmap: "
Token.lexeme: String→ span-based slicing or interning"). Span slicing was chosen over interning: the token stream never outlives the source buffer in the bootstrap pipeline, so a borrow is strictly simpler and interning buys nothing yet. No behavior change.Related Issue
None (PR #71 roadmap item).
Spec Sections Affected
None — implementation quality only.
Checklist
docs/pages — N/A, no behavior changeBuild Targets Tested
cargo fmt --all -- --check,cargo clippy --workspace --all-targets -- -D warnings,cargo test --workspaceall green locally (49 tests).Test Plan
every_numeric_suffix_lexesnow exercises the newToken::textAPI directly (text + span asserted per suffix).Summary by cubic
Switched tokens to span-based text slicing with
Token::text(source)and threadedsourcethrough the parser to read text only when needed. No behavior change; this removes per-token allocations and reduces memory use.Refactors
Tokennow stores{ kind, span }; addedToken::text(&source).source(Parser<'src>) and reads text for identifiers, literals, path segments,vechead checks, and extern targets.TokenKindinstead of stored lexemes.Token::text.Migration
token.lexemewithtoken.text(source).Written for commit 2974ffc. Summary will update on new commits.