2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2019-11-07 10:55:15 -08:00
|
|
|
|
|
|
|
pub(crate) struct Compiler;
|
|
|
|
|
|
|
|
impl Compiler {
|
2023-11-21 11:28:59 -08:00
|
|
|
pub(crate) fn compile<'src>(
|
|
|
|
loader: &'src Loader,
|
|
|
|
root: &Path,
|
|
|
|
) -> RunResult<'src, Compilation<'src>> {
|
2024-05-14 20:07:41 -07:00
|
|
|
let mut asts = HashMap::<PathBuf, Ast>::new();
|
2024-07-07 20:45:03 -07:00
|
|
|
let mut loaded = Vec::new();
|
2024-05-14 20:07:41 -07:00
|
|
|
let mut paths = HashMap::<PathBuf, PathBuf>::new();
|
|
|
|
let mut srcs = HashMap::<PathBuf, &str>::new();
|
2023-11-21 11:28:59 -08:00
|
|
|
|
2024-01-08 13:26:33 -08:00
|
|
|
let mut stack = Vec::new();
|
|
|
|
stack.push(Source::root(root));
|
2023-11-21 11:28:59 -08:00
|
|
|
|
2024-01-08 13:26:33 -08:00
|
|
|
while let Some(current) = stack.pop() {
|
|
|
|
let (relative, src) = loader.load(root, ¤t.path)?;
|
2023-11-24 23:15:41 -08:00
|
|
|
loaded.push(relative.into());
|
2023-11-21 20:17:38 -08:00
|
|
|
let tokens = Lexer::lex(relative, src)?;
|
2024-01-11 19:00:38 -08:00
|
|
|
let mut ast = Parser::parse(
|
2024-05-18 23:38:57 -07:00
|
|
|
current.file_depth,
|
2024-01-11 19:00:38 -08:00
|
|
|
¤t.path,
|
2024-05-25 18:10:06 -07:00
|
|
|
¤t.import_offsets,
|
2024-01-11 19:00:38 -08:00
|
|
|
¤t.namepath,
|
2024-05-18 23:38:57 -07:00
|
|
|
current.submodule_depth,
|
2024-01-11 19:00:38 -08:00
|
|
|
&tokens,
|
|
|
|
¤t.working_directory,
|
|
|
|
)?;
|
2023-11-21 11:28:59 -08:00
|
|
|
|
2024-01-08 13:26:33 -08:00
|
|
|
paths.insert(current.path.clone(), relative.into());
|
|
|
|
srcs.insert(current.path.clone(), src);
|
2023-11-21 11:28:59 -08:00
|
|
|
|
|
|
|
for item in &mut ast.items {
|
2023-12-27 20:27:15 -08:00
|
|
|
match item {
|
2023-12-29 12:16:31 -08:00
|
|
|
Item::Module {
|
2023-12-28 04:23:58 -08:00
|
|
|
absolute,
|
2023-12-29 12:16:31 -08:00
|
|
|
name,
|
|
|
|
optional,
|
|
|
|
relative,
|
2024-06-28 21:13:11 -07:00
|
|
|
..
|
2023-12-28 04:23:58 -08:00
|
|
|
} => {
|
2024-01-08 13:26:33 -08:00
|
|
|
let parent = current.path.parent().unwrap();
|
2023-12-27 20:27:15 -08:00
|
|
|
|
2023-12-29 12:16:31 -08:00
|
|
|
let import = if let Some(relative) = relative {
|
|
|
|
let path = parent.join(Self::expand_tilde(&relative.cooked)?);
|
|
|
|
|
|
|
|
if path.is_file() {
|
|
|
|
Some(path)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2023-12-28 04:23:58 -08:00
|
|
|
} else {
|
|
|
|
Self::find_module_file(parent, *name)?
|
|
|
|
};
|
2023-12-27 20:27:15 -08:00
|
|
|
|
2023-12-29 12:16:31 -08:00
|
|
|
if let Some(import) = import {
|
2024-05-20 01:04:03 -07:00
|
|
|
if current.file_path.contains(&import) {
|
2024-01-08 13:26:33 -08:00
|
|
|
return Err(Error::CircularImport {
|
|
|
|
current: current.path,
|
|
|
|
import,
|
|
|
|
});
|
2023-12-29 12:16:31 -08:00
|
|
|
}
|
|
|
|
*absolute = Some(import.clone());
|
2024-01-08 13:26:33 -08:00
|
|
|
stack.push(current.module(*name, import));
|
2023-12-29 12:16:31 -08:00
|
|
|
} else if !*optional {
|
|
|
|
return Err(Error::MissingModuleFile { module: *name });
|
2023-12-27 20:27:15 -08:00
|
|
|
}
|
2023-11-21 11:28:59 -08:00
|
|
|
}
|
2023-12-29 12:16:31 -08:00
|
|
|
Item::Import {
|
|
|
|
relative,
|
|
|
|
absolute,
|
|
|
|
optional,
|
|
|
|
path,
|
|
|
|
} => {
|
2023-12-28 18:08:02 -08:00
|
|
|
let import = current
|
2024-01-08 13:26:33 -08:00
|
|
|
.path
|
2023-12-28 18:08:02 -08:00
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join(Self::expand_tilde(&relative.cooked)?)
|
|
|
|
.lexiclean();
|
2023-12-29 12:16:31 -08:00
|
|
|
|
|
|
|
if import.is_file() {
|
2024-05-20 01:04:03 -07:00
|
|
|
if current.file_path.contains(&import) {
|
2024-01-08 13:26:33 -08:00
|
|
|
return Err(Error::CircularImport {
|
|
|
|
current: current.path,
|
|
|
|
import,
|
|
|
|
});
|
2023-12-29 12:16:31 -08:00
|
|
|
}
|
|
|
|
*absolute = Some(import.clone());
|
2024-05-25 18:10:06 -07:00
|
|
|
stack.push(current.import(import, path.offset));
|
2023-12-29 12:16:31 -08:00
|
|
|
} else if !*optional {
|
|
|
|
return Err(Error::MissingImportFile { path: *path });
|
2023-12-27 20:27:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2023-11-21 11:28:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 13:26:33 -08:00
|
|
|
asts.insert(current.path, ast.clone());
|
2023-11-21 11:28:59 -08:00
|
|
|
}
|
|
|
|
|
2024-06-28 21:13:11 -07:00
|
|
|
let justfile = Analyzer::analyze(&asts, None, &loaded, None, &paths, root)?;
|
2023-11-21 11:28:59 -08:00
|
|
|
|
|
|
|
Ok(Compilation {
|
|
|
|
asts,
|
|
|
|
justfile,
|
|
|
|
root: root.into(),
|
2024-07-07 20:45:03 -07:00
|
|
|
srcs,
|
2023-11-21 11:28:59 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-29 12:16:31 -08:00
|
|
|
fn find_module_file<'src>(parent: &Path, module: Name<'src>) -> RunResult<'src, Option<PathBuf>> {
|
2023-12-27 20:27:15 -08:00
|
|
|
let mut candidates = vec![format!("{module}.just"), format!("{module}/mod.just")]
|
|
|
|
.into_iter()
|
|
|
|
.filter(|path| parent.join(path).is_file())
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
let directory = parent.join(module.lexeme());
|
|
|
|
|
|
|
|
if directory.exists() {
|
|
|
|
let entries = fs::read_dir(&directory).map_err(|io_error| SearchError::Io {
|
|
|
|
io_error,
|
|
|
|
directory: directory.clone(),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
for entry in entries {
|
|
|
|
let entry = entry.map_err(|io_error| SearchError::Io {
|
|
|
|
io_error,
|
|
|
|
directory: directory.clone(),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if let Some(name) = entry.file_name().to_str() {
|
|
|
|
for justfile_name in search::JUSTFILE_NAMES {
|
|
|
|
if name.eq_ignore_ascii_case(justfile_name) {
|
|
|
|
candidates.push(format!("{module}/{name}"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match candidates.as_slice() {
|
2023-12-29 12:16:31 -08:00
|
|
|
[] => Ok(None),
|
|
|
|
[file] => Ok(Some(parent.join(file).lexiclean())),
|
2023-12-27 20:27:15 -08:00
|
|
|
found => Err(Error::AmbiguousModuleFile {
|
|
|
|
found: found.into(),
|
|
|
|
module,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 18:08:02 -08:00
|
|
|
fn expand_tilde(path: &str) -> RunResult<'static, PathBuf> {
|
|
|
|
Ok(if let Some(path) = path.strip_prefix("~/") {
|
|
|
|
dirs::home_dir()
|
|
|
|
.ok_or(Error::Homedir)?
|
|
|
|
.join(path.trim_start_matches('/'))
|
|
|
|
} else {
|
|
|
|
PathBuf::from(path)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:28:59 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn test_compile(src: &str) -> CompileResult<Justfile> {
|
2023-11-21 20:17:38 -08:00
|
|
|
let tokens = Lexer::test_lex(src)?;
|
2024-01-11 19:00:38 -08:00
|
|
|
let ast = Parser::parse(
|
2024-05-18 23:38:57 -07:00
|
|
|
0,
|
2024-01-11 19:00:38 -08:00
|
|
|
&PathBuf::new(),
|
2024-05-25 18:10:06 -07:00
|
|
|
&[],
|
2024-01-11 19:00:38 -08:00
|
|
|
&Namepath::default(),
|
|
|
|
0,
|
|
|
|
&tokens,
|
|
|
|
&PathBuf::new(),
|
|
|
|
)?;
|
2023-11-21 20:17:38 -08:00
|
|
|
let root = PathBuf::from("justfile");
|
2023-11-21 11:28:59 -08:00
|
|
|
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
|
|
|
|
asts.insert(root.clone(), ast);
|
2023-11-22 10:27:49 -08:00
|
|
|
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
|
|
|
|
paths.insert(root.clone(), root.clone());
|
2024-06-28 21:13:11 -07:00
|
|
|
Analyzer::analyze(&asts, None, &[], None, &paths, &root)
|
2023-11-21 11:28:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use {super::*, temptree::temptree};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn include_justfile() {
|
|
|
|
let justfile_a = r#"
|
|
|
|
# A comment at the top of the file
|
2023-12-19 20:31:51 -08:00
|
|
|
import "./justfile_b"
|
2023-11-21 11:28:59 -08:00
|
|
|
|
|
|
|
#some_recipe: recipe_b
|
|
|
|
some_recipe:
|
|
|
|
echo "some recipe"
|
|
|
|
"#;
|
|
|
|
|
2023-12-19 20:31:51 -08:00
|
|
|
let justfile_b = r#"import "./subdir/justfile_c"
|
2023-11-21 11:28:59 -08:00
|
|
|
|
|
|
|
recipe_b: recipe_c
|
|
|
|
echo "recipe b"
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let justfile_c = r#"recipe_c:
|
|
|
|
echo "recipe c"
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let tmp = temptree! {
|
|
|
|
justfile: justfile_a,
|
|
|
|
justfile_b: justfile_b,
|
|
|
|
subdir: {
|
|
|
|
justfile_c: justfile_c
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let loader = Loader::new();
|
|
|
|
|
|
|
|
let justfile_a_path = tmp.path().join("justfile");
|
2024-07-07 20:45:03 -07:00
|
|
|
let compilation = Compiler::compile(&loader, &justfile_a_path).unwrap();
|
2023-11-21 11:28:59 -08:00
|
|
|
|
|
|
|
assert_eq!(compilation.root_src(), justfile_a);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive_includes_fail() {
|
|
|
|
let tmp = temptree! {
|
2024-05-20 01:04:03 -07:00
|
|
|
justfile: "import './subdir/b'\na: b",
|
|
|
|
subdir: {
|
|
|
|
b: "import '../justfile'\nb:"
|
|
|
|
}
|
2023-11-21 11:28:59 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
let loader = Loader::new();
|
|
|
|
|
|
|
|
let justfile_a_path = tmp.path().join("justfile");
|
2024-07-07 20:45:03 -07:00
|
|
|
let loader_output = Compiler::compile(&loader, &justfile_a_path).unwrap_err();
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2023-12-19 20:31:51 -08:00
|
|
|
assert_matches!(loader_output, Error::CircularImport { current, import }
|
2024-05-20 01:04:03 -07:00
|
|
|
if current == tmp.path().join("subdir").join("b").lexiclean() &&
|
|
|
|
import == tmp.path().join("justfile").lexiclean()
|
2023-11-21 11:28:59 -08:00
|
|
|
);
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|