Skip to content

Commit

Permalink
WIP remove definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak committed Sep 20, 2024
1 parent e6ce5b8 commit c03e432
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@ 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>>,
unexports: HashSet<String>,
warnings: Vec<Warning>,
}

#[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))
Expand All @@ -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<PathBuf, Ast<'src>>,
doc: Option<String>,
Expand All @@ -58,15 +54,14 @@ 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();

while let Some(ast) = stack.pop() {
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());
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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<Name<'src>> = 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 {
Expand All @@ -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)
Expand Down

0 comments on commit c03e432

Please sign in to comment.