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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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 {
/// The token's variant.
pub kind: TokenKind,
/// Source location for error reporting.
pub span: Span,
}
/// All token types in the Keleusma language.
#[derive(Debug, Clone, PartialEq)]
pub enum TokenKind {
// Keywords
/// `fn` keyword.
Fn,
/// `yield` keyword.
Yield,
/// `loop` keyword.
Loop,
/// `break` keyword.
Break,
/// `let` keyword.
Let,
/// `for` keyword.
For,
/// `in` keyword.
In,
/// `if` keyword.
If,
/// `else` keyword.
Else,
/// `match` keyword.
Match,
/// `use` keyword (native function import).
Use,
/// `external` keyword as a modifier on a `use` declaration.
/// Marks the import as an external native call whose iteration
/// cost is tracked through a per-iteration invocation-count
/// bound rather than a per-call WCET/WCMU attestation.
External,
/// `struct` keyword.
Struct,
/// `enum` keyword.
Enum,
/// `newtype` keyword.
Newtype,
/// `where` clause keyword (refinement predicate).
Where,
/// `overflow` arm keyword in the numeric overflow construct.
Overflow,
/// `underflow` arm keyword in the numeric overflow construct.
Underflow,
/// `saturate_max` keyword inside an overflow arm.
SaturateMax,
/// `saturate_min` keyword inside an underflow arm.
SaturateMin,
/// `@` separator for information-flow labels on types and
/// `classify`/`declassify` operators.
At,
/// `true` boolean literal.
True,
/// `false` boolean literal.
False,
/// `as` cast keyword.
As,
/// `when` guard clause keyword.
When,
/// `not` logical-negation keyword.
Not,
/// `and` logical-and keyword.
And,
/// `or` logical-or keyword.
Or,
/// `pure` declaration modifier reserved for future use.
Pure,
/// `data` block declaration keyword.
Data,
/// `shared` modifier on a `data` declaration. Shared data is
/// host-visible through `Vm::set_data`/`Vm::get_data` and persists
/// across resets. Equivalent to today's bare `data` declaration;
/// the modifier is permitted explicitly for symmetry with
/// `private` and for self-documenting code.
Shared,
/// `private` modifier on a `data` declaration. Private data lives
/// in the arena's persistent region and is not exposed through
/// the host API. Persists across resets.
Private,
/// `const` modifier on a `data` declaration. Const data is
/// immutable; field reads compile to constant loads; field
/// writes are compile errors. Each field has a compile-time
/// literal initializer in the form `field: Type = literal`.
/// Const data does not consume runtime data-segment slots.
Const,
/// `ephemeral` modifier on an entry-point function declaration.
/// Asserts the module is provably ephemeral as defined in the
/// verifier rule. The compile pipeline errors when the assertion
/// does not hold.
Ephemeral,
/// `signed` modifier on an entry-point function declaration.
/// Sets [`crate::wire_format::FLAG_REQUIRES_SIGNATURE`] on the
/// resulting module so the load-time runtime refuses to admit
/// the module unless its cryptographic signature verifies
/// against the host's trust matrix. The keyword is admissible
/// only on the entry function and on any of the three
/// function categories (`fn` / `yield` / `loop`).
Signed,
/// `trait` declaration keyword.
Trait,
/// `impl` block keyword.
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
/// `+` arithmetic plus.
Plus,
/// `-` arithmetic minus (binary and unary).
Minus,
/// `*` arithmetic multiply.
Star,
/// `/` arithmetic divide.
Slash,
/// `%` arithmetic remainder.
Percent,
// Comparison operators
/// `==` equality test.
EqEq,
/// `!=` inequality test.
NotEq,
/// `<` strict-less-than test.
Lt,
/// `>` strict-greater-than test.
Gt,
/// `<=` less-than-or-equal test.
LtEq,
/// `>=` greater-than-or-equal test.
GtEq,
/// `!` punctuation. Distinct from the `not` keyword
/// ([`TokenKind::Not`]) and from `!=` ([`TokenKind::NotEq`]).
/// V0.2.0 admits this token only as the negative-label prefix
/// inside information-flow label sets at parameter and return
/// type positions: `T@!Label` or `T@{!N1, !N2}`. The lexer
/// emits the token unconditionally; the parser rejects it
/// outside its admissible positions.
Bang,
// Assignment
/// `=` assignment operator (data-segment write; let-binding initialiser).
Eq,
// Pipeline
/// `|>` pipeline operator (left-to-right composition).
Pipe,
/// `|` bar token (match-arm alternation, IFC label set delimiter).
Bar,
// Punctuation
/// `.` field-access punctuation.
Dot,
/// `..` exclusive-range punctuation.
DotDot,
/// `::` path separator (enum variants, qualified native names).
ColonColon,
/// `:` type-annotation punctuation.
Colon,
/// `;` statement separator.
Semicolon,
/// `,` list separator.
Comma,
/// `->` return-type arrow.
Arrow,
/// `=>` match-arm arrow.
FatArrow,
/// `_` wildcard pattern / placeholder identifier.
Underscore,
// Delimiters
/// `(` left parenthesis.
LParen,
/// `)` right parenthesis.
RParen,
/// `{` left brace.
LBrace,
/// `}` right brace.
RBrace,
/// `[` left bracket.
LBracket,
/// `]` right bracket.
RBracket,
// End of file
/// End of input.
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),
"external" => Some(TokenKind::External),
"struct" => Some(TokenKind::Struct),
"enum" => Some(TokenKind::Enum),
"newtype" => Some(TokenKind::Newtype),
"where" => Some(TokenKind::Where),
"overflow" => Some(TokenKind::Overflow),
"underflow" => Some(TokenKind::Underflow),
"saturate_max" => Some(TokenKind::SaturateMax),
"saturate_min" => Some(TokenKind::SaturateMin),
"signed" => Some(TokenKind::Signed),
// `classify` and `declassify` are intentionally NOT
// reserved as keywords because they may also be used
// as user-defined function names. The parser
// recognises them as information-flow operators
// through context-sensitive lookahead: in expression
// position, a `classify` or `declassify` identifier
// followed by anything other than `(` is the operator
// form; followed by `(` it is a function call.
"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),
"shared" => Some(TokenKind::Shared),
"private" => Some(TokenKind::Private),
"const" => Some(TokenKind::Const),
"ephemeral" => Some(TokenKind::Ephemeral),
"trait" => Some(TokenKind::Trait),
"impl" => Some(TokenKind::Impl),
_ => None,
}
}
}