1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
extern crate alloc;
use alloc::string::String;
/// Source location for error reporting.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Span {
/// Byte offset of the token start in the source.
pub start: usize,
/// Byte offset one past the token end in the source.
pub end: usize,
/// 1-based line number.
pub line: u32,
/// 1-based column number (byte offset from line start).
pub column: u32,
}
/// A token produced by the lexer.
#[derive(Debug, Clone, PartialEq)]
pub struct Token {
pub kind: TokenKind,
pub span: Span,
}
/// All token types in the Keleusma language.
#[derive(Debug, Clone, PartialEq)]
pub enum TokenKind {
// Keywords
Fn,
Yield,
Loop,
Break,
Let,
For,
In,
If,
Else,
Match,
Use,
Struct,
Enum,
True,
False,
As,
When,
Not,
And,
Or,
Pure,
Data,
Trait,
Impl,
// Identifiers
/// Lowercase identifier: `[a-z_][a-z0-9_]*`
LowerIdent(String),
/// Uppercase identifier: `[A-Z][A-Za-z0-9]*`
UpperIdent(String),
// Literals
/// Integer literal (decimal, hex, or binary).
IntLit(i64),
/// Floating-point literal.
FloatLit(f64),
/// String literal (escape sequences resolved).
StringLit(String),
// Arithmetic operators
Plus,
Minus,
Star,
Slash,
Percent,
// Comparison operators
EqEq,
NotEq,
Lt,
Gt,
LtEq,
GtEq,
// Assignment
Eq,
// Pipeline
Pipe,
Bar,
// Punctuation
Dot,
DotDot,
ColonColon,
Colon,
Semicolon,
Comma,
Arrow,
FatArrow,
Underscore,
// Delimiters
LParen,
RParen,
LBrace,
RBrace,
LBracket,
RBracket,
// End of file
Eof,
}
impl TokenKind {
/// Check if a string is a keyword and return the corresponding token kind.
pub fn keyword(s: &str) -> Option<TokenKind> {
match s {
"fn" => Some(TokenKind::Fn),
"yield" => Some(TokenKind::Yield),
"loop" => Some(TokenKind::Loop),
"break" => Some(TokenKind::Break),
"let" => Some(TokenKind::Let),
"for" => Some(TokenKind::For),
"in" => Some(TokenKind::In),
"if" => Some(TokenKind::If),
"else" => Some(TokenKind::Else),
"match" => Some(TokenKind::Match),
"use" => Some(TokenKind::Use),
"struct" => Some(TokenKind::Struct),
"enum" => Some(TokenKind::Enum),
"true" => Some(TokenKind::True),
"false" => Some(TokenKind::False),
"as" => Some(TokenKind::As),
"when" => Some(TokenKind::When),
"not" => Some(TokenKind::Not),
"and" => Some(TokenKind::And),
"or" => Some(TokenKind::Or),
"pure" => Some(TokenKind::Pure),
"data" => Some(TokenKind::Data),
"trait" => Some(TokenKind::Trait),
"impl" => Some(TokenKind::Impl),
_ => None,
}
}
}