2022-12-27 20:16:18 -08:00
|
|
|
use {super::*, CompileErrorKind::*};
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2021-01-22 23:34:01 -08:00
|
|
|
#[derive(Default)]
|
2019-11-10 23:17:47 -08:00
|
|
|
pub(crate) struct Analyzer<'src> {
|
|
|
|
assignments: Table<'src, Assignment<'src>>,
|
2021-09-16 06:44:40 -07:00
|
|
|
aliases: Table<'src, Alias<'src, Name<'src>>>,
|
|
|
|
sets: Table<'src, Set<'src>>,
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
2019-11-10 23:17:47 -08:00
|
|
|
impl<'src> Analyzer<'src> {
|
2023-11-21 11:28:59 -08:00
|
|
|
pub(crate) fn analyze(
|
2023-12-27 20:27:15 -08:00
|
|
|
loaded: &[PathBuf],
|
2023-11-22 10:27:49 -08:00
|
|
|
paths: &HashMap<PathBuf, PathBuf>,
|
2023-11-21 11:28:59 -08:00
|
|
|
asts: &HashMap<PathBuf, Ast<'src>>,
|
|
|
|
root: &Path,
|
2024-05-25 01:01:37 -07:00
|
|
|
name: Option<Name<'src>>,
|
2023-11-21 11:28:59 -08:00
|
|
|
) -> CompileResult<'src, Justfile<'src>> {
|
2024-05-25 01:01:37 -07:00
|
|
|
Self::default().justfile(loaded, paths, asts, root, name)
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
2023-11-21 11:28:59 -08:00
|
|
|
fn justfile(
|
|
|
|
mut self,
|
2023-12-27 20:27:15 -08:00
|
|
|
loaded: &[PathBuf],
|
2023-11-22 10:27:49 -08:00
|
|
|
paths: &HashMap<PathBuf, PathBuf>,
|
2023-11-21 11:28:59 -08:00
|
|
|
asts: &HashMap<PathBuf, Ast<'src>>,
|
|
|
|
root: &Path,
|
2024-05-25 01:01:37 -07:00
|
|
|
name: Option<Name<'src>>,
|
2023-11-21 11:28:59 -08:00
|
|
|
) -> CompileResult<'src, Justfile<'src>> {
|
2022-02-14 18:37:06 -08:00
|
|
|
let mut recipes = Vec::new();
|
|
|
|
|
2024-05-14 18:39:42 -07:00
|
|
|
let mut assignments = Vec::new();
|
|
|
|
|
2023-11-21 11:28:59 -08:00
|
|
|
let mut stack = Vec::new();
|
|
|
|
stack.push(asts.get(root).unwrap());
|
|
|
|
|
|
|
|
let mut warnings = Vec::new();
|
|
|
|
|
2024-05-25 01:01:37 -07:00
|
|
|
let mut modules: Table<Justfile> = Table::new();
|
2023-12-27 20:27:15 -08:00
|
|
|
|
2024-06-05 13:16:47 -07:00
|
|
|
let mut unexports: HashSet<String> = HashSet::new();
|
|
|
|
|
2023-12-27 20:27:15 -08:00
|
|
|
let mut definitions: HashMap<&str, (&'static str, Name)> = HashMap::new();
|
|
|
|
|
|
|
|
let mut define = |name: Name<'src>,
|
|
|
|
second_type: &'static str,
|
|
|
|
duplicates_allowed: bool|
|
2023-12-29 12:16:31 -08:00
|
|
|
-> CompileResult<'src> {
|
2023-12-27 20:27:15 -08:00
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
2024-01-08 13:26:33 -08:00
|
|
|
return Err(redefinition.token.error(Redefinition {
|
2023-12-27 20:27:15 -08:00
|
|
|
first_type,
|
|
|
|
second_type,
|
|
|
|
name: name.lexeme(),
|
|
|
|
first: original.line,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
definitions.insert(name.lexeme(), (second_type, name));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
2023-11-21 11:28:59 -08:00
|
|
|
while let Some(ast) = stack.pop() {
|
|
|
|
for item in &ast.items {
|
|
|
|
match item {
|
|
|
|
Item::Alias(alias) => {
|
2023-12-27 20:27:15 -08:00
|
|
|
define(alias.name, "alias", false)?;
|
|
|
|
Self::analyze_alias(alias)?;
|
2023-11-21 11:28:59 -08:00
|
|
|
self.aliases.insert(alias.clone());
|
|
|
|
}
|
|
|
|
Item::Assignment(assignment) => {
|
2024-05-14 18:39:42 -07:00
|
|
|
assignments.push(assignment);
|
2023-11-21 11:28:59 -08:00
|
|
|
}
|
|
|
|
Item::Comment(_) => (),
|
2023-12-27 20:27:15 -08:00
|
|
|
Item::Import { absolute, .. } => {
|
2023-12-29 12:16:31 -08:00
|
|
|
if let Some(absolute) = absolute {
|
|
|
|
stack.push(asts.get(absolute).unwrap());
|
|
|
|
}
|
2023-12-27 20:27:15 -08:00
|
|
|
}
|
2023-12-29 12:16:31 -08:00
|
|
|
Item::Module { absolute, name, .. } => {
|
|
|
|
if let Some(absolute) = absolute {
|
|
|
|
define(*name, "module", false)?;
|
2024-05-25 01:01:37 -07:00
|
|
|
modules.insert(Self::analyze(loaded, paths, asts, absolute, Some(*name))?);
|
2023-12-29 12:16:31 -08:00
|
|
|
}
|
2023-12-27 20:27:15 -08:00
|
|
|
}
|
2023-11-21 11:28:59 -08:00
|
|
|
Item::Recipe(recipe) => {
|
|
|
|
if recipe.enabled() {
|
|
|
|
Self::analyze_recipe(recipe)?;
|
|
|
|
recipes.push(recipe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Item::Set(set) => {
|
|
|
|
self.analyze_set(set)?;
|
|
|
|
self.sets.insert(set.clone());
|
|
|
|
}
|
2024-06-05 13:16:47 -07:00
|
|
|
Item::Unexport { name } => {
|
|
|
|
if !unexports.insert(name.lexeme().to_string()) {
|
|
|
|
return Err(name.token.error(DuplicateUnexport {
|
|
|
|
variable: name.lexeme(),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
2023-11-21 11:28:59 -08:00
|
|
|
|
|
|
|
warnings.extend(ast.warnings.iter().cloned());
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
2023-01-26 19:03:04 -08:00
|
|
|
let settings = Settings::from_setting_iter(self.sets.into_iter().map(|(_, set)| set.value));
|
2022-02-14 18:37:06 -08:00
|
|
|
|
2023-06-12 09:53:55 -07:00
|
|
|
let mut recipe_table: Table<'src, UnresolvedRecipe<'src>> = Table::default();
|
2023-01-24 20:06:33 -08:00
|
|
|
|
2024-05-14 18:39:42 -07:00
|
|
|
for assignment in assignments {
|
2024-06-05 13:16:47 -07:00
|
|
|
let variable = assignment.name.lexeme();
|
|
|
|
|
|
|
|
if !settings.allow_duplicate_variables && self.assignments.contains_key(variable) {
|
|
|
|
return Err(assignment.name.token.error(DuplicateVariable { variable }));
|
2024-05-14 18:39:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if self
|
|
|
|
.assignments
|
2024-06-05 13:16:47 -07:00
|
|
|
.get(variable)
|
2024-05-14 18:39:42 -07:00
|
|
|
.map_or(true, |original| assignment.depth <= original.depth)
|
|
|
|
{
|
|
|
|
self.assignments.insert(assignment.clone());
|
|
|
|
}
|
2024-06-05 13:16:47 -07:00
|
|
|
|
|
|
|
if unexports.contains(variable) {
|
|
|
|
return Err(assignment.name.token.error(ExportUnexported { variable }));
|
|
|
|
}
|
2024-05-14 18:39:42 -07:00
|
|
|
}
|
|
|
|
|
2023-01-26 19:03:04 -08:00
|
|
|
AssignmentResolver::resolve_assignments(&self.assignments)?;
|
2022-02-14 18:37:06 -08:00
|
|
|
|
|
|
|
for recipe in recipes {
|
2023-12-27 20:27:15 -08:00
|
|
|
define(recipe.name, "recipe", settings.allow_duplicate_recipes)?;
|
2023-12-28 17:34:37 -08:00
|
|
|
if recipe_table
|
|
|
|
.get(recipe.name.lexeme())
|
2024-05-18 23:38:57 -07:00
|
|
|
.map_or(true, |original| recipe.file_depth <= original.file_depth)
|
2023-12-28 17:34:37 -08:00
|
|
|
{
|
|
|
|
recipe_table.insert(recipe.clone());
|
|
|
|
}
|
2022-02-14 18:37:06 -08:00
|
|
|
}
|
|
|
|
|
2024-06-21 13:39:34 -07:00
|
|
|
let recipes = RecipeResolver::resolve_recipes(&self.assignments, &settings, recipe_table)?;
|
2022-02-14 18:37:06 -08:00
|
|
|
|
|
|
|
let mut aliases = Table::new();
|
|
|
|
while let Some(alias) = self.aliases.pop() {
|
|
|
|
aliases.insert(Self::resolve_alias(&recipes, alias)?);
|
|
|
|
}
|
|
|
|
|
2023-11-22 10:27:49 -08:00
|
|
|
let root = paths.get(root).unwrap();
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
Ok(Justfile {
|
2024-05-25 01:01:37 -07:00
|
|
|
aliases,
|
|
|
|
assignments: self.assignments,
|
2023-11-22 10:33:55 -08:00
|
|
|
default: recipes
|
2021-11-17 00:07:48 -08:00
|
|
|
.values()
|
2023-11-22 10:27:49 -08:00
|
|
|
.filter(|recipe| recipe.name.path == root)
|
2021-11-17 00:07:48 -08:00
|
|
|
.fold(None, |accumulator, next| match accumulator {
|
|
|
|
None => Some(Rc::clone(next)),
|
|
|
|
Some(previous) => Some(if previous.line_number() < next.line_number() {
|
|
|
|
previous
|
|
|
|
} else {
|
|
|
|
Rc::clone(next)
|
|
|
|
}),
|
|
|
|
}),
|
2023-12-27 20:27:15 -08:00
|
|
|
loaded: loaded.into(),
|
2024-05-25 01:01:37 -07:00
|
|
|
modules,
|
|
|
|
name,
|
2019-11-10 23:17:47 -08:00
|
|
|
recipes,
|
|
|
|
settings,
|
2024-05-28 20:06:30 -07:00
|
|
|
source: root.into(),
|
2024-06-05 13:16:47 -07:00
|
|
|
unexports,
|
2023-11-21 11:28:59 -08:00
|
|
|
warnings,
|
2019-11-07 10:55:15 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-29 12:16:31 -08:00
|
|
|
fn analyze_recipe(recipe: &UnresolvedRecipe<'src>) -> CompileResult<'src> {
|
2019-11-07 10:55:15 -08:00
|
|
|
let mut parameters = BTreeSet::new();
|
|
|
|
let mut passed_default = false;
|
|
|
|
|
|
|
|
for parameter in &recipe.parameters {
|
|
|
|
if parameters.contains(parameter.name.lexeme()) {
|
2024-01-08 13:26:33 -08:00
|
|
|
return Err(parameter.name.token.error(DuplicateParameter {
|
2021-09-16 06:44:40 -07:00
|
|
|
recipe: recipe.name.lexeme(),
|
2019-11-07 10:55:15 -08:00
|
|
|
parameter: parameter.name.lexeme(),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
parameters.insert(parameter.name.lexeme());
|
|
|
|
|
|
|
|
if parameter.default.is_some() {
|
|
|
|
passed_default = true;
|
|
|
|
} else if passed_default {
|
|
|
|
return Err(
|
|
|
|
parameter
|
|
|
|
.name
|
2024-01-08 13:26:33 -08:00
|
|
|
.token
|
2019-11-07 10:55:15 -08:00
|
|
|
.error(RequiredParameterFollowsDefaultParameter {
|
|
|
|
parameter: parameter.name.lexeme(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut continued = false;
|
|
|
|
for line in &recipe.body {
|
|
|
|
if !recipe.shebang && !continued {
|
|
|
|
if let Some(Fragment::Text { token }) = line.fragments.first() {
|
|
|
|
let text = token.lexeme();
|
|
|
|
|
|
|
|
if text.starts_with(' ') || text.starts_with('\t') {
|
|
|
|
return Err(token.error(ExtraLeadingWhitespace));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continued = line.is_continuation();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-12-29 12:16:31 -08:00
|
|
|
fn analyze_alias(alias: &Alias<'src, Name<'src>>) -> CompileResult<'src> {
|
2019-11-07 10:55:15 -08:00
|
|
|
let name = alias.name.lexeme();
|
|
|
|
|
2024-01-12 18:44:13 -08:00
|
|
|
for attribute in &alias.attributes {
|
|
|
|
if *attribute != Attribute::Private {
|
2024-01-08 13:26:33 -08:00
|
|
|
return Err(alias.name.token.error(AliasInvalidAttribute {
|
2022-12-20 00:44:19 -08:00
|
|
|
alias: name,
|
2024-01-12 18:44:13 -08:00
|
|
|
attribute: attribute.clone(),
|
2022-12-20 00:44:19 -08:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-11-10 23:17:47 -08:00
|
|
|
|
2023-12-29 12:16:31 -08:00
|
|
|
fn analyze_set(&self, set: &Set<'src>) -> CompileResult<'src> {
|
2019-11-10 23:17:47 -08:00
|
|
|
if let Some(original) = self.sets.get(set.name.lexeme()) {
|
|
|
|
return Err(set.name.error(DuplicateSet {
|
|
|
|
setting: original.name.lexeme(),
|
2021-09-16 06:44:40 -07:00
|
|
|
first: original.name.line,
|
2019-11-10 23:17:47 -08:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-11-21 07:39:32 -08:00
|
|
|
|
|
|
|
fn resolve_alias(
|
|
|
|
recipes: &Table<'src, Rc<Recipe<'src>>>,
|
|
|
|
alias: Alias<'src, Name<'src>>,
|
2021-07-26 01:26:06 -07:00
|
|
|
) -> CompileResult<'src, Alias<'src>> {
|
2019-11-21 07:39:32 -08:00
|
|
|
// Make sure the alias doesn't conflict with any recipe
|
|
|
|
if let Some(recipe) = recipes.get(alias.name.lexeme()) {
|
2024-01-08 13:26:33 -08:00
|
|
|
return Err(alias.name.token.error(AliasShadowsRecipe {
|
2021-09-16 06:44:40 -07:00
|
|
|
alias: alias.name.lexeme(),
|
2019-11-21 07:39:32 -08:00
|
|
|
recipe_line: recipe.line_number(),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the target recipe exists
|
|
|
|
match recipes.get(alias.target.lexeme()) {
|
2020-01-15 02:16:13 -08:00
|
|
|
Some(target) => Ok(alias.resolve(Rc::clone(target))),
|
2024-01-08 13:26:33 -08:00
|
|
|
None => Err(alias.name.token.error(UnknownAliasTarget {
|
2021-09-16 06:44:40 -07:00
|
|
|
alias: alias.name.lexeme(),
|
2019-11-21 07:39:32 -08:00
|
|
|
target: alias.target.lexeme(),
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: duplicate_alias,
|
2021-03-28 23:39:23 -07:00
|
|
|
input: "alias foo := bar\nalias foo := baz",
|
|
|
|
offset: 23,
|
2019-11-07 10:55:15 -08:00
|
|
|
line: 1,
|
|
|
|
column: 6,
|
|
|
|
width: 3,
|
2023-12-27 20:27:15 -08:00
|
|
|
kind: Redefinition { first_type: "alias", second_type: "alias", name: "foo", first: 0 },
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: unknown_alias_target,
|
2021-03-28 23:39:23 -07:00
|
|
|
input: "alias foo := bar\n",
|
2019-11-07 10:55:15 -08:00
|
|
|
offset: 6,
|
|
|
|
line: 0,
|
|
|
|
column: 6,
|
|
|
|
width: 3,
|
|
|
|
kind: UnknownAliasTarget {alias: "foo", target: "bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: alias_shadows_recipe_before,
|
2021-03-28 23:39:23 -07:00
|
|
|
input: "bar: \n echo bar\nalias foo := bar\nfoo:\n echo foo",
|
2023-12-27 20:27:15 -08:00
|
|
|
offset: 34,
|
|
|
|
line: 3,
|
|
|
|
column: 0,
|
2019-11-07 10:55:15 -08:00
|
|
|
width: 3,
|
2023-12-27 20:27:15 -08:00
|
|
|
kind: Redefinition { first_type: "alias", second_type: "recipe", name: "foo", first: 2 },
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: alias_shadows_recipe_after,
|
2021-03-28 23:39:23 -07:00
|
|
|
input: "foo:\n echo foo\nalias foo := bar\nbar:\n echo bar",
|
2019-11-07 10:55:15 -08:00
|
|
|
offset: 22,
|
|
|
|
line: 2,
|
|
|
|
column: 6,
|
|
|
|
width: 3,
|
2023-12-27 20:27:15 -08:00
|
|
|
kind: Redefinition { first_type: "alias", second_type: "recipe", name: "foo", first: 0 },
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: required_after_default,
|
|
|
|
input: "hello arg='foo' bar:",
|
|
|
|
offset: 16,
|
|
|
|
line: 0,
|
|
|
|
column: 16,
|
|
|
|
width: 3,
|
|
|
|
kind: RequiredParameterFollowsDefaultParameter{parameter: "bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: duplicate_parameter,
|
|
|
|
input: "a b b:",
|
|
|
|
offset: 4,
|
|
|
|
line: 0,
|
|
|
|
column: 4,
|
|
|
|
width: 1,
|
|
|
|
kind: DuplicateParameter{recipe: "a", parameter: "b"},
|
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: duplicate_variadic_parameter,
|
|
|
|
input: "a b +b:",
|
|
|
|
offset: 5,
|
|
|
|
line: 0,
|
|
|
|
column: 5,
|
|
|
|
width: 1,
|
|
|
|
kind: DuplicateParameter{recipe: "a", parameter: "b"},
|
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: duplicate_recipe,
|
|
|
|
input: "a:\nb:\na:",
|
|
|
|
offset: 6,
|
|
|
|
line: 2,
|
|
|
|
column: 0,
|
|
|
|
width: 1,
|
2023-12-27 20:27:15 -08:00
|
|
|
kind: Redefinition { first_type: "recipe", second_type: "recipe", name: "a", first: 0 },
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: duplicate_variable,
|
2021-03-28 23:39:23 -07:00
|
|
|
input: "a := \"0\"\na := \"0\"",
|
|
|
|
offset: 9,
|
2019-11-07 10:55:15 -08:00
|
|
|
line: 1,
|
|
|
|
column: 0,
|
|
|
|
width: 1,
|
|
|
|
kind: DuplicateVariable{variable: "a"},
|
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: extra_whitespace,
|
|
|
|
input: "a:\n blah\n blarg",
|
|
|
|
offset: 10,
|
|
|
|
line: 2,
|
|
|
|
column: 1,
|
|
|
|
width: 6,
|
|
|
|
kind: ExtraLeadingWhitespace,
|
|
|
|
}
|
|
|
|
}
|