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