hare

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

errors.ha (1022B)


      1 // License: GPL-3.0
      2 // (c) 2021 Drew DeVault <sir@cmpwn.com>
      3 // (c) 2021 Ember Sawady <ecs@d2evs.net>
      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::scanrune(file) as rune;
     30 		if (r == '\n') {
     31 			line += 1u;
     32 		};
     33 	};
     34 
     35 	let line = bufio::scanline(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 };