sana_derive 0.1.1

The derive macro for Sana
Documentation
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use syn::Ident;
use heck::ShoutySnakeCase;
use proc_macro2::TokenStream;
use proc_macro_error::abort;
use quote::{format_ident, quote};

use sana_core::ir::{Op, Ir};
use crate::{SanaSpec, Backend};

pub(crate) fn generate(spec: SanaSpec) -> TokenStream {
    let dfa = match spec.rules.construct_dfa() {
        Ok(dfa) => dfa,
        Err(sana_core::Error::AmbiguityError(ix, i)) =>
            abort!(spec.variants[i].span(), "Ambiguous rule";
            note = spec.variants[ix].span() => "Resolve conflicts with {}", spec.variants[ix]),
    };

    let ir = Ir::from_automata(dfa);

    let enum_ident = spec.enum_ident;
    let enum_const_name = enum_ident.to_string()
        .to_shouty_snake_case();
    let ir_var = format_ident!("_{}_IR", enum_const_name);
    let ir_code = generate_ir(&enum_ident, &ir, &spec.variants);

    let lexer_name = format_ident!("_{}_LEXER", enum_const_name);
    let bytecode = analyze_ir(&ir);
    let rust_code = compile_bytecode(bytecode, &enum_ident, &spec.variants);
    let error = spec.terminal;

    let uses_vm = spec.backend == Backend::Vm;

    quote! {
        #[doc(hidden)]
        const #ir_var: &'static [sana::ir::Op<#enum_ident>] = #ir_code;

        impl sana::Sana for #enum_ident {
            const ERROR: Self = #enum_ident::#error;
            const USES_VM: bool = #uses_vm;

            fn ir() -> &'static [sana::ir::Op<Self>] { #ir_var }
            fn lex<'input>(cursor: &mut sana::ir::Cursor<'input>) -> sana::ir::VmResult<Self> {
                let mut lexer = #lexer_name::new();
                lexer.run(cursor)
            }
        }

        struct #lexer_name {
            action: ::core::option::Option<#enum_ident>,
            end: usize,
        }

        impl #lexer_name {
            fn new() -> Self {
                let action = None;
                let end = 0;
                Self { action, end }
            }

            fn run<'input>(&mut self, cursor: &mut sana::ir::Cursor<'input>) -> sana::ir::VmResult<#enum_ident> {
                self.action = None;

                if cursor.is_eoi() {
                    return sana::ir::VmResult::Eoi
                }

                let start = cursor.position();

                // l0 is the entry point
                self._l0(cursor);

                if self.action.is_none() && !cursor.is_eoi() {
                    return sana::ir::VmResult::Error {
                        start,
                        end: cursor.position(),
                    }
                }

                if self.end != cursor.position() { cursor.rewind(self.end) }

                if let Some(action) = self.action.take() {
                    sana::ir::VmResult::Action { start, end: self.end, action }
                } else {
                    sana::ir::VmResult::Eoi
                }
            }

            // The implementation of compile-time lexer
            #rust_code
        }
    }
}

