Combine analyze() and justfile() functions

This commit is contained in:
Greg Shuflin 2024-06-03 15:47:56 -07:00
parent f5bb82dea3
commit 9f965094ba

View File

@ -15,17 +15,8 @@ impl<'src> Analyzer<'src> {
root: &Path,
name: Option<Name<'src>>,
) -> CompileResult<'src, Justfile<'src>> {
Self::default().justfile(loaded, paths, asts, root, name)
}
let mut analyzer = Self::default();
fn justfile(
mut self,
loaded: &[PathBuf],
paths: &HashMap<PathBuf, PathBuf>,
asts: &HashMap<PathBuf, Ast<'src>>,
root: &Path,
name: Option<Name<'src>>,
) -> CompileResult<'src, Justfile<'src>> {
let mut recipes = Vec::new();
let mut assignments = Vec::new();
@ -71,7 +62,7 @@ impl<'src> Analyzer<'src> {
Item::Alias(alias) => {
define(alias.name, "alias", false)?;
Self::analyze_alias(alias)?;
self.aliases.insert(alias.clone());
analyzer.aliases.insert(alias.clone());
}
Item::Assignment(assignment) => {
assignments.push(assignment);
@ -95,8 +86,8 @@ impl<'src> Analyzer<'src> {
}
}
Item::Set(set) => {
self.analyze_set(set)?;
self.sets.insert(set.clone());
analyzer.analyze_set(set)?;
analyzer.sets.insert(set.clone());
}
}
}
@ -104,29 +95,29 @@ impl<'src> Analyzer<'src> {
warnings.extend(ast.warnings.iter().cloned());
}
let settings = Settings::from_setting_iter(self.sets.into_iter().map(|(_, set)| set.value));
let settings = Settings::from_setting_iter(analyzer.sets.into_iter().map(|(_, set)| set.value));
let mut recipe_table: Table<'src, UnresolvedRecipe<'src>> = Table::default();
for assignment in assignments {
if !settings.allow_duplicate_variables
&& self.assignments.contains_key(assignment.name.lexeme())
&& analyzer.assignments.contains_key(assignment.name.lexeme())
{
return Err(assignment.name.token.error(DuplicateVariable {
variable: assignment.name.lexeme(),
}));
}
if self
if analyzer
.assignments
.get(assignment.name.lexeme())
.map_or(true, |original| assignment.depth <= original.depth)
{
self.assignments.insert(assignment.clone());
analyzer.assignments.insert(assignment.clone());
}
}
AssignmentResolver::resolve_assignments(&self.assignments)?;
AssignmentResolver::resolve_assignments(&analyzer.assignments)?;
for recipe in recipes {
define(recipe.name, "recipe", settings.allow_duplicate_recipes)?;
@ -138,10 +129,10 @@ impl<'src> Analyzer<'src> {
}
}
let recipes = RecipeResolver::resolve_recipes(recipe_table, &self.assignments)?;
let recipes = RecipeResolver::resolve_recipes(recipe_table, &analyzer.assignments)?;
let mut aliases = Table::new();
while let Some(alias) = self.aliases.pop() {
while let Some(alias) = analyzer.aliases.pop() {
aliases.insert(Self::resolve_alias(&recipes, alias)?);
}
@ -149,7 +140,7 @@ impl<'src> Analyzer<'src> {
Ok(Justfile {
aliases,
assignments: self.assignments,
assignments: analyzer.assignments,
default: recipes
.values()
.filter(|recipe| recipe.name.path == root)