| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use bufio; |
| 5 |
use fmt; |
| 6 |
use fs; |
| 7 |
use io; |
| 8 |
use os; |
| 9 |
use time; |
| 10 |
|
| 11 |
// Prints a context line from the source file. |
| 12 |
fn print_context( |
| 13 |
path: str, |
| 14 |
line: u64, |
| 15 |
col: u64, |
| 16 |
age: time::instant, |
| 17 |
) void = { |
| 18 |
const file = match (os::open(path)) { |
| 19 |
case fs::error => |
| 20 |
return; |
| 21 |
case let file: io::file => |
| 22 |
yield file; |
| 23 |
}; |
| 24 |
defer _ = io::close(file); |
| 25 |
|
| 26 |
match (os::fstat(file)) { |
| 27 |
case let st: fs::filestat => |
| 28 |
if (st.mask & fs::stat_mask::MTIME == 0) yield; |
| 29 |
if (time::compare(st.mtime, age) == 1) { |
| 30 |
_ = fmt::errorln("* Warning: file was modified after executable was built"); |
| 31 |
}; |
| 32 |
case => void; |
| 33 |
}; |
| 34 |
|
| 35 |
static let linebuf: [1024]u8 = [0...]; |
| 36 |
const scan = bufio::newscanner_static(file, linebuf); |
| 37 |
|
| 38 |
let context = ""; |
| 39 |
for (let i = 0u; i < line; i += 1) { |
| 40 |
match (bufio::scan_line(&scan)) { |
| 41 |
case let s: const str => |
| 42 |
context = s; |
| 43 |
case => |
| 44 |
return; |
| 45 |
}; |
| 46 |
}; |
| 47 |
|
| 48 |
_ = fmt::errorfln("| {}", context); |
| 49 |
|
| 50 |
if (col != 0) { |
| 51 |
_ = fmt::errorf(" "); |
| 52 |
for (let i = 1u; i < col - 1; i += 1) { |
| 53 |
_ = fmt::errorf(" "); |
| 54 |
}; |
| 55 |
_ = fmt::errorf("^"); |
| 56 |
}; |
| 57 |
|
| 58 |
_ = fmt::errorln(); |
| 59 |
}; |