mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-24 07:48:04 +03:00
remove old import parser code from compiler
This commit is contained in:
parent
d54749145b
commit
557fccd9da
@ -14,11 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use leo_ast::ParserError;
|
||||
use leo_typed::{Error as FormattedError, Identifier, ImportSymbol, Span};
|
||||
|
||||
use leo_core::LeoCoreError;
|
||||
use std::{io, path::PathBuf};
|
||||
use leo_typed::{Error as FormattedError, Identifier, ImportSymbol, Span};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ImportError {
|
||||
@ -27,9 +24,6 @@ pub enum ImportError {
|
||||
|
||||
#[error("{}", _0)]
|
||||
LeoCoreError(#[from] LeoCoreError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ParserError(#[from] ParserError),
|
||||
}
|
||||
|
||||
impl ImportError {
|
||||
@ -37,55 +31,6 @@ impl ImportError {
|
||||
ImportError::Error(FormattedError::new_from_span(message, span))
|
||||
}
|
||||
|
||||
fn new_from_span_with_path(message: String, span: Span, path: PathBuf) -> Self {
|
||||
ImportError::Error(FormattedError::new_from_span_with_path(message, span, path))
|
||||
}
|
||||
|
||||
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?");
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn current_directory_error(error: io::Error) -> Self {
|
||||
let span = Span {
|
||||
text: "".to_string(),
|
||||
line: 0,
|
||||
start: 0,
|
||||
end: 0,
|
||||
};
|
||||
let message = format!("compilation failed trying to find current directory - {:?}", error);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn directory_error(error: io::Error, span: Span, path: PathBuf) -> Self {
|
||||
let message = format!("compilation failed due to directory error - {:?}", error);
|
||||
|
||||
Self::new_from_span_with_path(message, span, path)
|
||||
}
|
||||
|
||||
pub fn star(path: PathBuf, span: Span) -> Self {
|
||||
let message = format!("cannot import `*` from path `{:?}`", path);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn expected_lib_file(entry: String, span: Span) -> Self {
|
||||
let message = format!(
|
||||
"expected library file`{}` when looking for symbol `{}`",
|
||||
entry, span.text
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn unknown_package(identifier: Identifier) -> Self {
|
||||
let message = format!(
|
||||
"cannot find imported package `{}` in source files or import directory",
|
||||
|
@ -13,13 +13,6 @@
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Imports are split up into two parts: parsing and storing
|
||||
|
||||
/// The import parser creates a hashmap of import program names -> import program structs
|
||||
pub mod parser;
|
||||
pub use self::parser::*;
|
||||
|
||||
/// The import store brings an imported symbol into the main program from an import program struct
|
||||
pub mod store;
|
||||
pub use self::store::*;
|
||||
|
@ -1,28 +0,0 @@
|
||||
// Copyright (C) 2019-2020 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{errors::ImportError, ImportParser};
|
||||
use leo_typed::Package;
|
||||
|
||||
pub static CORE_PACKAGE_NAME: &str = "core";
|
||||
|
||||
impl ImportParser {
|
||||
// import a core package into scope
|
||||
pub fn parse_core_package(&mut self, package: &Package) -> Result<(), ImportError> {
|
||||
self.insert_core_package(package);
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
// Copyright (C) 2019-2020 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::errors::ImportError;
|
||||
use leo_typed::{Package, Program};
|
||||
|
||||
use std::{collections::HashMap, env::current_dir};
|
||||
|
||||
/// Parses all relevant import files for a program.
|
||||
/// Stores compiled program structs.
|
||||
#[derive(Clone)]
|
||||
pub struct ImportParser {
|
||||
imports: HashMap<String, Program>,
|
||||
core_packages: Vec<Package>,
|
||||
}
|
||||
|
||||
impl ImportParser {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
imports: HashMap::new(),
|
||||
core_packages: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn insert_import(&mut self, file_name: String, program: Program) {
|
||||
// todo: handle conflicting versions for duplicate imports here
|
||||
let _res = self.imports.insert(file_name, program);
|
||||
}
|
||||
|
||||
pub(crate) fn insert_core_package(&mut self, package: &Package) {
|
||||
let _res = self.core_packages.push(package.clone());
|
||||
}
|
||||
|
||||
pub fn get_import(&self, file_name: &String) -> Option<&Program> {
|
||||
self.imports.get(file_name)
|
||||
}
|
||||
|
||||
pub fn core_packages(&self) -> &Vec<Package> {
|
||||
&self.core_packages
|
||||
}
|
||||
|
||||
pub fn parse(program: &Program) -> Result<Self, ImportError> {
|
||||
let mut imports = Self::new();
|
||||
|
||||
// Find all imports relative to current directory
|
||||
let path = current_dir().map_err(|error| ImportError::current_directory_error(error))?;
|
||||
|
||||
// Parse each imported file
|
||||
program
|
||||
.imports
|
||||
.iter()
|
||||
.map(|import| imports.parse_package(path.clone(), &import.package))
|
||||
.collect::<Result<Vec<()>, ImportError>>()?;
|
||||
|
||||
Ok(imports)
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
// Copyright (C) 2019-2020 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
/// The import parser creates a hashmap of import program names -> import program structs
|
||||
pub mod core_package;
|
||||
pub use self::core_package::*;
|
||||
|
||||
pub mod parse_symbol;
|
||||
pub use self::parse_symbol::*;
|
||||
|
||||
pub mod import_parser;
|
||||
pub use self::import_parser::*;
|
||||
|
||||
pub mod parse_package;
|
||||
pub use self::parse_package::*;
|
@ -1,115 +0,0 @@
|
||||
// Copyright (C) 2019-2020 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{errors::ImportError, ImportParser, CORE_PACKAGE_NAME};
|
||||
use leo_typed::{Package, PackageAccess};
|
||||
|
||||
use std::{fs, fs::DirEntry, path::PathBuf};
|
||||
|
||||
static SOURCE_FILE_EXTENSION: &str = ".leo";
|
||||
static SOURCE_DIRECTORY_NAME: &str = "src/";
|
||||
static IMPORTS_DIRECTORY_NAME: &str = "imports/";
|
||||
|
||||
impl ImportParser {
|
||||
// bring one or more import symbols into scope for the current constrained program
|
||||
// we will recursively traverse sub packages here until we find the desired symbol
|
||||
pub fn parse_package_access(&mut self, entry: &DirEntry, access: &PackageAccess) -> Result<(), ImportError> {
|
||||
tracing::debug!("import {:?}", entry.path());
|
||||
|
||||
match access {
|
||||
PackageAccess::Star(span) => self.parse_import_star(entry, span),
|
||||
PackageAccess::Symbol(symbol) => self.parse_import_symbol(entry, symbol),
|
||||
PackageAccess::SubPackage(package) => self.parse_package(entry.path(), package),
|
||||
PackageAccess::Multiple(accesses) => {
|
||||
for access in accesses {
|
||||
self.parse_package_access(entry, access)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_package(&mut self, mut path: PathBuf, package: &Package) -> Result<(), ImportError> {
|
||||
let error_path = path.clone();
|
||||
let package_name = package.name.clone();
|
||||
|
||||
// Fetch a core package
|
||||
let core_package = package_name.name.eq(CORE_PACKAGE_NAME);
|
||||
|
||||
// Trim path if importing from another file
|
||||
if path.is_file() {
|
||||
path.pop();
|
||||
}
|
||||
|
||||
// Search for package name in local directory
|
||||
let mut source_directory = path.clone();
|
||||
source_directory.push(SOURCE_DIRECTORY_NAME);
|
||||
|
||||
// Search for package name in `imports` directory
|
||||
let mut imports_directory = path.clone();
|
||||
imports_directory.push(IMPORTS_DIRECTORY_NAME);
|
||||
|
||||
// Read from local `src` directory or the current path
|
||||
if source_directory.exists() {
|
||||
path = source_directory
|
||||
}
|
||||
|
||||
let entries = fs::read_dir(path)
|
||||
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), error_path.clone()))?
|
||||
.into_iter()
|
||||
.collect::<Result<Vec<_>, std::io::Error>>()
|
||||
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), error_path.clone()))?;
|
||||
|
||||
let matched_source_entry = entries.into_iter().find(|entry| {
|
||||
entry
|
||||
.file_name()
|
||||
.to_os_string()
|
||||
.into_string()
|
||||
.unwrap()
|
||||
.trim_end_matches(SOURCE_FILE_EXTENSION)
|
||||
.eq(&package_name.name)
|
||||
});
|
||||
|
||||
if core_package {
|
||||
// Enforce core library package access
|
||||
self.parse_core_package(&package)
|
||||
} else if imports_directory.exists() {
|
||||
let entries = fs::read_dir(imports_directory)
|
||||
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), error_path.clone()))?
|
||||
.into_iter()
|
||||
.collect::<Result<Vec<_>, std::io::Error>>()
|
||||
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), error_path.clone()))?;
|
||||
|
||||
let matched_import_entry = entries
|
||||
.into_iter()
|
||||
.find(|entry| entry.file_name().into_string().unwrap().eq(&package_name.name));
|
||||
|
||||
match (matched_source_entry, matched_import_entry) {
|
||||
(Some(_), Some(_)) => Err(ImportError::conflicting_imports(package_name)),
|
||||
(Some(source_entry), None) => self.parse_package_access(&source_entry, &package.access),
|
||||
(None, Some(import_entry)) => self.parse_package_access(&import_entry, &package.access),
|
||||
(None, None) => Err(ImportError::unknown_package(package_name)),
|
||||
}
|
||||
} else {
|
||||
// Enforce local package access with no found imports directory
|
||||
match matched_source_entry {
|
||||
Some(source_entry) => self.parse_package_access(&source_entry, &package.access),
|
||||
None => Err(ImportError::unknown_package(package_name)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,124 +0,0 @@
|
||||
// Copyright (C) 2019-2020 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{errors::ImportError, ImportParser};
|
||||
use leo_ast::LeoAst;
|
||||
use leo_typed::{ImportSymbol, Program, Span};
|
||||
|
||||
use std::{ffi::OsString, fs::DirEntry, path::PathBuf};
|
||||
|
||||
static LIBRARY_FILE: &str = "src/lib.leo";
|
||||
static FILE_EXTENSION: &str = "leo";
|
||||
|
||||
fn parse_import_file(entry: &DirEntry, span: &Span) -> Result<Program, ImportError> {
|
||||
// make sure the given entry is file
|
||||
let file_type = entry
|
||||
.file_type()
|
||||
.map_err(|error| ImportError::directory_error(error, span.clone(), entry.path()))?;
|
||||
let file_name = entry
|
||||
.file_name()
|
||||
.to_os_string()
|
||||
.into_string()
|
||||
.map_err(|_| ImportError::convert_os_string(span.clone()))?;
|
||||
|
||||
let mut file_path = entry.path().to_path_buf();
|
||||
if file_type.is_dir() {
|
||||
file_path.push(LIBRARY_FILE);
|
||||
|
||||
if !file_path.exists() {
|
||||
return Err(ImportError::expected_lib_file(
|
||||
format!("{:?}", file_path.as_path()),
|
||||
span.clone(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Builds the abstract syntax tree.
|
||||
let program_string = &LeoAst::load_file(&file_path)?;
|
||||
let ast = &LeoAst::new(&file_path, &program_string)?;
|
||||
|
||||
// Generates the Leo program from file.
|
||||
Ok(Program::from(&file_name, ast.as_repr()))
|
||||
}
|
||||
|
||||
impl ImportParser {
|
||||
pub fn parse_import_star(&mut self, entry: &DirEntry, span: &Span) -> Result<(), ImportError> {
|
||||
let path = entry.path();
|
||||
let is_dir = path.is_dir();
|
||||
let is_leo_file = path
|
||||
.extension()
|
||||
.map_or(false, |ext| ext.eq(&OsString::from(FILE_EXTENSION)));
|
||||
|
||||
let mut package_path = path.to_path_buf();
|
||||
package_path.push(LIBRARY_FILE);
|
||||
|
||||
let is_package = is_dir && package_path.exists();
|
||||
|
||||
// import * can only be invoked on a package with a library file or a leo file
|
||||
if is_package || is_leo_file {
|
||||
// Generate aleo program from file
|
||||
let program = parse_import_file(entry, &span)?;
|
||||
|
||||
// Store program's imports in imports hashmap
|
||||
program
|
||||
.imports
|
||||
.iter()
|
||||
.map(|import| self.parse_package(entry.path(), &import.package))
|
||||
.collect::<Result<Vec<()>, ImportError>>()?;
|
||||
|
||||
// Store program in imports hashmap
|
||||
let file_name_path = PathBuf::from(entry.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
|
||||
|
||||
self.insert_import(file_name, program);
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
// importing * from a directory or non-leo file in `package/src/` is illegal
|
||||
Err(ImportError::star(entry.path().to_path_buf(), span.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_import_symbol(&mut self, entry: &DirEntry, symbol: &ImportSymbol) -> Result<(), ImportError> {
|
||||
// Generate aleo program from file
|
||||
let program = parse_import_file(entry, &symbol.span)?;
|
||||
|
||||
// Store program's imports in imports hashmap
|
||||
program
|
||||
.imports
|
||||
.iter()
|
||||
.map(|import| self.parse_package(entry.path(), &import.package))
|
||||
.collect::<Result<Vec<()>, ImportError>>()?;
|
||||
|
||||
// Store program in imports hashmap
|
||||
let file_name_path = PathBuf::from(entry.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
|
||||
|
||||
self.insert_import(file_name, program);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user