fn generate_ir(enum_ident: &Ident, ir: &Ir<usize>, variants: &[Ident]) -> TokenStream {
    let code = ir.flatten();
    let mut ops = vec![];

    for op in code {
        let op = match op {
            Op::Shift => quote! {
                sana::ir::Op::Shift
            },
            Op::JumpMatches { from, to, on_success } => quote! {
                sana::ir::Op::JumpMatches {
                    from: #from,
                    to: #to,
                    on_success: #on_success,
                }
            },
            Op::JumpNotMatches { from, to, on_failure } => quote! {
                sana::ir::Op::JumpNotMatches {
                    from: #from,
                    to: #to,
                    on_failure: #on_failure,
                }
            },
            Op::LoopMatches { from, to } => quote! {
                sana::ir::Op::LoopMatches {
                    from: #from,
                    to: #to,
                }
            },
            Op::Jump(loc) => quote! {
                sana::ir::Op::Jump(#loc)
            },
            Op::Set(act) => {
                let var = &variants[act];

                quote! { sana::ir::Op::Set(#enum_ident::#var) }
            },
            Op::Halt => quote! {
                sana::ir::Op::Halt
            },
        };

        ops.push(op)
    }

    quote! {
        &[
            #(#ops),*
        ]
    }
}

// A block is a list of of statements with additional information.
#[derive(Debug)]
struct Block {
    id: usize,
    // whether there are jumps from inner blocks to the beginning of this block
    is_loop: bool,
    // whether there are multiple jumps into this block
    // and it should not be inlined
    is_func: bool,
    code: Vec<Stmt>,
}

type BlockId = usize;

#[derive(Debug)]
enum Stmt {
    /// Set current action
    Set(usize),
    /// Shift the cursor to the next character
    Shift,
    /// A set of guards to jump to a guard's suitable block
    Match(Match),
    /// jump to the block if the head does not match the given range
    JumpNotMatches {
        range: (char, char),
        block: BlockId,
    },
    /// An unconditional jump to the block
    Jump(BlockId),
    /// Halt and return an action, if any
    Halt,
}

/// A set of guards to jump to a guard's suitable block
#[derive(Debug)]
struct Match {
    arms: Vec<MatchArm>
}

/// Jump if matches
#[derive(Debug, Clone)]
struct MatchArm {
    ranges: Vec<(char, char)>,
    block: BlockId,
}

#[derive(Debug)]
pub struct Bytecode {
    blocks: Vec<Block>,
}

fn analyze_ir(ir: &Ir<usize>) -> Bytecode {
    let mut blocks: Vec<Block> = ir.blocks.iter()
        .enumerate()
        .map(|(id, block)| {
            let (is_func, ops) = match block {
                sana_core::ir::Block::Block(ops) => (false, ops),
                sana_core::ir::Block::Func(ops) => (true, ops),
            };

            let mut is_loop = false;

            let mut code = vec![];

            // we collect match guards into a single match statement if they follow each other
            // once the are broken with another statement we should dump the accumulator
            let mut match_acc: Option<Match> = None;

            for op in ops {
                match op {
                    Op::Set(act) => {
                        if let Some(match_acc) = match_acc.take() {
                            // dump match acc
                            code.push(Stmt::Match(match_acc))
                        }
                        code.push(Stmt::Set(*act));
                    },
                    Op::Shift => {
                        if let Some(match_acc) = match_acc.take() {
                            // dump match acc
                            code.push(Stmt::Match(match_acc))
                        }
                        code.push(Stmt::Shift);
                    },
                    Op::JumpMatches { from, to, on_success } => {
                        let match_arm = MatchArm {
                            ranges: vec![(*from, *to)],
                            block: *on_success
                        };
                        if let Some(match_acc) = match_acc.as_mut() {
                            match_acc.arms.push(match_arm);
                        } else {
                            match_acc = Some(Match {
                                arms: vec![match_arm],
                            });
                        }
                    },
                    Op::LoopMatches { from, to } => {
                        is_loop = true;

                        let match_arm = MatchArm {
                            ranges: vec![(*from, *to)],
                            block: id // self block id
                        };
                        if let Some(match_acc) = match_acc.as_mut() {
                            match_acc.arms.push(match_arm);
                        } else {
                            match_acc = Some(Match {
                                arms: vec![match_arm],
                            });
                        }
                    },
                    Op::JumpNotMatches { from, to, on_failure } => {
                        if let Some(match_acc) = match_acc.take() {
                            // dump match acc
                            code.push(Stmt::Match(match_acc))
                        }
                        code.push(Stmt::JumpNotMatches { range: (*from, *to), block: *on_failure });
                    },
                    Op::Jump(id) => {
                        if let Some(match_acc) = match_acc.take() {
                            // dump match acc
                            code.push(Stmt::Match(match_acc))
                        }
                        code.push(Stmt::Jump(*id));
                    },
                    Op::Halt => {
                        if let Some(match_acc) = match_acc.take() {
                            code.push(Stmt::Match(match_acc))
                        }
                        code.push(Stmt::Halt);
                    },
                }
            }
            // if the last statement was a match guard, then we should dump match statement
            if let Some(match_acc) = match_acc.take() {
                code.push(Stmt::Match(match_acc))
            }

            Block {
                id,
                is_loop,
                is_func,
                code,
            }
        })
        .collect();

    for block in blocks.iter_mut() {
        for op in block.code.iter_mut() {
            if let Stmt::Match(match_stmt) = op {
                optimize_match(match_stmt);
            }
        }
    }

    Bytecode { blocks }
}

fn optimize_match(match_stmt: &mut Match) {
    if !match_stmt.arms.is_empty() {
        let mut arms = match_stmt.arms.clone();

        // sort by block
        arms.sort_by(|l, r| l.block.cmp(&r.block));

        // group by block
        let mut new_arms: Vec<MatchArm> = vec![];
        for arm in arms.iter() {
            match new_arms.last_mut() {
                Some(new_arm) if new_arm.block == arm.block => {
                    new_arm.ranges.extend(&arm.ranges);
                }
                _ => {
                    let match_arm = MatchArm {
                        ranges: arm.ranges.clone(),
                        block: arm.block,
                    };
                    new_arms.push(match_arm);
                },
            }
        }

        match_stmt.arms = new_arms;
    }
}

use std::collections::HashSet;

pub fn compile_bytecode(bytecode: Bytecode, enum_ident: &Ident, variants: &[Ident]) -> TokenStream {
    let mut fns: Vec<TokenStream> = vec![];

    // compile only functions
    for block in bytecode.blocks.iter().filter(|b| b.is_func) {
        let name = format_ident!("_l{}", block.id);

        let body = func_to_rust(&bytecode, block.id, enum_ident, variants);

        fns.push(quote! {
            #[allow(clippy::needless_return)]
            fn #name<'input>(&mut self, cursor: &mut sana::ir::Cursor<'input>) { #body }
        });
    }

    quote! {
        #(#fns)*
    }
}

macro_rules! format_lifetime {
    ($($arg:tt)*) => {{
        let name = format!($($arg)*);

        syn::Lifetime::new(&name, proc_macro2::Span::call_site())
    }}
}

fn func_to_rust(bytecode: &Bytecode, block_id: BlockId, enum_ident: &Ident, variants: &[Ident]) -> TokenStream {
    let block = &bytecode.blocks[block_id];
    assert_eq!(block.id, block_id);

    let mut call_stack = HashSet::new();
    call_stack.insert(block.id);

    let code = block.code.iter()
        .map(|stmt| stmt_to_rust(&mut call_stack, bytecode, stmt, enum_ident, variants))
        .collect::<Vec::<_>>();

    if block.is_loop {
        let label = format_lifetime!("'l{}", block.id);
        let break_op = match block.code.last() {
            Some(Stmt::Halt) => quote!{ },
            _ => quote!{ break },
        };

        quote! {
            #label: loop {
                #(#code);*;

                #break_op
            }
        }
    }
    else {
        quote! {
            #(#code);*;
        }
    }
}

fn block_to_rust(call_stack: &mut HashSet<BlockId>, bytecode: &Bytecode, block_id: BlockId, enum_ident: &Ident, variants: &[Ident]) -> TokenStream {
    let block = &bytecode.blocks[block_id];
    assert_eq!(block.id, block_id);

    if block.is_loop && call_stack.contains(&block.id) {
        // we are trying to jump into the beginning of a loop from the middle of a block
        let label = format_lifetime!("'l{}", block_id);
        return quote! { continue #label }
    }

    if block.is_func {
        // any call to a function inside a block results into a call
        let func = format_ident!("_l{}", block.id);
        return quote! { return self.#func(cursor); }
    }

    if call_stack.contains(&block.id) {
        panic!("recursion in block {} which is not a function nor a loop. call stack: {:?}", block.id, &call_stack);
    }

    call_stack.insert(block.id);

    let code = block.code.iter()
        .map(|stmt| stmt_to_rust(call_stack, bytecode, stmt, enum_ident, variants))
        .collect::<Vec::<_>>();

    call_stack.remove(&block.id);

    if block.is_loop {
        let label = format_lifetime!("'l{}", block.id);
        let break_op = match block.code.last() {
            Some(Stmt::Halt) => quote!{ },
            _ => quote!{ break },
        };

        quote! {
            #label: loop {
                #(#code);*;

                #break_op
            }
        }
    }
    else {
        quote! {
            #(#code);*;
        }
    }
}

fn match_arm_to_rust(call_stack: &mut HashSet<BlockId>, bytecode: &Bytecode, arm: &MatchArm, enum_ident: &Ident, variants: &[Ident]) -> TokenStream {
    let ranges = arm.ranges.iter()
        .map(|(from, to)| quote! { #from ..= #to });
    let block = block_to_rust(call_stack, bytecode, arm.block, enum_ident, variants);

    quote! {
        #(#ranges)|* => { #block }
    }
}

fn stmt_to_rust(call_stack: &mut HashSet<BlockId>, bytecode: &Bytecode, stmt: &Stmt, enum_ident: &Ident, variants: &[Ident]) -> TokenStream {
    match stmt {
        Stmt::Set(act) => {
            let var = &variants[*act];
            quote! {
                self.action = Some(#enum_ident::#var);
                self.end = cursor.position();
            }
        },
        Stmt::Shift => {
            quote! { cursor.shift() }
        },
        Stmt::Match(Match { arms }) => {
            let arms = arms.iter()
                .map(|arm| match_arm_to_rust(call_stack, bytecode, arm, enum_ident, variants))
                .collect::<Vec::<_>>();

            quote! {
                let ch = match cursor.head {
                    Some(ch) => ch,
                    None => return,
                };

                match ch {
                    #(#arms),*,
                    _ => (),
                }
            }
        },
        Stmt::JumpNotMatches { range, block } => {
            let (from, to) = range;
            let block = block_to_rust(call_stack, bytecode, *block, enum_ident, variants);

            quote! {
                let ch = match cursor.head {
                    Some(ch) => ch,
                    None => return,
                };

                if !(#from..=#to).contains(&ch) { #block }
            }
        },
        Stmt::Jump(block_id) => {
            block_to_rust(call_stack, bytecode, *block_id, enum_ident, variants)
        },
        Stmt::Halt =>
            quote! { return },
    }
}