Skip to content

Commit 32bd975

Browse files
Add BaseErrorExt
1 parent ab2c912 commit 32bd975

File tree

14 files changed

+143
-54
lines changed

14 files changed

+143
-54
lines changed

cli/src/error.rs

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,27 @@ use ariadne::{Label, Report, ReportKind, Source};
22
use clap::Command;
33
use colorful::Colorful;
44
use lento_core::{
5-
interpreter::error::RuntimeError, lexer::lexer::InputSource, parser::error::ParseError,
6-
type_checker::checker::TypeError, util::error::BaseError,
5+
interpreter::error::RuntimeError,
6+
lexer::lexer::InputSource,
7+
parser::error::ParseError,
8+
type_checker::checker::TypeError,
9+
util::error::{BaseError, BaseErrorExt},
710
};
811

912
pub fn print_parse_error(err: ParseError, content: &str, source: &InputSource) {
10-
// print_error_simple("parse error", err.message);
11-
// print_error_simple_at(err.info, source)
12-
print_error_report("parse error", err.inner, content, source);
13+
print_error_report("parse error", err.to_base(), content, source);
1314
}
1415

1516
pub fn print_runtime_error(err: RuntimeError, content: &str, source: &InputSource) {
16-
// print_error_simple("runtime error", err.message);
17-
// print_error_simple_at(err.info, source)
18-
print_error_report("runtime error", err.inner, content, source);
17+
print_error_report("runtime error", err.to_base(), content, source);
1918
}
2019

2120
pub fn print_type_error(err: TypeError, content: &str, source: &InputSource) {
22-
// print_error_simple("type error", err.message);
23-
// print_error_simple_at(err.info, source)
24-
print_error_report("type error", err.inner, content, source);
21+
print_error_report("type error", err.to_base(), content, source);
2522
}
2623

