Improve and expand errors

This commit is contained in:
Jeff 2024-03-06 16:50:44 -05:00
parent da5122358e
commit 799b5d838c
2 changed files with 17 additions and 1 deletions

View File

@ -10,6 +10,7 @@ pub enum Error {
Parse { expected: String, span: SimpleSpan },
Lex { expected: String, span: SimpleSpan },
Runtime(RuntimeError),
Validation(ValidationError),
}
impl From<Rich<'_, char>> for Error {
@ -92,7 +93,7 @@ pub fn create_report<'a>(errors: &'a [Error]) -> Report<'a> {
let mut report = Report::build(ReportKind::Error, (), 0);
for error in errors {
match error {
match &error {
Error::Parse { expected, span } => {
report = report.with_label(
Label::new(span.start..span.end).with_message(format!("Expected {expected}.")),
@ -109,6 +110,7 @@ pub fn create_report<'a>(errors: &'a [Error]) -> Report<'a> {
);
}
Error::Runtime(_) => todo!(),
Error::Validation(_) => todo!(),
}
}

View File

@ -24,6 +24,20 @@ impl Interpreter {
pub fn run(&mut self, source: &str) -> Result<Value, Vec<Error>> {
let tokens = lex(source)?;
let statements = parse(&tokens)?;
let errors = statements
.iter()
.filter_map(|(statement, _span)| {
if let Some(error) = statement.validate(&self.context).err() {
Some(Error::Validation(error))
} else {
None
}
})
.collect::<Vec<Error>>();
if !errors.is_empty() {
return Err(errors);
}
let mut value = Value::none();