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