Skip to content

Commit

Permalink
Fixing editor
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Aug 29, 2024
1 parent edbf668 commit 81c6f70
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 114 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ members = [
"ecosystem/rust/parser",
# "ecosystem/c/cmake",
# "ecosystem/c/generator",
# "tools/editor/dependencies/gui-runtime",
"tools/editor/dependencies/gui-runtime",
"tools/cli",
# "tools/editor",
"tools/editor",
]
resolver = "2"

Expand Down Expand Up @@ -47,7 +47,7 @@ ligen-rust-pyo3-importer = { path = "ecosystem/rust/pyo3-importer" }
#ligen-rust-exporter = { path = "ecosystem/rust/exporter" }
ligen-rust-parser = { path = "ecosystem/rust/parser" }
ligen-cargo = { path = "ecosystem/rust/cargo" }
# ligen-gui-runtime = { path = "tools/editor/dependencies/gui-runtime" }
ligen-gui-runtime = { path = "tools/editor/dependencies/gui-runtime" }
is-tree = "0.9.7"
serde = { version = "1", features = ["derive"] }
enum-as-inner = "0.6.0"
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/rust/parser/src/function/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Parser<syn::ImplItemMethod> for MethodParser {
let output: Option<Type> = match output {
syn::ReturnType::Default => None,
syn::ReturnType::Type(_x, y) => {
Some(TypeParser.parse(*y, config)?)
Some(TypeParser::new().parse(*y, config)?)
}
};
Ok(Self::Output {
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/rust/parser/src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl FunctionParser {
Ok(match output {
syn::ReturnType::Default => None,
syn::ReturnType::Type(_x, y) => {
Some(TypeParser.parse(*y, config)?)
Some(TypeParser::new().parse(*y, config)?)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/rust/parser/src/function/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Parser<syn::FnArg> for ParameterParser {
Ok(Self::Output {
attributes: AttributesParser::default().parse(attrs, config)?,
identifier: IdentifierParser::new().parse(ident, config)?,
type_: TypeParser.parse(*ty, config)?,
type_: TypeParser::new().parse(*ty, config)?,
default_value: Default::default(),
})
} else {
Expand Down
1 change: 1 addition & 0 deletions ecosystem/rust/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod function;
pub mod macro_attributes;
pub mod types;
pub mod visibility;
pub mod mutability;
pub mod path;
pub mod literal;
pub mod identifier;
Expand Down
17 changes: 17 additions & 0 deletions ecosystem/rust/parser/src/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ impl Parser<syn::Ident> for LiteralParser {
}
}

impl Parser<syn::Expr> for LiteralParser {
type Output = Literal;
fn parse(&self, input: syn::Expr, config: &ParserConfig) -> Result<Self::Output> {
match input {
syn::Expr::Lit(lit) => self.parse(lit, config),
_ => Err(Error::Message("Failed to parse literal from expression".into())),
}
}
}

impl Parser<syn::ExprLit> for LiteralParser {
type Output = Literal;
fn parse(&self, input: syn::ExprLit, config: &ParserConfig) -> Result<Self::Output> {
self.parse(input.lit, config)
}
}

impl Parser<proc_macro::TokenStream> for LiteralParser {
type Output = Literal;
fn parse(&self, input: proc_macro::TokenStream, config: &ParserConfig) -> Result<Self::Output> {
Expand Down
6 changes: 6 additions & 0 deletions ecosystem/rust/parser/src/macro_attributes/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ mod test {
fn parse_attributes() -> Result<()> {
assert_eq(AttributesParser::default(), mock::parse_attributes(), "c(int = \"sized\")")
}

// TODO: Finish this test
// #[test]
// fn parse_many_attributes() -> Result<()> {
// assert_eq(AttributesParser::default(), mock::parse_many_attributes(), "error(\"the {} field name: '{}' is invalid, path: {:?}\", .0.field_type, .0.field_name, .0.path)")
// }
}
24 changes: 24 additions & 0 deletions ecosystem/rust/parser/src/mutability/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! Mutability enumeration.
use crate::prelude::*;
use ligen::{ir::Mutability, parser::ParserConfig};

#[derive(Default)]
pub struct MutabilityParser;

impl MutabilityParser {
pub fn new() -> Self {
Default::default()
}
}

impl Parser<Option<syn::token::Mut>> for MutabilityParser {
type Output = Mutability;
fn parse(&self, mutability: Option<syn::token::Mut>, _config: &ParserConfig) -> Result<Self::Output> {
if mutability.is_some() {
Ok(Mutability::Mutable)
} else {
Ok(Mutability::Constant)
}
}
}
4 changes: 2 additions & 2 deletions ecosystem/rust/parser/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Parser<syn::ImplItemConst> for ObjectParser {
if let syn::Expr::Lit(syn::ExprLit { lit, .. }) = item_const.expr {
let mutability = Mutability::Constant;
let identifier = IdentifierParser::new().parse(item_const.ident.clone(), config)?;
let type_ = TypeParser.parse(item_const.ty, config)?;
let type_ = TypeParser::new().parse(item_const.ty, config)?;
let literal = LiteralParser.parse(lit, config)?;
Ok(Self::Output { mutability, identifier, type_, literal })
} else {
Expand All @@ -28,7 +28,7 @@ impl Parser<syn::ItemConst> for ObjectParser {
if let syn::Expr::Lit(syn::ExprLit { lit, .. }) = *item_const.expr {
let mutability = Mutability::Constant;
let identifier = IdentifierParser::new().parse(item_const.ident.clone(), config)?;
let type_ = TypeParser.parse(*item_const.ty, config)?;
let type_ = TypeParser::new().parse(*item_const.ty, config)?;
let literal = LiteralParser.parse(lit, config)?;
Ok(Self::Output { mutability, identifier, type_, literal })
} else {
Expand Down
4 changes: 2 additions & 2 deletions ecosystem/rust/parser/src/types/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Parser<syn::PathArguments> for GenericsParser {
.args
.into_iter()
.filter_map(|generic| match generic {
syn::GenericArgument::Type(type_) => Some(TypeParser.parse(type_, config).expect("Failed to parse generic type.")),
syn::GenericArgument::Type(type_) => Some(TypeParser::new().parse(type_, config).expect("Failed to parse generic type.")),
_ => None
})
.collect()
Expand All @@ -38,7 +38,7 @@ impl Parser<syn::Generics> for GenericsParser {
let mut generics = Generics::default();
for generic in input.params {
if let syn::GenericParam::Type(type_) = generic {
generics.types.push(TypeParser.parse(type_.ident, config)?);
generics.types.push(TypeParser::new().parse(type_.ident, config)?);
}
}
Ok(generics)
Expand Down
160 changes: 80 additions & 80 deletions ecosystem/rust/parser/src/types/type_.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
use ligen::{ir::{Mutability, Type}, parser::ParserConfig};
use crate::prelude::*;
use ligen::{ir::Type, parser::ParserConfig};
use syn::{TypeArray, TypeSlice};
use crate::{literal::LiteralParser, mutability::MutabilityParser, prelude::*};
use ligen::parser::Parser;
use crate::path::PathParser;

pub struct TypeParser;
#[derive(Default)]
pub struct TypeParser {
pub mutability_parser: MutabilityParser,
pub literal_parser: LiteralParser
}

impl TypeParser {
pub fn new() -> Self {
Default::default()
}
}

impl Parser<syn::Ident> for TypeParser {
type Output = Type;
Expand Down Expand Up @@ -46,25 +57,24 @@ impl Parser<syn::Type> for TypeParser {
if let syn::Type::Path(syn::TypePath { path, .. }) = syn_type {
Ok(self.parse(path, config)?)
} else {
let reference = match &syn_type {
syn::Type::Reference(syn::TypeReference {
elem, mutability, ..
}) => Some((elem, mutability)),
syn::Type::Ptr(syn::TypePtr {
elem, mutability, ..
}) => Some((elem, mutability)),
_ => None,
};
if let Some((elem, mutability)) = reference {
if let syn::Type::Path(syn::TypePath { path, .. }) = *elem.clone() {
let mutability = if mutability.is_none() { Mutability::Constant } else { Mutability::Mutable };
let type_ = TypeParser.parse(path, config)?;
Ok(Self::Output::reference(mutability, type_))
} else {
Err(Error::Message("Couldn't find path".into()))
}
} else {
Err(Error::Message("Only Path, Reference and Ptr Types are currently supported".into()))
match syn_type {
syn::Type::Reference(syn::TypeReference { elem, mutability, .. }) |
syn::Type::Ptr(syn::TypePtr { elem, mutability, .. }) => {
let mutability = self.mutability_parser.parse(mutability, config)?;
let type_ = TypeParser::new().parse(*elem, config)?;
Ok(Type::reference(mutability, type_))
},
syn::Type::Slice(TypeSlice { elem, .. }) => {
let type_ = TypeParser::new().parse(*elem, config)?;
Ok(Type::slice(type_))
},
syn::Type::Array(TypeArray { elem, len, .. }) => {
let len = self.literal_parser.parse(len, config)?;
let len = len.into_integer().map_err(|_| Error::Message("Array length literal isn't an integer.".into()))? as usize;
let type_ = TypeParser::new().parse(*elem, config)?;
Ok(Type::array(type_, len))
},
_ => Err(Error::Message("Only Path, Reference and Ptr Types are currently supported".into())),
}
}
}
Expand Down Expand Up @@ -95,71 +105,61 @@ mod test {

// FIXME: Update this tests to use the mock module.

#[test]
fn types_integer() {
let vec: Vec<Type> = vec![
quote! { u8 },
quote! { u16 },
quote! { u32 },
quote! { u64 },
quote! { u128 },
quote! { usize },
quote! { i8 },
quote! { i16 },
quote! { i32 },
quote! { i64 },
quote! { i128 },
quote! { isize },
]
.into_iter()
.map(|x| {
TypeParser.parse(x, &Default::default()).expect("Failed to convert from syn::Type")
})
.collect();
let expected: Vec<Type> = vec![
Type::u8(),
Type::u16(),
Type::u32(),
Type::u64(),
Type::u128(),
Type::usize(),
Type::i8(),
Type::i16(),
Type::i32(),
Type::i64(),
Type::i128(),
Type::isize(),
]
.into_iter()
.collect();

for (value, expected_value) in vec.iter().zip(expected.iter()) {
fn test_pairs(input: Vec<(proc_macro2::TokenStream, Type)>) {
let v: Vec<(Type, Type)> = input.into_iter().map(|(input, expected)| {
(TypeParser::new().parse(input, &Default::default()).expect("Failed to parse syn::Type"), expected)
}).collect();
for (value, expected_value) in v {
assert_eq!(value, expected_value);
}
}

#[test]
fn types_array() {
test_pairs(vec![
(quote! { [u8; 4] }, Type::array(Type::u8(), 4)),
(quote! { [u8] }, Type::slice(Type::u8()))
]);
}

#[test]
fn types_map() {
test_pairs(vec![
// (quote! { Vec<u8> }, Type::vector(Type::u8())),
]);
}

#[test]
fn types_integer() {
test_pairs(vec![
(quote! { u8 }, Type::u8()),
(quote! { u16 }, Type::u16()),
(quote! { u32 }, Type::u32()),
(quote! { u64 }, Type::u64()),
(quote! { u128 }, Type::u128()),
(quote! { usize }, Type::usize()),
(quote! { i8 }, Type::i8()),
(quote! { i16 }, Type::i16()),
(quote! { i32 }, Type::i32()),
(quote! { i64 }, Type::i64()),
(quote! { i128 }, Type::i128()),
(quote! { isize }, Type::isize()),
]);
}

#[test]
fn types_float() {
let vec: Vec<Type> = vec![quote! { f32 }, quote! { f64 }]
.into_iter()
.map(|x| {
TypeParser.parse(x, &Default::default()).expect("Failed to convert from syn::Type")
})
.collect();
let expected: Vec<Type> = vec![Type::f32(), Type::f64()]
.into_iter()
.collect();

for (value, expected_value) in vec.iter().zip(expected.iter()) {
assert_eq!(value, expected_value);
}
test_pairs(vec![
(quote! { f32 }, Type::f32()),
(quote! { f64 }, Type::f64()),
]);
}

#[test]
fn types_boolean() -> Result<()> {
assert_eq!(
Type::boolean(),
TypeParser.parse(quote! {bool}, &Default::default())?
TypeParser::new().parse(quote! {bool}, &Default::default())?
);
Ok(())
}
Expand All @@ -168,7 +168,7 @@ mod test {
fn types_character() -> Result<()> {
assert_eq!(
Type::character(),
TypeParser.parse(quote! {char}, &Default::default())?
TypeParser::new().parse(quote! {char}, &Default::default())?
);
Ok(())
}
Expand All @@ -177,7 +177,7 @@ mod test {
fn types_borrow_constant() -> Result<()> {
assert_eq!(
Type::constant_reference(Type::i32()),
TypeParser.parse(quote! {&i32}, &Default::default())?
TypeParser::new().parse(quote! {&i32}, &Default::default())?
);
Ok(())
}
Expand All @@ -186,7 +186,7 @@ mod test {
fn types_borrow_mutable() -> Result<()> {
assert_eq!(
Type::mutable_reference(Type::i32()),
TypeParser.parse(quote! {&mut i32}, &Default::default())?
TypeParser::new().parse(quote! {&mut i32}, &Default::default())?
);
Ok(())
}
Expand All @@ -195,7 +195,7 @@ mod test {
fn types_pointer_constant() -> Result<()> {
assert_eq!(
Type::constant_reference(Type::i32()),
TypeParser.parse(quote! {*const i32}, &Default::default())?
TypeParser::new().parse(quote! {*const i32}, &Default::default())?
);
Ok(())
}
Expand All @@ -204,7 +204,7 @@ mod test {
fn types_pointer_mutable() -> Result<()> {
assert_eq!(
Type::mutable_reference(Type::i32()),
TypeParser.parse(quote! {*mut i32}, &Default::default())?
TypeParser::new().parse(quote! {*mut i32}, &Default::default())?
);
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Parser<syn::Field> for FieldParser {
let attributes = AttributesParser::default().parse(field.attrs, config)?;
let visibility = VisibilityParser.parse(field.vis, config)?;
let identifier = field.ident.map(|identifier| IdentifierParser::new().parse(identifier, config).expect("Failed to parse identifier."));
let type_ = TypeParser.parse(field.ty, config)?;
let type_ = TypeParser::new().parse(field.ty, config)?;
Ok(Self::Output { attributes, visibility, identifier, type_ })
}
}
Expand Down
Loading

0 comments on commit 81c6f70

Please sign in to comment.