hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

errors.ha (1007B)


      1 // SPDX-License-Identifier: GPL-3.0-only
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use bufio;
      5 use fmt;
      6 use hare::lex;
      7 use hare::parse;
      8 use io;
      9 use os;
     10 use strings;
     11 
     12 // TODO: Expand to more kinds of errors
     13 fn printerr(err: parse::error) void = {
     14 	match (err) {
     15 	case let err: lex::syntax =>
     16 		printerr_syntax(err);
     17 	case let err: io::error =>
     18 		fmt::errorln(io::strerror(err))!;
     19 	};
     20 };
     21 
     22 fn printerr_syntax(err: lex::syntax) void = {
     23 	let location = err.0, details = err.1;
     24 	let file = os::open(location.path)!;
     25 	defer io::close(file)!;
     26 
     27 	let line = 1u;
     28 	for (line < location.line) {
     29 		let r = bufio::read_rune(file) as rune;
     30 		if (r == '\n') {
     31 			line += 1u;
     32 		};
     33 	};
     34 
     35 	let line = bufio::read_line(file) as []u8;
     36 	defer free(line);
     37 	let line = strings::fromutf8_unsafe(line);
     38 	fmt::errorfln("{}:{}:{}: syntax error: {}",
     39 		location.path, location.line, location.col, details)!;
     40 	fmt::errorln(line)!;
     41 	for (let i = 0u; i < location.col - 2; i += 1) {
     42 		fmt::error(" ")!;
     43 	};
     44 	fmt::errorln("^--- here")!;
     45 };