Batch changes for imports module

This commit is contained in:
collin 2020-10-27 13:13:16 -07:00
parent 817a3bd450
commit f275a7cddf
6 changed files with 28 additions and 61 deletions

View File

@ -45,15 +45,6 @@ impl ImportParserError {
Self::new_from_span(message, identifier.span)
}
///
/// A package name has been imported twice.
///
pub fn duplicate_import(name: String, span: Span) -> Self {
let message = format!("Duplicate imports found for `{}`.", name);
Self::new_from_span(message, span)
}
///
/// A core package name has been imported twice.
///

View File

@ -17,7 +17,10 @@
use crate::errors::ImportParserError;
use leo_typed::{Package, Program};
use std::{collections::HashMap, env::current_dir};
use std::{
collections::{HashMap, HashSet},
env::current_dir,
};
/// Stores imported packages.
///
@ -26,7 +29,7 @@ use std::{collections::HashMap, env::current_dir};
#[derive(Clone)]
pub struct ImportParser {
imports: HashMap<String, Program>,
core_packages: Vec<Package>,
core_packages: HashSet<Package>,
}
impl ImportParser {
@ -36,7 +39,7 @@ impl ImportParser {
pub fn new() -> Self {
Self {
imports: HashMap::new(),
core_packages: vec![],
core_packages: HashSet::new(),
}
}
@ -48,7 +51,7 @@ impl ImportParser {
///
pub(crate) fn insert_import(&mut self, file_name: String, program: Program) {
// Insert the imported program.
let _program = self.imports.insert(file_name.clone(), program);
let _program = self.imports.insert(file_name, program);
}
///
@ -65,7 +68,7 @@ impl ImportParser {
}
// Append the core package.
self.core_packages.push(package.clone());
self.core_packages.insert(package.clone());
Ok(())
}
@ -81,16 +84,7 @@ impl ImportParser {
/// Returns a reference to the core package corresponding to the given package.
///
pub fn get_core_package(&self, package: &Package) -> Option<&Package> {
self.core_packages()
.iter()
.find(|core_package| core_package.eq(&package))
}
///
/// Returns a reference to the vector of core packages.
///
pub fn core_packages(&self) -> &Vec<Package> {
&self.core_packages
self.core_packages.iter().find(|core_package| core_package.eq(&package))
}
///
@ -108,11 +102,9 @@ impl ImportParser {
let path = current_dir().map_err(|error| ImportParserError::current_directory_error(error))?;
// Parse each import statement.
program
.imports
.iter()
.map(|import| imports.parse_package(path.clone(), &import.package))
.collect::<Result<Vec<()>, ImportParserError>>()?;
for import in &program.imports {
imports.parse_package(path.clone(), &import.package)?;
}
Ok(imports)
}

View File

@ -82,29 +82,7 @@ impl ImportParser {
// import * can only be invoked on a package with a library file or a leo file
if is_package || is_leo_file {
// Get the package typed syntax tree.
let program = parse_import_file(package, span)?;
// Insert the package's imports into the import parser.
program
.imports
.iter()
.map(|import| self.parse_package(package.path(), &import.package))
.collect::<Result<Vec<()>, ImportParserError>>()?;
// Get the package file name from the path.
let file_name_path = PathBuf::from(package.file_name());
let file_name = file_name_path
.file_stem()
.unwrap()
.to_os_string()
.into_string()
.unwrap(); // the file exists so these will not fail
// Attempt to insert the typed syntax tree for the imported package.
self.insert_import(file_name, program);
Ok(())
self.parse_import_package(package, span)
} else {
// importing * from a directory or non-leo file in `package/src/` is illegal
Err(ImportParserError::star(&package.path(), span.clone()))
@ -116,14 +94,20 @@ impl ImportParser {
///
pub fn parse_import_symbol(&mut self, package: &DirEntry, symbol: &ImportSymbol) -> Result<(), ImportParserError> {
// Get the package typed syntax tree.
let program = parse_import_file(package, &symbol.span)?;
self.parse_import_package(package, &symbol.span)
}
///
/// Import a symbol from a given package.
///
pub fn parse_import_package(&mut self, package: &DirEntry, span: &Span) -> Result<(), ImportParserError> {
// Get the package typed syntax tree.
let program = parse_import_file(package, span)?;
// Insert the package's imports into the import parser.
program
.imports
.iter()
.map(|import| self.parse_package(package.path(), &import.package))
.collect::<Result<Vec<()>, ImportParserError>>()?;
for import in &program.imports {
self.parse_package(package.path(), &import.package)?;
}
// Get the package file name from the path.
let file_name_path = PathBuf::from(package.file_name());

View File

@ -20,7 +20,7 @@ use leo_ast::imports::ImportSymbol as AstImportSymbol;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct ImportSymbol {
pub symbol: Identifier,
pub alias: Option<Identifier>,

View File

@ -20,7 +20,7 @@ use leo_ast::imports::Package as AstPackage;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Package {
pub name: Identifier,
pub access: PackageAccess,

View File

@ -20,7 +20,7 @@ use leo_ast::imports::PackageAccess as AstPackageAccess;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum PackageAccess {
Star(Span),
SubPackage(Box<Package>),