Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyzer::analyze borrows rather than clones Ast #1527

Merged
merged 5 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,39 @@ const VALID_ALIAS_ATTRIBUTES: [Attribute; 1] = [Attribute::Private];

#[derive(Default)]
pub(crate) struct Analyzer<'src> {
recipes: Table<'src, UnresolvedRecipe<'src>>,
assignments: Table<'src, Assignment<'src>>,
aliases: Table<'src, Alias<'src, Name<'src>>>,
sets: Table<'src, Set<'src>>,
}

impl<'src> Analyzer<'src> {
pub(crate) fn analyze(ast: Ast<'src>) -> CompileResult<'src, Justfile> {
pub(crate) fn analyze(ast: &Ast<'src>) -> CompileResult<'src, Justfile<'src>> {
Analyzer::default().justfile(ast)
}

pub(crate) fn justfile(mut self, ast: Ast<'src>) -> CompileResult<'src, Justfile<'src>> {
fn justfile(mut self, ast: &Ast<'src>) -> CompileResult<'src, Justfile<'src>> {
let mut recipes = Vec::new();

for item in ast.items {
for item in &ast.items {
match item {
Item::Alias(alias) => {
self.analyze_alias(&alias)?;
self.aliases.insert(alias);
self.analyze_alias(alias)?;
self.aliases.insert(alias.clone());
}
Item::Assignment(assignment) => {
self.analyze_assignment(&assignment)?;
self.assignments.insert(assignment);
self.analyze_assignment(assignment)?;
self.assignments.insert(assignment.clone());
}
Item::Comment(_) => (),
Item::Recipe(recipe) => {
if recipe.enabled() {
Self::analyze_recipe(&recipe)?;
Self::analyze_recipe(recipe)?;
recipes.push(recipe);
}
}
Item::Set(set) => {
self.analyze_set(&set)?;
self.sets.insert(set);
self.analyze_set(set)?;
self.sets.insert(set.clone());
}
}
}
Expand Down Expand Up @@ -81,29 +80,31 @@ impl<'src> Analyzer<'src> {

let assignments = self.assignments;

let mut recipe_table: Table<'src, UnresolvedRecipe<'src>> = Default::default();

AssignmentResolver::resolve_assignments(&assignments)?;

for recipe in recipes {
if let Some(original) = self.recipes.get(recipe.name.lexeme()) {
if let Some(original) = recipe_table.get(recipe.name.lexeme()) {
if !settings.allow_duplicate_recipes {
return Err(recipe.name.token().error(DuplicateRecipe {
recipe: original.name(),
first: original.line_number(),
}));
}
}
self.recipes.insert(recipe);
recipe_table.insert(recipe.clone());
}

let recipes = RecipeResolver::resolve_recipes(self.recipes, &assignments)?;
let recipes = RecipeResolver::resolve_recipes(recipe_table, &assignments)?;

let mut aliases = Table::new();
while let Some(alias) = self.aliases.pop() {
aliases.insert(Self::resolve_alias(&recipes, alias)?);
}

Ok(Justfile {
warnings: ast.warnings,
warnings: ast.warnings.clone(),
first: recipes
.values()
.fold(None, |accumulator, next| match accumulator {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ impl Compiler {

let ast = Parser::parse(&tokens)?;

Analyzer::analyze(ast)
Analyzer::analyze(&ast)
}
}
2 changes: 1 addition & 1 deletion src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Subcommand {

let tokens = Lexer::lex(src)?;
let ast = Parser::parse(&tokens)?;
let justfile = Analyzer::analyze(ast.clone())?;
let justfile = Analyzer::analyze(&ast)?;

if config.verbosity.loud() {
for warning in &justfile.warnings {
Expand Down
2 changes: 1 addition & 1 deletion src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub(crate) fn analysis_error(

let ast = Parser::parse(&tokens).expect("Parsing failed in analysis test...");

match Analyzer::analyze(ast) {
match Analyzer::analyze(&ast) {
Ok(_) => panic!("Analysis unexpectedly succeeded"),
Err(have) => {
let want = CompileError {
Expand Down