Skip to content

A allows recovering the location in the original source from a byte offset.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

richard-uk1/sourcefile-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A allows recovering the location in the original source from a byte offset.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages