File: README.md

package info (click to toggle)
rust-sourcefile 0.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 92 kB
  • sloc: makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,098 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# `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`

```python
x = 1
y = 1
```

and that the following file is at `partial2.py`

```python
x = 1
y = 1 oops
```

then the following code

```rust
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

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