1#![warn(clippy::all)]
5use cl_structures::span::Span;
8
9#[derive(Clone, Debug)]
11pub struct Token {
12 pub lexeme: Lexeme,
13 pub kind: TKind,
14 pub span: Span,
15}
16
17impl Token {
18 pub const fn kind(&self) -> TKind {
20 self.kind
21 }
22}
23
24#[derive(Clone, Debug)]
26pub enum Lexeme {
27 String(String),
28 Integer(u128, u32),
29 Char(char),
30}
31
32impl Lexeme {
33 pub fn string(self) -> Option<String> {
34 match self {
35 Self::String(s) => Some(s),
36 _ => None,
37 }
38 }
39 pub fn str(&self) -> Option<&str> {
40 match self {
41 Self::String(s) => Some(s),
42 _ => None,
43 }
44 }
45 pub const fn int(&self) -> Option<u128> {
46 match self {
47 Self::Integer(i, _) => Some(*i),
48 _ => None,
49 }
50 }
51 pub const fn char(&self) -> Option<char> {
52 match self {
53 Self::Char(c) => Some(*c),
54 _ => None,
55 }
56 }
57}
58
59impl std::fmt::Display for Lexeme {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 match self {
62 Self::String(v) => v.fmt(f),
63 Self::Integer(v, _) => v.fmt(f),
64 Self::Char(v) => v.fmt(f),
65 }
66 }
67}
68
69#[derive(Clone, Copy, Debug, PartialEq, Eq)]
71pub enum TKind {
72 Comment,
74 OutDoc,
76 InDoc,
78
79 As,
80 Break,
81 Const,
82 Continue,
83 Do,
84 Else,
85 Enum,
86 False,
87 Fn,
88 For,
89 If,
90 Impl,
91 In,
92 Let,
93 Loop,
94 Macro,
95 Match,
96 Mod,
97 Mut,
98 Pub,
99 Return,
100 Static,
101 Struct,
102 True,
103 Type,
104 Use,
105 While,
106
107 Identifier, Character,
109 String,
110 Integer,
112 LCurly,
114 RCurly,
116 LBrack,
118 RBrack,
120 LParen,
122 RParen,
124 Amp,
126 AmpAmp,
128 AmpEq,
130 Arrow,
132 At,
134 Backslash,
136 Bang,
138 BangBang,
140 BangEq,
142 Bar,
144 BarBar,
146 BarEq,
148 Colon,
150 ColonColon,
152 Comma,
154 Dollar,
156 Dot,
158 DotDot,
160 DotDotDot,
162 DotDotEq,
164 Eq,
166 EqEq,
168 FatArrow,
170 Grave,
172 Gt,
174 GtEq,
176 GtGt,
178 GtGtEq,
180 Hash,
182 HashBang,
184 Lt,
186 LtEq,
188 LtLt,
190 LtLtEq,
192 Minus,
194 MinusEq,
196 Plus,
198 PlusEq,
200 Question,
202 Rem,
204 RemEq,
206 Semi,
208 Slash,
210 SlashEq,
212 Star,
214 StarEq,
216 Tilde,
218 Xor,
220 XorEq,
222 XorXor,
224}
225
226impl TKind {
227 pub const fn flip(self) -> Self {
228 match self {
229 Self::LCurly => Self::RCurly,
230 Self::RCurly => Self::LCurly,
231 Self::LBrack => Self::RBrack,
232 Self::RBrack => Self::LBrack,
233 Self::LParen => Self::RParen,
234 Self::RParen => Self::LParen,
235 Self::Gt => Self::Lt,
236 Self::GtGt => Self::LtLt,
237 Self::LtLt => Self::GtGt,
238 Self::Lt => Self::Gt,
239 _ => self,
240 }
241 }
242
243 pub const fn split(self) -> Result<(Self, Self), Self> {
245 Ok(match self {
246 Self::AmpAmp => (Self::Amp, Self::Amp),
247 Self::AmpEq => (Self::Amp, Self::Eq),
248 Self::Arrow => (Self::Minus, Self::Gt),
249 Self::BangBang => (Self::Bang, Self::Bang),
250 Self::BangEq => (Self::Bang, Self::Eq),
251 Self::BarBar => (Self::Bar, Self::Bar),
252 Self::BarEq => (Self::Bar, Self::Eq),
253 Self::ColonColon => (Self::Colon, Self::Colon),
254 Self::DotDot => (Self::Dot, Self::Dot),
255 Self::DotDotEq => (Self::DotDot, Self::Eq),
256 Self::EqEq => (Self::Eq, Self::Eq),
257 Self::FatArrow => (Self::Eq, Self::Gt),
258 Self::GtEq => (Self::Gt, Self::Eq),
259 Self::GtGt => (Self::Gt, Self::Gt),
260 Self::GtGtEq => (Self::Gt, Self::GtEq),
261 Self::HashBang => (Self::Hash, Self::Bang),
262 Self::LtEq => (Self::Lt, Self::Eq),
263 Self::LtLt => (Self::Lt, Self::Lt),
264 Self::LtLtEq => (Self::Lt, Self::LtEq),
265 Self::MinusEq => (Self::Minus, Self::Eq),
266 Self::PlusEq => (Self::Plus, Self::Eq),
267 Self::RemEq => (Self::Rem, Self::Eq),
268 Self::SlashEq => (Self::Slash, Self::Eq),
269 Self::StarEq => (Self::Star, Self::Eq),
270 Self::XorEq => (Self::Xor, Self::Eq),
271 Self::XorXor => (Self::Xor, Self::Xor),
272 _ => return Err(self),
273 })
274 }
275}