add import directory parsing

This commit is contained in:
collin 2020-06-26 23:47:11 -07:00
parent 9c2a0e4ec6
commit 878310f793
3 changed files with 41 additions and 22 deletions

View File

@ -10,6 +10,8 @@ use leo_types::{ImportSymbol, Program, Span};
use snarkos_models::curves::{Field, PrimeField};
use std::fs::DirEntry;
static LIBRARY_FILE: &str = "src/lib.leo";
fn parse_import_file(entry: &DirEntry, span: &Span) -> Result<Program, ImportError> {
// make sure the given entry is file
let file_type = entry
@ -20,14 +22,18 @@ fn parse_import_file(entry: &DirEntry, span: &Span) -> Result<Program, ImportErr
.into_string()
.map_err(|_| ImportError::convert_os_string(span.clone()))?;
let mut file_path = entry.path();
if file_type.is_dir() {
return Err(ImportError::expected_file(file_name, span.clone()));
file_path.push(LIBRARY_FILE);
if !file_path.exists() {
return Err(ImportError::expected_file(file_name, span.clone()));
}
}
// Build the abstract syntax tree
let file_path = &entry.path();
let input_file = &LeoParser::load_file(file_path)?;
let syntax_tree = LeoParser::parse_file(file_path, input_file)?;
let input_file = &LeoParser::load_file(&file_path)?;
let syntax_tree = LeoParser::parse_file(&file_path, input_file)?;
// Generate aleo program from file
Ok(Program::from(syntax_tree, file_name.clone()))

View File

@ -6,7 +6,7 @@ use std::{fs, fs::DirEntry, path::PathBuf};
static SOURCE_FILE_EXTENSION: &str = ".leo";
static SOURCE_DIRECTORY_NAME: &str = "src/";
// pub(crate) static IMPORTS_DIRECTORY_NAME: &str = "imports/";
static IMPORTS_DIRECTORY_NAME: &str = "imports/";
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn enforce_package_access(
@ -54,26 +54,33 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
});
// search for package name in imports directory
// let mut source_directory = path.clone();
// source_directory.push(IMPORTS_DIRECTORY_NAME);
//
// let entries = fs::read_dir(source_directory)
// .map_err(|error| ImportError::directory_error(error, package_name.span.clone()))?
// .into_iter()
// .collect::<Result<Vec<_>, std::io::Error>>()
// .map_err(|error| ImportError::directory_error(error, package_name.span.clone()))?;
//
// let matched_import_entry = entries.into_iter().find(|entry| {
// entry.file_name().eq(&package_name.name)
// });
let mut imports_directory = path.clone();
imports_directory.push(IMPORTS_DIRECTORY_NAME);
// todo: return error if package name is present in both directories
if imports_directory.exists() {
let entries = fs::read_dir(imports_directory)
.map_err(|error| ImportError::directory_error(error, package_name.span.clone()))?
.into_iter()
.collect::<Result<Vec<_>, std::io::Error>>()
.map_err(|error| ImportError::directory_error(error, package_name.span.clone()))?;
// Enforce package access
if let Some(entry) = matched_source_entry {
self.enforce_package_access(scope, &entry, package.access)
let matched_import_entry = entries
.into_iter()
.find(|entry| entry.file_name().into_string().unwrap().eq(&package_name.name));
// Enforce package access and potential collisions
match (matched_source_entry, matched_import_entry) {
(Some(_), Some(_)) => Err(ImportError::conflicting_imports(package_name)),
(Some(source_entry), None) => self.enforce_package_access(scope, &source_entry, package.access),
(None, Some(import_entry)) => self.enforce_package_access(scope, &import_entry, package.access),
(None, None) => Err(ImportError::unknown_package(package_name)),
}
} else {
Err(ImportError::unknown_package(package_name))
// Enforce local package access with no found imports directory
match matched_source_entry {
Some(source_entry) => self.enforce_package_access(scope, &source_entry, package.access),
None => Err(ImportError::unknown_package(package_name)),
}
}
}
}

View File

@ -17,6 +17,12 @@ impl ImportError {
ImportError::Error(FormattedError::new_from_span(message, span))
}
pub fn conflicting_imports(identifier: Identifier) -> Self {
let message = format!("conflicting imports found for `{}`", identifier.name);
Self::new_from_span(message, identifier.span)
}
pub fn convert_os_string(span: Span) -> Self {
let message = format!("failed to convert file string name, maybe an illegal character?");