refactor pacakge_type -> package_or_packages

This commit is contained in:
gluaxspeed 2021-02-09 10:57:37 -05:00
parent f7add0fc98
commit d211d729d1
8 changed files with 42 additions and 38 deletions

View File

@ -25,7 +25,7 @@ mod function;
pub use function::*; pub use function::*;
use crate::{AsgConvertError, ImportResolver, InnerScope, Input, Scope}; use crate::{AsgConvertError, ImportResolver, InnerScope, Input, Scope};
use leo_ast::{Identifier, PackageAccess, PackageType, Span}; use leo_ast::{Identifier, PackageAccess, PackageOrPackages, Span};
use indexmap::IndexMap; use indexmap::IndexMap;
use std::{cell::RefCell, sync::Arc}; use std::{cell::RefCell, sync::Arc};
@ -74,14 +74,14 @@ enum ImportSymbol {
fn resolve_import_package( fn resolve_import_package(
output: &mut Vec<(Vec<String>, ImportSymbol, Span)>, output: &mut Vec<(Vec<String>, ImportSymbol, Span)>,
mut package_segments: Vec<String>, mut package_segments: Vec<String>,
package_type: &PackageType, package_or_packages: &PackageOrPackages,
) { ) {
match package_type { match package_or_packages {
PackageType::Package(package) => { PackageOrPackages::Package(package) => {
package_segments.push(package.name.name.clone()); package_segments.push(package.name.name.clone());
resolve_import_package_access(output, package_segments, &package.access); resolve_import_package_access(output, package_segments, &package.access);
} }
PackageType::Packages(packages) => { PackageOrPackages::Packages(packages) => {
package_segments.push(packages.name.name.clone()); package_segments.push(packages.name.name.clone());
for access in packages.accesses.clone() { for access in packages.accesses.clone() {
resolve_import_package_access(output, package_segments.clone(), &access); resolve_import_package_access(output, package_segments.clone(), &access);
@ -100,7 +100,11 @@ fn resolve_import_package_access(
output.push((package_segments, ImportSymbol::All, span.clone())); output.push((package_segments, ImportSymbol::All, span.clone()));
} }
PackageAccess::SubPackage(subpackage) => { PackageAccess::SubPackage(subpackage) => {
resolve_import_package(output, package_segments, &PackageType::Package(*(*subpackage).clone())); resolve_import_package(
output,
package_segments,
&PackageOrPackages::Package(*(*subpackage).clone()),
);
} }
PackageAccess::Symbol(symbol) => { PackageAccess::Symbol(symbol) => {
let span = symbol.symbol.span.clone(); let span = symbol.symbol.span.clone();
@ -136,7 +140,7 @@ impl InternalProgram {
// Recursively extract imported symbols. // Recursively extract imported symbols.
let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![]; let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![];
for import in program.imports.iter() { for import in program.imports.iter() {
resolve_import_package(&mut imported_symbols, vec![], &import.package_type); resolve_import_package(&mut imported_symbols, vec![], &import.package_or_packages);
} }
// Create package list. // Create package list.
@ -393,7 +397,7 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program {
imports: core_programs imports: core_programs
.iter() .iter()
.map(|(module, _)| leo_ast::ImportStatement { .map(|(module, _)| leo_ast::ImportStatement {
package_type: leo_ast::PackageType::Package(leo_ast::Package { package_or_packages: leo_ast::PackageOrPackages::Package(leo_ast::Package {
name: Identifier::new(module.clone()), name: Identifier::new(module.clone()),
access: leo_ast::PackageAccess::Star(Span::default()), access: leo_ast::PackageAccess::Star(Span::default()),
span: Default::default(), span: Default::default(),

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License // 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/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{PackageType, Span}; use crate::{PackageOrPackages, Span};
use leo_grammar::imports::Import as GrammarImport; use leo_grammar::imports::Import as GrammarImport;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -23,7 +23,7 @@ use std::fmt;
/// Represents an import statement in a Leo program. /// Represents an import statement in a Leo program.
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)] #[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ImportStatement { pub struct ImportStatement {
pub package_type: PackageType, pub package_or_packages: PackageOrPackages,
pub span: Span, pub span: Span,
} }
@ -32,9 +32,9 @@ impl ImportStatement {
/// Returns the the package file name of the self import statement. /// Returns the the package file name of the self import statement.
/// ///
pub fn get_file_name(&self) -> &str { pub fn get_file_name(&self) -> &str {
match self.package_type { match self.package_or_packages {
PackageType::Package(ref package) => &package.name.name, PackageOrPackages::Package(ref package) => &package.name.name,
PackageType::Packages(ref packages) => &packages.name.name, PackageOrPackages::Packages(ref packages) => &packages.name.name,
} }
} }
} }
@ -42,7 +42,7 @@ impl ImportStatement {
impl<'ast> From<GrammarImport<'ast>> for ImportStatement { impl<'ast> From<GrammarImport<'ast>> for ImportStatement {
fn from(import: GrammarImport<'ast>) -> Self { fn from(import: GrammarImport<'ast>) -> Self {
ImportStatement { ImportStatement {
package_type: PackageType::from(import.package_type), package_or_packages: PackageOrPackages::from(import.package_or_packages),
span: Span::from(import.span), span: Span::from(import.span),
} }
} }
@ -50,7 +50,7 @@ impl<'ast> From<GrammarImport<'ast>> for ImportStatement {
impl ImportStatement { impl ImportStatement {
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "import {};", self.package_type) write!(f, "import {};", self.package_or_packages)
} }
} }

View File

@ -26,8 +26,8 @@ pub use package::*;
pub mod packages; pub mod packages;
pub use packages::*; pub use packages::*;
pub mod package_type; pub mod package_or_packages;
pub use package_type::*; pub use package_or_packages::*;
pub mod package_access; pub mod package_access;
pub use package_access::*; pub use package_access::*;

View File

@ -15,31 +15,31 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{Package, Packages}; use crate::{Package, Packages};
use leo_grammar::imports::PackageType as GrammarPackageType; use leo_grammar::imports::PackageOrPackages as GrammarPackageOrPackages;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
#[derive(Clone, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum PackageType { pub enum PackageOrPackages {
Package(Package), Package(Package),
Packages(Packages), Packages(Packages),
} }
impl<'ast> From<GrammarPackageType<'ast>> for PackageType { impl<'ast> From<GrammarPackageOrPackages<'ast>> for PackageOrPackages {
fn from(package_type: GrammarPackageType<'ast>) -> Self { fn from(package_or_packages: GrammarPackageOrPackages<'ast>) -> Self {
match package_type { match package_or_packages {
GrammarPackageType::Package(package) => PackageType::Package(Package::from(package)), GrammarPackageOrPackages::Package(package) => PackageOrPackages::Package(Package::from(package)),
GrammarPackageType::Packages(packages) => PackageType::Packages(Packages::from(packages)), GrammarPackageOrPackages::Packages(packages) => PackageOrPackages::Packages(Packages::from(packages)),
} }
} }
} }
impl PackageType { impl PackageOrPackages {
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
PackageType::Package(ref package) => write!(f, "{}", package), PackageOrPackages::Package(ref package) => write!(f, "{}", package),
PackageType::Packages(ref packages) => { PackageOrPackages::Packages(ref packages) => {
write!(f, "(")?; write!(f, "(")?;
for (i, access) in packages.accesses.iter().enumerate() { for (i, access) in packages.accesses.iter().enumerate() {
write!(f, "{}", access)?; write!(f, "{}", access)?;
@ -53,13 +53,13 @@ impl PackageType {
} }
} }
impl fmt::Debug for PackageType { impl fmt::Debug for PackageOrPackages {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f) self.format(f)
} }
} }
impl fmt::Display for PackageType { impl fmt::Display for PackageOrPackages {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f) self.format(f)
} }

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License // 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/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{ast::Rule, common::LineEnd, imports::PackageType, SpanDef}; use crate::{ast::Rule, common::LineEnd, imports::PackageOrPackages, SpanDef};
use pest::Span; use pest::Span;
use pest_ast::FromPest; use pest_ast::FromPest;
@ -23,7 +23,7 @@ use serde::Serialize;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)] #[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::import))] #[pest_ast(rule(Rule::import))]
pub struct Import<'ast> { pub struct Import<'ast> {
pub package_type: PackageType<'ast>, pub package_or_packages: PackageOrPackages<'ast>,
pub line_end: LineEnd, pub line_end: LineEnd,
#[pest_ast(outer())] #[pest_ast(outer())]
#[serde(with = "SpanDef")] #[serde(with = "SpanDef")]

View File

@ -26,8 +26,8 @@ pub use package::*;
pub mod packages; pub mod packages;
pub use packages::*; pub use packages::*;
pub mod package_type; pub mod package_or_packages;
pub use package_type::*; pub use package_or_packages::*;
pub mod package_access; pub mod package_access;
pub use package_access::*; pub use package_access::*;

View File

@ -23,8 +23,8 @@ use pest_ast::FromPest;
use serde::Serialize; use serde::Serialize;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)] #[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::package_type))] #[pest_ast(rule(Rule::package_or_packages))]
pub enum PackageType<'ast> { pub enum PackageOrPackages<'ast> {
Package(Package<'ast>), Package(Package<'ast>),
Packages(Packages<'ast>), Packages(Packages<'ast>),
} }

View File

@ -454,13 +454,13 @@ input_tuple = _{ "(" ~ (input ~ ("," ~ input)* ~ ","?)? ~ ")"}
/// Imports /// Imports
// Declared in imports/import.rs // Declared in imports/import.rs
import = { "import " ~ package_type ~ LINE_END} import = { "import " ~ package_or_packages ~ LINE_END}
// Declared in imports/package_name.rs // Declared in imports/package_name.rs
package_name = @{ (ASCII_ALPHA_LOWER | ASCII_DIGIT)+ ~ ( "-" ~ (ASCII_ALPHA_LOWER | ASCII_DIGIT)+)* } package_name = @{ (ASCII_ALPHA_LOWER | ASCII_DIGIT)+ ~ ( "-" ~ (ASCII_ALPHA_LOWER | ASCII_DIGIT)+)* }
// Declared in imports/package_type.rs // Declared in imports/package_or_packages.rs
package_type = { package_or_packages = {
packages packages
| package | package
} }