2022-12-27 20:16:18 -08:00
|
|
|
use {super::*, CompileErrorKind::*};
|
2017-11-17 17:28:06 -08:00
|
|
|
|
2019-12-07 03:09:21 -08:00
|
|
|
pub(crate) struct RecipeResolver<'src: 'run, 'run> {
|
2019-12-07 04:03:03 -08:00
|
|
|
unresolved_recipes: Table<'src, UnresolvedRecipe<'src>>,
|
2021-09-16 06:44:40 -07:00
|
|
|
resolved_recipes: Table<'src, Rc<Recipe<'src>>>,
|
|
|
|
assignments: &'run Table<'src, Assignment<'src>>,
|
2017-11-18 01:18:04 -08:00
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2019-12-07 03:09:21 -08:00
|
|
|
impl<'src: 'run, 'run> RecipeResolver<'src, 'run> {
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn resolve_recipes(
|
2019-12-07 04:03:03 -08:00
|
|
|
unresolved_recipes: Table<'src, UnresolvedRecipe<'src>>,
|
2023-12-28 19:16:46 -08:00
|
|
|
assignments: &'run Table<'src, Assignment<'src>>,
|
2021-07-26 01:26:06 -07:00
|
|
|
) -> CompileResult<'src, Table<'src, Rc<Recipe<'src>>>> {
|
2023-12-28 19:16:46 -08:00
|
|
|
let mut resolver = Self {
|
2021-07-19 18:10:35 -07:00
|
|
|
resolved_recipes: Table::new(),
|
2019-11-21 06:23:32 -08:00
|
|
|
unresolved_recipes,
|
2019-04-11 23:58:08 -07:00
|
|
|
assignments,
|
2017-11-18 01:18:04 -08:00
|
|
|
};
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2019-11-21 06:23:32 -08:00
|
|
|
while let Some(unresolved) = resolver.unresolved_recipes.pop() {
|
|
|
|
resolver.resolve_recipe(&mut Vec::new(), unresolved)?;
|
2017-11-18 01:18:04 -08:00
|
|
|
}
|
|
|
|
|
2019-11-21 06:23:32 -08:00
|
|
|
for recipe in resolver.resolved_recipes.values() {
|
2019-04-11 23:58:08 -07:00
|
|
|
for parameter in &recipe.parameters {
|
|
|
|
if let Some(expression) = ¶meter.default {
|
|
|
|
for variable in expression.variables() {
|
2019-11-07 10:55:15 -08:00
|
|
|
resolver.resolve_variable(&variable, &[])?;
|
2019-04-11 23:58:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 04:03:03 -08:00
|
|
|
for dependency in &recipe.dependencies {
|
|
|
|
for argument in &dependency.arguments {
|
|
|
|
for variable in argument.variables() {
|
|
|
|
resolver.resolve_variable(&variable, &recipe.parameters)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
for line in &recipe.body {
|
|
|
|
for fragment in &line.fragments {
|
|
|
|
if let Fragment::Interpolation { expression, .. } = fragment {
|
2017-11-18 01:18:04 -08:00
|
|
|
for variable in expression.variables() {
|
2019-11-07 10:55:15 -08:00
|
|
|
resolver.resolve_variable(&variable, &recipe.parameters)?;
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 06:23:32 -08:00
|
|
|
Ok(resolver.resolved_recipes)
|
2019-04-11 23:58:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_variable(
|
|
|
|
&self,
|
2019-12-07 03:09:21 -08:00
|
|
|
variable: &Token<'src>,
|
2019-04-11 23:58:08 -07:00
|
|
|
parameters: &[Parameter],
|
2023-12-29 12:16:31 -08:00
|
|
|
) -> CompileResult<'src> {
|
2019-04-15 22:40:02 -07:00
|
|
|
let name = variable.lexeme();
|
2024-05-18 16:12:11 -07:00
|
|
|
let undefined = !self.assignments.contains_key(name)
|
|
|
|
&& !parameters.iter().any(|p| p.name.lexeme() == name)
|
|
|
|
&& !constants().contains_key(name);
|
2019-11-07 11:24:08 -08:00
|
|
|
|
2019-04-11 23:58:08 -07:00
|
|
|
if undefined {
|
2019-11-07 11:24:08 -08:00
|
|
|
return Err(variable.error(UndefinedVariable { variable: name }));
|
2019-04-11 23:58:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-11-21 06:23:32 -08:00
|
|
|
fn resolve_recipe(
|
|
|
|
&mut self,
|
2019-12-07 03:09:21 -08:00
|
|
|
stack: &mut Vec<&'src str>,
|
2019-12-07 04:03:03 -08:00
|
|
|
recipe: UnresolvedRecipe<'src>,
|
2021-07-26 01:26:06 -07:00
|
|
|
) -> CompileResult<'src, Rc<Recipe<'src>>> {
|
2019-11-21 06:23:32 -08:00
|
|
|
if let Some(resolved) = self.resolved_recipes.get(recipe.name()) {
|
2020-01-15 02:16:13 -08:00
|
|
|
return Ok(Rc::clone(resolved));
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
2019-11-21 06:23:32 -08:00
|
|
|
|
|
|
|
stack.push(recipe.name());
|
|
|
|
|
2019-12-07 04:03:03 -08:00
|
|
|
let mut dependencies: Vec<Rc<Recipe>> = Vec::new();
|
2019-11-21 06:23:32 -08:00
|
|
|
for dependency in &recipe.dependencies {
|
2019-12-07 04:03:03 -08:00
|
|
|
let name = dependency.recipe.lexeme();
|
2019-11-21 06:23:32 -08:00
|
|
|
|
|
|
|
if let Some(resolved) = self.resolved_recipes.get(name) {
|
|
|
|
// dependency already resolved
|
2022-01-30 12:16:10 -08:00
|
|
|
dependencies.push(Rc::clone(resolved));
|
2019-11-21 06:23:32 -08:00
|
|
|
} else if stack.contains(&name) {
|
|
|
|
let first = stack[0];
|
|
|
|
stack.push(first);
|
|
|
|
return Err(
|
2019-12-07 04:03:03 -08:00
|
|
|
dependency.recipe.error(CircularRecipeDependency {
|
2019-11-07 10:55:15 -08:00
|
|
|
recipe: recipe.name(),
|
2019-11-21 06:23:32 -08:00
|
|
|
circle: stack
|
|
|
|
.iter()
|
2019-12-07 04:03:03 -08:00
|
|
|
.skip_while(|name| **name != dependency.recipe.lexeme())
|
2022-01-30 12:16:10 -08:00
|
|
|
.copied()
|
2019-11-21 06:23:32 -08:00
|
|
|
.collect(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else if let Some(unresolved) = self.unresolved_recipes.remove(name) {
|
|
|
|
// resolve unresolved dependency
|
2019-12-07 04:03:03 -08:00
|
|
|
dependencies.push(self.resolve_recipe(stack, unresolved)?);
|
2019-11-21 06:23:32 -08:00
|
|
|
} else {
|
|
|
|
// dependency is unknown
|
2019-12-07 04:03:03 -08:00
|
|
|
return Err(dependency.recipe.error(UnknownDependency {
|
2021-09-16 06:44:40 -07:00
|
|
|
recipe: recipe.name(),
|
2019-11-21 06:23:32 -08:00
|
|
|
unknown: name,
|
|
|
|
}));
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
}
|
2019-11-21 06:23:32 -08:00
|
|
|
|
2021-07-22 00:20:25 -07:00
|
|
|
stack.pop();
|
|
|
|
|
2019-12-07 04:03:03 -08:00
|
|
|
let resolved = Rc::new(recipe.resolve(dependencies)?);
|
2020-01-15 02:16:13 -08:00
|
|
|
self.resolved_recipes.insert(Rc::clone(&resolved));
|
2019-11-21 06:23:32 -08:00
|
|
|
Ok(resolved)
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-11-07 10:55:15 -08:00
|
|
|
mod tests {
|
2017-11-16 23:30:08 -08:00
|
|
|
use super::*;
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
analysis_error! {
|
2017-11-18 01:44:59 -08:00
|
|
|
name: circular_recipe_dependency,
|
|
|
|
input: "a: b\nb: a",
|
2019-04-15 22:40:02 -07:00
|
|
|
offset: 8,
|
2017-11-18 01:44:59 -08:00
|
|
|
line: 1,
|
|
|
|
column: 3,
|
2019-04-15 22:40:02 -07:00
|
|
|
width: 1,
|
2017-11-18 01:44:59 -08:00
|
|
|
kind: CircularRecipeDependency{recipe: "b", circle: vec!["a", "b", "a"]},
|
2017-11-17 17:28:06 -08:00
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
analysis_error! {
|
2017-11-18 01:44:59 -08:00
|
|
|
name: self_recipe_dependency,
|
|
|
|
input: "a: a",
|
2019-04-15 22:40:02 -07:00
|
|
|
offset: 3,
|
2017-11-18 01:44:59 -08:00
|
|
|
line: 0,
|
|
|
|
column: 3,
|
2019-04-15 22:40:02 -07:00
|
|
|
width: 1,
|
2017-11-18 01:44:59 -08:00
|
|
|
kind: CircularRecipeDependency{recipe: "a", circle: vec!["a", "a"]},
|
2017-11-17 17:28:06 -08:00
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
analysis_error! {
|
2017-11-18 01:44:59 -08:00
|
|
|
name: unknown_dependency,
|
|
|
|
input: "a: b",
|
2019-04-15 22:40:02 -07:00
|
|
|
offset: 3,
|
2017-11-18 01:44:59 -08:00
|
|
|
line: 0,
|
|
|
|
column: 3,
|
2019-04-15 22:40:02 -07:00
|
|
|
width: 1,
|
2017-11-18 01:44:59 -08:00
|
|
|
kind: UnknownDependency{recipe: "a", unknown: "b"},
|
2017-11-17 17:28:06 -08:00
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
analysis_error! {
|
2017-11-18 01:44:59 -08:00
|
|
|
name: unknown_interpolation_variable,
|
|
|
|
input: "x:\n {{ hello}}",
|
2019-04-15 22:40:02 -07:00
|
|
|
offset: 9,
|
2017-11-18 01:44:59 -08:00
|
|
|
line: 1,
|
|
|
|
column: 6,
|
2019-04-15 22:40:02 -07:00
|
|
|
width: 5,
|
2017-11-18 01:44:59 -08:00
|
|
|
kind: UndefinedVariable{variable: "hello"},
|
2017-11-17 17:28:06 -08:00
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
analysis_error! {
|
2017-11-18 01:44:59 -08:00
|
|
|
name: unknown_second_interpolation_variable,
|
2021-03-28 23:39:23 -07:00
|
|
|
input: "wtf:=\"x\"\nx:\n echo\n foo {{wtf}} {{ lol }}",
|
|
|
|
offset: 34,
|
2017-11-18 01:44:59 -08:00
|
|
|
line: 3,
|
|
|
|
column: 16,
|
2019-04-15 22:40:02 -07:00
|
|
|
width: 3,
|
2017-11-18 01:44:59 -08:00
|
|
|
kind: UndefinedVariable{variable: "lol"},
|
2017-11-17 17:28:06 -08:00
|
|
|
}
|
2017-12-02 05:37:10 -08:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
analysis_error! {
|
2019-04-11 23:58:08 -07:00
|
|
|
name: unknown_variable_in_default,
|
|
|
|
input: "a f=foo:",
|
2019-04-15 22:40:02 -07:00
|
|
|
offset: 4,
|
2019-04-11 23:58:08 -07:00
|
|
|
line: 0,
|
|
|
|
column: 4,
|
2019-04-15 22:40:02 -07:00
|
|
|
width: 3,
|
2019-04-11 23:58:08 -07:00
|
|
|
kind: UndefinedVariable{variable: "foo"},
|
|
|
|
}
|
2019-12-07 04:03:03 -08:00
|
|
|
|
|
|
|
analysis_error! {
|
|
|
|
name: unknown_variable_in_dependency_argument,
|
|
|
|
input: "bar x:\nfoo: (bar baz)",
|
|
|
|
offset: 17,
|
|
|
|
line: 1,
|
|
|
|
column: 10,
|
|
|
|
width: 3,
|
|
|
|
kind: UndefinedVariable{variable: "baz"},
|
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|