From d534d6ae11d6f08bdbc3e73525d8f797b9b9cf74 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Thu, 31 Oct 2024 17:33:01 +1300 Subject: Add location() method to SourceSpan struct A SourceSpan contains up to two SourceLocations: the location of the span in the parsed source file (called in_merged), and the location of the span in an original source file as per a path comment in the parsed source file (called in_source). In places where only one location can be reported, the in_source location is preferred but is not guaranteed to exist, so the in_merged location is used as a fallback. Because this pattern is used in multiple places, it was added as a method to SourceSpan and all occurrences of the pattern were replaced with a method call. --- src/print.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'src/print.rs') diff --git a/src/print.rs b/src/print.rs index 0c81c07..b2c2ba8 100644 --- a/src/print.rs +++ b/src/print.rs @@ -71,14 +71,10 @@ fn get_message_for_semantic_error(error: &SemanticParseError) -> String { format!("Block was not closed, add a '}}' character to close"), SemErr::UndefinedSymbol(name) => format!("Undefined symbol, no label or macro has been defined with the name '{name}'"), - SemErr::RedefinedSymbol((_, source)) => { - let location = source.in_source.as_ref().unwrap_or(&source.in_merged); - format!("Redefined symbol, first defined at {location}") - } - SemErr::MacroInvocationBeforeDefinition((_, source)) => { - let location = source.in_source.as_ref().unwrap_or(&source.in_merged); - format!("Macro used before definition, definition is at {location}") - } + SemErr::RedefinedSymbol((_, source)) => + format!("Redefined symbol, first defined at {}", source.location()), + SemErr::MacroInvocationBeforeDefinition((_, source)) => + format!("Macro used before definition, definition is at {}", source.location()), SemErr:: SyntaxError(syntax_error) => match syntax_error { SynErr::UnterminatedComment => format!("Unclosed comment, add a ')' character to close"), -- cgit v1.2.3-70-g09d2