From 3e44ea65340c1ef36e63f0b442d1bb07d671085d Mon Sep 17 00:00:00 2001 From: Robert-M-Lucas Date: Wed, 5 Jun 2024 05:44:56 +0100 Subject: [PATCH] Improved error handling --- .idea/workspace.xml | 23 ++++++++++++----------- main.why | 2 +- src/root/errors/name_resolver_errors.rs | 6 +++++- src/root/name_resolver/name_resolvers.rs | 11 ++++------- src/root/parser/parse.rs | 8 ++++++-- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3168d82..f18aa56 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -9,18 +9,9 @@ - - - - - - - - - diff --git a/main.why b/main.why index 761d81c..573de27 100644 --- a/main.why +++ b/main.why @@ -3,6 +3,6 @@ struct A { b: int } -fn main() -> int { +fn main() -> intt { return 12; } diff --git a/src/root/errors/name_resolver_errors.rs b/src/root/errors/name_resolver_errors.rs index 93b8ac1..4bb543e 100644 --- a/src/root/errors/name_resolver_errors.rs +++ b/src/root/errors/name_resolver_errors.rs @@ -11,5 +11,9 @@ pub enum NRErrors { #[error("Cannot find method ({0}) of type ({1})")] CannotFindMethod(String, String), #[error("Two attributes found with the same name ({0})")] - SameAttributeName(String) + SameAttributeName(String), + #[error("Function reference cannot have indirection here")] + FunctionIndirectionError, + #[error("Identifier ({0}) not found")] + IdentifierNotFound(String) } diff --git a/src/root/name_resolver/name_resolvers.rs b/src/root/name_resolver/name_resolvers.rs index c2f6f62..6c9578f 100644 --- a/src/root/name_resolver/name_resolvers.rs +++ b/src/root/name_resolver/name_resolvers.rs @@ -4,6 +4,7 @@ use std::rc::Rc; use derive_getters::Getters; use crate::root::compiler::local_variable_table::LocalVariableTable; use crate::root::errors::name_resolver_errors::NRErrors; +use crate::root::errors::name_resolver_errors::NRErrors::IdentifierNotFound; use crate::root::errors::WError; use crate::root::name_resolver::resolve_function_signatures::FunctionSignature; use crate::root::parser::parse::Location; @@ -195,10 +196,6 @@ impl GlobalDefinitionTable { todo!() } - pub fn resolve_global_name(&self, name: &UnresolvedNameToken) -> NameResult { - todo!() - } - pub fn resolve_global_name_to_id(&self, name: &UnresolvedNameToken, location: &Location) -> Result { let path = name.location().path(); @@ -214,7 +211,7 @@ impl GlobalDefinitionTable { return Err(WError::n(NRErrors::FunctionSubname(next.clone(), name.base().clone()), location.clone())); } if name.indirection().has_indirection() { - // TODO + return Err(WError::n(NRErrors::FunctionIndirectionError, name.location().clone())); } Ok(Some(NameResultId::Function(*fid))) } @@ -299,12 +296,12 @@ impl GlobalDefinitionTable { return Err(WError::n(NRErrors::FunctionSubname(next.clone(), name.base().clone()), location.clone())); } if name.indirection().has_indirection() { - // TODO + return Err(WError::n(NRErrors::FunctionIndirectionError, name.location().clone())); } return Ok(NameResultId::Function(*id)) } - todo!() + Err(WError::n(IdentifierNotFound(name.base().clone()), name.location().clone())) } } diff --git a/src/root/parser/parse.rs b/src/root/parser/parse.rs index 9e6ac32..9f6c422 100644 --- a/src/root/parser/parse.rs +++ b/src/root/parser/parse.rs @@ -42,8 +42,12 @@ impl Location { const CHAR_LIMIT: usize = 61; + impl Display for Location { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + // TODO: Inefficient! + // (Maybe fine because it is a 'bad' path?) + writeln!(f, "{}", cformat!("In File:"))?; writeln!(f, " {}", self.path.as_path().to_string_lossy())?; writeln!(f, "{}", cformat!("At:"))?; @@ -65,7 +69,6 @@ impl Display for Location { offset += 1; } } - println!("O: {offset}"); let mut line_iter = file.lines(); @@ -103,7 +106,8 @@ impl Display for Location { end += 1; writeln!(f, "{:0width$} | {}", self.line, line.chars().skip(start).take(end - start).collect::(), width=largest_num_len)?; - writeln!(f, "{:0width$} | {}^Here", "E", (0..(offset - start)).map(|_| ' ').collect::(), width=largest_num_len)?; + let err_line = format!("{:0width$} | {}^Here", "E", (0..(offset - start)).map(|_| ' ').collect::(), width=largest_num_len); + writeln!(f, "{}", cformat!("{}", err_line))?; if let Some(line) = line_iter.next() { let line = if line.chars().count() > CHAR_LIMIT { format!("{} ...", line.chars().take(CHAR_LIMIT - 4).collect::()) } else { line.to_string() };