Skip to content

Commit

Permalink
Simplifying Type
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Nov 2, 2023
1 parent ecad271 commit a3a7f3a
Show file tree
Hide file tree
Showing 32 changed files with 318 additions and 667 deletions.
18 changes: 9 additions & 9 deletions ecosystem/python/parser/src/types/type_.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustpython_parser::ast::{ExprName, Expr, ExprSubscript, ExprTuple};
use ligen::ir::{Type, Primitive, Integer, Float, Identifier};
use ligen::ir::Type;
use crate::prelude::*;

#[derive(Default)]
Expand All @@ -15,12 +15,12 @@ impl Parser<&ExprName> for TypeParser {
type Output = Type;
fn parse(&self, input: &ExprName) -> Result<Self::Output> {
match input.id.as_str() {
"bool" => Ok(Primitive::Boolean.into()),
"char" => Ok(Primitive::Character.into()),
"byte" => Ok(Integer::I8.into()),
"int" => Ok(Integer::I32.into()),
"float" => Ok(Float::F32.into()),
name => Ok(Type::Composite(Identifier::from(name).into()))
"bool" => Ok(Type::boolean()),
"char" => Ok(Type::character()),
"byte" => Ok(Type::i8()),
"int" => Ok(Type::i32()),
"float" => Ok(Type::f32()),
name => Ok(Type::Path(name.into()))
}
}
}
Expand All @@ -29,9 +29,9 @@ impl Parser<&ExprSubscript> for TypeParser {
type Output = Type;
fn parse(&self, input: &ExprSubscript) -> Result<Self::Output> {
let mut type_ = self.parse(&*input.value)?;
if let Type::Composite(composite) = &mut type_ {
if let Type::Path(path) = &mut type_ {
let type_ = self.parse(&*input.slice)?;
composite.generics.types.push(type_);
path.last_mut().generics.types.push(type_);
}
Ok(type_)
}
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/python/parser/src/types/type_definition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ dynamic_parser! {
symbol_parser::SymbolParser,
TypeDefinition,
WithSource<StmtClassDef>
}
}
6 changes: 4 additions & 2 deletions ecosystem/rust/parser/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ligen::ir::Path;
use ligen::ir::{Path, PathSegment};
use ligen::parsing::parser::Parser;
use crate::identifier::IdentifierParser;
use crate::prelude::*;
Expand All @@ -11,7 +11,9 @@ impl Parser<syn::Path> for PathParser {
let segments = path
.segments
.iter()
// FIXME: This isn't parsing generics, just the identifiers.
.map(|segment| IdentifierParser::new().parse(segment.ident.clone()).expect("Failed to parse segment."))
.map(PathSegment::from)
.collect();
Ok(Self::Output { segments })
}
Expand All @@ -20,7 +22,7 @@ impl Parser<syn::Path> for PathParser {
impl Parser<syn::Ident> for PathParser {
type Output = Path;
fn parse(&self, identifier: syn::Ident) -> Result<Self::Output> {
let segments = vec![IdentifierParser::new().parse(identifier)?];
let segments = vec![IdentifierParser::new().parse(identifier)?.into()];
Ok(Self::Output { segments })
}
}
Expand Down
139 changes: 0 additions & 139 deletions ecosystem/rust/parser/src/types/primitive/mod.rs

This file was deleted.

101 changes: 41 additions & 60 deletions ecosystem/rust/parser/src/types/type_.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
use ligen::ir::{Primitive, Reference, Mutability, Type, Composite};
use ligen::ir::{Reference, Mutability, Type};
use crate::prelude::*;
use ligen::parsing::parser::Parser;
use crate::path::PathParser;
use crate::types::GenericsParser;
use crate::types::primitive::PrimitiveParser;

pub struct TypeParser;

impl Parser<syn::Path> for TypeParser {
type Output = Type;
fn parse(&self, path: syn::Path) -> Result<Self::Output> {
if Primitive::is_primitive(PathParser.parse(path.clone())?) {
Ok(Self::Output::Primitive(PrimitiveParser.parse(path)?))
} else {
let generics = path
.segments
.last()
.map(|segment| GenericsParser.parse(segment.arguments.clone()).expect("Failed to parse generics."))
.unwrap_or_default();
let path = PathParser.parse(path)?;
let composite = Composite { path, generics };
Ok(Self::Output::Composite(composite))
let mut path = PathParser.parse(path)?;
if path.segments.len() == 1 {
let segment = path.first_mut();
match segment.identifier.name.as_str() {
"i8" | "i16" | "i32" | "i64" | "i128" |
"u8" | "u16" | "u32" | "u64" | "u128" |
"f16" | "f32" | "f64" | "f128" =>
segment
.identifier
.name
.replace_range(..1, &segment.identifier.name[..1].to_uppercase()),
"usize" | "isize" =>
segment
.identifier
.name
.replace_range(..2, &segment.identifier.name[..2].to_uppercase()),
"char" => segment.identifier.name = "Character".into(),
"bool" => segment.identifier.name = "Boolean".into(),
_ => ()
}
}
Ok(Type::Path(path))
}
}

Expand Down Expand Up @@ -73,7 +81,6 @@ impl Parser<proc_macro2::TokenStream> for TypeParser {

#[cfg(test)]
mod test {
use ligen::ir::{Float, Integer, Mutability};
use ligen::parsing::parser::Parser;
use crate::types::type_::TypeParser;
use crate::prelude::*;
Expand Down Expand Up @@ -101,21 +108,20 @@ mod test {
})
.collect();
let expected: Vec<Type> = vec![
Integer::U8,
Integer::U16,
Integer::U32,
Integer::U64,
Integer::U128,
Integer::USize,
Integer::I8,
Integer::I16,
Integer::I32,
Integer::I64,
Integer::I128,
Integer::ISize,
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()
.map(|x| Type::Primitive(Primitive::Integer(x)))
.collect();

for (value, expected_value) in vec.iter().zip(expected.iter()) {
Expand All @@ -131,9 +137,8 @@ mod test {
TypeParser.parse(x).expect("Failed to convert from syn::Type")
})
.collect();
let expected: Vec<Type> = vec![Float::F32, Float::F64]
let expected: Vec<Type> = vec![Type::f32(), Type::f64()]
.into_iter()
.map(|x| Type::Primitive(Primitive::Float(x)))
.collect();

for (value, expected_value) in vec.iter().zip(expected.iter()) {
Expand All @@ -144,7 +149,7 @@ mod test {
#[test]
fn types_boolean() -> Result<()> {
assert_eq!(
Type::Primitive(Primitive::Boolean),
Type::boolean(),
TypeParser.parse(quote! {bool})?
);
Ok(())
Expand All @@ -153,7 +158,7 @@ mod test {
#[test]
fn types_character() -> Result<()> {
assert_eq!(
Type::Primitive(Primitive::Character),
Type::character(),
TypeParser.parse(quote! {char})?
);
Ok(())
Expand All @@ -165,13 +170,7 @@ mod test {
Type::Reference(
Reference {
mutability: Mutability::Constant,
type_: Box::new(
Type::Primitive(
Primitive::Integer(
Integer::I32
)
)
)
type_: Type::i32().into()
}
),
TypeParser.parse(quote! {&i32})?
Expand All @@ -185,13 +184,7 @@ mod test {
Type::Reference(
Reference {
mutability: Mutability::Mutable,
type_: Box::new(
Type::Primitive(
Primitive::Integer(
Integer::I32
)
)
)
type_: Type::i32().into()
}
),
TypeParser.parse(quote! {&mut i32})?
Expand All @@ -204,13 +197,7 @@ mod test {
assert_eq!(
Type::Reference(Reference {
mutability: Mutability::Constant,
type_: Box::new(
Type::Primitive(
Primitive::Integer(
Integer::I32
)
)
)
type_: Type::i32().into()
}),
TypeParser.parse(quote! {*const i32})?
);
Expand All @@ -222,13 +209,7 @@ mod test {
assert_eq!(
Type::Reference(Reference {
mutability: Mutability::Mutable,
type_: Box::new(
Type::Primitive(
Primitive::Integer(
Integer::I32
)
)
)
type_: Type::i32().into()
}),
TypeParser.parse(quote! {*mut i32})?
);
Expand Down
Loading

0 comments on commit a3a7f3a

Please sign in to comment.