Fix typedefs parser not generating nillable unions for function unions where one has no args

This commit is contained in:
Filip Tibell 2023-02-22 13:06:47 +01:00
parent 41ba23d7ab
commit c3422edeb7
No known key found for this signature in database

View file

@ -3,6 +3,7 @@ use std::collections::HashMap;
use full_moon::{ use full_moon::{
ast::types::{TypeArgument, TypeInfo}, ast::types::{TypeArgument, TypeInfo},
tokenizer::{Symbol, Token, TokenReference, TokenType}, tokenizer::{Symbol, Token, TokenReference, TokenType},
ShortString,
}; };
use super::kind::DefinitionsItemKind; use super::kind::DefinitionsItemKind;
@ -15,7 +16,7 @@ pub(crate) trait TypeInfoExt {
parent_typ: Option<&TypeInfo>, parent_typ: Option<&TypeInfo>,
type_lookup_table: &HashMap<String, TypeInfo>, type_lookup_table: &HashMap<String, TypeInfo>,
) -> String; ) -> String;
fn extract_args(&self, base: Vec<TypeArgument>) -> Vec<TypeArgument>; fn extract_args(&self) -> Vec<TypeArgument>;
fn extract_args_normalized( fn extract_args_normalized(
&self, &self,
type_lookup_table: &HashMap<String, TypeInfo>, type_lookup_table: &HashMap<String, TypeInfo>,
@ -114,14 +115,15 @@ impl TypeInfoExt for TypeInfo {
) )
} }
TypeInfo::Basic(tok) => { TypeInfo::Basic(tok) => {
let tok_str = tok.token().to_string(); let tok_str = tok.token().to_string().trim().to_string();
let mut any_str = None; let mut any_str = None;
// If the function that contains this arg has generic and a // If the function that contains this arg has generic and a
// generic is the same as this token, we stringify it as any // generic is the same as this token, we stringify it as any
if let Some(parent) = parent_typ {
if let Some(TypeInfo::Callback { if let Some(TypeInfo::Callback {
generics: Some(callback_generics), generics: Some(callback_generics),
.. ..
}) = parent_typ }) = try_extract_callback_type_info(parent)
{ {
if callback_generics if callback_generics
.generics() .generics()
@ -131,6 +133,7 @@ impl TypeInfoExt for TypeInfo {
any_str = Some("any".to_string()); any_str = Some("any".to_string());
} }
} }
}
// Also check if we got a referenced type, meaning that it // Also check if we got a referenced type, meaning that it
// exists in the lookup table of global types passed to us // exists in the lookup table of global types passed to us
if let Some(any_str) = any_str { if let Some(any_str) = any_str {
@ -163,29 +166,34 @@ impl TypeInfoExt for TypeInfo {
.stringify_simple(Some(self), type_lookup_table) .stringify_simple(Some(self), type_lookup_table)
) )
} }
// TODO: Stringify custom table types properly, these show up as basic tokens // FUTURE: Is there any other type that we can
// and we should be able to look up the real type using found top level items // stringify to a primitive in an obvious way?
_ => "...".to_string(), _ => "...".to_string(),
} }
} }
fn extract_args(&self, base: Vec<TypeArgument>) -> Vec<TypeArgument> { fn extract_args(&self) -> Vec<TypeArgument> {
if self.is_fn() {
match self { match self {
TypeInfo::Callback { arguments, .. } => { TypeInfo::Callback { arguments, .. } => {
merge_type_argument_vecs(base, arguments.iter().cloned().collect::<Vec<_>>()) arguments.iter().cloned().collect::<Vec<_>>()
} }
TypeInfo::Tuple { types, .. } => types TypeInfo::Tuple { types, .. } => types
.iter() .iter()
.next() .next()
.expect("Function tuple type was empty") .expect("Function tuple type was empty")
.extract_args(base), .extract_args(),
TypeInfo::Union { left, right, .. } | TypeInfo::Intersection { left, right, .. } => { TypeInfo::Union { left, right, .. }
let mut result = base; | TypeInfo::Intersection { left, right, .. } => {
result = left.extract_args(result.clone()); let mut result = Vec::new();
result = right.extract_args(result.clone()); result = merge_type_argument_vecs(result, left.extract_args());
result = merge_type_argument_vecs(result, right.extract_args());
result result
} }
_ => base, _ => vec![],
}
} else {
vec![]
} }
} }
@ -196,7 +204,7 @@ impl TypeInfoExt for TypeInfo {
if self.is_fn() { if self.is_fn() {
let separator = format!(" {} ", Symbol::Pipe); let separator = format!(" {} ", Symbol::Pipe);
let args_stringified_not_normalized = self let args_stringified_not_normalized = self
.extract_args(Vec::new()) .extract_args()
.iter() .iter()
.map(|type_arg| { .map(|type_arg| {
type_arg type_arg
@ -247,6 +255,17 @@ impl TypeInfoExt for TypeInfo {
} }
} }
fn try_extract_callback_type_info(type_info: &TypeInfo) -> Option<&TypeInfo> {
match type_info {
TypeInfo::Callback { .. } => Some(type_info),
TypeInfo::Tuple { types, .. } => types.iter().find_map(try_extract_callback_type_info),
TypeInfo::Union { left, right, .. } | TypeInfo::Intersection { left, right, .. } => {
try_extract_callback_type_info(left).or_else(|| try_extract_callback_type_info(right))
}
_ => None,
}
}
fn make_empty_type_argument() -> TypeArgument { fn make_empty_type_argument() -> TypeArgument {
TypeArgument::new(TypeInfo::Basic(TokenReference::new( TypeArgument::new(TypeInfo::Basic(TokenReference::new(
vec![], vec![],
@ -261,11 +280,15 @@ fn merge_type_arguments(left: TypeArgument, right: TypeArgument) -> TypeArgument
TypeArgument::new(TypeInfo::Union { TypeArgument::new(TypeInfo::Union {
left: Box::new(left.type_info().clone()), left: Box::new(left.type_info().clone()),
pipe: TokenReference::new( pipe: TokenReference::new(
vec![], vec![Token::new(TokenType::Whitespace {
characters: ShortString::new(" "),
})],
Token::new(TokenType::Symbol { Token::new(TokenType::Symbol {
symbol: Symbol::Pipe, symbol: Symbol::Pipe,
}), }),
vec![], vec![Token::new(TokenType::Whitespace {
characters: ShortString::new(" "),
})],
), ),
right: Box::new(right.type_info().clone()), right: Box::new(right.type_info().clone()),
}) })