mago_type_syntax/ast/
mod.rs

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    NonPositiveInt(Keyword<'input>),
82    NonNegativeInt(Keyword<'input>),
83    String(Keyword<'input>),
84    StringableObject(Keyword<'input>),
85    ArrayKey(Keyword<'input>),
86    Object(Keyword<'input>),
87    Numeric(Keyword<'input>),
88    Scalar(Keyword<'input>),
89    NumericString(Keyword<'input>),
90    NonEmptyString(Keyword<'input>),
91    NonEmptyLowercaseString(Keyword<'input>),
92    LowercaseString(Keyword<'input>),
93    TruthyString(Keyword<'input>),
94    NonFalsyString(Keyword<'input>),
95    UnspecifiedLiteralInt(Keyword<'input>),
96    UnspecifiedLiteralString(Keyword<'input>),
97    NonEmptyUnspecifiedLiteralString(Keyword<'input>),
98    LiteralFloat(LiteralFloatType<'input>),
99    LiteralInt(LiteralIntType<'input>),
100    LiteralString(LiteralStringType<'input>),
101    MemberReference(MemberReferenceType<'input>),
102    Shape(ShapeType<'input>),
103    Callable(CallableType<'input>),
104    Variable(VariableType<'input>),
105    Conditional(ConditionalType<'input>),
106    KeyOf(KeyOfType<'input>),
107    ValueOf(ValueOfType<'input>),
108    IndexAccess(IndexAccessType<'input>),
109    Negated(NegatedType<'input>),
110    Posited(PositedType<'input>),
111    IntRange(IntRangeType<'input>),
112    PropertiesOf(PropertiesOfType<'input>),
113    Slice(SliceType<'input>),
114}
115
116impl HasSpan for Type<'_> {
117    fn span(&self) -> Span {
118        match self {
119            Type::Parenthesized(ty) => ty.span(),
120            Type::Union(ty) => ty.span(),
121            Type::Intersection(ty) => ty.span(),
122            Type::Nullable(ty) => ty.span(),
123            Type::Array(ty) => ty.span(),
124            Type::NonEmptyArray(ty) => ty.span(),
125            Type::AssociativeArray(ty) => ty.span(),
126            Type::List(ty) => ty.span(),
127            Type::NonEmptyList(ty) => ty.span(),
128            Type::Iterable(ty) => ty.span(),
129            Type::ClassString(ty) => ty.span(),
130            Type::InterfaceString(ty) => ty.span(),
131            Type::EnumString(ty) => ty.span(),
132            Type::TraitString(ty) => ty.span(),
133            Type::Reference(ty) => ty.span(),
134            Type::Mixed(ty) => ty.span(),
135            Type::Null(ty) => ty.span(),
136            Type::Void(ty) => ty.span(),
137            Type::Never(ty) => ty.span(),
138            Type::Resource(ty) => ty.span(),
139            Type::ClosedResource(ty) => ty.span(),
140            Type::OpenResource(ty) => ty.span(),
141            Type::True(ty) => ty.span(),
142            Type::False(ty) => ty.span(),
143            Type::Bool(ty) => ty.span(),
144            Type::Float(ty) => ty.span(),
145            Type::Int(ty) => ty.span(),
146            Type::PositiveInt(ty) => ty.span(),
147            Type::NegativeInt(ty) => ty.span(),
148            Type::NonPositiveInt(ty) => ty.span(),
149            Type::NonNegativeInt(ty) => ty.span(),
150            Type::String(ty) => ty.span(),
151            Type::ArrayKey(ty) => ty.span(),
152            Type::Scalar(ty) => ty.span(),
153            Type::Object(ty) => ty.span(),
154            Type::Numeric(ty) => ty.span(),
155            Type::NumericString(ty) => ty.span(),
156            Type::StringableObject(ty) => ty.span(),
157            Type::NonEmptyString(ty) => ty.span(),
158            Type::NonEmptyLowercaseString(ty) => ty.span(),
159            Type::LowercaseString(ty) => ty.span(),
160            Type::TruthyString(ty) => ty.span(),
161            Type::NonFalsyString(ty) => ty.span(),
162            Type::UnspecifiedLiteralInt(ty) => ty.span(),
163            Type::UnspecifiedLiteralString(ty) => ty.span(),
164            Type::NonEmptyUnspecifiedLiteralString(ty) => ty.span(),
165            Type::LiteralFloat(ty) => ty.span(),
166            Type::LiteralInt(ty) => ty.span(),
167            Type::LiteralString(ty) => ty.span(),
168            Type::MemberReference(ty) => ty.span(),
169            Type::Shape(ty) => ty.span(),
170            Type::Callable(ty) => ty.span(),
171            Type::Conditional(ty) => ty.span(),
172            Type::Variable(ty) => ty.span(),
173            Type::KeyOf(ty) => ty.span(),
174            Type::ValueOf(ty) => ty.span(),
175            Type::IndexAccess(ty) => ty.span(),
176            Type::Negated(ty) => ty.span(),
177            Type::Posited(ty) => ty.span(),
178            Type::IntRange(ty) => ty.span(),
179            Type::PropertiesOf(ty) => ty.span(),
180            Type::Slice(ty) => ty.span(),
181        }
182    }
183}
184
185impl std::fmt::Display for Type<'_> {
186    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
187        match self {
188            Type::Parenthesized(ty) => write!(f, "{ty}"),
189            Type::Union(ty) => write!(f, "{ty}"),
190            Type::Intersection(ty) => write!(f, "{ty}"),
191            Type::Nullable(ty) => write!(f, "{ty}"),
192            Type::Array(ty) => write!(f, "{ty}"),
193            Type::NonEmptyArray(ty) => write!(f, "{ty}"),
194            Type::AssociativeArray(ty) => write!(f, "{ty}"),
195            Type::List(ty) => write!(f, "{ty}"),
196            Type::NonEmptyList(ty) => write!(f, "{ty}"),
197            Type::Iterable(ty) => write!(f, "{ty}"),
198            Type::ClassString(ty) => write!(f, "{ty}"),
199            Type::InterfaceString(ty) => write!(f, "{ty}"),
200            Type::EnumString(ty) => write!(f, "{ty}"),
201            Type::TraitString(ty) => write!(f, "{ty}"),
202            Type::Reference(ty) => write!(f, "{ty}"),
203            Type::Mixed(ty) => write!(f, "{ty}"),
204            Type::Null(ty) => write!(f, "{ty}"),
205            Type::Void(ty) => write!(f, "{ty}"),
206            Type::Never(ty) => write!(f, "{ty}"),
207            Type::Resource(ty) => write!(f, "{ty}"),
208            Type::ClosedResource(ty) => write!(f, "{ty}"),
209            Type::OpenResource(ty) => write!(f, "{ty}"),
210            Type::True(ty) => write!(f, "{ty}"),
211            Type::False(ty) => write!(f, "{ty}"),
212            Type::Bool(ty) => write!(f, "{ty}"),
213            Type::Float(ty) => write!(f, "{ty}"),
214            Type::Int(ty) => write!(f, "{ty}"),
215            Type::PositiveInt(ty) => write!(f, "{ty}"),
216            Type::NegativeInt(ty) => write!(f, "{ty}"),
217            Type::NonPositiveInt(ty) => write!(f, "{ty}"),
218            Type::NonNegativeInt(ty) => write!(f, "{ty}"),
219            Type::String(ty) => write!(f, "{ty}"),
220            Type::ArrayKey(ty) => write!(f, "{ty}"),
221            Type::Scalar(ty) => write!(f, "{ty}"),
222            Type::Object(ty) => write!(f, "{ty}"),
223            Type::Numeric(ty) => write!(f, "{ty}"),
224            Type::NumericString(ty) => write!(f, "{ty}"),
225            Type::StringableObject(ty) => write!(f, "{ty}"),
226            Type::NonEmptyString(ty) => write!(f, "{ty}"),
227            Type::NonEmptyLowercaseString(ty) => write!(f, "{ty}"),
228            Type::LowercaseString(ty) => write!(f, "{ty}"),
229            Type::TruthyString(ty) => write!(f, "{ty}"),
230            Type::NonFalsyString(ty) => write!(f, "{ty}"),
231            Type::UnspecifiedLiteralInt(ty) => write!(f, "{ty}"),
232            Type::UnspecifiedLiteralString(ty) => write!(f, "{ty}"),
233            Type::NonEmptyUnspecifiedLiteralString(ty) => write!(f, "{ty}"),
234            Type::LiteralFloat(ty) => write!(f, "{ty}"),
235            Type::LiteralInt(ty) => write!(f, "{ty}"),
236            Type::LiteralString(ty) => write!(f, "{ty}"),
237            Type::MemberReference(ty) => write!(f, "{ty}"),
238            Type::Shape(ty) => write!(f, "{ty}"),
239            Type::Callable(ty) => write!(f, "{ty}"),
240            Type::Conditional(ty) => write!(f, "{ty}"),
241            Type::Variable(ty) => write!(f, "{ty}"),
242            Type::KeyOf(ty) => write!(f, "{ty}"),
243            Type::ValueOf(ty) => write!(f, "{ty}"),
244            Type::IndexAccess(ty) => write!(f, "{ty}"),
245            Type::Negated(ty) => write!(f, "{ty}"),
246            Type::Posited(ty) => write!(f, "{ty}"),
247            Type::IntRange(ty) => write!(f, "{ty}"),
248            Type::PropertiesOf(ty) => write!(f, "{ty}"),
249            Type::Slice(ty) => write!(f, "{ty}"),
250        }
251    }
252}