Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
use crate::cmdline;
use fil_ast as ast;
use fil_utils::{Error, FilamentResult};
use std::{
collections::HashSet,
fs,
path::{Path, PathBuf},
};
/// Completely parse all dependecies of a Filament program
pub struct Resolver {
// Location of the library
lib: PathBuf,
// Location of the base file
input: PathBuf,
// Files that have already been imported
already_imported: HashSet<PathBuf>,
}
impl From<&cmdline::Opts> for Resolver {
fn from(opts: &cmdline::Opts) -> Self {
Self {
lib: opts.library.clone(),
input: opts.input.clone(),
already_imported: HashSet::new(),
}
}
}
impl Resolver {
/// Resolve import either using opts.library or relative the parent directory of the input file.
fn resolve_import(
&self,
imp: &String,
dir: &Path,
) -> FilamentResult<PathBuf> {
// Resolve against the library path
let mut lib_base = self.lib.clone();
lib_base.push(imp);
// Resove against base
let mut cur_base = dir.to_path_buf();
cur_base.push(imp);
if cur_base.exists() && lib_base.exists() {
let canon_base = fs::canonicalize(cur_base.clone()).unwrap();
let canon_lib_base = fs::canonicalize(lib_base.clone()).unwrap();
if canon_base != canon_lib_base {
Err(Error::misc(format!(
"Refusing to resolve ambiguous import: {}. Conflicting candidates found:\n{}\n{}",
imp,
fs::canonicalize(lib_base).unwrap().display(),
fs::canonicalize(cur_base).unwrap().display(),
)))
} else {
Ok(cur_base)
}
} else if cur_base.exists() {
Ok(cur_base)
} else if lib_base.exists() {
Ok(lib_base)
} else {
Err(Error::misc(format!(
"Could not resolve import path: {}. Neither {} nor {} exist.",
imp,
lib_base.display(),
cur_base.display()
)))
}
}
// Get absolute path for a relative path `ext_path` wrt to the parent of the file `file_path`.
fn absolute(ext_path: String, base: &Path) -> String {
let ext: PathBuf = ext_path.into();
if ext.is_relative() {
let mut base = base.to_path_buf();
base.push(ext);
base
} else {
ext
}
.to_string_lossy()
.to_string()
}
/// Get the parent of a given file
fn parent(p: &Path) -> PathBuf {
let mut p = p.to_path_buf();
p.pop();
p
}
/// Returns the path if it has not already been parsed before
fn add_import(&mut self, p: PathBuf) -> Option<PathBuf> {
let c = fs::canonicalize(&p).unwrap();
if self.already_imported.contains(&c) {
None
} else {
self.already_imported.insert(c);
Some(p)
}
}
pub fn parse_namespace(&mut self) -> FilamentResult<ast::Namespace> {
// Parse the top-level file
let mut ns = ast::FilamentParser::parse_file(&self.input)?;
// Extern are resolved to thier absolute path relative to the input file.
let base = Self::parent(&self.input);
let mut imports: Vec<PathBuf> = ns
.imports
.drain(..)
.filter_map(|s| match self.resolve_import(&s, &base) {
Ok(file) => self.add_import(file).map(Ok),
f @ Err(_) => Some(f),
})
.collect::<FilamentResult<_>>()?;
ns.externs = ns
.externs
.drain(..)
.map(|ext| ext.map_path(|p| Self::absolute(p, &base)))
.collect();
while let Some(path) = imports.pop() {
let mut imp = ast::FilamentParser::parse_file(&path)?;
let base = Self::parent(&path);
imp.components.append(&mut ns.components);
ns.components = imp.components;
ns.externs.extend(
imp.externs
.into_iter()
.map(|ext| ext.map_path(|p| Self::absolute(p, &base))),
);
let base = Self::parent(&path);
imports.extend(
imp.imports
.into_iter()
.filter_map(|s| match self.resolve_import(&s, &base) {
Ok(file) => self.add_import(file).map(Ok),
f @ Err(_) => Some(f),
})
.collect::<FilamentResult<Vec<_>>>()?,
);
}
log::trace!("Imported: {:#?}", self.already_imported);
log::trace!(
"Components: {:#?}",
ns.components
.iter()
.map(|c| c.sig.name.as_ref())
.collect::<Vec<_>>()
);
log::trace!(
"Externs: {:#?}",
ns.externs
.iter()
.flat_map(|ast::Extern { comps, .. }| comps)
.map(|c| c.name.as_ref())
.collect::<Vec<_>>()
);
Ok(ns)
}
}
70914e6feThis file is pinned to the selected release. Download the original to open it in your design tool.