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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use std::collections::HashMap;
use fast_loop::fast_loop_macro;
use fnv::FnvHashMap as Map;
use proc_macro2::TokenStream;
use quote::quote;
use syn::Ident;
use crate::graph::{ByteClass, Graph, State, StateType};
use crate::leaf::{Callback, InlineCallback};
use crate::util::ToIdent;
mod fast_loop;
mod fork;
mod leaf;
pub struct Config {
pub use_state_machine_codegen: bool,
}
pub struct Generator<'a> {
/// Configuration for the code generation
config: Config,
/// Name of the type we are implementing the `Logos` trait for
name: &'a Ident,
/// Name of the type with any generics it might need
this: &'a TokenStream,
/// Reference to the graph with all the nodes
graph: &'a Graph,
/// Mapping of states to their identifiers.
/// First is snake_case, second is PascalCase
state_idents: Map<State, [Ident; 2]>,
/// Mapping of the leaf indexes to their identifiers.
leaf_idents: Vec<[Ident; 2]>,
/// Callback for the default error type
error_callback: &'a Option<Callback>,
/// Bit masks that will be compressed into LUTs for fast looping
loop_masks: HashMap<[bool; 256], usize>,
}
impl<'a> Generator<'a> {
pub fn new(
config: Config,
name: &'a Ident,
this: &'a TokenStream,
graph: &'a Graph,
error_callback: &'a Option<Callback>,
) -> Self {
let state_idents = graph
.iter_states()
.map(|state| {
(
state,
[
state.snake_case().to_ident(),
state.pascal_case().to_ident(),
],
)
})
.collect();
let leaf_idents = (0..graph.leaves().len())
.map(|leaf_idx| {
[
format!("leaf{leaf_idx}").to_ident(),
format!("Leaf{leaf_idx}").to_ident(),
]
})
.collect();
Generator {
config,
name,
this,
graph,
state_idents,
leaf_idents,
error_callback,
loop_masks: HashMap::new(),
}
}
/// Generates the implementation (body) of the [Logos::lex] function
pub fn generate(&mut self) -> TokenStream {
let mut states = self.graph.iter_states().collect::<Vec<_>>();
// Sort for repeatability (not dependent on hashmap iteration order)
states.sort_unstable();
let states_rendered = states
.iter()
.map(|&state| self.generate_state(state))
.collect::<Vec<_>>();
let init_state = self.get_ident(self.graph.root());
let mut all_idents_pascal = self
.state_idents
.values()
.map(|[_snake, pascal]| pascal)
.collect::<Vec<_>>();
// Sort for repeatability (not dependent on hashmap iteration order)
all_idents_pascal.sort_unstable();
let leaves_pascal = self
.leaf_idents
.iter()
.map(|[_snake, pascal]| pascal)
.collect::<Vec<_>>();
let leaves_index = 0..(self.graph.leaves().len() as isize);
let make_token_fn = self.make_token_fn();
let fast_loop_macro = fast_loop_macro(8);
let take_action_macro = self.take_action_macro();
let loop_luts = self.render_luts();
let common = quote! {
#fast_loop_macro
#take_action_macro
#loop_luts
#make_token_fn
#[derive(Clone, Copy)]
enum LogosLeaf {
#(#leaves_pascal = #leaves_index),*
}
};
if self.config.use_state_machine_codegen {
quote! {
#common
#[derive(Clone, Copy)]
enum LogosState {
#(#all_idents_pascal),*
}
let mut state = LogosState::#init_state;
let mut offset = lex.offset();
let mut context: _Option<LogosLeaf> = None;
loop {
match state {
#(#states_rendered)*
}
}
}
} else {
quote! {
#common
#(#states_rendered)*
#init_state(lex, lex.offset(), None)
}
}
}
fn get_ident(&self, state: State) -> &Ident {
let idx = match self.config.use_state_machine_codegen {
true => 1,
false => 0,
};
&self
.state_idents
.get(&state)
.expect("Unreachable state found")[idx]
}
// Generates the definition for the `_make_error` function. Its body can be
// changed using the `callback` argument of the `error` attribute.
// Otherwise, it defaults to the `Default::default()`value.
//
// Also generates the `_make_token` function. This function uses the context to create a new
// Self instance (or an error if the context is zero).
fn make_token_fn(&self) -> TokenStream {
let this = self.this;
let leaf_bodies = self
.graph
.leaves()
.iter()
.map(|leaf| self.generate_callback(leaf))
.collect::<Vec<_>>();
let leaf_indices = self
.leaf_idents
.iter()
.map(|[_snake, camel]| camel)
.collect::<Vec<_>>();
let error_body = match self.error_callback {
Some(Callback::Label(label)) => quote! {
let error = #label(lex);
error.into()
},
Some(Callback::Inline(InlineCallback { arg, body, .. })) => quote! {
let #arg = lex;
let error = { #body };
error.into()
},
None => quote! {
<#this as Logos<'s>>::Error::default()
},
};
// This is needed in rust versions before 1.83 where you need to match Some
// for Option<Enum> even if the enum has no variants.
let default_case = if self.graph.leaves().is_empty() {
Some(quote! {
Some(_) => unreachable!("There are no matchable tokens"),
})
} else {
None
};
quote! {
#[inline]
fn _make_error<'s>(lex: &mut _Lexer<'s>) -> <#this as Logos<'s>>::Error {
#error_body
}
#[inline]
fn _get_action<'s>(lex: &mut _Lexer<'s>, offset: usize, context: _Option<LogosLeaf>)
-> CallbackResult<'s, #this>
{
match context {
None => {
lex.end_to_boundary(offset.max(lex.offset() + 1));
CallbackResult::Error(_make_error(lex))
},
#(Some(LogosLeaf::#leaf_indices) => {
#leaf_bodies
}),*
#default_case
}
}
}
}
/// Generates the code to transition to a state.
fn state_transition(&self, state: State) -> TokenStream {
self.state_action(self.state_value(state))
}
/// Generates the code to transition to a state stored in an identifier
fn state_action(&self, state_ident: TokenStream) -> TokenStream {
match self.config.use_state_machine_codegen {
true => quote! { state = #state_ident; continue; },
false => quote! { return #state_ident(lex, offset, context); },
}
}
/// Generates the code to quote a state's representation
fn state_value(&self, state: State) -> TokenStream {
let state_ident = self.get_ident(state);
match self.config.use_state_machine_codegen {
true => quote!(LogosState::#state_ident),
false => quote!(#state_ident),
}
}
/// Generates the body of a state. This is a match statement over
/// the next byte, which determines the next state.
///
/// It also instantiates the relevant leaf, if `state` has a context.
///
/// In state machine codegen, the body is wrapped in a match arm for the
/// `state`'s variant. In tail-call codegen, the body is inside
/// `state`'s function.
fn generate_state(&mut self, state: State) -> TokenStream {
let state_data = self.graph.get_state(state);
// If we are in a match state, update the current token to
// end at the current offset - 1.
// The 1 comes from the 1 byte delayed match behavior
// of the regex-automata crate.
let setup = match state_data.state_type {
StateType {
early: Some(idx), ..
} => {
let leaf = &self.leaf_idents[idx.0][1];
quote! {
lex.end(offset);
context = Some(LogosLeaf::#leaf);
}
}
StateType {
accept: Some(idx), ..
} => {
let leaf = &self.leaf_idents[idx.0][1];
quote! {
lex.end(offset - 1);
context = Some(LogosLeaf::#leaf);
}
}
StateType { .. } => quote!(),
};
let fast_loop = self.maybe_impl_fast_loop(state);
let fork = self.impl_fork(state, state_data, true);
// Wrap body in a match arm or function depending on the current codegen
let this_ident = self.get_ident(state);
if self.config.use_state_machine_codegen {
quote! {
LogosState::#this_ident => {
#fast_loop
#setup
#fork
}
}
} else {
let this = self.this;
quote! {
fn #this_ident<'s>(lex: &mut _Lexer<'s>, mut offset: usize, mut context: _Option<LogosLeaf>)
-> _Option<_Result<#this, <#this as Logos<'s>>::Error>> {
#fast_loop
#setup
#fork
}
}
}
}
/// Returns the identifier used to access the "index"th LUT.
fn table_ident(index: usize) -> Ident {
format!("_TABLE_{index}").to_ident()
}
/// Return the identifier and bit mask used to reference a LUT containing a bit mask. The bit
/// mask is generated to match the given edge.
fn add_test_to_lut(&mut self, edge: &ByteClass) -> (Ident, u8) {
let table_bits = edge.to_table();
let loop_id = if let Some(&existing) = self.loop_masks.get(&table_bits) {
existing
} else {
let loop_id = self.loop_masks.len();
self.loop_masks.insert(table_bits, loop_id);
loop_id
};
let loop_table = loop_id / 8;
let ident = Self::table_ident(loop_table);
let loop_mask = 1u8 << (loop_id % 8);
(ident, loop_mask)
}
/// Stack the bit tables into chunks of 8 and render them as byte table constants into a
/// TokenStream.
pub fn render_luts(&self) -> TokenStream {
let mut sorted = self.loop_masks.iter().collect::<Vec<_>>();
sorted.sort_unstable_by_key(|(_bits, id)| **id);
let decls = sorted.chunks(8).enumerate().map(|(lut_idx, bit_arrs)| {
let mut byte_arr = [0u8; 256];
for (bit_index, (bits, _id)) in bit_arrs.iter().enumerate() {
for (arr_idx, &bit) in bits.iter().enumerate() {
if bit {
byte_arr[arr_idx] |= 1 << bit_index;
}
}
}
let ident = Self::table_ident(lut_idx);
quote! { const #ident: [u8; 256] = [#(#byte_arr),*]; }
});
quote! { #(#decls)* }
}
}
macro_rules! match_quote {
($source:expr; $($byte:tt,)* ) => {match $source {
$( $byte => quote!($byte), )*
byte => quote!(#byte),
}}
}
/// Converts a byte to a byte literal that can be used to match it
fn byte_to_tokens(byte: u8) -> TokenStream {
match_quote! {
byte;
b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9',
b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j',
b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's', b't',
b'u', b'v', b'w', b'x', b'y', b'z',
b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J',
b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S', b'T',
b'U', b'V', b'W', b'X', b'Y', b'Z',
b'!', b'@', b'#', b'$', b'%', b'^', b'&', b'*', b'(', b')',
b'{', b'}', b'[', b']', b'<', b'>', b'-', b'=', b'_', b'+',
b':', b';', b',', b'.', b'/', b'?', b'|', b'"', b'\'', b'\\',
}
}