Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Spans

This page explains how source spans are represented and returned during parsing.


Span Type

  • Type: bsharp_parser::syntax::span::Span<'a>
  • Alias: type Span<'a> = nom_locate::LocatedSpan<&'a str>;
  • Provides line/column offsets and byte positions for parser errors and mapping.
#![allow(unused)]
fn main() {
// src/bsharp_parser/src/syntax/span.rs
pub type Span<'a> = nom_locate::LocatedSpan<&'a str>;
}

Parsing With Spans

Use the parser facade to parse and also get a span table for top-level declarations.

#![allow(unused)]
fn main() {
use bsharp_parser::facade::Parser;

let source = std::fs::read_to_string("Program.cs")?;
let (cu, spans) = Parser::new().parse_with_spans(&source)?;
}
  • The return value is (CompilationUnit, SpanTable).
  • SpanTable maps top-level declarations to byte ranges for later mapping.

Error Reporting

Pretty error formatting uses Span to print line/column with context:

#![allow(unused)]
fn main() {
use bsharp_parser::syntax::errors::format_error_tree;

let msg = format_error_tree(&source, &error_tree);
}

See: docs/parser/error-handling.md for details.

2025-11-17 15:18:26 • commit: 03a4e25