mago_type_syntax/
token.rs

1use serde::Deserialize;
2use serde::Serialize;
3use strum::Display;
4
5use mago_span::Span;
6
7#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord, Display)]
8#[repr(C)]
9pub enum TypeTokenKind {
10    Int,
11    Integer,
12    String,
13    Float,
14    Real,
15    Double,
16    Bool,
17    Boolean,
18    False,
19    True,
20    Object,
21    Callable,
22    Array,
23    NonEmptyArray,
24    NonEmptyString,
25    NonEmptyLowercaseString,
26    NonFalsyString,
27    LowercaseString,
28    TruthyString,
29    Iterable,
30    Null,
31    Mixed,
32    NumericString,
33    ClassString,
34    InterfaceString,
35    TraitString,
36    EnumString,
37    StringableObject,
38    PureCallable,
39    PureClosure,
40    UnspecifiedLiteralString,
41    UnspecifiedLiteralInt,
42    NonEmptyUnspecifiedLiteralString,
43    Resource,
44    Void,
45    Scalar,
46    Numeric,
47    NoReturn,
48    NeverReturn,
49    NeverReturns,
50    Never,
51    Nothing,
52    ArrayKey,
53    List,
54    NonEmptyList,
55    OpenResource,
56    ClosedResource,
57    AssociativeArray,
58    KeyOf,
59    ValueOf,
60    Min,
61    Max,
62    PropertiesOf,
63    PublicPropertiesOf,
64    PrivatePropertiesOf,
65    ProtectedPropertiesOf,
66    PositiveInt,
67    NegativeInt,
68    NonPositiveInt,
69    NonNegativeInt,
70    As,
71    Is,
72    Not,
73    Identifier,
74    QualifiedIdentifier,
75    FullyQualifiedIdentifier,
76    Plus,
77    Minus,
78    LessThan,
79    GreaterThan,
80    Pipe,
81    Ampersand,
82    Question,
83    Comma,
84    Colon,
85    ColonColon,
86    LeftBrace,
87    RightBrace,
88    LeftBracket,
89    RightBracket,
90    LeftParenthesis,
91    RightParenthesis,
92    Equals,
93    Ellipsis,
94    PartialLiteralString,
95    LiteralString,
96    LiteralInteger,
97    LiteralFloat,
98    Variable,
99    Whitespace,
100    SingleLineComment,
101    Asterisk,
102}
103
104#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
105#[repr(C)]
106pub struct TypeToken<'input> {
107    pub kind: TypeTokenKind,
108    pub value: &'input str,
109    pub span: Span,
110}
111
112impl TypeTokenKind {
113    #[inline]
114    pub const fn is_trivia(&self) -> bool {
115        matches!(self, Self::SingleLineComment | Self::Whitespace)
116    }
117
118    #[inline]
119    pub const fn is_simple_identifier(&self) -> bool {
120        matches!(self, Self::Identifier)
121    }
122
123    #[inline]
124    pub const fn is_identifier(&self) -> bool {
125        matches!(self, Self::Identifier | Self::QualifiedIdentifier | Self::FullyQualifiedIdentifier)
126    }
127
128    #[inline]
129    pub const fn is_keyword(&self) -> bool {
130        matches!(
131            self,
132            Self::Int
133                | Self::Integer
134                | Self::Double
135                | Self::String
136                | Self::Float
137                | Self::Real
138                | Self::Bool
139                | Self::Boolean
140                | Self::False
141                | Self::True
142                | Self::Object
143                | Self::Callable
144                | Self::Array
145                | Self::NonEmptyArray
146                | Self::NonEmptyString
147                | Self::NonEmptyLowercaseString
148                | Self::LowercaseString
149                | Self::TruthyString
150                | Self::NonFalsyString
151                | Self::Iterable
152                | Self::Null
153                | Self::Mixed
154                | Self::NumericString
155                | Self::ClassString
156                | Self::InterfaceString
157                | Self::TraitString
158                | Self::EnumString
159                | Self::StringableObject
160                | Self::PureCallable
161                | Self::PureClosure
162                | Self::UnspecifiedLiteralString
163                | Self::NonEmptyUnspecifiedLiteralString
164                | Self::Resource
165                | Self::Void
166                | Self::Scalar
167                | Self::Numeric
168                | Self::NoReturn
169                | Self::NeverReturn
170                | Self::NeverReturns
171                | Self::Never
172                | Self::Nothing
173                | Self::ArrayKey
174                | Self::List
175                | Self::NonEmptyList
176                | Self::OpenResource
177                | Self::ClosedResource
178                | Self::AssociativeArray
179                | Self::Is
180                | Self::As
181                | Self::Not
182                | Self::KeyOf
183                | Self::ValueOf
184                | Self::Min
185                | Self::Max
186                | Self::UnspecifiedLiteralInt
187                | Self::PropertiesOf
188                | Self::PublicPropertiesOf
189                | Self::PrivatePropertiesOf
190                | Self::ProtectedPropertiesOf
191                | Self::PositiveInt
192                | Self::NegativeInt
193                | Self::NonPositiveInt
194                | Self::NonNegativeInt
195        )
196    }
197
198    #[inline]
199    pub const fn is_array_like(&self) -> bool {
200        matches!(self, Self::Array | Self::NonEmptyArray | Self::AssociativeArray | Self::List | Self::NonEmptyList)
201    }
202}