Module: compiler.source
Source: ./compiler/source.reef
Overview
compiler/source - Source Location Tracking for Compilers
Provides types for tracking source code positions and spans. Essential for error messages, source mapping, and debugging.
Usage: import compiler.source
let pos = source.create_pos("main.reef", 10, 5) let span = source.create_span(start_pos, end_pos) println(source.pos_to_string(pos)) // "main.reef:10:5" println(source.span_to_string(span)) // "main.reef:10:5-15:20"
Types: SourcePos - Single source position (file, line, column) SourceSpan - Range in source (start pos, end pos)
Functions: create_pos(file, line, col) -> SourcePos create_span(start, end) -> SourceSpan create_span_from_pos(pos) -> SourceSpan // single-position span pos_to_string(pos) -> string span_to_string(span) -> string combine_spans(a, b) -> SourceSpan // merge two spans pos_compare(a, b) -> int // -1, 0, 1 for ordering span_contains_pos(span, pos) -> bool is_same_file(a, b) -> bool
Types
SourcePos
Fields:
| Name | Type |
|---|---|
file |
string |
line |
int |
col |
int |
SourceSpan
Fields:
| Name | Type |
|---|---|
start |
SourcePos |
ending |
SourcePos |
Functions
fn create_pos(file: string, line: int, col: int): SourcePos
Create a source position
fn unknown_pos(): SourcePos
Create an unknown/invalid position
fn pos_file(pos: SourcePos): string
Get the file name from a position
fn pos_line(pos: SourcePos): int
Get the line number from a position
fn pos_col(pos: SourcePos): int
Get the column number from a position
fn pos_to_string(pos: SourcePos): string
Format position as "file:line:col"
fn short_pos_string(pos: SourcePos): string
Format position as "line:col" (no filename)
fn pos_compare(a: SourcePos, b: SourcePos): int
Compare two positions: -1 if a < b, 0 if equal, 1 if a > b
fn is_same_file(a: SourcePos, b: SourcePos): bool
Check if two positions are in the same file
fn create_span(start: SourcePos, ending: SourcePos): SourceSpan
Create a span from start to end positions
fn create_span_from_pos(pos: SourcePos): SourceSpan
Create a zero-width span at a single position
fn unknown_span(): SourceSpan
Create an unknown/invalid span
fn span_start(span: SourceSpan): SourcePos
Get the start position of a span
fn span_end(span: SourceSpan): SourcePos
Get the end position of a span
fn span_to_string(span: SourceSpan): string
Format span as "file:start_line:start_col-end_line:end_col" or "file:line:col" if single position
fn combine_spans(a: SourceSpan, b: SourceSpan): SourceSpan
Combine two spans into one that covers both
fn get_earlier_pos(a: SourcePos, b: SourcePos): SourcePos
Helper: return earlier of two positions
fn get_later_pos(a: SourcePos, b: SourcePos): SourcePos
Helper: return later of two positions
fn span_contains_pos(span: SourceSpan, pos: SourcePos): bool
Check if a span contains a position
Generated by reefc doc