1use std::fmt;
2
3#[derive(Debug, Clone, Copy, PartialEq)]
4pub struct Token<'source> {
5 pub kind: TokenKind,
6 pub text: &'source str,
7 pub byte_offset: u32,
8 pub(crate) byte_length: u32,
9}
10
11impl Token<'_> {
12 pub fn end_offset(self) -> u32 {
13 self.byte_offset + self.byte_length
14 }
15}
16
17#[derive(Debug, Clone, Copy, PartialEq)]
18pub enum TokenKind {
19 Integer,
20 Imaginary,
21 String,
22 RawString,
23 FormatStringStart,
24 FormatStringText,
25 FormatStringInterpolationStart,
26 FormatStringInterpolationEnd,
27 FormatStringEnd,
28 Char,
29 Boolean,
30 Float,
31 Identifier,
32 Comment,
33 DocComment,
34 FileComment,
35 Shebang,
36 Semicolon,
37 LeftParen,
38 RightParen,
39 LeftSquareBracket,
40 RightSquareBracket,
41 LeftCurlyBrace,
42 RightCurlyBrace,
43 LeftAngleBracket,
44 RightAngleBracket,
45 Arrow,
46 ArrowDouble,
47 Equal,
48 EqualDouble,
49 NotEqual,
50 GreaterThanOrEqual,
51 LessThanOrEqual,
52 Colon,
53 Pipe,
54 PipeDouble,
55 Pipeline,
56 Ampersand,
57 AmpersandDouble,
58 Plus,
59 Minus,
60 Star,
61 Slash,
62 PlusEqual,
63 MinusEqual,
64 StarEqual,
65 SlashEqual,
66 PercentEqual,
67 AmpersandEqual,
68 PipeEqual,
69 CaretEqual,
70 ShiftLeftEqual, ShiftRightEqual, AndNotEqual, Caret,
74 Percent,
75 Bang,
76 QuestionMark,
77 Dot,
78 Comma,
79 Hash,
80 DotDot,
81 DotDotEqual,
82 Ellipsis,
83 Backtick,
84 ShiftLeft, ShiftRight, AndNot, Function,
88 Let,
89 If,
90 Else,
91 Match,
92 Enum,
93 Struct,
94 Type,
95 Interface,
96 Impl,
97 Const,
98 Var,
99 Return,
100 Defer,
101 Import,
102 Mut,
103 Pub,
104 For,
105 In_,
106 While,
107 Loop,
108 Break,
109 Continue,
110 Select,
111 Task,
112 Try,
113 Recover,
114 Assert,
115 As,
116 Directive,
117 EOF,
118 Placeholder,
119 Error,
120}
121
122impl fmt::Display for TokenKind {
123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 use TokenKind::*;
125 let s = match self {
126 Integer => "integer",
127 Imaginary => "imaginary",
128 String => "string",
129 RawString => "raw string",
130 FormatStringStart => "format string",
131 FormatStringText => "format string",
132 FormatStringInterpolationStart => "`{`",
133 FormatStringInterpolationEnd => "`}`",
134 FormatStringEnd => "format string",
135 Char => "rune",
136 Boolean => "boolean",
137 Float => "float",
138 Identifier => "identifier",
139 Comment => "comment",
140 DocComment => "doc comment",
141 FileComment => "file comment",
142 Shebang => "shebang",
143 Semicolon => "`;`",
144 LeftParen => "`(`",
145 RightParen => "`)`",
146 LeftSquareBracket => "`[`",
147 RightSquareBracket => "`]`",
148 LeftCurlyBrace => "`{`",
149 RightCurlyBrace => "`}`",
150 LeftAngleBracket => "`<`",
151 RightAngleBracket => "`>`",
152 Arrow => "`->`",
153 ArrowDouble => "`=>`",
154 Equal => "`=`",
155 EqualDouble => "`==`",
156 NotEqual => "`!=`",
157 GreaterThanOrEqual => "`>=`",
158 LessThanOrEqual => "`<=`",
159 Colon => "`:`",
160 Pipe => "`|`",
161 PipeDouble => "`||`",
162 Pipeline => "`|>`",
163 Ampersand => "`&`",
164 AmpersandDouble => "`&&`",
165 Plus => "`+`",
166 Minus => "`-`",
167 Star => "`*`",
168 Slash => "`/`",
169 PlusEqual => "`+=`",
170 MinusEqual => "`-=`",
171 StarEqual => "`*=`",
172 SlashEqual => "`/=`",
173 PercentEqual => "`%=`",
174 AmpersandEqual => "`&=`",
175 PipeEqual => "`|=`",
176 CaretEqual => "`^=`",
177 ShiftLeftEqual => "`<<=`",
178 ShiftRightEqual => "`>>=`",
179 AndNotEqual => "`&^=`",
180 Caret => "`^`",
181 Percent => "`%`",
182 Bang => "`!`",
183 QuestionMark => "`?`",
184 Dot => "`.`",
185 Comma => "`,`",
186 Hash => "`#`",
187 DotDot => "`..`",
188 DotDotEqual => "`..=`",
189 Ellipsis => "`...`",
190 Backtick => "`` ` ``",
191 ShiftLeft => "`<<`",
192 ShiftRight => "`>>`",
193 AndNot => "`&^`",
194 Function => "`fn`",
195 Let => "`let`",
196 If => "`if`",
197 Else => "`else`",
198 Match => "`match`",
199 Enum => "`enum`",
200 Struct => "`struct`",
201 Type => "`type`",
202 Interface => "`interface`",
203 Impl => "`impl`",
204 Const => "`const`",
205 Var => "`var`",
206 Return => "`return`",
207 Defer => "`defer`",
208 Import => "`import`",
209 Mut => "`mut`",
210 Pub => "`pub`",
211 For => "`for`",
212 In_ => "`in`",
213 While => "`while`",
214 Loop => "`loop`",
215 Break => "`break`",
216 Continue => "`continue`",
217 Select => "`select`",
218 Task => "`task`",
219 Try => "`try`",
220 Recover => "`recover`",
221 Assert => "`assert`",
222 As => "`as`",
223 Directive => "directive",
224 EOF => "end of file",
225 Placeholder => "placeholder",
226 Error => "error",
227 };
228 write!(f, "{}", s)
229 }
230}
231
232impl TokenKind {
233 pub(crate) fn from_keyword(s: &str) -> Option<Self> {
234 use TokenKind::*;
235
236 match s {
237 "fn" => Some(Function),
238 "let" => Some(Let),
239 "if" => Some(If),
240 "else" => Some(Else),
241 "match" => Some(Match),
242 "enum" => Some(Enum),
243 "struct" => Some(Struct),
244 "type" => Some(Type),
245 "interface" => Some(Interface),
246 "impl" => Some(Impl),
247 "const" => Some(Const),
248 "var" => Some(Var),
249 "return" => Some(Return),
250 "defer" => Some(Defer),
251 "import" => Some(Import),
252 "mut" => Some(Mut),
253 "pub" => Some(Pub),
254 "for" => Some(For),
255 "in" => Some(In_),
256 "while" => Some(While),
257 "loop" => Some(Loop),
258 "break" => Some(Break),
259 "continue" => Some(Continue),
260 "select" => Some(Select),
261 "task" => Some(Task),
262 "try" => Some(Try),
263 "recover" => Some(Recover),
264 "assert" => Some(Assert),
265 "as" => Some(As),
266 _ => None,
267 }
268 }
269
270 pub(crate) fn is_keyword(&self) -> bool {
271 use TokenKind::*;
272 matches!(
273 self,
274 Function
275 | Let
276 | If
277 | Else
278 | Match
279 | Enum
280 | Struct
281 | Type
282 | Interface
283 | Impl
284 | Const
285 | Var
286 | Return
287 | Defer
288 | Import
289 | Mut
290 | Pub
291 | For
292 | In_
293 | While
294 | Loop
295 | Break
296 | Continue
297 | Select
298 | Task
299 | Try
300 | Recover
301 | Assert
302 | As
303 )
304 }
305
306 pub(crate) fn from_three_char_symbol(c1: char, c2: char, c3: char) -> Option<Self> {
307 match (c1, c2, c3) {
308 ('.', '.', '=') => Some(TokenKind::DotDotEqual),
309 ('.', '.', '.') => Some(TokenKind::Ellipsis),
310 ('<', '<', '=') => Some(TokenKind::ShiftLeftEqual),
311 ('>', '>', '=') => Some(TokenKind::ShiftRightEqual),
312 ('&', '^', '=') => Some(TokenKind::AndNotEqual),
313 _ => None,
314 }
315 }
316
317 pub(crate) fn from_two_char_symbol(c1: char, c2: char) -> Option<Self> {
318 use TokenKind::*;
319
320 match (c1, c2) {
321 ('-', '>') => Some(Arrow),
322 ('=', '>') => Some(ArrowDouble),
323 ('=', '=') => Some(EqualDouble),
324 ('!', '=') => Some(NotEqual),
325 ('>', '=') => Some(GreaterThanOrEqual),
326 ('<', '=') => Some(LessThanOrEqual),
327 ('|', '|') => Some(PipeDouble),
328 ('|', '>') => Some(Pipeline),
329 ('&', '&') => Some(AmpersandDouble),
330 ('.', '.') => Some(DotDot),
331 ('+', '=') => Some(PlusEqual),
332 ('-', '=') => Some(MinusEqual),
333 ('*', '=') => Some(StarEqual),
334 ('/', '=') => Some(SlashEqual),
335 ('%', '=') => Some(PercentEqual),
336 ('&', '=') => Some(AmpersandEqual),
337 ('|', '=') => Some(PipeEqual),
338 ('^', '=') => Some(CaretEqual),
339 ('&', '^') => Some(AndNot),
340 ('<', '<') => Some(ShiftLeft),
341 ('>', '>') => Some(ShiftRight),
342 _ => None,
343 }
344 }
345
346 pub(crate) fn from_one_char_symbol(c: char) -> Option<Self> {
347 use TokenKind::*;
348
349 match c {
350 '(' => Some(LeftParen),
351 ')' => Some(RightParen),
352 '[' => Some(LeftSquareBracket),
353 ']' => Some(RightSquareBracket),
354 '{' => Some(LeftCurlyBrace),
355 '}' => Some(RightCurlyBrace),
356 '<' => Some(LeftAngleBracket),
357 '>' => Some(RightAngleBracket),
358 '=' => Some(Equal),
359 ':' => Some(Colon),
360 '|' => Some(Pipe),
361 '&' => Some(Ampersand),
362 '+' => Some(Plus),
363 '-' => Some(Minus),
364 '*' => Some(Star),
365 '^' => Some(Caret),
366 '%' => Some(Percent),
367 '!' => Some(Bang),
368 '?' => Some(QuestionMark),
369 '.' => Some(Dot),
370 ',' => Some(Comma),
371 '#' => Some(Hash),
372 _ => None,
373 }
374 }
375}