diff options
Diffstat (limited to 'src/reports')
-rw-r--r-- | src/reports/mod.rs | 69 | ||||
-rw-r--r-- | src/reports/resolver_error.rs | 30 | ||||
-rw-r--r-- | src/reports/source_hierarchy.rs | 58 |
3 files changed, 157 insertions, 0 deletions
diff --git a/src/reports/mod.rs b/src/reports/mod.rs new file mode 100644 index 0000000..9ab3da3 --- /dev/null +++ b/src/reports/mod.rs @@ -0,0 +1,69 @@ +mod resolver_error; +mod source_hierarchy; + +pub use resolver_error::*; +pub use source_hierarchy::*; + +use crate::*; + +use ansi::*; +use log::LogLevel; + + +pub fn report_source_issue(level: LogLevel, context: &Context, message: &str) { + // Prepare variables. + let in_merged = &context.source.in_merged; + let line_num = in_merged.start.line + 1; + let digits = line_num.to_string().len(); + let w = digits + 3; + let arrow = "-->"; + let mut string = message.to_string(); + + macro_rules! push { + ($($tokens:tt)*) => { string.push_str(&format!($($tokens)*)) }; + } + + // Format message and locations. + push!("{NORMAL}\n"); + push!("{BLUE}{arrow:>w$}{NORMAL} {in_merged}\n", w=w); + if let Some(in_source) = &context.source.in_source { + push!("{BLUE}{arrow:>w$}{NORMAL} {in_source}\n", w=w); + } + + // Format source context. + let left = in_merged.start.column; + let right = in_merged.end.column + 1; + let source_line = context.source_code.split('\n').nth(in_merged.start.line) + .unwrap_or("<error reading line from source>"); + let space = " "; + let colour = match level { + LogLevel::Info => BLUE, + LogLevel::Warn => YELLOW, + LogLevel::Error => RED, + LogLevel::Fatal => RED, + }; + + // Print source code line. + push!("{BLUE} {line_num} | {NORMAL}"); + for (i, c) in source_line.chars().enumerate() { + if i == left { push!("{colour}") } + if i == right { push!("{NORMAL}") } + push!("{c}"); + } + push!("{NORMAL}\n"); + + // Print source code underline. + push!("{BLUE} {space:>w$} | {NORMAL}", w=digits); + for _ in 0..left { push!(" "); } + push!("{colour}"); + for _ in left..right { push!("^"); } + push!("{NORMAL}"); + + // Print the completed message. + match level { + LogLevel::Info => log::info!( "{}", string), + LogLevel::Warn => log::warn!( "{}", string), + LogLevel::Error => log::error!("{}", string), + LogLevel::Fatal => log::fatal!("{}", string), + } +} diff --git a/src/reports/resolver_error.rs b/src/reports/resolver_error.rs new file mode 100644 index 0000000..97fc761 --- /dev/null +++ b/src/reports/resolver_error.rs @@ -0,0 +1,30 @@ +use crate::*; + +use log::LogLevel; + + +pub struct ResolverError<'a> { + pub resolver: &'a Resolver, +} + +impl<'a> ResolverError<'a> { + pub fn report(&self) { + for reference in &self.resolver.unresolved { + let message = format!( + "Undefined symbol, no label or macro has been defined with the name {:?}", + &reference.symbol.name, + ); + let context = reference.context(&self.resolver); + report_source_issue(LogLevel::Error, &context, &message); + } + for redefinition in &self.resolver.redefinitions { + let definition = self.resolver.definitions.get(redefinition.1).unwrap(); + let message = format!( + "Redefined symbol, first defined at {}", + &definition.symbol.source.in_merged, + ); + let context = redefinition.0.context(&self.resolver); + report_source_issue(LogLevel::Error, &context, &message); + } + } +} diff --git a/src/reports/source_hierarchy.rs b/src/reports/source_hierarchy.rs new file mode 100644 index 0000000..9478c56 --- /dev/null +++ b/src/reports/source_hierarchy.rs @@ -0,0 +1,58 @@ +use crate::*; + +use ansi::*; + + +pub struct SourceHierarchy<'a> { + pub resolver: &'a Resolver, +} + +impl<'a> SourceHierarchy<'a> { + pub fn report(&self) { + eprintln!("."); + let len = self.resolver.root_unit_ids.len(); + for (i, id) in self.resolver.root_unit_ids.iter().enumerate() { + let end = i + 1 == len; + self.report_leaf(*id, Vec::new(), end); + } + } + + fn report_leaf(&self, id: usize, mut levels: Vec<bool>, end: bool) { + // A level entry is true if all entries in that level have been printed. + for level in &levels { + match level { + false => eprint!("│ "), + true => eprint!(" "), + } + } + // The end value is true if all siblings of this entry have been printed. + match end { + false => eprint!("├── "), + true => eprint!("└── "), + } + if let Some(unit) = self.resolver.source_units.get(id) { + let path_str = &unit.source_unit.main.path.as_os_str().to_string_lossy(); + if let Some(name_str) = unit.source_unit.name() { + eprint!("{name_str}{BLUE}"); + if unit.source_unit.head.is_some() { eprint!(" +head") } + if unit.source_unit.tail.is_some() { eprint!(" +tail") } + let mut unresolved = 0; + for symbol in &self.resolver.unresolved { + if symbol.source_id == id { unresolved += 1; } + } + if unresolved > 0 { eprint!("{RED} ({unresolved})"); } + eprintln!("{NORMAL} {DIM}({path_str}){NORMAL}"); + } else { + eprintln!("{path_str}"); + } + levels.push(end); + let len = unit.child_ids.len(); + for (i, id) in unit.child_ids.iter().enumerate() { + let end = i + 1 == len; + self.report_leaf(*id, levels.clone(), end); + } + } else { + eprintln!("<error loading source unit details>"); + } + } +} |