bhc-span
Source location tracking and span management for the Basel Haskell Compiler.
Overview
This crate provides types for tracking source locations throughout the compilation pipeline, enabling accurate error reporting and source mapping.
Key Types
| Type | Description |
|---|---|
BytePos |
A byte offset into a source file |
Span |
A half-open byte range [lo, hi) representing a region of source code |
Spanned<T> |
A value with an associated span |
FileId |
Unique identifier for a source file |
FullSpan |
A span with an associated file ID for cross-file spans |
LineCol |
Line and column information (1-indexed) |
SourceFile |
Information about a source file including content and line offsets |
Usage
use ;
// Create a span from byte positions
let span = new;
assert_eq!;
// Create a source file and look up line/column
let file = new;
let loc = file.lookup_line_col;
assert_eq!;
assert_eq!;
// Merge spans
let span1 = from_raw;
let span2 = from_raw;
let merged = span1.merge;
assert_eq!;
Features
- Zero-cost span representation (just two
u32values) - Efficient line/column lookup with precomputed line starts
- Span merging and manipulation operations
- Serialization support via
serde
Design Notes
- Spans use byte offsets rather than character offsets for efficiency
- The
DUMMYspan ([0, 0)) is used for generated code or when location is irrelevant - Line and column numbers are 1-indexed to match editor conventions
Related Crates
bhc-diagnostics- Uses spans for error reportingbhc-lexer- Attaches spans to tokensbhc-parser- Attaches spans to AST nodes