#source-map #source #location #map #error-message #file-line #parser

sourcefile

Retain mapping information when concatenating source files, to make error messages more useful

7 releases

Uses old Rust 2015

0.2.1 Jul 4, 2021
0.2.0 Jul 4, 2021
0.1.4 Sep 26, 2018
0.1.1 Jul 27, 2018

#2454 in Parser implementations

Download history 1269/week @ 2024-11-23 1297/week @ 2024-11-30 1258/week @ 2024-12-07 1179/week @ 2024-12-14 440/week @ 2024-12-21 156/week @ 2024-12-28 813/week @ 2025-01-04 1433/week @ 2025-01-11 734/week @ 2025-01-18 409/week @ 2025-01-25 1335/week @ 2025-02-01 1000/week @ 2025-02-08 491/week @ 2025-02-15 813/week @ 2025-02-22 601/week @ 2025-03-01 522/week @ 2025-03-08

2,519 downloads per month
Used in 3 crates

Apache-2.0/MIT

11KB
169 lines

sourcefile

A library for concatenating source from multiple files, whilst keeping track of where each new file and line starts.

Examples

Assume the following file is at partial1.py

x = 1
y = 1

and that the following file is at partial2.py

x = 1
y = 1 oops

then the following code

extern crate sourcefile;

use sourcefile::SourceFile;

// Assume this function exists, error is offset of unexpected char.
fn parse(source: &str) -> Result<Ast, usize> {
    // ...
}

fn main() {
    let mut source = SourceFile::new();
    source = source.add_file("./partial1.py");
    source = source.add_file("./partial2.py");

    let ast = match parse(&source.content) {
        Ok(ast) => ast,
        Err(offset) => {
            let pos = source.resolve_offset(offset);
            eprintln!("error compiling in \"{}\", line {}, col {}.", 
                      pos.filename, pos.line + 1, pos.col + 1);
        }
    }
}

prints

error compiling in "./partial2.py", line 2, col 7.

No runtime deps