| 1 | module errors |
| 2 | |
| 3 | import v3.token |
| 4 | import term |
| 5 | |
| 6 | // Kind lists kind values used by errors. |
| 7 | pub enum Kind { |
| 8 | warning |
| 9 | notice |
| 10 | error |
| 11 | } |
| 12 | |
| 13 | // str returns the string form for Kind. |
| 14 | pub fn (e Kind) str() string { |
| 15 | return match e { |
| 16 | .warning { 'warning' } |
| 17 | .notice { 'notice' } |
| 18 | .error { 'error' } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // color supports color handling for Kind. |
| 23 | pub fn (e Kind) color(s string) string { |
| 24 | return match e { |
| 25 | .warning { term.yellow(s) } |
| 26 | .notice { term.blue(s) } |
| 27 | .error { term.red(s) } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // error supports error handling for errors. |
| 32 | pub fn error(msg string, details string, kind Kind, pos token.Position) { |
| 33 | eprintln(pos.str() + ' -> ' + term.bold(kind.color(kind.str())) + ': ' + msg) |
| 34 | if details.len > 0 { |
| 35 | eprintln(details) |
| 36 | } |
| 37 | } |
| 38 | |