Skip to main content

mago_type_syntax/ast/
mod.rs

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