2724
pub fn print_error_report(kind: &str, base: BaseError, content: &str, source: &InputSource) {
2825
let mut colors = ariadne::ColorGenerator::new();
29-
3026
let mut report = Report::build(
3127
ReportKind::Custom(kind, ariadne::Color::BrightRed),
3228
(source.name(), base.info.start.index..base.info.end.index),
@@ -71,21 +67,6 @@ pub fn print_error_simple(kind: &str, msg: String) {
7167
println!("{}: {}", kind.light_red(), msg);
7268
}
7369

74-
// pub fn print_error_simple_at(info: LineInfo, source: &InputSource) {
75-
// let msg = match info.end.eof {
76-
// true => format!("line {}:{} to end of", info.start.line, info.start.column),
77-
// false if info.start.line == info.end.line => format!(
78-
// "line {}:{} to {}",
79-
// info.start.line, info.start.column, info.end.column
80-
// ),
81-
// false => format!(
82-
// "line {}:{} to line {}:{} in",
83-
// info.start.line, info.start.column, info.end.line, info.end.column
84-
// ),
85-
// };
86-
// print_error_simple("└─ at", format!("{} {}\n", msg, source.human_readable()));
87-
// }
88-
8970
pub fn print_error(msg: String) {
9071
print_error_simple("error", msg)
9172
}

core/src/compiler/error.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
use crate::util::error::{BaseError, LineInfo};
1+
use crate::util::error::{BaseError, BaseErrorExt, LineInfo};
22

33
/// Lexer error
44
#[derive(Debug, Clone)]
55
pub struct CompileError {
66
pub inner: BaseError,
77
}
88

9-
impl CompileError {
10-
pub fn new(message: String, info: LineInfo) -> Self {
9+
impl BaseErrorExt for CompileError {
10+
fn new(message: String, info: LineInfo) -> Self {
1111
Self {
1212
inner: BaseError::new(message, info),
1313
}
1414
}
15+
16+
fn with_hint(self, hint: String) -> Self {
17+
Self {
18+
inner: self.inner.with_hint(hint),
19+
}
20+
}
21+
22+
fn with_label(self, message: String, info: LineInfo) -> Self {
23+
Self {
24+
inner: self.inner.with_label(message, info),
25+
}
26+
}
27+
28+
fn base(&self) -> &BaseError {
29+
&self.inner
30+
}
31+
32+
fn to_base(self) -> BaseError {
33+
self.inner
34+
}
1535
}

core/src/interpreter/env.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ use std::collections::HashMap;
33
use colorful::Colorful;
44

55
use crate::{
6-
util::error::LineInfo,
76
type_checker::types::Type,
8-
util::{failable::Failable, str::Str},
7+
util::{
8+
error::{BaseErrorExt, LineInfo},
9+
failable::Failable,
10+
str::Str,
11+
},
912
};
1013

1114
use super::{

core/src/interpreter/error.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
use crate::util::error::{BaseError, LineInfo};
1+
use crate::util::error::{BaseError, BaseErrorExt, LineInfo};
22

33
/// Runtime error
44
#[derive(Debug, Clone)]
55
pub struct RuntimeError {
66
pub inner: BaseError,
77
}
88

9-
impl RuntimeError {
10-
pub fn new(message: String, info: LineInfo) -> Self {
9+
impl BaseErrorExt for RuntimeError {
10+
fn new(message: String, info: LineInfo) -> Self {
1111
Self {
1212
inner: BaseError::new(message, info),
1313
}
1414
}
15+
16+
fn with_hint(self, hint: String) -> Self {
17+
Self {
18+
inner: self.inner.with_hint(hint),
19+
}
20+
}
21+
22+
fn with_label(self, message: String, info: LineInfo) -> Self {
23+
Self {
24+
inner: self.inner.with_label(message, info),
25+
}
26+
}
27+
28+
fn base(&self) -> &BaseError {
29+
&self.inner
30+
}
31+
32+
fn to_base(self) -> BaseError {
33+
self.inner
34+
}
1535
}

core/src/interpreter/eval.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use crate::{
88
checked_ast::CheckedAst,
99
types::{GetType, Type},
1010
},
11-
util::error::LineInfo,
12-
util::str::Str,
11+
util::{
12+
error::{BaseErrorExt, LineInfo},
13+
str::Str,
14+
},
1315
};
1416

1517
use super::{

core/src/interpreter/number.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use malachite::{
99
};
1010

1111
use crate::{
12-
util::error::LineInfo,
1312
type_checker::types::{std_types, GetType, Type},
13+
util::error::{BaseErrorExt, LineInfo},
1414
};
1515

1616
use super::error::RuntimeError;

core/src/lexer/error.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,42 @@ use colorful::Colorful;
22

33
use crate::type_checker::types::Type;
44

5-
use crate::util::error::{BaseError, LineInfo};
5+
use crate::util::error::{BaseError, BaseErrorExt, LineInfo};
66

77
#[derive(Debug, Clone)]
88
pub struct LexerError {
99
pub inner: BaseError,
1010
}
1111

12-
impl LexerError {
13-
pub fn new(message: String, info: LineInfo) -> Self {
12+
impl BaseErrorExt for LexerError {
13+
fn new(message: String, info: LineInfo) -> Self {
1414
Self {
1515
inner: BaseError::new(message, info),
1616
}
1717
}
1818

19+
fn with_hint(self, hint: String) -> Self {
20+
Self {
21+
inner: self.inner.with_hint(hint),
22+
}
23+
}
24+
25+
fn with_label(self, message: String, info: LineInfo) -> Self {
26+
Self {
27+
inner: self.inner.with_label(message, info),
28+
}
29+
}
30+
31+
fn base(&self) -> &BaseError {
32+
&self.inner
33+
}
34+
35+
fn to_base(self) -> BaseError {
36+
self.inner
37+
}
38+
}
39+
40+
impl LexerError {
1941
pub fn unexpected_end_of_file(info: LineInfo) -> Self {
2042
Self::new("Unexpected end of program".to_string(), info)
2143
}

core/src/lexer/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use malachite::{num::conversion::traits::FromSciString, Integer, Natural, Ration
1313
use crate::{
1414
interpreter::number::{BitSize, FloatingPoint, Number, SignedInteger, UnsignedInteger},
1515
type_checker::types::std_types,
16-
util::error::LineInfo,
16+
util::error::{BaseErrorExt, LineInfo},
1717
};
1818

1919
use super::{

core/src/parser/error.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
use std::fmt::Debug;
22

3-
use crate::util::error::{BaseError, LineInfo};
3+
use crate::util::error::{BaseError, BaseErrorExt, LineInfo};
44

55
#[derive(Debug, Clone, PartialEq)]
66
pub struct ParseError {
7-
pub inner: BaseError,
7+
inner: BaseError,
88
}
99

10-
impl ParseError {
11-
pub fn new(message: String, info: LineInfo) -> Self {
10+
impl BaseErrorExt for ParseError {
11+
fn new(message: String, info: LineInfo) -> Self {
1212
Self {
1313
inner: BaseError::new(message, info),
1414
}
1515
}
16+
17+
fn with_hint(self, hint: String) -> Self {
18+
Self {
19+
inner: self.inner.with_hint(hint),
20+
}
21+
}
22+
23+
fn with_label(self, message: String, info: LineInfo) -> Self {
24+
Self {
25+
inner: self.inner.with_label(message, info),
26+
}
27+
}
28+
29+
fn base(&self) -> &BaseError {
30+
&self.inner
31+
}
32+
33+
fn to_base(self) -> BaseError {
34+
self.inner
35+
}
1636
}
1737

1838
/// Errors for when an operator is inserted into the parser

core/src/parser/parser.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use crate::{
1515
token::{TokenInfo, TokenKind},
1616
},
1717
parser::ast::{ParamAst, TypeAst},
18-
util::{error::LineInfo, failable::Failable},
18+
util::{
19+
error::{BaseErrorExt, LineInfo},
20+
failable::Failable,
21+
},
1922
};
2023

2124
use crate::lexer::lexer::Lexer;
@@ -359,7 +362,7 @@ impl<R: Read> Parser<R> {
359362
let body = match self.parse_top_expr() {
360363
Ok(body) => body,
361364
Err(err) => {
362-
log::warn!("Failed to parse function body: {}", err.inner.message);
365+
log::warn!("Failed to parse function body: {}", err.base().message);
363366
return Some(Err(err));
364367
}
365368
};
@@ -425,7 +428,7 @@ impl<R: Read> Parser<R> {
425428
Some(Ok(t)) => t,
426429
Some(Err(err)) => {
427430
return Err(ParseError::new(
428-
format!("Failed to parse function parameter: {}", err.inner.message),
431+
format!("Failed to parse function parameter: {}", err.base().message),
429432
LineInfo::eof(info.end, self.lexer.current_index()),
430433
));
431434
}

0 commit comments

Comments
 (0)