Skip to content

Commit

Permalink
Removing Objects concept
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Oct 16, 2023
1 parent df79245 commit 9591c73
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 273 deletions.
12 changes: 8 additions & 4 deletions ecosystem/python/parser/src/module/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;
use ligen::ir::{Constant, Function, Import, Module, Object};
use ligen::ir::{Constant, Function, Import, Module, TypeDefinition, Interface};
use rustpython_parser::ast::{ModModule, Stmt};
use crate::function::FunctionParser;

Expand All @@ -26,8 +26,9 @@ impl Parser<WithSource<ModModule>> for ModuleParser {
let imports = self.extract_imports(&input)?;
let constants = self.extract_constants(&input)?;
let functions = self.extract_functions(&input)?;
let objects = self.extract_objects(&input)?;
Ok(Module { attributes, visibility, identifier, modules, imports, constants, functions, objects })
let types = self.extract_types(&input)?;
let interfaces = self.extract_interfaces(&input)?;
Ok(Module { attributes, visibility, identifier, modules, imports, constants, functions, types, interfaces })
}
}

Expand All @@ -52,10 +53,13 @@ impl ModuleParser {
Ok(functions)
}

fn extract_objects(&self, _input: &WithSource<ModModule>) -> Result<Vec<Object>> {
fn extract_types(&self, _input: &WithSource<ModModule>) -> Result<Vec<TypeDefinition>> {
Ok(Default::default())
}

fn extract_interfaces(&self, _input: &WithSource<ModModule>) -> Result<Vec<Interface>> {
Ok(Default::default())
}
}

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion ecosystem/rust/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ pub mod identifier;
pub mod module;
pub mod constant;

extern crate proc_macro;
extern crate proc_macro;
extern crate core;
202 changes: 67 additions & 135 deletions ecosystem/rust/parser/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
mod import;

use syn::spanned::Spanned;
use ligen::ir::{Constant, Object};
use ligen::ir::Constant;
use ligen::parsing::parser::Parser;
use crate::prelude::*;
use ligen::ir::{Function, Module, Import};
use ligen::ir::{Function, Module, Import, TypeDefinition, Interface};
use crate::constant::ConstantParser;
use crate::function::FunctionParser;
use crate::identifier::IdentifierParser;
Expand Down Expand Up @@ -40,9 +40,11 @@ impl Parser<syn::ItemMod> for ModuleParser {

let imports = self.extract_imports(items.as_slice())?;
let functions = self.extract_functions(items.as_slice())?;
let constants = self.extract_constants(false, items.as_slice())?;
let modules = self.extract_modules(false, items)?;
Ok(Self::Output { attributes, visibility, identifier, imports, functions, constants, modules })
let constants = self.extract_constants(items.as_slice())?;
let types = self.extract_types(items.as_slice())?;
let interfaces = self.extract_interfaces(items.as_slice())?;
let modules = self.extract_modules(items)?;
Ok(Self::Output { attributes, visibility, identifier, imports, functions, constants, types, interfaces, modules })
}
}

Expand All @@ -63,6 +65,29 @@ impl Parser<&std::path::Path> for ModuleParser {
}

