Compare commits

..

No commits in common. "analyzer-refactor" and "master" have entirely different histories.

View File

@ -1,46 +1,10 @@
use {super::*, CompileErrorKind::*};
#[derive(Default)]
struct DefinitionTracker<'src> {
definitions: HashMap<&'src str, (&'static str, Name<'src>)>,
}
impl<'src> DefinitionTracker<'src> {
fn define(
&mut self,
name: Name<'src>,
second_type: &'static str,
duplicates_allowed: bool,
) -> CompileResult<'src> {
if let Some((first_type, original)) = self.definitions.get(name.lexeme()) {
if !(*first_type == second_type && duplicates_allowed) {
let (original, redefinition) = if name.line < original.line {
(name, *original)
} else {
(*original, name)
};
return Err(redefinition.token.error(Redefinition {
first_type,
second_type,
name: name.lexeme(),
first: original.line,
}));
}
}
self.definitions.insert(name.lexeme(), (second_type, name));
Ok(())
}
}
#[derive(Default)]
pub(crate) struct Analyzer<'src> {
assignments: Table<'src, Assignment<'src>>,
aliases: Table<'src, Alias<'src, Name<'src>>>,
sets: Table<'src, Set<'src>>,
warnings: Vec<Warning>,
modules: Table<'src, Justfile<'src>>,
}
impl<'src> Analyzer<'src> {
@ -51,25 +15,66 @@ impl<'src> Analyzer<'src> {
root: &Path,
name: Option<Name<'src>>,
) -> CompileResult<'src, Justfile<'src>> {
let mut analyzer = Self::default();
Self::default().justfile(loaded, paths, asts, root, name)
}
let mut definitions = DefinitionTracker::default();
let mut unprocessed_recipes = Vec::new();
let mut unprocessed_assignments = Vec::new();
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();
let mut stack = Vec::new();
stack.push(asts.get(root).unwrap());
let mut warnings = Vec::new();
let mut modules: Table<Justfile> = Table::new();
let mut definitions: HashMap<&str, (&'static str, Name)> = HashMap::new();
let mut define = |name: Name<'src>,
second_type: &'static str,
duplicates_allowed: bool|
-> CompileResult<'src> {
if let Some((first_type, original)) = definitions.get(name.lexeme()) {
if !(*first_type == second_type && duplicates_allowed) {
let (original, redefinition) = if name.line < original.line {
(name, *original)
} else {
(*original, name)
};
return Err(redefinition.token.error(Redefinition {
first_type,
second_type,
name: name.lexeme(),
first: original.line,
}));
}
}
definitions.insert(name.lexeme(), (second_type, name));
Ok(())
};
while let Some(ast) = stack.pop() {
for item in &ast.items {
match item {
Item::Alias(alias) => {
definitions.define(alias.name, "alias", false)?;
define(alias.name, "alias", false)?;
Self::analyze_alias(alias)?;
analyzer.aliases.insert(alias.clone());
self.aliases.insert(alias.clone());
}
Item::Assignment(assignment) => {
unprocessed_assignments.push(assignment);
assignments.push(assignment);
}
Item::Comment(_) => (),
Item::Import { absolute, .. } => {
@ -79,67 +84,64 @@ impl<'src> Analyzer<'src> {
}
Item::Module { absolute, name, .. } => {
if let Some(absolute) = absolute {
definitions.define(*name, "module", false)?;
analyzer
.modules
.insert(Self::analyze(loaded, paths, asts, absolute, Some(*name))?);
define(*name, "module", false)?;
modules.insert(Self::analyze(loaded, paths, asts, absolute, Some(*name))?);
}
}
Item::Recipe(recipe) => {
if recipe.enabled() {
Self::analyze_recipe(recipe)?;
unprocessed_recipes.push(recipe);
recipes.push(recipe);
}
}
Item::Set(set) => {
analyzer.analyze_set(set)?;
analyzer.sets.insert(set.clone());
self.analyze_set(set)?;
self.sets.insert(set.clone());
}
}
}
analyzer.warnings.extend(ast.warnings.iter().cloned());
warnings.extend(ast.warnings.iter().cloned());
}
let settings = Settings::from_setting_iter(analyzer.sets.into_iter().map(|(_, set)| set.value));
let settings = Settings::from_setting_iter(self.sets.into_iter().map(|(_, set)| set.value));
for assignment in unprocessed_assignments {
let mut recipe_table: Table<'src, UnresolvedRecipe<'src>> = Table::default();
for assignment in assignments {
if !settings.allow_duplicate_variables
&& analyzer.assignments.contains_key(assignment.name.lexeme())
&& self.assignments.contains_key(assignment.name.lexeme())
{
return Err(assignment.name.token.error(DuplicateVariable {
variable: assignment.name.lexeme(),
}));
}
if analyzer
if self
.assignments
.get(assignment.name.lexeme())
.map_or(true, |original| assignment.depth <= original.depth)
{
analyzer.assignments.insert(assignment.clone());
self.assignments.insert(assignment.clone());
}
}
AssignmentResolver::resolve_assignments(&analyzer.assignments)?;
AssignmentResolver::resolve_assignments(&self.assignments)?;
let recipes = {
let mut recipe_table: Table<'src, UnresolvedRecipe<'src>> = Table::default();
for recipe in unprocessed_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)
{
recipe_table.insert(recipe.clone());
}
for recipe in recipes {
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)
{
recipe_table.insert(recipe.clone());
}
}
RecipeResolver::resolve_recipes(recipe_table, &analyzer.assignments)?
};
let recipes = RecipeResolver::resolve_recipes(recipe_table, &self.assignments)?;
let mut aliases = Table::new();
while let Some(alias) = analyzer.aliases.pop() {
while let Some(alias) = self.aliases.pop() {
aliases.insert(Self::resolve_alias(&recipes, alias)?);
}
@ -147,7 +149,7 @@ impl<'src> Analyzer<'src> {
Ok(Justfile {
aliases,
assignments: analyzer.assignments,
assignments: self.assignments,
default: recipes
.values()
.filter(|recipe| recipe.name.path == root)
@ -160,12 +162,12 @@ impl<'src> Analyzer<'src> {
}),
}),
loaded: loaded.into(),
modules: analyzer.modules,
modules,
name,
recipes,
settings,
source: root.into(),
warnings: analyzer.warnings,
warnings,
})
}