1use serde::Serialize;
2
3use mago_span::HasSpan;
4use mago_span::Span;
5
6pub use crate::ast::array::*;
7pub use crate::ast::callable::*;
8pub use crate::ast::class_like_string::*;
9pub use crate::ast::composite::*;
10pub use crate::ast::conditional::*;
11pub use crate::ast::generics::*;
12pub use crate::ast::identifier::*;
13pub use crate::ast::index_access::*;
14pub use crate::ast::int_range::*;
15pub use crate::ast::iterable::*;
16pub use crate::ast::key_of::*;
17pub use crate::ast::keyword::*;
18pub use crate::ast::literal::*;
19pub use crate::ast::properties_of::*;
20pub use crate::ast::reference::*;
21pub use crate::ast::shape::*;
22pub use crate::ast::slice::*;
23pub use crate::ast::unary::*;
24pub use crate::ast::value_of::*;
25pub use crate::ast::variable::*;
26
27pub mod array;
28pub mod callable;
29pub mod class_like_string;
30pub mod composite;
31pub mod conditional;
32pub mod generics;
33pub mod identifier;
34pub mod index_access;
35pub mod int_range;
36pub mod iterable;
37pub mod key_of;
38pub mod keyword;
39pub mod literal;
40pub mod properties_of;
41pub mod reference;
42pub mod shape;
43pub mod slice;
44pub mod unary;
45pub mod value_of;
46pub mod variable;
47
48#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
49#[serde(tag = "type", content = "value")]
50#[non_exhaustive]
51pub enum Type<'input> {
52 Parenthesized(ParenthesizedType<'input>),
53 Union(UnionType<'input>),
54 Intersection(IntersectionType<'input>),
55 Nullable(NullableType<'input>),
56 Array(ArrayType<'input>),
57 NonEmptyArray(NonEmptyArrayType<'input>),
58 AssociativeArray(AssociativeArrayType<'input>),
59 List(ListType<'input>),
60 NonEmptyList(NonEmptyListType<'input>),
61 Iterable(IterableType<'input>),
62 ClassString(ClassStringType<'input>),
63 InterfaceString(InterfaceStringType<'input>),
64 EnumString(EnumStringType<'input>),
65 TraitString(TraitStringType<'input>),
66 Reference(ReferenceType<'input>),
67 Mixed(Keyword<'input>),
68 Null(Keyword<'input>),
69 Void(Keyword<'input>),
70 Never(Keyword<'input>),
71 Resource(Keyword<'input>),
72 ClosedResource(Keyword<'input>),
73 OpenResource(Keyword<'input>),
74 True(Keyword<'input>),
75 False(Keyword<'input>),
76 Bool(Keyword<'input>),
77 Float(Keyword<'input>),
78 Int(Keyword<'input>),
79 PositiveInt(Keyword<'input>),
80 NegativeInt(Keyword<'input>),
81 String(Keyword<'input>),
82 StringableObject(Keyword<'input>),
83 ArrayKey(Keyword<'input>),
84 Object(Keyword<'input>),
85 Numeric(Keyword<'input>),
86 Scalar(Keyword<'input>),
87 NumericString(Keyword<'input>),
88 NonEmptyString(Keyword<'input>),
89 LowercaseString(Keyword<'input>),
90 TruthyString(Keyword<'input>),
91 UnspecifiedLiteralInt(Keyword<'input>),
92 UnspecifiedLiteralString(Keyword<'input>),
93 NonEmptyUnspecifiedLiteralString(Keyword<'input>),
94 LiteralFloat(LiteralFloatType<'input>),
95 LiteralInt(LiteralIntType<'input>),
96 LiteralString(LiteralStringType<'input>),
97 MemberReference(MemberReferenceType<'input>),
98 Shape(ShapeType<'input>),
99 Callable(CallableType<'input>),
100 Variable(VariableType<'input>),
101 Conditional(ConditionalType<'input>),
102 KeyOf(KeyOfType<'input>),
103 ValueOf(ValueOfType<'input>),
104 IndexAccess(IndexAccessType<'input>),
105 Negated(NegatedType<'input>),
106 Posited(PositedType<'input>),
107 IntRange(IntRangeType<'input>),
108 PropertiesOf(PropertiesOfType<'input>),
109 Slice(SliceType<'input>),
110}
111
112impl HasSpan for Type<'_> {
113 fn span(&self) -> Span {
114 match self {
115 Type::Parenthesized(ty) => ty.span(),
116 Type::Union(ty) => ty.span(),
117 Type::Intersection(ty) => ty.span(),
118 Type::Nullable(ty) => ty.span(),
119 Type::Array(ty) => ty.span(),
120 Type::NonEmptyArray(ty) => ty.span(),
121 Type::AssociativeArray(ty) => ty.span(),
122 Type::List(ty) => ty.span(),
123 Type::NonEmptyList(ty) => ty.span(),
124 Type::Iterable(ty) => ty.span(),
125 Type::ClassString(ty) => ty.span(),
126 Type::InterfaceString(ty) => ty.span(),
127 Type::EnumString(ty) => ty.span(),
128 Type::TraitString(ty) => ty.span(),
129 Type::Reference(ty) => ty.span(),
130 Type::Mixed(ty) => ty.span(),
131 Type::Null(ty) => ty.span(),
132 Type::Void(ty) => ty.span(),
133 Type::Never(ty) => ty.span(),
134 Type::Resource(ty) => ty.span(),
135 Type::ClosedResource(ty) => ty.span(),
136 Type::OpenResource(ty) => ty.span(),
137 Type::True(ty) => ty.span(),
138 Type::False(ty) => ty.span(),
139 Type::Bool(ty) => ty.span(),
140 Type::Float(ty) => ty.span(),
141 Type::Int(ty) => ty.span(),
142 Type::PositiveInt(ty) => ty.span(),
143 Type::NegativeInt(ty) => ty.span(),
144 Type::String(ty) => ty.span(),
145 Type::ArrayKey(ty) => ty.span(),
146 Type::Scalar(ty) => ty.span(),
147 Type::Object(ty) => ty.span(),
148 Type::Numeric(ty) => ty.span(),
149 Type::NumericString(ty) => ty.span(),
150 Type::StringableObject(ty) => ty.span(),
151 Type::NonEmptyString(ty) => ty.span(),
152 Type::LowercaseString(ty) => ty.span(),
153 Type::TruthyString(ty) => ty.span(),
154 Type::UnspecifiedLiteralInt(ty) => ty.span(),
155 Type::UnspecifiedLiteralString(ty) => ty.span(),
156 Type::NonEmptyUnspecifiedLiteralString(ty) => ty.span(),
157 Type::LiteralFloat(ty) => ty.span(),
158 Type::LiteralInt(ty) => ty.span(),
159 Type::LiteralString(ty) => ty.span(),
160 Type::MemberReference(ty) => ty.span(),
161 Type::Shape(ty) => ty.span(),
162 Type::Callable(ty) => ty.span(),
163 Type::Conditional(ty) => ty.span(),
164 Type::Variable(ty) => ty.span(),
165 Type::KeyOf(ty) => ty.span(),
166 Type::ValueOf(ty) => ty.span(),
167 Type::IndexAccess(ty) => ty.span(),
168 Type::Negated(ty) => ty.span(),
169 Type::Posited(ty) => ty.span(),
170 Type::IntRange(ty) => ty.span(),
171 Type::PropertiesOf(ty) => ty.span(),
172 Type::Slice(ty) => ty.span(),
173 }
174 }
175}
176
177impl std::fmt::Display for Type<'_> {
178 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179 match self {
180 Type::Parenthesized(ty) => write!(f, "{ty}"),
181 Type::Union(ty) => write!(f, "{ty}"),
182 Type::Intersection(ty) => write!(f, "{ty}"),
183 Type::Nullable(ty) => write!(f, "{ty}"),
184 Type::Array(ty) => write!(f, "{ty}"),
185 Type::NonEmptyArray(ty) => write!(f, "{ty}"),
186 Type::AssociativeArray(ty) => write!(f, "{ty}"),
187 Type::List(ty) => write!(f, "{ty}"),
188 Type::NonEmptyList(ty) => write!(f, "{ty}"),
189 Type::Iterable(ty) => write!(f, "{ty}"),
190 Type::ClassString(ty) => write!(f, "{ty}"),
191 Type::InterfaceString(ty) => write!(f, "{ty}"),
192 Type::EnumString(ty) => write!(f, "{ty}"),
193 Type::TraitString(ty) => write!(f, "{ty}"),
194 Type::Reference(ty) => write!(f, "{ty}"),
195 Type::Mixed(ty) => write!(f, "{ty}"),
196 Type::Null(ty) => write!(f, "{ty}"),
197 Type::Void(ty) => write!(f, "{ty}"),
198 Type::Never(ty) => write!(f, "{ty}"),
199 Type::Resource(ty) => write!(f, "{ty}"),
200 Type::ClosedResource(ty) => write!(f, "{ty}"),
201 Type::OpenResource(ty) => write!(f, "{ty}"),
202 Type::True(ty) => write!(f, "{ty}"),
203 Type::False(ty) => write!(f, "{ty}"),
204 Type::Bool(ty) => write!(f, "{ty}"),
205 Type::Float(ty) => write!(f, "{ty}"),
206 Type::Int(ty) => write!(f, "{ty}"),
207 Type::PositiveInt(ty) => write!(f, "{ty}"),
208 Type::NegativeInt(ty) => write!(f, "{ty}"),
209 Type::String(ty) => write!(f, "{ty}"),
210 Type::ArrayKey(ty) => write!(f, "{ty}"),
211 Type::Scalar(ty) => write!(f, "{ty}"),
212 Type::Object(ty) => write!(f, "{ty}"),
213 Type::Numeric(ty) => write!(f, "{ty}"),
214 Type::NumericString(ty) => write!(f, "{ty}"),
215 Type::StringableObject(ty) => write!(f, "{ty}"),
216 Type::NonEmptyString(ty) => write!(f, "{ty}"),
217 Type::LowercaseString(ty) => write!(f, "{ty}"),
218 Type::TruthyString(ty) => write!(f, "{ty}"),
219 Type::UnspecifiedLiteralInt(ty) => write!(f, "{ty}"),
220 Type::UnspecifiedLiteralString(ty) => write!(f, "{ty}"),
221 Type::NonEmptyUnspecifiedLiteralString(ty) => write!(f, "{ty}"),
222 Type::LiteralFloat(ty) => write!(f, "{ty}"),
223 Type::LiteralInt(ty) => write!(f, "{ty}"),
224 Type::LiteralString(ty) => write!(f, "{ty}"),
225 Type::MemberReference(ty) => write!(f, "{ty}"),
226 Type::Shape(ty) => write!(f, "{ty}"),
227 Type::Callable(ty) => write!(f, "{ty}"),
228 Type::Conditional(ty) => write!(f, "{ty}"),
229 Type::Variable(ty) => write!(f, "{ty}"),
230 Type::KeyOf(ty) => write!(f, "{ty}"),
231 Type::ValueOf(ty) => write!(f, "{ty}"),
232 Type::IndexAccess(ty) => write!(f, "{ty}"),
233 Type::Negated(ty) => write!(f, "{ty}"),
234 Type::Posited(ty) => write!(f, "{ty}"),
235 Type::IntRange(ty) => write!(f, "{ty}"),
236 Type::PropertiesOf(ty) => write!(f, "{ty}"),
237 Type::Slice(ty) => write!(f, "{ty}"),
238 }
239 }
240}