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