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 InDoc,
76 OutDoc,
78
79 As,
80 Break,
81 Catch,
82 Const,
83 Continue,
84 Defer,
85 Do,
86 Else,
87 Enum,
88 False,
89 Fn,
90 For,
91 If,
92 Impl,
93 In,
94 Let,
95 Loop,
96 Macro,
97 Match,
98 Mod,
99 Mut,
100 Pub,
101 Return,
102 Static,
103 Struct,
104 True,
105 Try,
106 Type,
107 Use,
108 While,
109
110 Identifier, Character,
112 String,
113 Integer,
115 LCurly,
117 RCurly,
119 LBrack,
121 RBrack,
123 LParen,
125 RParen,
127 Amp,
129 AmpAmp,
131 AmpEq,
133 Arrow,
135 At,
137 Backslash,
139 Bang,
141 BangBang,
143 BangEq,
145 Bar,
147 BarBar,
149 BarEq,
151 Colon,
153 ColonColon,
155 Comma,
157 Dollar,
159 Dot,
161 DotDot,
163 DotDotDot,
165 DotDotEq,
167 Eq,
169 EqEq,
171 FatArrow,
173 Grave,
175 Gt,
177 GtEq,
179 GtGt,
181 GtGtEq,
183 Hash,
185 HashBang,
187 Lt,
189 LtEq,
191 LtLt,
193 LtLtEq,
195 Minus,
197 MinusEq,
199 Plus,
201 PlusEq,
203 Question,
205 Rem,
207 RemEq,
209 Semi,
211 Slash,
213 SlashEq,
215 Star,
217 StarEq,
219 Tilde,
221 Xor,
223 XorEq,
225 XorXor,
227}
228
229impl TKind {
230 pub const fn flip(self) -> Self {
231 match self {
232 Self::LCurly => Self::RCurly,
233 Self::RCurly => Self::LCurly,
234 Self::LBrack => Self::RBrack,
235 Self::RBrack => Self::LBrack,
236 Self::LParen => Self::RParen,
237 Self::RParen => Self::LParen,
238 Self::Gt => Self::Lt,
239 Self::GtGt => Self::LtLt,
240 Self::LtLt => Self::GtGt,
241 Self::Lt => Self::Gt,
242 _ => self,
243 }
244 }
245
246 pub const fn split(self) -> Result<(Self, Self), Self> {
248 Ok(match self {
249 Self::AmpAmp => (Self::Amp, Self::Amp),
250 Self::AmpEq => (Self::Amp, Self::Eq),
251 Self::Arrow => (Self::Minus, Self::Gt),
252 Self::BangBang => (Self::Bang, Self::Bang),
253 Self::BangEq => (Self::Bang, Self::Eq),
254 Self::BarBar => (Self::Bar, Self::Bar),
255 Self::BarEq => (Self::Bar, Self::Eq),
256 Self::ColonColon => (Self::Colon, Self::Colon),
257 Self::DotDot => (Self::Dot, Self::Dot),
258 Self::DotDotEq => (Self::DotDot, Self::Eq),
259 Self::EqEq => (Self::Eq, Self::Eq),
260 Self::FatArrow => (Self::Eq, Self::Gt),
261 Self::GtEq => (Self::Gt, Self::Eq),
262 Self::GtGt => (Self::Gt, Self::Gt),
263 Self::GtGtEq => (Self::Gt, Self::GtEq),
264 Self::HashBang => (Self::Hash, Self::Bang),
265 Self::LtEq => (Self::Lt, Self::Eq),
266 Self::LtLt => (Self::Lt, Self::Lt),
267 Self::LtLtEq => (Self::Lt, Self::LtEq),
268 Self::MinusEq => (Self::Minus, Self::Eq),
269 Self::PlusEq => (Self::Plus, Self::Eq),
270 Self::RemEq => (Self::Rem, Self::Eq),
271 Self::SlashEq => (Self::Slash, Self::Eq),
272 Self::StarEq => (Self::Star, Self::Eq),
273 Self::XorEq => (Self::Xor, Self::Eq),
274 Self::XorXor => (Self::Xor, Self::Xor),
275 _ => return Err(self),
276 })
277 }
278}