impl ModuleParser {
fn extract_interfaces(&self, _items: &[syn::Item]) -> Result<Vec<Interface>> {
Ok(Default::default())
}
fn extract_types(&self, items: &[syn::Item]) -> Result<Vec<TypeDefinition>> {
let mut types = Vec::new();
for item in items {
match item {
syn::Item::Enum(enumeration) =>
types.push(EnumerationParser::new().parse(enumeration.clone())?.into()),
syn::Item::Struct(structure) =>
types.push(StructureParser::new().parse(structure.clone())?.into()),
syn::Item::Type(_type) => {
todo!("Type object isn't implemented yet.")
},
syn::Item::Union(_union) => {
todo!("Union object isn't implemented yet.")
},
_ => ()
}
}
Ok(types)
}

fn extract_imports(&self, items: &[syn::Item]) -> Result<Vec<Import>> {
let mut imports: Vec<Import> = Default::default();
for item in items {
Expand All @@ -82,136 +107,24 @@ impl ModuleParser {
Ok(functions)
}

fn extract_modules(&self, ignored: bool, items: Vec<syn::Item>) -> Result<Vec<Module>> {
fn extract_modules(&self, items: Vec<syn::Item>) -> Result<Vec<Module>> {
let mut modules = Vec::new();
if !ignored {
let items = items
.into_iter()
.filter_map(|item| {
if let syn::Item::Mod(module) = item {
Some(module)
} else {
None
}
});
for module in items {
modules.push(self.parse(module)?)
}
}
Ok(modules)
}

// TODO: Is it still useful?
// fn parse_ligen_attributes(attributes: &Attributes, items: &[syn::Item]) -> Result<Attributes> {
// let mut attributes = attributes.clone();
// for item in items {
// match item {
// syn::Item::Macro(call) => {
// let attribute = Attribute::try_from(SynItemMacro(call.clone()))?;
// if let Attribute::Group(identifier, grouped_attributes) = &attribute {
// if *identifier == Identifier::from("inner_ligen") {
// attributes.attributes.push(Attribute::Group("ligen".into(), grouped_attributes.clone()));
// }
// }
// },
// _ => ()
// }
// }
// Ok(attributes)
// }

fn extract_object_definitions(&self, ignored: bool, items: &[syn::Item]) -> Result<Vec<Object>> {
let mut objects = Vec::new();
if !ignored {
for item in items {
match item {
syn::Item::Enum(enumeration) => {
let attributes = AttributesParser::default().parse(enumeration.attrs.clone())?;
let identifier = IdentifierParser::new().parse(enumeration.ident.clone())?;
let visibility = VisibilityParser.parse(enumeration.vis.clone())?;
let enumeration = EnumerationParser.parse(enumeration.clone())?;
objects.push(Object {
attributes,
identifier,
visibility,
definition: enumeration.into(),
.. Default::default()
});
},
syn::Item::Struct(structure) => {
let attributes = AttributesParser::default().parse(structure.attrs.clone())?;
let identifier = IdentifierParser::new().parse(structure.ident.clone())?;
let visibility = VisibilityParser.parse(structure.vis.clone())?;
let structure = StructureParser.parse(structure.clone())?;
objects.push(Object {
attributes,
identifier,
visibility,
definition: structure.into(),
.. Default::default()
});
},
syn::Item::Type(_type) => {
todo!("Type object isn't implemented yet.")
},
syn::Item::Union(_union) => {
todo!("Union object isn't implemented yet.")
},
_ => ()
let items = items
.into_iter()
.filter_map(|item| {
if let syn::Item::Mod(module) = item {
Some(module)
} else {
None
}
}
});
for module in items {
modules.push(self.parse(module)?)
}
Ok(objects)
Ok(modules)
}

// FIXME: Implement it.
// fn extract_object_implementations(project: &mut Project, ignored: bool, items: &[syn::Item]) -> Result<()> {
// if !ignored {
// for item in items {
// match item {
// syn::Item::Mod(module) => if let Some((_, items)) = &module.content {
// // FIXME: Hardcoded ignored.
// Self::extract_object_implementations(project, false, items.as_slice())?;
// },
// syn::Item::Impl(implementation) => {
// // TODO: Consider `impl Trait for Object`?
// if implementation.trait_.is_none() {
// if let syn::Type::Path(syn::TypePath { path, .. }) = &*implementation.self_ty {
// // FIXME: Transform relative path to absolute path.
// let path = PathParser.parse(path.clone())?;
// if let Some(object) = project.root_module.find_object_mut(&path) {
// // TODO: Parse attributes and merge them with individual items.
// // let attributes = implementation.attrs;
// for item in &implementation.items {
// match item {
// syn::ImplItem::Const(constant) => {
// let constant = ConstantParser.parse(constant.clone())?;
// object.constants.push(constant)
// },
// syn::ImplItem::Method(method) => {
// if method.sig.receiver().is_some() {
// let method = MethodParser.parse(method.clone())?;
// object.methods.push(method)
// } else {
// let function = FunctionParser.parse(method.clone())?;
// object.functions.push(function)
// }
// }
// _ => ()
// }
// }
// }
// }
// }
// }
// _ => ()
// }
// }
// }
// Ok(())
// }

fn extract_constants(&self, _: bool, items: &[syn::Item]) -> Result<Vec<Constant>> {
fn extract_constants(&self, items: &[syn::Item]) -> Result<Vec<Constant>> {
let mut constants = Vec::new();
for item in items {
if let syn::Item::Const(constant) = item {
Expand Down Expand Up @@ -248,14 +161,33 @@ mod tests {
}

#[test]
fn module_objects() -> Result<()> {
assert_eq(ModuleParser, mock::module_objects(), quote! {
pub mod objects {
fn module_types() -> Result<()> {
assert_eq(ModuleParser, mock::module_types(), quote! {
pub mod types {
pub struct Structure;
pub enum Enumeration {}
pub const CONSTANT: bool = false;
pub fn function() {}
}
})
}

// TODO: Implement these:
// #[test]
// fn module_functions() -> Result<()> {
// // pub fn function() {}
//
// todo!()
// }
//
// #[test]
// fn module_constants() -> Result<()> {
// // pub const CONSTANT: bool = false;
// todo!()
// }
//
// #[test]
// fn module_interfaces() -> Result<()> {
// // pub trait Interface {
// // }
// todo!()
// }
}
18 changes: 16 additions & 2 deletions ecosystem/rust/parser/src/types/type_definition/enumeration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,32 @@ pub mod variant;
use crate::prelude::*;
use ligen::ir::Enumeration;
use ligen::parsing::parser::Parser;
use crate::identifier::IdentifierParser;
use crate::macro_attributes::attributes::AttributesParser;
use crate::types::type_definition::enumeration::variant::VariantParser;
use crate::visibility::VisibilityParser;

#[derive(Default)]
pub struct EnumerationParser;

impl EnumerationParser {
pub fn new() -> Self {
Self
}
}

impl Parser<syn::ItemEnum> for EnumerationParser {
type Output = Enumeration;
fn parse(&self, enumeration: syn::ItemEnum) -> Result<Self::Output> {
let attributes = AttributesParser::default().parse(enumeration.attrs)?;
let identifier = IdentifierParser::new().parse(enumeration.ident)?;
let visibility = VisibilityParser::new().parse(enumeration.vis)?;
let interfaces = Default::default();
let mut variants = Vec::new();
for variant in enumeration.variants {
variants.push(VariantParser.parse(variant)?);
}
Ok(Self::Output { variants })
Ok(Enumeration { attributes, visibility, identifier, variants, interfaces })
}
}

Expand Down Expand Up @@ -45,7 +59,7 @@ mod tests {
#[test]
fn enumeration() -> Result<()> {
assert_eq(EnumerationParser, mock::enumeration(), quote !{
enum Enumeration {
pub enum Enumeration {
Integer,
Float,
Boolean
Expand Down
18 changes: 16 additions & 2 deletions ecosystem/rust/parser/src/types/type_definition/structure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ pub use field::*;
use crate::prelude::*;
use ligen::ir::Structure;
use ligen::parsing::parser::Parser;
use crate::identifier::IdentifierParser;
use crate::macro_attributes::attributes::AttributesParser;
use crate::visibility::VisibilityParser;

#[derive(Default)]
pub struct StructureParser;

impl StructureParser {
pub fn new() -> Self {
Self
}
}

impl Parser<proc_macro::TokenStream> for StructureParser {
type Output = Structure;
fn parse(&self, token_stream: proc_macro::TokenStream) -> Result<Self::Output> {
Expand All @@ -29,11 +39,15 @@ impl Parser<proc_macro2::TokenStream> for StructureParser {
impl Parser<syn::ItemStruct> for StructureParser {
type Output = Structure;
fn parse(&self, structure: syn::ItemStruct) -> Result<Self::Output> {
let attributes = AttributesParser::default().parse(structure.attrs)?;
let identifier = IdentifierParser::new().parse(structure.ident)?;
let visibility = VisibilityParser::new().parse(structure.vis)?;
let interfaces = Default::default();
let mut fields = Vec::new();
for field in structure.fields {
fields.push(FieldParser.parse(field)?);
}
Ok(Self::Output { fields })
Ok(Self::Output { attributes, visibility, identifier, fields, interfaces })
}
}

Expand All @@ -48,7 +62,7 @@ mod tests {
#[test]
fn structure() -> Result<()> {
assert_eq(StructureParser, mock::structure(), quote! {
struct Structure {
pub struct Structure {
integer: i32
}
})
Expand Down
7 changes: 7 additions & 0 deletions ecosystem/rust/parser/src/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
use crate::prelude::*;
use ligen::ir::Visibility;

#[derive(Default)]
pub struct VisibilityParser;

impl VisibilityParser {
pub fn new() -> Self {
Self
}
}

impl Parser<syn::Visibility> for VisibilityParser {
type Output = Visibility;
fn parse(&self, visibility: syn::Visibility) -> Result<Self::Output> {
Expand Down
Loading

0 comments on commit 9591c73

Please sign in to comment.