1use std::fmt::{ Debug, Display };
2
3use bstr::BString;
4use serde::{ Deserialize, Serialize };
5
6use crate::lexer::ControlSnapshot;
7
8#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Serialize, Deserialize)]
9pub enum TokenType {
10 Inline,
11 Type,
12 Magic,
13 MagicMethod,
14
15 UnqualifiedName,
16 QualifiedName,
17 FullyQualifiedName,
18 RelativeName,
19
20 NumberBinary,
21 NumberHex,
22 Number,
23
24 Variable,
25 VariableBracketOpen,
26 VariableBracketClose,
27
28 Arrow,
29 Assignment,
30 IsEqual,
31 IsIdentical,
32
33 BitwiseAndAssignment,
34 BitwiseAnd,
35 BooleanAnd,
36 ReferenceAssignment,
37
38 Attribute,
39 CommentLine,
40
41 NullsafeObjectAccess,
42 CoalesceAssignment,
43 Coalesce,
44 QuestionMark,
45 Elvis,
46
47 ModulusAssignment,
48 Modulus,
49
50 BitwiseXorAssignment,
51 BitwiseXor,
52
53 ExponentiationAssignment,
54 MultiplicationAssignment,
55 Exponentiation,
56 Multiplication,
57
58 DivisionAssignment,
59 CommentDoc,
60 CommentBlock,
61 Division,
62
63 ConcatenationAssignment,
64 Ellipsis,
65 Concatenation,
66
67 BitwiseOrAssignment,
68 BooleanOr,
69 BitwiseOr,
70
71 SubtractionAssignment,
72 ObjectAccess,
73 Subtraction,
74
75 IsGreaterOrEqual,
76 IsGreater,
77 BitwiseShiftRightAssignment,
78 BitwiseShiftRight,
79
80 IsLesserOrEqual,
81 IsLesser,
82 IsNotEqual,
83 BitwiseShiftLeftAssignment,
84 BitwiseShiftLeft,
85 Spaceship,
86
87 Colon,
88 DoubleColon,
89
90 BooleanNegate,
91 IsNotIdentical,
92
93 AdditionAssignment,
94 Addition,
95
96 LeftCurlyBracket,
97
98 AdvanceInterpolationOpen,
99 AdvanceInterpolationClose,
100 EncapsedStringOpen,
101 EncapsedStringClose,
102 EncapsedString,
103 String,
104
105 PostDecrement,
106 PostIncrement,
107 PreDecrement,
108 PreIncrement,
109
110 LeftParenthesis,
111 RightParenthesis,
112 RightCurlyBracket,
113 LeftSquareBracket,
114 RightSquareBracket,
115 Comma,
116 Semicolon,
117 AtSign,
118
119 NowDocOpen,
120 NowDocClose,
121 HeredocOpen,
122 HeredocClose,
123
124 Abstract,
125 Array,
126 As,
127 Break,
128 Callable,
129 Case,
130 Catch,
131 Class,
132 Clone,
133 Const,
134 Continue,
135 Declare,
136 Default,
137 Do,
138 Echo,
139 Else,
140 ElseIf,
141 EndDeclare,
142 EndFor,
143 EndForeach,
144 EndIf,
145 EndSwitch,
146 EndWhile,
147 Enum,
148 Exit,
149 Eval,
150 Die,
151 Extends,
152 False,
153 Final,
154 Finally,
155 Fn,
156 For,
157 Foreach,
158 From,
159 Function,
160 Get,
161 Global,
162 Goto,
163 If,
164 Implements,
165 Include,
166 IncludeOnce,
167 InstanceOf,
168 InsteadOf,
169 Interface,
170 List,
171 And,
172 Or,
173 Match,
174 Namespace,
175 New,
176 Null,
177 Print,
178 Private,
179 PrivateGet,
180 PrivateSet,
181 Protected,
182 ProtectedGet,
183 ProtectedSet,
184 Public,
185 PublicGet,
186 PublicSet,
187 Readonly,
188 Require,
189 RequireOnce,
190 Return,
191 Static,
192 Parent,
193 SelfKeyword,
194 Set,
195 Switch,
196 This,
197 Throw,
198 Trait,
199 True,
200 Try,
201 Use,
202 Var,
203 While,
204 Yield,
205 Xor,
206}
207
208#[derive(Clone, PartialEq, Serialize, Deserialize)]
209pub struct Token {
210 pub token_type: TokenType,
211 pub value: BString,
212 pub line: u32,
213 pub column: u32,
214 pub offset: u32,
215}
216
217impl Token {
218 pub(crate) fn new(token_type: TokenType, value: BString, snapshot: &ControlSnapshot) -> Self {
219 Token {
220 token_type,
221 value,
222 line: snapshot.line as u32,
223 column: snapshot.column as u32,
224 offset: snapshot.offset as u32,
225 }
226 }
227}
228
229impl Debug for Token {
230 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
231 write!(
232 f,
233 "Token {{ token_type: {:?}, value: {:?}, line: {:?}, column: {:?}, offset: {:?} }}",
234 self.token_type,
235 self.value.to_string(),
236 self.line,
237 self.column,
238 self.offset
239 )
240 }
241}
242
243impl Display for Token {
244 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
245 write!(
246 f,
247 "Token {{ token_type: {:?}, value: {:?}, line: {:?}, column: {:?}, offset: {:?} }}",
248 self.token_type,
249 self.value.to_string(),
250 self.line,
251 self.column,
252 self.offset
253 )
254 }
255}