From 1409b636a58d87f7c759e5839cb2116ec7e7e970 Mon Sep 17 00:00:00 2001 From: Laurence Tratt Date: Tue, 4 Jun 2024 12:01:20 +0100 Subject: [PATCH] Give better warnings if lexer/grammar can't be read at build time. If the user gave an incorrect path, they got an internal `unwrap`, or similarly unhelpful message, from lrlex and lrpar. This commit tells them what file was attempting to be read, which makes working out what's actually gone wrong much easier. --- lrlex/src/lib/ctbuilder.rs | 3 ++- lrpar/src/lib/ctbuilder.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lrlex/src/lib/ctbuilder.rs b/lrlex/src/lib/ctbuilder.rs index e5c4b5d93..a0d317a75 100644 --- a/lrlex/src/lib/ctbuilder.rs +++ b/lrlex/src/lib/ctbuilder.rs @@ -325,7 +325,8 @@ where lk.insert(outp.clone()); } - let lex_src = read_to_string(lexerp)?; + let lex_src = read_to_string(lexerp) + .map_err(|e| format!("When reading '{}': {e}", lexerp.display()))?; let line_cache = NewlineCache::from_str(&lex_src).unwrap(); let mut lexerdef: Box> = match self.lexerkind { LexerKind::LRNonStreamingLexer => Box::new( diff --git a/lrpar/src/lib/ctbuilder.rs b/lrpar/src/lib/ctbuilder.rs index b2435d9e2..0819f75c2 100644 --- a/lrpar/src/lib/ctbuilder.rs +++ b/lrpar/src/lib/ctbuilder.rs @@ -414,7 +414,8 @@ where lk.insert(outp.clone()); } - let inc = read_to_string(grmp).unwrap(); + let inc = + read_to_string(grmp).map_err(|e| format!("When reading '{}': {e}", grmp.display()))?; let ast_validation = ASTWithValidityInfo::new(yk, &inc); let warnings = ast_validation.ast().warnings(); let spanned_fmt = |x: &dyn Spanned, inc: &str, line_cache: &NewlineCache| {