From c03e43202e796ff511d03ebe073c83e6e5f99d7c Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 20 Sep 2024 02:05:28 -0700 Subject: [PATCH] WIP remove definitions --- src/analyzer.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/analyzer.rs b/src/analyzer.rs index 03a31c89a0..3132047004 100644 --- a/src/analyzer.rs +++ b/src/analyzer.rs @@ -4,6 +4,7 @@ use {super::*, CompileErrorKind::*}; pub(crate) struct Analyzer<'run, 'src> { aliases: Table<'src, Alias<'src, Name<'src>>>, assignments: Vec<&'run Binding<'src, Expression<'src>>>, + definitions: HashMap<&'src str, (&'static str, Name<'src>)>, modules: Table<'src, Justfile<'src>>, recipes: Vec<&'run Recipe<'src, UnresolvedDependency<'src>>>, sets: Table<'src, Set<'src>>, @@ -11,17 +12,15 @@ pub(crate) struct Analyzer<'run, 'src> { warnings: Vec, } -#[derive(Default)] -struct Definitions<'src>(HashMap<&'src str, (&'static str, Name<'src>)>); -impl<'src> Definitions<'src> { +impl<'run, 'src> Analyzer<'run, 'src> { fn define( &mut self, name: Name<'src>, second_type: &'static str, duplicates_allowed: bool, ) -> CompileResult<'src> { - if let Some((first_type, original)) = self.0.get(name.lexeme()) { + if let Some((first_type, original)) = self.definitions.get(name.lexeme()) { if !(*first_type == second_type && duplicates_allowed) { let ((first_type, second_type), (original, redefinition)) = if name.line < original.line { ((second_type, *first_type), (name, *original)) @@ -38,13 +37,10 @@ impl<'src> Definitions<'src> { } } - self.0.insert(name.lexeme(), (second_type, name)); + self.definitions.insert(name.lexeme(), (second_type, name)); Ok(()) } -} - -impl<'run, 'src> Analyzer<'run, 'src> { pub(crate) fn analyze( asts: &HashMap>, doc: Option, @@ -58,7 +54,6 @@ impl<'run, 'src> Analyzer<'run, 'src> { let ast = asts.get(root).unwrap(); stack.push(ast); - let mut definitions = Definitions::default(); let mut analyzer = Analyzer::default(); @@ -66,7 +61,7 @@ impl<'run, 'src> Analyzer<'run, 'src> { for item in &ast.items { match item { Item::Alias(alias) => { - definitions.define(alias.name, "alias", false)?; + analyzer.define(alias.name, "alias", false)?; Self::analyze_alias(alias)?; analyzer.aliases.insert(alias.clone()); } @@ -103,7 +98,7 @@ impl<'run, 'src> Analyzer<'run, 'src> { } if let Some(absolute) = absolute { - definitions.define(*name, "module", false)?; + analyzer.define(*name, "module", false)?; analyzer.modules.insert(Self::analyze( asts, doc_attr.or(*doc).map(ToOwned::to_owned), @@ -138,7 +133,11 @@ impl<'run, 'src> Analyzer<'run, 'src> { analyzer.warnings.extend(ast.warnings.iter().cloned()); } - let settings = Settings::from_setting_iter(analyzer.sets.into_iter().map(|(_, set)| set.value)); + let recipe_names: Vec> = analyzer.recipes.iter().map(|recipe| recipe.name.clone()).collect(); + let settings = Settings::from_setting_iter(analyzer.sets.iter().map(|(_, set)| set.value)); + for name in recipe_names { + analyzer.define(name, "recipe", settings.allow_duplicate_recipes)?; + } let mut assignments: Table<'src, Assignment<'src>> = Table::default(); for assignment in analyzer.assignments { @@ -163,7 +162,6 @@ impl<'run, 'src> Analyzer<'run, 'src> { let mut recipe_table: Table<'src, UnresolvedRecipe<'src>> = Table::default(); for recipe in analyzer.recipes { - definitions.define(recipe.name, "recipe", settings.allow_duplicate_recipes)?; if recipe_table .get(recipe.name.lexeme()) .map_or(true, |original| recipe.file_depth <= original.file_depth)