Compare commits
No commits in common. "fc70cea9b08afc13314012965791882709bed834" and "f5bb82dea3edde07b5aca4303e2d73a300f45d5e" have entirely different histories.
fc70cea9b0
...
f5bb82dea3
148
src/analyzer.rs
148
src/analyzer.rs
@ -1,46 +1,10 @@
|
|||||||
use {super::*, CompileErrorKind::*};
|
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)]
|
#[derive(Default)]
|
||||||
pub(crate) struct Analyzer<'src> {
|
pub(crate) struct Analyzer<'src> {
|
||||||
assignments: Table<'src, Assignment<'src>>,
|
assignments: Table<'src, Assignment<'src>>,
|
||||||
aliases: Table<'src, Alias<'src, Name<'src>>>,
|
aliases: Table<'src, Alias<'src, Name<'src>>>,
|
||||||
sets: Table<'src, Set<'src>>,
|
sets: Table<'src, Set<'src>>,
|
||||||
warnings: Vec<Warning>,
|
|
||||||
modules: Table<'src, Justfile<'src>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'src> Analyzer<'src> {
|
impl<'src> Analyzer<'src> {
|
||||||
@ -51,25 +15,66 @@ impl<'src> Analyzer<'src> {
|
|||||||
root: &Path,
|
root: &Path,
|
||||||
name: Option<Name<'src>>,
|
name: Option<Name<'src>>,
|
||||||
) -> CompileResult<'src, Justfile<'src>> {
|
) -> CompileResult<'src, Justfile<'src>> {
|
||||||
let mut analyzer = Self::default();
|
Self::default().justfile(loaded, paths, asts, root, name)
|
||||||
|
}
|
||||||
|
|
||||||
let mut definitions = DefinitionTracker::default();
|
fn justfile(
|
||||||
let mut unprocessed_recipes = Vec::new();
|
mut self,
|
||||||
let mut unprocessed_assignments = Vec::new();
|
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();
|
let mut stack = Vec::new();
|
||||||
stack.push(asts.get(root).unwrap());
|
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() {
|
while let Some(ast) = stack.pop() {
|
||||||
for item in &ast.items {
|
for item in &ast.items {
|
||||||
match item {
|
match item {
|
||||||
Item::Alias(alias) => {
|
Item::Alias(alias) => {
|
||||||
definitions.define(alias.name, "alias", false)?;
|
define(alias.name, "alias", false)?;
|
||||||
Self::analyze_alias(alias)?;
|
Self::analyze_alias(alias)?;
|
||||||
analyzer.aliases.insert(alias.clone());
|
self.aliases.insert(alias.clone());
|
||||||
}
|
}
|
||||||
Item::Assignment(assignment) => {
|
Item::Assignment(assignment) => {
|
||||||
unprocessed_assignments.push(assignment);
|
assignments.push(assignment);
|
||||||
}
|
}
|
||||||
Item::Comment(_) => (),
|
Item::Comment(_) => (),
|
||||||
Item::Import { absolute, .. } => {
|
Item::Import { absolute, .. } => {
|
||||||
@ -79,67 +84,64 @@ impl<'src> Analyzer<'src> {
|
|||||||
}
|
}
|
||||||
Item::Module { absolute, name, .. } => {
|
Item::Module { absolute, name, .. } => {
|
||||||
if let Some(absolute) = absolute {
|
if let Some(absolute) = absolute {
|
||||||
definitions.define(*name, "module", false)?;
|
define(*name, "module", false)?;
|
||||||
analyzer
|
modules.insert(Self::analyze(loaded, paths, asts, absolute, Some(*name))?);
|
||||||
.modules
|
|
||||||
.insert(Self::analyze(loaded, paths, asts, absolute, Some(*name))?);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Item::Recipe(recipe) => {
|
Item::Recipe(recipe) => {
|
||||||
if recipe.enabled() {
|
if recipe.enabled() {
|
||||||
Self::analyze_recipe(recipe)?;
|
Self::analyze_recipe(recipe)?;
|
||||||
unprocessed_recipes.push(recipe);
|
recipes.push(recipe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Item::Set(set) => {
|
Item::Set(set) => {
|
||||||
analyzer.analyze_set(set)?;
|
self.analyze_set(set)?;
|
||||||
analyzer.sets.insert(set.clone());
|
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
|
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 {
|
return Err(assignment.name.token.error(DuplicateVariable {
|
||||||
variable: assignment.name.lexeme(),
|
variable: assignment.name.lexeme(),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
if analyzer
|
if self
|
||||||
.assignments
|
.assignments
|
||||||
.get(assignment.name.lexeme())
|
.get(assignment.name.lexeme())
|
||||||
.map_or(true, |original| assignment.depth <= original.depth)
|
.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 = {
|
for recipe in recipes {
|
||||||
let mut recipe_table: Table<'src, UnresolvedRecipe<'src>> = Table::default();
|
define(recipe.name, "recipe", settings.allow_duplicate_recipes)?;
|
||||||
for recipe in unprocessed_recipes {
|
if recipe_table
|
||||||
definitions.define(recipe.name, "recipe", settings.allow_duplicate_recipes)?;
|
.get(recipe.name.lexeme())
|
||||||
if recipe_table
|
.map_or(true, |original| recipe.file_depth <= original.file_depth)
|
||||||
.get(recipe.name.lexeme())
|
{
|
||||||
.map_or(true, |original| recipe.file_depth <= original.file_depth)
|
recipe_table.insert(recipe.clone());
|
||||||
{
|
|
||||||
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();
|
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)?);
|
aliases.insert(Self::resolve_alias(&recipes, alias)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +149,7 @@ impl<'src> Analyzer<'src> {
|
|||||||
|
|
||||||
Ok(Justfile {
|
Ok(Justfile {
|
||||||
aliases,
|
aliases,
|
||||||
assignments: analyzer.assignments,
|
assignments: self.assignments,
|
||||||
default: recipes
|
default: recipes
|
||||||
.values()
|
.values()
|
||||||
.filter(|recipe| recipe.name.path == root)
|
.filter(|recipe| recipe.name.path == root)
|
||||||
@ -160,12 +162,12 @@ impl<'src> Analyzer<'src> {
|
|||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
loaded: loaded.into(),
|
loaded: loaded.into(),
|
||||||
modules: analyzer.modules,
|
modules,
|
||||||
name,
|
name,
|
||||||
recipes,
|
recipes,
|
||||||
settings,
|
settings,
|
||||||
source: root.into(),
|
source: root.into(),
|
||||||
warnings: analyzer.warnings,
|
warnings,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user