Crates.io | sourcefile |
lib.rs | sourcefile |
version | 0.2.1 |
source | src |
created_at | 2018-07-26 14:34:22.389305 |
updated_at | 2021-07-04 19:59:18.683076 |
description | Retain mapping information when concatenating source files, to make error messages more useful |
homepage | |
repository | https://github.com/derekdreery/sourcefile-rs |
max_upload_size | |
id | 76049 |
size | 23,018 |
sourcefile
A library for concatenating source from multiple files, whilst keeping track of where each new file and line starts.
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.