mago_type_syntax/
token.rs

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