2019-11-07 10:55:15 -08:00
|
|
|
use crate::common::*;
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
use 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> {
|
2021-09-16 06:44:40 -07:00
|
|
|
recipes: Table<'src, UnresolvedRecipe<'src>>,
|
2019-11-10 23:17:47 -08:00
|
|
|
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> {
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) fn analyze(ast: Ast<'src>) -> CompileResult<'src, Justfile> {
|
2021-07-23 20:26:27 -07:00
|
|
|
Analyzer::default().justfile(ast)
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
pub(crate) fn justfile(mut self, ast: Ast<'src>) -> CompileResult<'src, Justfile<'src>> {
|
2022-02-14 18:37:06 -08:00
|
|
|
let mut recipes = Vec::new();
|
|
|
|
|
2021-07-23 20:26:27 -07:00
|
|
|
for item in ast.items {
|
2019-11-07 10:55:15 -08:00
|
|
|
match item {
|
|
|
|
Item::Alias(alias) => {
|
|
|
|
self.analyze_alias(&alias)?;
|
|
|
|
self.aliases.insert(alias);
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-11-07 10:55:15 -08:00
|
|
|
Item::Assignment(assignment) => {
|
|
|
|
self.analyze_assignment(&assignment)?;
|
|
|
|
self.assignments.insert(assignment);
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2021-06-08 01:01:27 -07:00
|
|
|
Item::Comment(_) => (),
|
2019-11-07 10:55:15 -08:00
|
|
|
Item::Recipe(recipe) => {
|
2022-02-14 18:37:06 -08:00
|
|
|
Self::analyze_recipe(&recipe)?;
|
|
|
|
recipes.push(recipe);
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-11-10 23:17:47 -08:00
|
|
|
Item::Set(set) => {
|
|
|
|
self.analyze_set(&set)?;
|
|
|
|
self.sets.insert(set);
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-10 23:17:47 -08:00
|
|
|
let mut settings = Settings::new();
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
for (_, set) in self.sets {
|
2019-11-10 23:17:47 -08:00
|
|
|
match set.value {
|
2022-02-14 18:37:06 -08:00
|
|
|
Setting::AllowDuplicateRecipes(allow_duplicate_recipes) => {
|
|
|
|
settings.allow_duplicate_recipes = allow_duplicate_recipes;
|
|
|
|
}
|
2021-03-28 22:38:07 -07:00
|
|
|
Setting::DotenvLoad(dotenv_load) => {
|
2021-03-30 17:30:32 -07:00
|
|
|
settings.dotenv_load = Some(dotenv_load);
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2021-03-28 22:38:07 -07:00
|
|
|
Setting::Export(export) => {
|
|
|
|
settings.export = export;
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2021-04-24 18:29:58 -07:00
|
|
|
Setting::PositionalArguments(positional_arguments) => {
|
|
|
|
settings.positional_arguments = positional_arguments;
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-11-10 23:17:47 -08:00
|
|
|
Setting::Shell(shell) => {
|
|
|
|
assert!(settings.shell.is_none());
|
|
|
|
settings.shell = Some(shell);
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2022-01-18 11:02:15 -08:00
|
|
|
Setting::WindowsPowerShell(windows_powershell) => {
|
|
|
|
settings.windows_powershell = windows_powershell;
|
|
|
|
}
|
2019-11-10 23:17:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 18:37:06 -08:00
|
|
|
let assignments = self.assignments;
|
|
|
|
|
|
|
|
AssignmentResolver::resolve_assignments(&assignments)?;
|
|
|
|
|
|
|
|
for recipe in recipes {
|
|
|
|
if let Some(original) = self.recipes.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);
|
|
|
|
}
|
|
|
|
|
|
|
|
let recipes = RecipeResolver::resolve_recipes(self.recipes, &assignments)?;
|
|
|
|
|
|
|
|
for recipe in recipes.values() {
|
|
|
|
for parameter in &recipe.parameters {
|
|
|
|
if assignments.contains_key(parameter.name.lexeme()) {
|
|
|
|
return Err(parameter.name.token().error(ParameterShadowsVariable {
|
|
|
|
parameter: parameter.name.lexeme(),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut aliases = Table::new();
|
|
|
|
while let Some(alias) = self.aliases.pop() {
|
|
|
|
aliases.insert(Self::resolve_alias(&recipes, alias)?);
|
|
|
|
}
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
Ok(Justfile {
|
2021-07-23 20:26:27 -07:00
|
|
|
warnings: ast.warnings,
|
2021-11-17 00:07:48 -08:00
|
|
|
first: recipes
|
|
|
|
.values()
|
|
|
|
.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)
|
|
|
|
}),
|
|
|
|
}),
|
2019-11-07 10:55:15 -08:00
|
|
|
aliases,
|
2019-11-10 23:17:47 -08:00
|
|
|
assignments,
|
|
|
|
recipes,
|
|
|
|
settings,
|
2019-11-07 10:55:15 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-14 18:37:06 -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()) {
|
|
|
|
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
|
|
|
|
.token()
|
|
|
|
.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(())
|
|
|
|
}
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
fn analyze_assignment(&self, assignment: &Assignment<'src>) -> CompileResult<'src, ()> {
|
2019-11-07 10:55:15 -08:00
|
|
|
if self.assignments.contains_key(assignment.name.lexeme()) {
|
|
|
|
return Err(assignment.name.token().error(DuplicateVariable {
|
|
|
|
variable: assignment.name.lexeme(),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
fn analyze_alias(&self, alias: &Alias<'src, Name<'src>>) -> CompileResult<'src, ()> {
|
2019-11-07 10:55:15 -08:00
|
|
|
let name = alias.name.lexeme();
|
|
|
|
|
|
|
|
if let Some(original) = self.aliases.get(name) {
|
|
|
|
return Err(alias.name.token().error(DuplicateAlias {
|
|
|
|
alias: name,
|
|
|
|
first: original.line_number(),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-11-10 23:17:47 -08:00
|
|
|
|
2021-07-26 01:26:06 -07: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
|
|
|
let token = alias.name.token();
|
|
|
|
// Make sure the alias doesn't conflict with any recipe
|
|
|
|
if let Some(recipe) = recipes.get(alias.name.lexeme()) {
|
|
|
|
return Err(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))),
|
2019-11-21 07:39:32 -08:00
|
|
|
None => Err(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,
|
|
|
|
kind: DuplicateAlias { alias: "foo", first: 0 },
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2019-11-07 10:55:15 -08:00
|
|
|
offset: 23,
|
|
|
|
line: 2,
|
|
|
|
column: 6,
|
|
|
|
width: 3,
|
|
|
|
kind: AliasShadowsRecipe {alias: "foo", recipe_line: 3},
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
kind: AliasShadowsRecipe { alias: "foo", recipe_line: 0 },
|
|
|
|
}
|
|
|
|
|
|
|
|
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: parameter_shadows_varible,
|
2021-03-28 23:39:23 -07:00
|
|
|
input: "foo := \"h\"\na foo:",
|
|
|
|
offset: 13,
|
2019-11-07 10:55:15 -08:00
|
|
|
line: 1,
|
|
|
|
column: 2,
|
|
|
|
width: 3,
|
|
|
|
kind: ParameterShadowsVariable{parameter: "foo"},
|
|
|
|
}
|
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: duplicate_recipe,
|
|
|
|
input: "a:\nb:\na:",
|
|
|
|
offset: 6,
|
|
|
|
line: 2,
|
|
|
|
column: 0,
|
|
|
|
width: 1,
|
|
|
|
kind: DuplicateRecipe{recipe: "a", first: 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|