Position type provides precise location information for tokens in source code, tracking bytes, lines, and character positions.
Position
Used to represent exact positions of tokens in code.Methods
bytes
How many bytes, ignoring lines, it would take to find this position.
character
Index of the character on the line for this position.
line
Line the position lies on.
Usage Example
Positions are automatically tracked by the tokenizer and attached to every token:Position Tracking
Positions are tracked in three ways:- Bytes: Total byte offset from the start of the source code
- Line: Line number (1-indexed)
- Character: Character position within the line (1-indexed)
- Accurate error reporting
- Source code navigation
- Precise token location for IDE features
- Byte-level operations on UTF-8 source code
Comparison and Ordering
Positions implementOrd and are ordered by their byte position:
Default Position
Position implements Default, creating a position at the start of the source:
Usage with Tokens
Every token has both a start and end position:Multi-byte Characters
The tokenizer correctly handles UTF-8 multi-byte characters:- Bytes increment by the actual UTF-8 byte length of each character
- Character increments by 1 for each Unicode character (regardless of byte length)
- Line increments on newline characters